void accept_cb(EV_P_ struct ev_io *w, int revent) { client_t *client = (client_t *) w->data; drizzle_con_st *dc = (drizzle_con_st *) malloc(sizeof(drizzle_con_st)); drizzle_return_t ret; int fd; connection_t *con; dbgin(); /* Manually set connection to ready */ drizzle_con_add_options(client->dc, DRIZZLE_CON_IO_READY); drizzle_con_accept(client->drizzle, dc, &ret); if (ret != DRIZZLE_RETURN_OK) { if (ret == DRIZZLE_RETURN_IO_WAIT) { printf("io_wait\n"); return; } printf("drizzle_con_accpet error:%s\n", drizzle_error(client->drizzle)); return; } fd = drizzle_con_fd(dc); printf("Accepted. fd:%d\n", fd); con = (connection_t *) calloc(1, sizeof(connection_t)); con->fd = fd; con->client = client; con->dc = dc; add_connection(client, con); }
static drizzle_return_t _event_watch(drizzle_con_st *con, short events, void *context) { (void) con; (void) events; (void) context; /* fake register the file descriptor */ int fd= drizzle_con_fd(con); assert(0 <= fd && fd < (int) sizeof(_event_watch_read_bits) * 8); if (events & POLLIN) { _event_watch_read_bits|= 1 << fd; } else { _event_watch_read_bits&= ~(1 << fd); } if (events & POLLOUT) { _event_watch_write_bits|= 1 << fd; } else { _event_watch_write_bits&= ~(1 << fd); } return DRIZZLE_RETURN_OK; }
int main(int argc, char *argv[]) { int c; drizzle_st drizzle; drizzle_con_st con_listen; in_port_t port = 3307; char *host = NULL; struct ev_loop *loop = ev_default_loop(ev_recommended_backends() | EVBACKEND_KQUEUE); ev_io accept_w; int accept_fd; client_t client; dbgin(); while ((c = getopt(argc, argv, "p:h:")) != -1) { switch (c) { case 'p': port = (in_port_t) atoi(optarg); break; case 'h': host = optarg; break; default: usage(argv[0]); return 1; } } if (drizzle_create(&drizzle) == NULL) { printf("drizzle_create error: NULL\n"); return 1; } drizzle_add_options(&drizzle, DRIZZLE_FREE_OBJECTS); drizzle_add_options(&drizzle, DRIZZLE_NON_BLOCKING); drizzle_set_verbose(&drizzle, DRIZZLE_VERBOSE_NEVER); if (drizzle_con_create(&drizzle, &con_listen) == NULL) { printf("drizzle_con_create error: NULL\n"); return 1; } drizzle_con_add_options(&con_listen, DRIZZLE_CON_LISTEN); drizzle_con_set_tcp(&con_listen, host, port); /* Add mysql protocol support */ drizzle_con_add_options(&con_listen, DRIZZLE_CON_MYSQL); if (drizzle_con_listen(&con_listen) != DRIZZLE_RETURN_OK) { printf("drizzle_con_listen:%s\n", drizzle_error(&drizzle)); return 1; } printf("Server started on host: %s, port: %d\n", host, port); client.drizzle = &drizzle; client.dc = &con_listen; accept_fd = drizzle_con_fd(&con_listen); ev_io_init(&accept_w, accept_cb, accept_fd, EV_READ); ev_io_start(EV_A_ &accept_w); client.loop = loop; accept_w.data = &client; ev_loop(EV_A_ 0); drizzle_con_free(&con_listen); drizzle_free(&drizzle); return 0; }
int io_wait(drizzle_con_st *con, drizzle_return_t ret) { drizzle_return_t dret; int fd = 0, events = 0; PyObject *fileno, *state, *args, *res; if (ret == DRIZZLE_RETURN_OK) { return 0; }else if (ret == DRIZZLE_RETURN_IO_WAIT) { events = con->events; YDEBUG("IO_WAIT con:%p events:%d", con, events); if (external_io_wait) { fd = drizzle_con_fd(con); if (fd == -1){ return -1; } fileno = PyLong_FromLong((long)fd); if (fileno == NULL) { return -1; } state = PyLong_FromLong((long)events); if (state == NULL) { Py_DECREF(fileno); return -1; } args = PyTuple_Pack(2, fileno, state); if (args == NULL) { Py_DECREF(fileno); Py_DECREF(state); return -1; } YDEBUG("call external_io_wait ..."); res = PyObject_CallObject(external_io_wait, args); Py_DECREF(args); if (res == NULL) { return -1; } Py_XDECREF(res); dret = drizzle_con_set_revents(con, events); if (dret != DRIZZLE_RETURN_OK){ RDEBUG("ret %d:%s", dret, drizzle_error(drizzle)); return -1; } return 1; } else { DEBUG("call drizzle_con_wait ..."); dret = drizzle_con_wait(drizzle); if (dret != DRIZZLE_RETURN_OK){ RDEBUG("ret %d:%s", dret, drizzle_error(drizzle)); return -1; } return 1; } }else{ RDEBUG("ret %d:%s", ret, drizzle_error(drizzle)); return -1; } return 0; }