1354280cfSShuo Chen// excerpts from http://code.google.com/p/muduo/
2354280cfSShuo Chen//
3354280cfSShuo Chen// Use of this source code is governed by a BSD-style license
4354280cfSShuo Chen// that can be found in the License file.
5354280cfSShuo Chen//
6354280cfSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
7354280cfSShuo Chen
8354280cfSShuo Chen#ifndef MUDUO_NET_TCPSERVER_H
9354280cfSShuo Chen#define MUDUO_NET_TCPSERVER_H
10354280cfSShuo Chen
11354280cfSShuo Chen#include "Callbacks.h"
12354280cfSShuo Chen#include "TcpConnection.h"
13354280cfSShuo Chen
14354280cfSShuo Chen#include <map>
15354280cfSShuo Chen#include <boost/noncopyable.hpp>
16354280cfSShuo Chen#include <boost/scoped_ptr.hpp>
17354280cfSShuo Chen
18354280cfSShuo Chennamespace muduo
19354280cfSShuo Chen{
20354280cfSShuo Chen
21354280cfSShuo Chenclass Acceptor;
22354280cfSShuo Chenclass EventLoop;
23354280cfSShuo Chenclass EventLoopThreadPool;
24354280cfSShuo Chen
25354280cfSShuo Chenclass TcpServer : boost::noncopyable
26354280cfSShuo Chen{
27354280cfSShuo Chen public:
28354280cfSShuo Chen
29354280cfSShuo Chen  TcpServer(EventLoop* loop, const InetAddress& listenAddr);
30354280cfSShuo Chen  ~TcpServer();  // force out-line dtor, for scoped_ptr members.
31354280cfSShuo Chen
32354280cfSShuo Chen  /// Set the number of threads for handling input.
33354280cfSShuo Chen  ///
34354280cfSShuo Chen  /// Always accepts new connection in loop's thread.
35354280cfSShuo Chen  /// Must be called before @c start
36354280cfSShuo Chen  /// @param numThreads
37354280cfSShuo Chen  /// - 0 means all I/O in loop's thread, no thread will created.
38354280cfSShuo Chen  ///   this is the default value.
39354280cfSShuo Chen  /// - 1 means all I/O in another thread.
40354280cfSShuo Chen  /// - N means a thread pool with N threads, new connections
41354280cfSShuo Chen  ///   are assigned on a round-robin basis.
42354280cfSShuo Chen  void setThreadNum(int numThreads);
43354280cfSShuo Chen
44354280cfSShuo Chen  /// Starts the server if it's not listenning.
45354280cfSShuo Chen  ///
46354280cfSShuo Chen  /// It's harmless to call it multiple times.
47354280cfSShuo Chen  /// Thread safe.
48354280cfSShuo Chen  void start();
49354280cfSShuo Chen
50354280cfSShuo Chen  /// Set connection callback.
51354280cfSShuo Chen  /// Not thread safe.
52354280cfSShuo Chen  void setConnectionCallback(const ConnectionCallback& cb)
53354280cfSShuo Chen  { connectionCallback_ = cb; }
54354280cfSShuo Chen
55354280cfSShuo Chen  /// Set message callback.
56354280cfSShuo Chen  /// Not thread safe.
57354280cfSShuo Chen  void setMessageCallback(const MessageCallback& cb)
58354280cfSShuo Chen  { messageCallback_ = cb; }
59354280cfSShuo Chen
60354280cfSShuo Chen  /// Set write complete callback.
61354280cfSShuo Chen  /// Not thread safe.
62354280cfSShuo Chen  void setWriteCompleteCallback(const WriteCompleteCallback& cb)
63354280cfSShuo Chen  { writeCompleteCallback_ = cb; }
64354280cfSShuo Chen
65354280cfSShuo Chen private:
66354280cfSShuo Chen  /// Not thread safe, but in loop
67354280cfSShuo Chen  void newConnection(int sockfd, const InetAddress& peerAddr);
68354280cfSShuo Chen  /// Thread safe.
69354280cfSShuo Chen  void removeConnection(const TcpConnectionPtr& conn);
70354280cfSShuo Chen  /// Not thread safe, but in loop
71354280cfSShuo Chen  void removeConnectionInLoop(const TcpConnectionPtr& conn);
72354280cfSShuo Chen
73354280cfSShuo Chen  typedef std::map<std::string, TcpConnectionPtr> ConnectionMap;
74354280cfSShuo Chen
75354280cfSShuo Chen  EventLoop* loop_;  // the acceptor loop
76354280cfSShuo Chen  const std::string name_;
77354280cfSShuo Chen  boost::scoped_ptr<Acceptor> acceptor_; // avoid revealing Acceptor
78354280cfSShuo Chen  boost::scoped_ptr<EventLoopThreadPool> threadPool_;
79354280cfSShuo Chen  ConnectionCallback connectionCallback_;
80354280cfSShuo Chen  MessageCallback messageCallback_;
81354280cfSShuo Chen  WriteCompleteCallback writeCompleteCallback_;
82354280cfSShuo Chen  bool started_;
83354280cfSShuo Chen  int nextConnId_;  // always in loop thread
84354280cfSShuo Chen  ConnectionMap connections_;
85354280cfSShuo Chen};
86354280cfSShuo Chen
87354280cfSShuo Chen}
88354280cfSShuo Chen
89354280cfSShuo Chen#endif  // MUDUO_NET_TCPSERVER_H
90