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