Exemplo n.º 1
0
void _mainloop(void *zsock)
{
	while(true)
	{
		zmq_msg_t *zmsg = malloc(sizeof(zmq_msg_t));
		zmq_msg_init(zmsg);

		// zmq_recv blocks with default flags, so we don't need to usleep
		int rc = zmq_recv(zsock, zmsg, 0);

		if(rc == 0) // zmq_recv 0 is non-error
		{
			int size = zmq_msg_size(zmsg);
			char *_msg = malloc(size + 1);
			memcpy(_msg, zmq_msg_data(zmsg), size);
			zmq_msg_close(zmsg);
			_msg[size] = 0;

			conn_t *connection = conn_init(zsock);
			char *resp = conn_process(connection, _msg);

			zmq_msg_t *zmqresp = malloc(sizeof(zmq_msg_t));
			zmq_msg_init_size(zmqresp, strlen(resp));
			memcpy(zmq_msg_data(zmqresp), resp, strlen(resp));

			zmq_send(zsock, zmqresp, 0);
			zmq_msg_close(zmqresp);

			free(_msg);
		}
	}
}
Exemplo n.º 2
0
/*
 * Processes an incoming "handle a new connection" item. This is called when
 * input arrives on the libevent wakeup pipe.
 */
static void thread_libevent_process(int fd, short which, void *arg) {
    LIBEVENT_THREAD *me = (LIBEVENT_THREAD *)arg;
    CQ_ITEM *item;
    char buf[1];
    
    if (read(fd, buf, 1) != 1)
        if (settings.verbose > 0)
            fprintf(stderr, "Can't read from libevent pipe\n");
    
    switch (buf[0]) {
    case 'c':
        item = cq_pop(me->new_conn_queue);
        
        if (NULL != item) {
            conn_process(item, me);
        }
        cqi_free(item);
        
        break;
    }
    /* we were told to flip the lock type and report in */
    
    
}