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; }
ClientSocketChannel* ChannelService::NewClientSocketChannel() { ClientSocketChannel* ch = NULL; NEW(ch, ClientSocketChannel(*this)); if (NULL != ch) { Channel* c = ch; c->m_id = (((c->m_id) << 4) + TCP_CLIENT_SOCKET_CHANNEL_ID_BIT_MASK); m_channel_table[c->m_id] = c; ch->GetPipeline().Attach(ch); } return ch; }
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; }
void ServerSocketChannel::OnRead() { int fd; char addrbuf[128]; while (1) { socklen_t salen = sizeof(addrbuf); struct sockaddr* sa = (struct sockaddr*) addrbuf; #if (HAVE_ACCEPT4) fd = accept4(m_fd, sa, &salen,SOCK_NONBLOCK); #else fd = ::accept(m_fd, sa, &salen); if (fd != -1 && -1 == make_fd_nonblocking(fd)) { ERROR_LOG("failed to set opt for accept socket"); ::close(fd); return; } #endif if (-1 == fd) { if (errno == EINTR) { continue; } else { return; } } ClientSocketChannel * ch = GetService().NewClientSocketChannel(); // if (aeCreateFileEvent(serv.GetRawEventLoop(), fd, AE_READABLE, // Channel::IOEventCallback, ch) == AE_ERR) // { // int err = errno; // ERROR_LOG( // "Failed to add event for accepted client for fd:%d for reason:%s", fd, strerror(err)); // serv.DeleteChannel(ch); // ::close(fd); // return; // } ch->m_fd = fd; if (m_user_configed) { ch->Configure(m_options); } if (NULL != m_pipeline_initializor) { DEBUG_LOG("Inherit pipeline initializor from server socket."); ch->SetChannelPipelineInitializor(m_pipeline_initializor, m_pipeline_initailizor_user_data); } if (NULL != m_pipeline_finallizer) { DEBUG_LOG("Inherit pipeline finalizer from server socket."); ch->SetChannelPipelineFinalizer(m_pipeline_finallizer, m_pipeline_finallizer_user_data); } ch->SetParent(this); if (sa->sa_family != AF_UNIX) { SocketHostAddress* remote = NULL; NEW(remote, SocketHostAddress(get_host_address(*sa))); ch->setRemoteAddress(remote); } DEBUG_LOG( "Server channel(%u) Accept a client channel(%u) for fd:%d", GetID(), ch->GetID(), fd); // fire_channel_open(ch); // fire_channel_connected(ch); m_connected_socks++; GetService().GetIdlestChannelService().AttachAcceptedChannel(ch); } }
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; } }