bool master_service::thread_on_accept(acl::socket_stream* conn) { if (0) acl_tcp_so_linger(conn->sock_handle(), 1, 0); logger("thread id: %lu", acl_pthread_self()); return true; }
void db_service::on_accept(acl::aio_socket_stream* client) { ACL_SOCKET fd = client->get_socket(); // 在此处设置服务端接收到的套接口的 SO_LINGER 选项, // 以保证在套接口关闭后其资源能得到立刻释放,虽然一般 // 而言设置此选项会有危害,但考虑到服务端只有接收到 // 完整的客户端数据后才会调用关闭操作,所以应该不会 // 造成数据发送不全的问题,切记,不应在客户端的关闭 // 操作中设置 SO_LINGER 选项,以防数据未完整发送 // 在服务端设置接收连接的 SO_LINGER 选项,有助于操作 // 系统快速回收套接口资源 acl_tcp_so_linger(fd, 1, 0); ipc_client* ipc = NEW db_ipc(this); ipc->open(client); // 添加服务线程的消息处理 ipc->append_message(DB_OK); ipc->append_message(DB_ERR_OPEN); ipc->append_message(DB_ERR_EXEC_SQL); // 异步等待消息 ipc->wait(); }
socket_stream& socket_stream::set_tcp_solinger(bool on, int linger) { ACL_SOCKET sock = sock_handle(); if (sock == ACL_SOCKET_INVALID) { logger_error("invalid socket handle"); return *this; } acl_tcp_so_linger(sock, on ? 1 : 0, linger); return *this; }
static void run(const char *addr, int n, int keep_alive, int cmd_num) { ACL_VSTREAM *client = NULL; int i, ret, out; time_t begin, end; time(&begin); for (i = 0; i < n; i++) { if (client == NULL) { client = acl_vstream_connect(addr, ACL_BLOCKING, 0, 0, 0); if (client == NULL) { printf("connect to %s error %s\n", addr, acl_last_serror()); break; } acl_tcp_so_linger(ACL_VSTREAM_SOCK(client), 1, 0); } if ((i > 0 && i % 10000 == 0) || n <= 20) out = 1; else out = 0; ret = request(client, cmd_num, out); if (ret < 0) break; if (!keep_alive) { acl_vstream_close(client); client = NULL; } if (out) { printf(">>>i: %d\n", i); ACL_METER_TIME("----------"); } } time(&end); printf("ok, total: %d, time spent: %ld\n", i, end - begin); if (client) acl_vstream_close(client); }