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 main(int argc, char** argv) { if (argc != 2) { printf("Need one arg for this programme."); return -1; } int total = atoi(argv[1]); printf("Total:%d\n", total); ChannelService service; ClientSocketChannel* client = service.NewClientSocketChannel(); ChannelOptions ops; ops.user_write_buffer_water_mark = 8192; ops.user_write_buffer_flush_timeout_mills = 1; SocketHostAddress address("127.0.0.1", 48100); SocketUnixAddress unix_address("./server.unix"); SocketHostAddress local("127.0.0.1", 5678); SocketUnixAddress local_unix_address("./client.unix"); client->Bind(&local_unix_address); client->Connect(&unix_address); client->Configure(ops); client->GetPipeline().AddLast("decoder", new DelimiterBasedFrameDecoder(8192, CommonDelimiters::CRLFDelimiter())); client->GetPipeline().AddLast("default", new ClientHandler( total)); uint64 start = get_current_epoch_millis(); arch::logging::Logger* logger = arch::logging::LoggerFactory::GetDevelopLogger(); logger->SetLevel(arch::logging::INFO_LOG_LEVEL); service.Start(true); uint64 end = get_current_epoch_millis(); printf("Cost %llums for total %d send/recv transaction.\n", (end - start), total); return 1; }
static int http_client_connect(const char* host, int port) { ChannelService service; ClientSocketChannel* client = g_serv->NewClientSocketChannel(); SocketHostAddress address(host, port); client->Connect(&address); client->SetChannelPipelineInitializor(http_pipeline_init, client); client->SetChannelPipelineFinalizer(http_pipeline_finallize, NULL); return 0; }
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; } }