void TcpServer::newConnection(int sockfd, const InetAddress& peerAddr) { loop_->assertInLoopThread(); EventLoop* ioLoop = threadPool_->getNextLoop(); char buf[64]; snprintf(buf, sizeof buf, "-%s#%d", ipPort_.c_str(), nextConnId_); ++nextConnId_; std::string connName = name_ + buf; LOG_PRINT(LogType_Info, "TcpServer::newConnection [%s] - new connection [%s] from %s", name_.c_str(), connName.c_str(), peerAddr.toIpPort().c_str()); InetAddress localAddr(SocketOps::getLocalAddr(sockfd)); // FIXME poll with zero timeout to double confirm the new connection // FIXME use make_shared if necessary TcpConnectionPtr conn(new TcpConnection(ioLoop, connName, sockfd, localAddr, peerAddr)); connections_[connName] = conn; conn->setConnectionCallback(connectionCallback_); conn->setMessageCallback(messageCallback_); conn->setWriteCompleteCallback(writeCompleteCallback_); conn->setCloseCallback( std::bind(&TcpServer::removeConnection, this, std::placeholders::_1)); // FIXME: unsafe ioLoop->runInLoop(std::bind(&TcpConnection::connectEstablished, conn)); }
void TcpServer::newConnection(){ struct sockaddr_in addr; bzero(&addr, sizeof(addr)); int sockfd = acceptor.accept(addr); InetAddress iaddr(addr); EventLoop* ioloop = evthreadpool.getNextLoop(); shared_ptr<TcpConnection> conn(new TcpConnection(ioloop, sockfd)); conn->setInetAddr(iaddr); connectionmap.insert(std::make_pair(sockfd, conn)); conn->setConnectionCallback(connectioncb); conn->setMessageCallback(std::bind(&TcpServer::CodecAndHandle, this, _1, _2)); conn->setCloseCallback(std::bind(&TcpServer::removeConnection, this, _1)); conn->setErrorCallback(std::bind(&TcpServer::error, this, _1)); ioloop->runInLoop(std::bind(&TcpConnection::established, conn)); }
int main(int argc, char* argv[]) { printf("main:tid:%d\n", CurrentThread::tid()); EventLoopThread loopThread; EventLoop* loop = loopThread.startLoop(); loop->runInLoop(runInThread); sleep(1); loop->runAfter(2, runInThread); loop->quit(); printf("exit main\n"); }
void ComServer::createComEntityAndInsert(const string& entityName, unsigned char dataBits, unsigned long baudRate, ComEntity::eMBParity parity) { loop_->assertInLoopThread(); LOG_INFO << "Comserver::createComEntityAndInsert: name[" << entityName << "]"; ComEntityMap::iterator itr = comEntities_.find(entityName); if (itr !=comEntities_.end()) { LOG_INFO << "ComServer::insertComEntity: The Entity [ " << entityName << " ] is already contained in ComServer"; return; } EventLoop* ioLoop = threadPool_->getNextLoop(); nextEntityId_++; boost::shared_ptr<ComEntity> comEntity0(new ComEntity(ioLoop, "/dev/ttyS0", 8, 9600, ComEntity::MB_PAR_NONE)); comEntity0->setMessageCallback(messageCallback_); comEntity0->setWriteCompleteCallback(writeCompleteCallback_); comEntities_[entityName] = comEntity0; ioLoop->runInLoop(boost::bind(&ComEntity::comEstablished, comEntity0)); }
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)); }
void TcpServer::newConnection(int sockfd, const AddrInet& peerAddr) { _loop->assertInLoopThread(); EventLoop* ioLoop = _threadPool->getNextLoop(); char buf[128]; snprintf(buf, sizeof buf, "%s:%s#%d",_name.c_str(), _hostport.c_str(), _nextConnId); ++_nextConnId; string connName(buf); AddrInet localAddr; localAddr.upDateLocal(sockfd); TcpConnectionPtr conn(new TcpConnection(ioLoop, connName, sockfd, localAddr, peerAddr)); _connections[connName] = conn; conn->setConnectionCallback(_connectionCallback); conn->setMessageCallback(_messageCallback); conn->setWriteCompleteCallback(_writeCompleteCallback); conn->setCloseCallback( std::bind(&TcpServer::removeConnection, this, _1)); ioLoop->runInLoop(std::bind(&TcpConnection::connectEstablished, conn)); }
void TcpServer::removeConnectionInLoop(TcpConnectionPtr& conn){ EventLoop* loop = conn->getEventLoop(); loop->runInLoop(std::bind(&TcpConnection::handleClose, conn)); connectionmap.erase(conn->getFd()); }