Esempio n. 1
0
void TcpServer::newConnection(int sockfd, const InetAddress& peerAddr)
{
    m_loop->assertInLoopThread();
    
    char buf[32];
    sprintf_s(buf, sizeof buf, ":%s#%d", m_listenAddr.IpAndPort().c_str(), nextConnId_);

    ++nextConnId_;

    LOG_INFO << "TcpServer<" << m_name << " - " << buf << "> receive new connection from <" << peerAddr.IpAndPort() << ">";
    
    EventLoop* ioLoop = threadPool_->getNextLoop();
    string connName = m_name + buf;

    InetAddress localAddr(sockets::getLocalAddr(sockfd));
    TcpConnectionPtr conn(new TcpConnection(ioLoop, connName, sockfd, localAddr, peerAddr));
        connections_[connName] = conn;
        conn->setConnectionCallback(connectionCallback_);
        conn->setMessageCallback(messageCallback_);
        conn->setWriteCompleteCallback(writeCompleteCallback_);
        conn->setCloseCallback(boost::bind(&TcpServer::removeConnection, this, _1)); // FIXME: unsafe

    ioLoop->runInLoop(boost::bind(&TcpConnection::connectEstablished, conn));
}