int main() { EventLoop el; InetAddress ia("127.0.0.1", 9527); TcpServer ts(&el, ia, "TcpServerTest", ServerOption_t::eReusePort); ts.Start(); el.Loop(); return 0; }
void EventLoopThread::threadFunc() { EventLoop eventLoop; { MutexLockGuard lockGuard(m_Mutex); m_pEventLoop = &eventLoop; m_Cond.Notify(); } eventLoop.Loop(); }
void test_timer() { //signal_init<> _signal_init_; ThreadInit _init; EventLoop service; service.RunAt(LocalClock::Now() + Seconds(2), []() { std::cout << "..." << std::endl; }); service.Loop(); }
int main(int argc, char* argv[]) { EventLoop loop; InetAddress listenAddr(10000); TcpServer server(&loop, listenAddr); server.SetConnectionCallBack(onConnection); server.SetMessageCallBack(onMessage); if (argc > 1) { server.SetThreadNum(atoi(argv[1])); } server.Start(); loop.Loop(); }
int main(int argc, char* argv[]) { EventLoop loop; Socket s; SocketUtil::SetBlock(s.GetFd()); vector<string> ip = SocketUtil::GetHostByName("ip.sysop.duowan.com"); if (ip.empty()) { cout << "GetHost failed" << endl; return -1; } //ip[0] = "127.0.0.1"; if (SocketUtil::Connect(s.GetFd(), ip[0], 80) != 0) { cout << "Connect to:" << ip[0] << " failed" << endl; return -1; } int file_fd = open("./ip.txt", O_RDWR | O_CREAT | O_TRUNC, 0664); if (file_fd < 0) { cout << "open file ip.txt error:" << strerror(file_fd) << "|" << file_fd << endl; return -1; } SocketUtil::SetNonBlock(s.GetFd()); Channel c(&loop, s.GetFd()); c.SetReadCallback(std::bind(HttpRead, file_fd, &loop, &c, _1)); c.SetWriteCallback(std::bind(HttpWrite, &c)); c.EnableWriting(); c.EnableReading(); loop.Loop(); return 0; }
void test_net() { // Signal_ignore<SIGPIPE> __; Signal::signal(SIGPIPE, []() { LOG_INFO << "SIGPIPE OCCR"; }); ThreadInit _init; EventLoop service; assert(service.IsInLoop()); LOG_INFO << "test time " << LocalClock::Now().ToString() ; LOG_INFO << "test time " << LocalClock::ToDay().ToString() ; NetAddr addr(8060); Accepter accepter(&service, addr, false); accepter.setNewConnectionCallBack([&service](int sockfd, const NetAddr & a) { LOG_INFO << "socket " << sockfd << " New Connection ,address " << a.IpPort(); TcpConPtr ptr( new TcpConnection(&service, sockfd)); ptr->ReadCallBack([](const TcpConPtr & conn) { auto &buf = *( conn->readBuffer() ); int len = buf.ReadAbleBytes(); std::string str(buf.data(), len); buf.retireRead(len); LOG_INFO << " read " << len ;//<< " msg:" << str; conn->Send(str); }); ptr->closeCallBack([](const TcpConPtr & conn) { if (conn.use_count() == 1) { LOG_INFO << "close And debug----"; } else { LOG_INFO << "just erase"; } auto &m = Conns(); auto i = std::find(m.begin(), m.end(), conn); if (i != m.end()) { m.erase(i); } LOG_INFO << "close TcpConnection from server"; }); ptr->Start(); Conns().push_back(ptr); }); LOG_INFO << fmt("%p", (void*)&service); accepter.listen(); Signal::signal(SIGINT, [&]() { auto & ts = Conns(); for (auto &i : ts) { i->forceClose(); } ts.clear(); service.quit(); }); service.Loop(); LOG_DEBUG << "out of loop"; };