int main(void) { hExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL); clients = (int*)malloc(sizeof(int)*MAX_CLIENTS); ss = socket_server_create(socket_callback); HANDLE h = (HANDLE)_beginthreadex(NULL, 0, test, 0, 0, NULL); WaitForSingleObject(h, INFINITE); int i; for (i=0; i<MAX_CLIENTS; ++i) { socket_server_close(ss, 100+i, clients[i]); } socket_server_exit(ss); socket_server_wait_for_exit(ss); socket_server_release(ss); printf("test end\n"); printf("expected bytes: %d send bytes: %d recv bytes: %d\n", EXPECTED_BYTES, sent_bytes, recv_bytes); free(clients); system("pause"); return 0; }
int main() { struct sigaction sa; sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, 0); struct socket_server * ss = socket_server_create(); test(ss); socket_server_release(ss); return 0; }
int main(void) { clients = (int*)malloc(sizeof(int)*MAX_CLIENTS); ss = socket_server_create(socket_callback); HANDLE h = (HANDLE)_beginthreadex(NULL, 0, test, 0, 0, NULL); WaitForSingleObject(h, INFINITE); int i; for (i=0; i<MAX_CLIENTS; ++i) { socket_server_close(ss, 100+i, clients[i]); } socket_server_exit(ss); socket_server_wait_for_exit(ss); socket_server_release(ss); printf("test end\n"); free(clients); system("pause"); return 0; }
void skynet_socket_free() { socket_server_release(SOCKET_SERVER); SOCKET_SERVER = NULL; }
int main(int argc, char *argv[]) { if (argc != 4) { printf("Copyright (c) 2014, Dali Wang<*****@*****.**>.\n"); printf("Used: sms ip port serial\n"); printf("./sms 127.0.0.1 8888 /dev/ttyS0"); return 0; } sigign(); // 信号处理 // 注册返回函数 settings.on_message_begin = begin_cb; settings.on_url = url_cb; settings.on_header_field = field_cb; settings.on_header_value = value_cb; settings.on_headers_complete = herders_cb; settings.on_body = body_cb; settings.on_message_complete = end_cb; settings.on_status = status_cb; cssl_start(); // 开启CSSL库 //"/dev/ttyS1" serial = cssl_open(argv[3], callback, 0, 9600, 8, 0, 1); // 打开串口 // 打开失败 if (!serial) { printf("Open COM Fail = %s\n", cssl_geterrormsg()); // 获得错误信息 return -1; } ss = socket_server_create(); // 创建 socket_server int listen_id = socket_server_listen(ss, 100, argv[1], atoi(argv[2]), 32); // 监听 socket socket_server_start(ss, 200, listen_id); // 纳入事件管理 struct socket_message result; // 声明一个soket消息的结构 for (;;) { int type = socket_server_poll(ss, &result, NULL); // 事件循环 switch (type) { case SOCKET_EXIT: printf("exit(%lu) [id=%d]\n", result.opaque, result.id); goto EXIT_LOOP; case SOCKET_DATA: //printf("data(%lu) [id=%d] %s\n", result.opaque, result.id, result.data); OnReceive(result); // 收到数据,调用函数处理 break; case SOCKET_CLOSE: printf("close(%lu) [id=%d]\n", result.opaque, result.id); free((void*)result.opaque); break; case SOCKET_OPEN: printf("open(%lu) [id=%d] %s\n", result.opaque, result.id, result.data); break; case SOCKET_ERROR: printf("error(%lu) [id=%d]\n", result.opaque, result.id); break; case SOCKET_ACCEPT: { printf("accept(%lu) [id=%d %s] from [%d]\n", result.opaque, result.ud, result.data, result.id); // 初始化 HTTP 结构 struct http_parser *parser; parser = (http_parser *) malloc(sizeof(http_parser)); http_parser_init(parser, HTTP_REQUEST); socket_server_start(ss, (uintptr_t)parser, result.ud); // 纳入事件管理 id = result.ud; // 临时解决办法,应该使用 parser->data来传输 。 } break; } // switch } // for EXIT_LOOP: socket_server_release(ss); // 关闭 socket_server return 0; }