Exemple #1
0
int swFactoryThread_create(swFactory *factory, int worker_num)
{
    swFactoryThread *object;
    swServer *serv = factory->ptr;

    object = sw_calloc(worker_num, sizeof(swFactoryThread));
    if (object == NULL)
    {
        swWarn("malloc[0] failed");
        return SW_ERR;
    }

    if (swThreadPool_create(&object->workers, worker_num) < 0)
    {
        return SW_ERR;
    }

    int i;
    swReactorThread *thread;
    for (i = 0; i < serv->reactor_num; i++)
    {
        thread = swServer_get_thread(serv, i);
        swMutex_create(&thread->lock, 0);
    }

    object->worker_num = worker_num;

    factory->object = object;
    factory->dispatch = swFactoryThread_dispatch;
    factory->finish = swFactoryThread_finish;
    factory->end = swFactory_end;
    factory->start = swFactoryThread_start;
    factory->shutdown = swFactoryThread_shutdown;
    factory->notify = swFactory_notify;

    factory->onTask = NULL;
    factory->onFinish = NULL;

    object->workers.onStart = swFactoryThread_onStart;
    object->workers.onStop = swFactoryThread_onStop;
    object->workers.onTask = swFactoryThread_onTask;

    object->workers.ptr1 = factory->ptr;
    object->workers.ptr2 = factory;

    return SW_OK;
}
Exemple #2
0
static sw_inline int swWorker_discard_data(swServer *serv, swEventData *task)
{
    int fd = task->info.fd;
    //check connection
    swConnection *conn = swServer_connection_verify(serv, task->info.fd);
    if (conn == NULL)
    {
        if (serv->disable_notify && !serv->discard_timeout_request)
        {
            return SW_FALSE;
        }
        goto discard_data;
    }
    else
    {
        if (conn->closed)
        {
            goto discard_data;
        }
        else
        {
            return SW_FALSE;
        }
    }
    discard_data:
#ifdef SW_USE_RINGBUFFER
    if (task->info.type == SW_EVENT_PACKAGE)
    {
        swPackage package;
        memcpy(&package, task->data, sizeof(package));
        swReactorThread *thread = swServer_get_thread(SwooleG.serv, task->info.from_id);
        thread->buffer_input->free(thread->buffer_input, package.data);
        swoole_error_log(SW_LOG_WARNING, SW_ERROR_SESSION_DISCARD_TIMEOUT_DATA, "[1]received the wrong data[%d bytes] from socket#%d", package.length, fd);
    }
    else
#endif
    {
        swoole_error_log(SW_LOG_WARNING, SW_ERROR_SESSION_DISCARD_TIMEOUT_DATA, "[1]received the wrong data[%d bytes] from socket#%d", task->info.len, fd);
    }
    return SW_TRUE;
}