int main() { printf("main(): pid = %d, tid = %d\n", ::getpid(), CurrentThread::GetTid()); EventLoopThread evtLoopThread; EventLoop* pLoop = evtLoopThread.StartLoop(); pLoop->RunInLoop(runInThread); ::sleep(1); pLoop->RunAfter(2, runInThread); ::sleep(2); pLoop->Quit(); printf("exit main().\n"); }
void TcpServer::NewConnection(socket_t fd, const InternetAddress& peerAddr) { loop_->AssertInLoopThreadOrDie(); EventLoop* ioLoop = threadGroup_->GetNextLoop(); InternetAddress localAddr(SocketsApi::GetLocalAddr(fd)); std::stringstream connNameSs; connNameSs << name_ << hostIpPort_ << "#" << nextConnId_; nextConnId_++; std::string connName = connNameSs.str(); std::cout << "[" << name_ << "] - new connection [" << connName << "] from " << peerAddr.GetIpAndPort() << std::endl; TcpConnectionPtr conn(new TcpConnection(loop_, connName, fd, localAddr, peerAddr)); //Save the new TcpConnection to this map, because life cycle is manage by shared_ptr allConnections_[connNameSs.str()] = conn; conn->SetConnectionCallBack(connCallBack_); conn->SetMessageCallBack(msgCallBack_); conn->SetCloseCallBack(std::bind(&TcpServer::RemoveConnection, this, std::placeholders::_1)); ioLoop->RunInLoop(std::bind(&TcpConnection::ConnectionEstablished, conn)); }