Beispiel #1
0
void buf_write_callback(struct bufferevent *incoming,
                        void *arg)
{	
    conn *c;
    c = (conn*) arg;
    assert(c != NULL);
    int fd;
    fd = (int)bufferevent_getfd(incoming);
    if (fd != c->sfd){
		perror("fd != sfd");
		conn_close(c);
		return;
	}
	drive_machine(c, incoming);
	return;
}
Beispiel #2
0
void master_event_handler(const int fd, const short which, void *arg) {
    conn *c;
    c = (conn *)arg;

    assert(c != NULL);
    // c->which = which;

    if (fd != c->sfd)
    {
        perror("fd != sfd");
        conn_close(c);
        return;
    }
    drive_machine(c, NULL);
    return;
}
Beispiel #3
0
// 改成用bufferevent之后,接受的参数得改。。。
void event_handler(struct bufferevent *incoming, void *arg) {
    conn *c;
    c = (conn *)arg;

    assert(c != NULL);
    // c->which = which;
    int fd;
    fd = (int)bufferevent_getfd(incoming);

    if (fd != c->sfd)
    {
        perror("fd != sfd");
        conn_close(c);
        return;
    }
    drive_machine(c, incoming);
    return;

}
void event_handler(int fd, short which, void *arg) {
    conn *c;
    
    c = (conn *)arg;
    c->which = which;

    /* sanity */
    if (fd != c->sfd) {
        fprintf(stderr, "Catastrophic: event fd doesn't match conn fd!\n");
        conn_close(c);
        return;
    }

    /* do as much I/O as possible until we block */
    drive_machine(c);

    /* wait for next event */
    return;
}
Beispiel #5
0
// leader/flower模式
static void *worker_main(void *arg) {
    pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);

    // struct timeval {
    //     time_t       tv_sec;     /* seconds */
    //     suseconds_t   tv_usec; /* microseconds */
    // };
    // tv_usec 的说明为时间的毫秒部分
    struct timeval tv = {1, 0};
    // daemon_quit 退出标识。 0 没有退出、1退出
    while (!daemon_quit) {
        pthread_mutex_lock(&leader);

AGAIN:
        while(loop.nready == 0 && daemon_quit == 0)
            // 取出事件
            loop.nready = aeApiPoll(&loop, &tv);
        if (daemon_quit) {
            pthread_mutex_unlock(&leader);
            break;
        }

        // 遍历事件
        loop.nready --;
        int fd = loop.fired[loop.nready];
        conn *c = loop.conns[fd];
        if (c == NULL){
            fprintf(stderr, "Bug: conn %d should not be NULL\n", fd);
            delete_event(fd);
            close(fd);
            goto AGAIN;
        }
        //loop.conns[fd] = NULL;
        pthread_mutex_unlock(&leader);

        if (drive_machine(c)) {
            if (update_event(fd, c->ev_flags, c)) conn_close(c);
        }
    }
    return NULL;
}