在第三篇时,有提到server根据factory_mode创建不同的factory对象,之前的文章分析了基于多线程的factory对象,这里我们分析多进程模式的factory对象的创建过程,这里分析的代码的入口在文件在E:\swoole-src-master\src\network\ReactorThread.c里面,入口函数为:swFactoryProcess_create,而swFactoryProcess_create的实现代码在E:\swoole-src-master\src\factory\FactoryProcess.c里面。
int swFactoryProcess_create(swFactory *factory, int worker_num)
{
swFactoryProcess *object;
//从swoole自己的内存池申请空间及初始化
object = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swFactoryProcess));
if (object == NULL)
{
swWarn("[Master] malloc[object] failed");
return SW_ERR;
}
factory->object = object;//factory对象的object属性设置为object,这里具体的作用后续分析
factory->dispatch = swFactoryProcess_dispatch;//设置factory对象的dispatch回调函数
factory->finish = swFactoryProcess_finish;//设置factory对象的finish回调函数
factory->start = swFactoryProcess_start;//设置factory对象的start回调函数
factory->notify = swFactoryProcess_notify;//设置factory对象的notify回调函数
factory->shutdown = swFactoryProcess_shutdown;//设置factory对象的shutdown回调函数
factory->end = swFactoryProcess_end;//设置factory对象的end回调函数
return SW_OK;
}
这里再把第三篇里的另外个逻辑做下分析,即factory_mode为SW_MODE_BASE,这种模式时worker和reactor是同类角色。
int swFactory_create(swFactory *factory)
{
factory->dispatch = swFactory_dispatch;//factory对象的dispatch回调函数
factory->finish = swFactory_finish;//factory对象的finish回调函数
factory->start = swFactory_start;//factory对象的start回调函数
factory->shutdown = swFactory_shutdown;//factory对象的shutdown回调函数
factory->end = swFactory_end;//factory对象的end回调函数
factory->notify = swFactory_notify;//factory对象的notify回调函数
return SW_OK;
}