int swFactory_dispatch(swFactory *factory, swDispatchData *task) { swServer *serv = SwooleG.serv; factory->last_from_id = task->data.info.from_id; 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; task->data.info.from_fd = conn->from_fd; } return swWorker_onTask(factory, &task->data); }
/** * [ReactorThread] dispatch request to worker */ static int swFactoryProcess_dispatch(swFactory *factory, swDispatchData *task) { uint32_t schedule_key; uint32_t send_len = sizeof(task->data.info) + task->data.info.len; uint16_t target_worker_id; swServer *serv = SwooleG.serv; if (task->target_worker_id < 0) { schedule_key = task->data.info.fd; #ifndef SW_USE_RINGBUFFER if (SwooleTG.factory_lock_target) { if (SwooleTG.factory_target_worker < 0) { target_worker_id = swServer_worker_schedule(serv, schedule_key); SwooleTG.factory_target_worker = target_worker_id; } else { target_worker_id = SwooleTG.factory_target_worker; } } else #endif { target_worker_id = swServer_worker_schedule(serv, schedule_key); } } else { target_worker_id = task->target_worker_id; } 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) { if (!(task->data.info.type == SW_EVENT_CLOSE && conn->close_force)) { swWarn("dispatch[type=%d] failed, connection#%d[session_id=%d] is closed by server.", task->data.info.type, task->data.info.fd, conn->session_id); return SW_OK; } } //converted fd to session_id task->data.info.fd = conn->session_id; task->data.info.from_fd = conn->from_fd; } return swReactorThread_send2worker((void *) &(task->data), send_len, target_worker_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; } }