int swAio_dispatch(swAio_event *_event) { if (SwooleAIO.init == 0) { swAio_init(); } _event->task_id = SwooleAIO.current_id++; swAio_event *event = (swAio_event *) sw_malloc(sizeof(swAio_event)); if (event == NULL) { swWarn("malloc failed"); return SW_ERR; } memcpy(event, _event, sizeof(swAio_event)); if (swThreadPool_dispatch(&pool, event, sizeof(event)) < 0) { return SW_ERR; } else { SwooleAIO.task_num++; return _event->task_id; } }
int swAio_dns_lookup(void *hostname, void *ip_addr, size_t size) { swAio_event *aio_ev = (swAio_event *) sw_malloc(sizeof(swAio_event)); if (aio_ev == NULL) { swWarn("malloc failed."); return SW_ERR; } bzero(aio_ev, sizeof(swAio_event)); aio_ev->buf = ip_addr; aio_ev->req = hostname; aio_ev->type = SW_AIO_DNS_LOOKUP; aio_ev->nbytes = size; aio_ev->task_id = SwooleAIO.current_id++; if (swThreadPool_dispatch(&swAioBase_thread_pool, aio_ev, sizeof(aio_ev)) < 0) { return SW_ERR; } else { SwooleAIO.task_num++; return aio_ev->task_id; } }
static int swAioBase_read(int fd, void *inbuf, size_t size, off_t offset) { swAio_event *aio_ev = (swAio_event *) sw_malloc(sizeof(swAio_event)); if (aio_ev == NULL) { swWarn("malloc failed."); return SW_ERR; } bzero(aio_ev, sizeof(swAio_event)); aio_ev->fd = fd; aio_ev->buf = inbuf; aio_ev->type = SW_AIO_READ; aio_ev->nbytes = size; aio_ev->offset = offset; aio_ev->task_id = SwooleAIO.current_id++; if (swThreadPool_dispatch(&swAioBase_thread_pool, aio_ev, sizeof(aio_ev)) < 0) { return SW_ERR; } else { SwooleAIO.task_num++; return aio_ev->task_id; } }
/** * 写线程模式 */ int swFactoryThread_dispatch(swFactory *factory, swDispatchData *task) { swServer *serv = SwooleG.serv; swFactoryThread *object = factory->object; if (swEventData_is_stream(task->data.info.type)) { swConnection *conn = swServer_connection_get(serv, task->data.info.fd); if (conn == NULL || conn->active == 0) { swWarn("dispatch[type=%d] failed, connection#%d is not active.", task->data.info.type, task->data.info.fd); return SW_ERR; } //server active close, discard data. if (conn->closed) { swWarn("dispatch[type=%d] failed, connection#%d is closed by server.", task->data.info.type, task->data.info.fd); return SW_OK; } //converted fd to session_id task->data.info.fd = conn->session_id; } int mem_size = sizeof(swDataHead) + task->data.info.len + 1; char *data = sw_malloc(mem_size); if (data == NULL) { swWarn("malloc failed"); return SW_ERR; } memcpy(data, &(task->data), mem_size); data[sizeof(swDataHead) + task->data.info.len] = 0; if (swThreadPool_dispatch(&object->workers, (void *) data, 0) < 0) { swWarn("RingQueue is full"); return SW_ERR; } else { return SW_OK; } }