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); }
VALUE rb_drizzle_con_add_options(VALUE self, VALUE options) { net_drizzle_con_st *context; Data_Get_Struct(self, net_drizzle_con_st, context); drizzle_con_add_options(context->con, FIX2INT(options)); return self; }
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; }
static int _drizzle_ConnectionObject_Initialize( _drizzle_ConnectionObject *self, PyObject *args, PyObject *kwargs) { drizzle_st *drizzle= NULL; drizzle_con_st *con= NULL; drizzle_return_t ret; PyObject *conv = NULL; char *host= NULL, *user= NULL, *passwd= NULL, *db= NULL, *unix_socket= NULL, *init_command= NULL; uint16_t port= 0; int connect_timeout= 0, client_flag= 0; static char *kwlist[]= { "host", "user", "passwd", "db", "port", "conv", "unix_socket", "connect_timeout", "init_command", "client_flag", NULL }; self->open= 0; self->converter= NULL; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssHOsisi:connect", kwlist, &host, &user, &passwd, &db, &port, &conv, &unix_socket, &connect_timeout, &init_command, &client_flag)) { return -1; } drizzle = drizzle_create(NULL); con = drizzle_con_create(drizzle, NULL); if (unix_socket != NULL) { drizzle_con_set_uds(con, unix_socket); } else { drizzle_con_set_tcp(con, host, port); } drizzle_con_set_auth(con, user, passwd); drizzle_con_set_db(con, db); drizzle_con_add_options(con, client_flag); ret = drizzle_con_connect(con); if (ret != DRIZZLE_RETURN_OK) { _drizzle_Exception(self); return -1; } if (!conv) { conv= PyDict_New(); } else { Py_INCREF(conv); } if (!conv) { return -1; } self->converter = conv; self->con= con; self->open= 1; return 0; }
int main(int argc, char *argv[]) { const char *query= "SELECT table_schema,table_name FROM tables"; drizzle_st drizzle; drizzle_con_st con[SIMPLE_MULTI_COUNT]; drizzle_result_st result[SIMPLE_MULTI_COUNT]; drizzle_query_st ql[SIMPLE_MULTI_COUNT]; drizzle_return_t ret; drizzle_row_t row; int x; if (drizzle_create(&drizzle) == NULL) { printf("drizzle_create:NULL\n"); return 1; } /* Create SIMPLE_MULTI_COUNT connections and initialize query list. */ for (x= 0; x < SIMPLE_MULTI_COUNT; x++) { if (x == 0) { if (drizzle_con_create(&drizzle, &(con[0])) == NULL) { printf("drizzle_con_create:%s\n", drizzle_error(&drizzle)); return 1; } if (argc == 2 && !strcmp(argv[1], "-m")) drizzle_con_add_options(&(con[0]), DRIZZLE_CON_MYSQL); else if (argc != 1) { printf("usage: %s [-m]\n", argv[0]); return 1; } drizzle_con_set_db(&(con[0]), "information_schema"); } else { if (drizzle_con_clone(&drizzle, &(con[x]), &(con[0])) == NULL) { printf("drizzle_con_clone:%s\n", drizzle_error(&drizzle)); return 1; } } if (drizzle_query_add(&drizzle, &(ql[x]), &(con[x]), &(result[x]), query, strlen(query), 0, NULL) == NULL) { printf("drizzle_query_add:%s\n", drizzle_error(&drizzle)); return 1; } } ret= drizzle_query_run_all(&drizzle); if (ret != DRIZZLE_RETURN_OK) { printf("drizzle_query_run_all:%s\n", drizzle_error(&drizzle)); return 1; } for (x= 0; x < SIMPLE_MULTI_COUNT; x++) { if (drizzle_result_error_code(&(result[x])) != 0) { printf("%d:%s\n", drizzle_result_error_code(&(result[x])), drizzle_result_error(&(result[x]))); continue; } while ((row= drizzle_row_next(&(result[x]))) != NULL) printf("%d %s:%s\n", x, row[0], row[1]); } drizzle_free(&drizzle); return 0; }
int main(int argc, char *argv[]) { int c; uint32_t count= 0; const char *host= NULL; bool mysql= false; in_port_t port= 0; drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER; drizzle_return_t ret; drizzle_st drizzle; drizzle_con_st con_listen; drizzle_con_st con; drizzle_result_st result; drizzle_column_st column; while((c = getopt(argc, argv, "c:h:mp:v")) != -1) { switch(c) { case 'c': count= (uint32_t)atoi(optarg); break; case 'h': host= optarg; break; case 'm': mysql= true; break; case 'p': port= (in_port_t)atoi(optarg); break; case 'v': verbose++; break; default: printf("\nusage: %s [-c <count>] [-h <host>] [-m] [-p <port>] [-v]\n", argv[0]); printf("\t-c <count> - Number of connections to accept before exiting\n"); printf("\t-h <host> - Host to listen on\n"); printf("\t-m - Use the MySQL protocol\n"); printf("\t-p <port> - Port to listen on\n"); printf("\t-v - Increase verbosity level\n"); return 1; } } if (drizzle_create(&drizzle) == NULL) { printf("drizzle_create:NULL\n"); return 1; } drizzle_add_options(&drizzle, DRIZZLE_FREE_OBJECTS); drizzle_set_verbose(&drizzle, verbose); if (drizzle_con_create(&drizzle, &con_listen) == NULL) { printf("drizzle_con_create:NULL\n"); return 1; } drizzle_con_add_options(&con_listen, DRIZZLE_CON_LISTEN); drizzle_con_set_tcp(&con_listen, host, port); if (mysql) 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; } while (1) { (void)drizzle_con_accept(&drizzle, &con, &ret); if (ret != DRIZZLE_RETURN_OK) { printf("drizzle_con_accept:%s\n", drizzle_error(&drizzle)); return 1; } server(&drizzle, &con, &result, &column); drizzle_con_free(&con); if (count > 0) { count--; if (count == 0) break; } } drizzle_con_free(&con_listen); drizzle_free(&drizzle); return 0; }