1e254a845SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2e254a845SShuo Chen// 3e254a845SShuo Chen// Use of this source code is governed by a BSD-style license 4e254a845SShuo Chen// that can be found in the License file. 5e254a845SShuo Chen// 6e254a845SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7e254a845SShuo Chen 8e254a845SShuo Chen#ifndef MUDUO_NET_TCPSERVER_H 9e254a845SShuo Chen#define MUDUO_NET_TCPSERVER_H 10e254a845SShuo Chen 11e254a845SShuo Chen#include "Callbacks.h" 12e254a845SShuo Chen#include "TcpConnection.h" 13e254a845SShuo Chen 14e254a845SShuo Chen#include <map> 15e254a845SShuo Chen#include <boost/noncopyable.hpp> 16e254a845SShuo Chen#include <boost/scoped_ptr.hpp> 17e254a845SShuo Chen 18e254a845SShuo Chennamespace muduo 19e254a845SShuo Chen{ 20e254a845SShuo Chen 21e254a845SShuo Chenclass Acceptor; 22e254a845SShuo Chenclass EventLoop; 2399dd5949SShuo Chenclass EventLoopThreadPool; 24e254a845SShuo Chen 25e254a845SShuo Chenclass TcpServer : boost::noncopyable 26e254a845SShuo Chen{ 27e254a845SShuo Chen public: 28e254a845SShuo Chen 29e254a845SShuo Chen TcpServer(EventLoop* loop, const InetAddress& listenAddr); 30e254a845SShuo Chen ~TcpServer(); // force out-line dtor, for scoped_ptr members. 31e254a845SShuo Chen 3299dd5949SShuo Chen /// Set the number of threads for handling input. 3399dd5949SShuo Chen /// 3499dd5949SShuo Chen /// Always accepts new connection in loop's thread. 3599dd5949SShuo Chen /// Must be called before @c start 3699dd5949SShuo Chen /// @param numThreads 3799dd5949SShuo Chen /// - 0 means all I/O in loop's thread, no thread will created. 3899dd5949SShuo Chen /// this is the default value. 3999dd5949SShuo Chen /// - 1 means all I/O in another thread. 4099dd5949SShuo Chen /// - N means a thread pool with N threads, new connections 4199dd5949SShuo Chen /// are assigned on a round-robin basis. 4299dd5949SShuo Chen void setThreadNum(int numThreads); 4399dd5949SShuo Chen 44e254a845SShuo Chen /// Starts the server if it's not listenning. 45e254a845SShuo Chen /// 46e254a845SShuo Chen /// It's harmless to call it multiple times. 47e254a845SShuo Chen /// Thread safe. 48e254a845SShuo Chen void start(); 49e254a845SShuo Chen 50e254a845SShuo Chen /// Set connection callback. 51e254a845SShuo Chen /// Not thread safe. 52e254a845SShuo Chen void setConnectionCallback(const ConnectionCallback& cb) 53e254a845SShuo Chen { connectionCallback_ = cb; } 54e254a845SShuo Chen 55e254a845SShuo Chen /// Set message callback. 56e254a845SShuo Chen /// Not thread safe. 57e254a845SShuo Chen void setMessageCallback(const MessageCallback& cb) 58e254a845SShuo Chen { messageCallback_ = cb; } 59e254a845SShuo Chen 60e254a845SShuo Chen /// Set write complete callback. 61e254a845SShuo Chen /// Not thread safe. 62e254a845SShuo Chen void setWriteCompleteCallback(const WriteCompleteCallback& cb) 63e254a845SShuo Chen { writeCompleteCallback_ = cb; } 64e254a845SShuo Chen 65e254a845SShuo Chen private: 66e254a845SShuo Chen /// Not thread safe, but in loop 67e254a845SShuo Chen void newConnection(int sockfd, const InetAddress& peerAddr); 6899dd5949SShuo Chen /// Thread safe. 69e254a845SShuo Chen void removeConnection(const TcpConnectionPtr& conn); 7099dd5949SShuo Chen /// Not thread safe, but in loop 7199dd5949SShuo Chen void removeConnectionInLoop(const TcpConnectionPtr& conn); 72e254a845SShuo Chen 73e254a845SShuo Chen typedef std::map<std::string, TcpConnectionPtr> ConnectionMap; 74e254a845SShuo Chen 75e254a845SShuo Chen EventLoop* loop_; // the acceptor loop 76e254a845SShuo Chen const std::string name_; 77e254a845SShuo Chen boost::scoped_ptr<Acceptor> acceptor_; // avoid revealing Acceptor 7899dd5949SShuo Chen boost::scoped_ptr<EventLoopThreadPool> threadPool_; 79e254a845SShuo Chen ConnectionCallback connectionCallback_; 80e254a845SShuo Chen MessageCallback messageCallback_; 81e254a845SShuo Chen WriteCompleteCallback writeCompleteCallback_; 82e254a845SShuo Chen bool started_; 83e254a845SShuo Chen int nextConnId_; // always in loop thread 84e254a845SShuo Chen ConnectionMap connections_; 85e254a845SShuo Chen}; 86e254a845SShuo Chen 87e254a845SShuo Chen} 88e254a845SShuo Chen 89e254a845SShuo Chen#endif // MUDUO_NET_TCPSERVER_H 90