TcpServer.cc revision bfe73648
1bfe73648SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2bfe73648SShuo Chen// 3bfe73648SShuo Chen// Use of this source code is governed by a BSD-style license 4bfe73648SShuo Chen// that can be found in the License file. 5bfe73648SShuo Chen// 6bfe73648SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7bfe73648SShuo Chen 8bfe73648SShuo Chen#include "TcpServer.h" 9bfe73648SShuo Chen 10bfe73648SShuo Chen#include "logging/Logging.h" 11bfe73648SShuo Chen#include "Acceptor.h" 12bfe73648SShuo Chen#include "EventLoop.h" 13bfe73648SShuo Chen#include "SocketsOps.h" 14bfe73648SShuo Chen 15bfe73648SShuo Chen#include <boost/bind.hpp> 16bfe73648SShuo Chen 17bfe73648SShuo Chen#include <stdio.h> // snprintf 18bfe73648SShuo Chen 19bfe73648SShuo Chenusing namespace muduo; 20bfe73648SShuo Chen 21bfe73648SShuo ChenTcpServer::TcpServer(EventLoop* loop, const InetAddress& listenAddr) 22bfe73648SShuo Chen : loop_(CHECK_NOTNULL(loop)), 23bfe73648SShuo Chen name_(listenAddr.toHostPort()), 24bfe73648SShuo Chen acceptor_(new Acceptor(loop, listenAddr)), 25bfe73648SShuo Chen started_(false), 26bfe73648SShuo Chen nextConnId_(1) 27bfe73648SShuo Chen{ 28bfe73648SShuo Chen acceptor_->setNewConnectionCallback( 29bfe73648SShuo Chen boost::bind(&TcpServer::newConnection, this, _1, _2)); 30bfe73648SShuo Chen} 31bfe73648SShuo Chen 32bfe73648SShuo ChenTcpServer::~TcpServer() 33bfe73648SShuo Chen{ 34bfe73648SShuo Chen} 35bfe73648SShuo Chen 36bfe73648SShuo Chenvoid TcpServer::start() 37bfe73648SShuo Chen{ 38bfe73648SShuo Chen if (!started_) 39bfe73648SShuo Chen { 40bfe73648SShuo Chen started_ = true; 41bfe73648SShuo Chen } 42bfe73648SShuo Chen 43bfe73648SShuo Chen if (!acceptor_->listenning()) 44bfe73648SShuo Chen { 45bfe73648SShuo Chen loop_->runInLoop( 46bfe73648SShuo Chen boost::bind(&Acceptor::listen, get_pointer(acceptor_))); 47bfe73648SShuo Chen } 48bfe73648SShuo Chen} 49bfe73648SShuo Chen 50bfe73648SShuo Chenvoid TcpServer::newConnection(int sockfd, const InetAddress& peerAddr) 51bfe73648SShuo Chen{ 52bfe73648SShuo Chen loop_->assertInLoopThread(); 53bfe73648SShuo Chen char buf[32]; 54bfe73648SShuo Chen snprintf(buf, sizeof buf, "#%d", nextConnId_); 55bfe73648SShuo Chen ++nextConnId_; 56bfe73648SShuo Chen std::string connName = name_ + buf; 57bfe73648SShuo Chen 58bfe73648SShuo Chen LOG_INFO << "TcpServer::newConnection [" << name_ 59bfe73648SShuo Chen << "] - new connection [" << connName 60bfe73648SShuo Chen << "] from " << peerAddr.toHostPort(); 61bfe73648SShuo Chen InetAddress localAddr(sockets::getLocalAddr(sockfd)); 62bfe73648SShuo Chen // FIXME poll with zero timeout to double confirm the new connection 63bfe73648SShuo Chen TcpConnectionPtr conn( 64bfe73648SShuo Chen new TcpConnection(loop_, connName, sockfd, localAddr, peerAddr)); 65bfe73648SShuo Chen connections_[connName] = conn; 66bfe73648SShuo Chen conn->setConnectionCallback(connectionCallback_); 67bfe73648SShuo Chen conn->setMessageCallback(messageCallback_); 68bfe73648SShuo Chen conn->setCloseCallback( 69bfe73648SShuo Chen boost::bind(&TcpServer::removeConnection, this, _1)); 70bfe73648SShuo Chen conn->connectEstablished(); 71bfe73648SShuo Chen} 72bfe73648SShuo Chen 73bfe73648SShuo Chenvoid TcpServer::removeConnection(const TcpConnectionPtr& conn) 74bfe73648SShuo Chen{ 75bfe73648SShuo Chen // TODO 76bfe73648SShuo Chen} 77bfe73648SShuo Chen 78