예제 #1
0
int main()
{
    zmq::context_t context(1);

    zmq::socket_t socket_rec(context, ZMQ_PULL);
    socket_rec.connect("tcp://192.168.8.102:5557");    //localhost

    zmq::socket_t socket_sink(context, ZMQ_PUSH);
    socket_sink.connect("tcp://192.168.8.102:5558");    //localhost

    int line;
    std::string scanned_line;

    while (true)
    {
        line = std::stoi(s_recv(socket_rec));
        scanned_line = scan_line(line);
        std::cout << "received: " << line << std::endl;
        s_send(socket_sink, std::string("head3: ") + std::to_string(line) + " :: " + scanned_line);
    }
}
예제 #2
0
int fcgi_server_recv_stdout_stderr_record(fcgi_request_t *fr,
		uint16_t request_id, void *buffer)
{
	apr_status_t status = OK;

	/* state information */
	uint8_t type = FCGI_UNKNOWN_TYPE;
	uint32_t payload_len = 0;
	uint16_t padding_len = 0;
	int seen_eos = 0;
	int is_cgi_header = 1;

	char *p, *data;
	int i;

	do {
		payload_len = 0;

		/* Step 1: read FCGI packet header */
		ssize_t bytes_read = socket_recv(fr, buffer, FCGI_HEADER_LEN);

		if (bytes_read == -1) {
			status = HTTP_INTERNAL_SERVER_ERROR;
			goto out;
		}

		if (bytes_read < FCGI_HEADER_LEN) {
			ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, fr->r,
					"FastCGI: premature end of header from backend server (id=%u, bytes_read=%lu, needed=%u)",
					request_id, bytes_read, FCGI_HEADER_LEN);
			status = HTTP_INTERNAL_SERVER_ERROR;
			goto out;
		}

		/* Step 2: parse header */
		status = fcgi_server_recv_check_header(fr, request_id,
				(fcgi_header_t)buffer, &type, &payload_len, &padding_len);

		if (status != OK)
			goto out;

		ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
				"FastCGI: packet received (id=%u, type=%u, payload_len=%u)",
				request_id, type, payload_len);

		/* Step 3: read FCGI payload */
		bytes_read = socket_recv(fr, buffer, payload_len);

		if (bytes_read == -1) {
			status = HTTP_INTERNAL_SERVER_ERROR;
			goto out;
		}

		if (bytes_read < payload_len) {
			ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, fr->r,
					"FastCGI: premature end of payload from backend server (id=%u, bytes_read=%lu, needed=%u)",
					request_id, bytes_read, payload_len);
			status = HTTP_INTERNAL_SERVER_ERROR;
			goto out;
		}

		payload_len -= padding_len;
		data = buffer;
		data[payload_len] = '\0';

		/* Step 4: handle packet types */
		switch (type) {
			case FCGI_END_REQUEST:
				seen_eos = 1;
				break;

			case FCGI_STDOUT:
				p = data;

				if (is_cgi_header) {
					is_cgi_header = 0;

					/* TODO: nph */
					/* XXX: this assumes that the backend does not send more
					 * than 64K of headers, which is probably safe, but it
					 * should be fixed nevertheless. */

					status = fcgi_server_parse_headers(fr, request_id,
							&data);

					if (status != OK) {
						ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
								"FastCGI: => CGI headers return not OK (id=%u, status=%i)",
								request_id, status);
						goto out;
					}

					if (data) {
						payload_len -= (data - p);
					}
				}

				/* content-length = 0 */
				if (data && *data) {
					ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
							"FastCGI: sending CGI data (id=%u, payload_len=%u)",
							request_id, payload_len);

					status = fcgi_server_send_stdout_data(fr, request_id,
							data, payload_len);

					if (status != OK) {
						goto out;
					}
				}
				break;

			case FCGI_STDERR:
				p = data;

				for (i = 0; i < payload_len; i++) {
					if (data[i] == '\n' || data[i] == '\r' || data[i] == '\0') {
						data[i] = '\0';

						if ((data - p) > 0) {
							ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, fr->r,
									"FastCGI: STDERR(id=%u): %s", request_id, p);
						}

						p = data + i + 1;
					}
				}
				break;
		}
	} while (!seen_eos);

out:
	socket_sink(fr);
	fcgi_server_disconnect(fr);
	return status;
}