140161064SShuo Chen // excerpts from http://code.google.com/p/muduo/
240161064SShuo Chen //
340161064SShuo Chen // Use of this source code is governed by a BSD-style license
440161064SShuo Chen // that can be found in the License file.
540161064SShuo Chen //
640161064SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
740161064SShuo Chen 
840161064SShuo Chen #ifndef MUDUO_NET_TCPSERVER_H
940161064SShuo Chen #define MUDUO_NET_TCPSERVER_H
1040161064SShuo Chen 
1140161064SShuo Chen #include "Callbacks.h"
1240161064SShuo Chen #include "TcpConnection.h"
1340161064SShuo Chen 
1440161064SShuo Chen #include <map>
1540161064SShuo Chen #include <boost/noncopyable.hpp>
1640161064SShuo Chen #include <boost/scoped_ptr.hpp>
1740161064SShuo Chen 
1840161064SShuo Chen namespace muduo
1940161064SShuo Chen {
2040161064SShuo Chen 
2140161064SShuo Chen class Acceptor;
2240161064SShuo Chen class EventLoop;
2340161064SShuo Chen+class EventLoopThreadPool;
2440161064SShuo Chen 
2540161064SShuo Chen class TcpServer : boost::noncopyable
2640161064SShuo Chen {
2740161064SShuo Chen  public:
2840161064SShuo Chen 
2940161064SShuo Chen   TcpServer(EventLoop* loop, const InetAddress& listenAddr);
3040161064SShuo Chen   ~TcpServer();  // force out-line dtor, for scoped_ptr members.
3140161064SShuo Chen 
3240161064SShuo Chen+  /// Set the number of threads for handling input.
3340161064SShuo Chen+  ///
3440161064SShuo Chen+  /// Always accepts new connection in loop's thread.
3540161064SShuo Chen+  /// Must be called before @c start
3640161064SShuo Chen+  /// @param numThreads
3740161064SShuo Chen+  /// - 0 means all I/O in loop's thread, no thread will created.
3840161064SShuo Chen+  ///   this is the default value.
3940161064SShuo Chen+  /// - 1 means all I/O in another thread.
4040161064SShuo Chen+  /// - N means a thread pool with N threads, new connections
4140161064SShuo Chen+  ///   are assigned on a round-robin basis.
4240161064SShuo Chen+  void setThreadNum(int numThreads);
4340161064SShuo Chen+
4440161064SShuo Chen   /// Starts the server if it's not listenning.
4540161064SShuo Chen   ///
4640161064SShuo Chen   /// It's harmless to call it multiple times.
4740161064SShuo Chen   /// Thread safe.
4840161064SShuo Chen   void start();
4940161064SShuo Chen 
5040161064SShuo Chen   /// Set connection callback.
5140161064SShuo Chen   /// Not thread safe.
5240161064SShuo Chen   void setConnectionCallback(const ConnectionCallback& cb)
5340161064SShuo Chen   { connectionCallback_ = cb; }
5440161064SShuo Chen 
5540161064SShuo Chen   /// Set message callback.
5640161064SShuo Chen   /// Not thread safe.
5740161064SShuo Chen   void setMessageCallback(const MessageCallback& cb)
5840161064SShuo Chen   { messageCallback_ = cb; }
5940161064SShuo Chen 
6040161064SShuo Chen   /// Set write complete callback.
6140161064SShuo Chen   /// Not thread safe.
6240161064SShuo Chen   void setWriteCompleteCallback(const WriteCompleteCallback& cb)
6340161064SShuo Chen   { writeCompleteCallback_ = cb; }
6440161064SShuo Chen 
6540161064SShuo Chen  private:
6640161064SShuo Chen   /// Not thread safe, but in loop
6740161064SShuo Chen   void newConnection(int sockfd, const InetAddress& peerAddr);
6840161064SShuo Chen+  /// Thread safe.
6940161064SShuo Chen   void removeConnection(const TcpConnectionPtr& conn);
7040161064SShuo Chen+  /// Not thread safe, but in loop
7140161064SShuo Chen+  void removeConnectionInLoop(const TcpConnectionPtr& conn);
7240161064SShuo Chen 
7340161064SShuo Chen   typedef std::map<std::string, TcpConnectionPtr> ConnectionMap;
7440161064SShuo Chen 
7540161064SShuo Chen   EventLoop* loop_;  // the acceptor loop
7640161064SShuo Chen   const std::string name_;
7740161064SShuo Chen   boost::scoped_ptr<Acceptor> acceptor_; // avoid revealing Acceptor
7840161064SShuo Chen+  boost::scoped_ptr<EventLoopThreadPool> threadPool_;
7940161064SShuo Chen   ConnectionCallback connectionCallback_;
8040161064SShuo Chen   MessageCallback messageCallback_;
8140161064SShuo Chen   WriteCompleteCallback writeCompleteCallback_;
8240161064SShuo Chen   bool started_;
8340161064SShuo Chen   int nextConnId_;  // always in loop thread
8440161064SShuo Chen   ConnectionMap connections_;
8540161064SShuo Chen };
8640161064SShuo Chen 
8740161064SShuo Chen }
8840161064SShuo Chen 
8940161064SShuo Chen #endif  // MUDUO_NET_TCPSERVER_H
90