bool aio_listen_stream::open(const char* addr) { ACL_VSTREAM *sstream = acl_vstream_listen(addr, 128); if (sstream == NULL) return false; snprintf(addr_, sizeof(addr_), "%s", ACL_VSTREAM_LOCAL(sstream)); stream_ = acl_aio_open(handle_->get_handle(), sstream); // 调用基类的 hook_error 以向 handle 中增加异步流计数, // 同时 hook 关闭及超时回调过程 hook_error(); // hook 监听的回调过程 hook_accept(); return true; }
aio_fstream::aio_fstream(aio_handle* handle, ACL_FILE_HANDLE fd, unsigned int oflags /* = 600 */) : aio_stream(handle), aio_istream(handle), aio_ostream(handle) { acl_assert(handle); acl_assert(fd != ACL_FILE_INVALID); ACL_VSTREAM* vstream = acl_vstream_fhopen(fd, oflags); stream_ = acl_aio_open(handle->get_handle(), vstream); // 调用基类的 hook_error 以向 handle 中增加异步流计数, // 同时 hook 关闭及超时回调过程 hook_error(); // 只有当流连接成功后才可 hook IO 读写状态 // hook 读回调过程 hook_read(); // hook 写回调过程 hook_write(); }
bool aio_fstream::open(const char* path, unsigned int oflags, unsigned int mode) { ACL_VSTREAM* fp = acl_vstream_fopen(path, oflags, mode, 8192); if (fp == NULL) return false; stream_ = acl_aio_open(handle_->get_handle(), fp); // 调用基类的 hook_error 以向 handle 中增加异步流计数, // 同时 hook 关闭及超时回调过程 hook_error(); // 只有当流连接成功后才可 hook IO 读写状态 // hook 读回调过程 if ((oflags & (O_RDONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC))) hook_read(); // hook 写回调过程 if ((oflags & (O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC))) hook_write(); return true; }
SERVICE *service_create(const char *local_ip, short local_port, const char *dns_ip, short dns_port) { const char *myname = "service_create"; SERVICE *service; ACL_VSTREAM *sstream; char addr[64]; // 创建提供 TCP 方式查询时的监听流 snprintf(addr, sizeof(addr), "%s:%d", local_ip, local_port); sstream = acl_vstream_listen_ex(addr, 128, ACL_NON_BLOCKING, 1024, 10); if (sstream == NULL) { acl_msg_error("%s(%d): can't listen on addr(%s)", myname, __LINE__, addr); return (NULL); } service = (SERVICE*) acl_mycalloc(1, sizeof(SERVICE)); ACL_SAFE_STRNCPY(service->listen_addr, addr, sizeof(service->listen_addr)); ACL_SAFE_STRNCPY(service->dns_ip, dns_ip, sizeof(service->dns_ip)); service->dns_port = dns_port; snprintf(service->dns_addr, sizeof(service->dns_addr), "%s:%d", dns_ip, dns_port); service->conn_timeout = 10; service->rw_timeout = 10; service->table = acl_htable_create(100, 0); service->aio = acl_aio_create(ACL_EVENT_SELECT); service->sstream = acl_aio_open(service->aio, sstream); acl_aio_ctl(service->sstream, ACL_AIO_CTL_ACCEPT_FN, accept_callback, ACL_AIO_CTL_CTX, service, ACL_AIO_CTL_END); acl_aio_accept(service->sstream); service_udp_init(service, local_ip, local_port, dns_ip, dns_port); return (service); }
/* 某个线程服务实例接收到一个新的远程客户端连接请求 */ static int msg_ipc_accept(int msg_type acl_unused, ACL_MSGIO *mio acl_unused, const ACL_MSGIO_INFO *info, void *arg) { IPC_CTX ctx; ACL_VSTREAM *stream; ACL_ASTREAM *client; SERVICE *service = (SERVICE*) arg; memcpy(&ctx, acl_vstring_str(info->body.buf), ACL_VSTRING_LEN(info->body.buf)); /* 打开异步流 */ stream = acl_vstream_fdopen(ctx.fd, O_RDWR, var_cfg_aio_buf_size, 0, ACL_VSTREAM_TYPE_SOCK); client = acl_aio_open(ctx.aio, stream); /* 开始处理该客户端连接 */ __service_callback(service, client); return (0); } /* 单线程实例非阻塞处理过程 */ static void *service_thread(void *arg) { ACL_MSGIO *ipc_client = (ACL_MSGIO*) arg; ACL_AIO *aio = acl_msgio_aio(ipc_client); /* 内存垃圾回收定时器 */ service_set_gctimer(aio, 10);