ClientSocketChannel* ServiceProcess::ConnectManagerUnixSocketServer( SocketUnixAddress& remote, SocketUnixAddress& local) { ClientSocketChannel* client = GetChannelService().NewClientSocketChannel(); client->SetChannelPipelineInitializor(IPCChannelInit, &m_ipc_event_handler); client->SetChannelPipelineFinalizer(IPCChannelFinalize, NULL); if (!client->Bind(&local)) { ERROR_LOG( "Failed to bind local unix socket:%s", local.GetPath().c_str()); client->Close(); return NULL; } ChannelOptions ops; ops.user_write_buffer_water_mark = 8182; ops.user_write_buffer_flush_timeout_mills = 1; ops.max_write_buffer_size = g_manager_proc->GetFrameworkOptions().max_ipc_buffer_size; client->Configure(ops); if (!client->Connect(&remote)) { ERROR_LOG( "Failed to connect remote unix socket:%s", remote.GetPath().c_str()); client->Close(); return NULL; } return client; }
int ServiceProcess::ConnectDispatcher(const ProcessAddress& addr) { //Dispatcher使用FIFO和其它进程通信 if (m_dispatcher_ipc_type == IPC_FIFO) { string fifodir = g_manager_proc->GetFrameworkOptions().home_dir + "/ipc"; ServiceProcessFactory* pfactory = g_manager_proc->GetServiceProcessFactory(addr.proc_type); string name = pfactory->GetName(); char temp[fifodir.size() + name.size() + 100]; sprintf(temp, "%s/%s%u_%s%u", fifodir.c_str(), GetServiceName().c_str(), GetProcessIndex(), name.c_str(), addr.proc_idx); FIFOChannel* fifo = GetChannelService().NewFIFOChannel(); fifo->SetReadPath(temp); sprintf(temp, "%s/%s%u_%s%u", fifodir.c_str(), name.c_str(), addr.proc_idx, GetServiceName().c_str(), GetProcessIndex()); fifo->SetWritePath(temp); fifo->SetOpenFlag(O_NONBLOCK); fifo->Open(); ChannelOptions ops; ops.user_write_buffer_water_mark = 8192; ops.user_write_buffer_flush_timeout_mills = 10; ops.max_write_buffer_size = g_manager_proc->GetFrameworkOptions().max_ipc_buffer_size; fifo->Configure(ops); fifo->SetChannelPipelineInitializor(IPCChannelInit, &m_ipc_event_handler); fifo->SetChannelPipelineFinalizer(IPCChannelFinalize, NULL); m_dispacher_ipc_channels[addr] = fifo; return 1; } else if (m_dispatcher_ipc_type == IPC_UnixDomainSocket) { //Dispatcher使用Unix Socket和其它进程通信 string ipcdir = g_manager_proc->GetFrameworkOptions().home_dir + "/ipc"; int retcode = chdir(ipcdir.c_str()); if (retcode != 0) { ERROR_LOG("Failed to chdir to %s", ipcdir.c_str()); return -1; } std::string remote_path; std::string local_path; ProcessAddress proc_addr; proc_addr.proc_type = GetServiceType(); proc_addr.proc_idx = GetProcessIndex(); GetManagerProcess()->GetDispatcherClientUnixSocketFileName(proc_addr, addr.proc_idx, local_path); remote_path = DispatcherProcessFactory::GetServerUnixSocketFileName( addr.proc_idx); SocketUnixAddress local(local_path); SocketUnixAddress remote(remote_path); ClientSocketChannel* client = GetChannelService().NewClientSocketChannel(); client->SetChannelPipelineInitializor(IPCChannelInit, &m_ipc_event_handler); client->SetChannelPipelineFinalizer(IPCChannelFinalize, NULL); if (!client->Bind(&local)) { ERROR_LOG( "Failed to bind local unix socket:%s", local.GetPath().c_str()); client->Close(); return -1; } ChannelOptions ops; ops.user_write_buffer_water_mark = 8182; ops.user_write_buffer_flush_timeout_mills = 1; ops.max_write_buffer_size = g_manager_proc->GetFrameworkOptions().max_ipc_buffer_size; client->Configure(ops); if (!client->Connect(&remote)) { ERROR_LOG( "Failed to connect remote unix socket:%s", remote.GetPath().c_str()); client->Close(); return -1; } m_dispacher_ipc_channels[addr] = client; chdir(g_manager_proc->GetFrameworkOptions().home_dir.c_str()); return 1; } else { ERROR_LOG( "Not supported dispatcher IPC type:%u", m_dispatcher_ipc_type); return -1; } }