1048f6023SShuo Chen // excerpts from http://code.google.com/p/muduo/
2048f6023SShuo Chen //
3048f6023SShuo Chen // Use of this source code is governed by a BSD-style license
4048f6023SShuo Chen // that can be found in the License file.
5048f6023SShuo Chen //
6048f6023SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
7048f6023SShuo Chen 
8048f6023SShuo Chen #ifndef MUDUO_NET_TCPSERVER_H
9048f6023SShuo Chen #define MUDUO_NET_TCPSERVER_H
10048f6023SShuo Chen 
11048f6023SShuo Chen #include "Callbacks.h"
12048f6023SShuo Chen #include "TcpConnection.h"
13048f6023SShuo Chen 
14048f6023SShuo Chen #include <map>
15048f6023SShuo Chen #include <boost/noncopyable.hpp>
16048f6023SShuo Chen #include <boost/scoped_ptr.hpp>
17048f6023SShuo Chen 
18048f6023SShuo Chen namespace muduo
19048f6023SShuo Chen {
20048f6023SShuo Chen 
21048f6023SShuo Chen class Acceptor;
22048f6023SShuo Chen class EventLoop;
23048f6023SShuo Chen 
24048f6023SShuo Chen class TcpServer : boost::noncopyable
25048f6023SShuo Chen {
26048f6023SShuo Chen  public:
27048f6023SShuo Chen 
28048f6023SShuo Chen   TcpServer(EventLoop* loop, const InetAddress& listenAddr);
29048f6023SShuo Chen   ~TcpServer();  // force out-line dtor, for scoped_ptr members.
30048f6023SShuo Chen 
31048f6023SShuo Chen   /// Starts the server if it's not listenning.
32048f6023SShuo Chen   ///
33048f6023SShuo Chen   /// It's harmless to call it multiple times.
34048f6023SShuo Chen   /// Thread safe.
35048f6023SShuo Chen   void start();
36048f6023SShuo Chen 
37048f6023SShuo Chen   /// Set connection callback.
38048f6023SShuo Chen   /// Not thread safe.
39048f6023SShuo Chen   void setConnectionCallback(const ConnectionCallback& cb)
40048f6023SShuo Chen   { connectionCallback_ = cb; }
41048f6023SShuo Chen 
42048f6023SShuo Chen   /// Set message callback.
43048f6023SShuo Chen   /// Not thread safe.
44048f6023SShuo Chen   void setMessageCallback(const MessageCallback& cb)
45048f6023SShuo Chen   { messageCallback_ = cb; }
46048f6023SShuo Chen 
47048f6023SShuo Chen  private:
48048f6023SShuo Chen   /// Not thread safe, but in loop
49048f6023SShuo Chen   void newConnection(int sockfd, const InetAddress& peerAddr);
50048f6023SShuo Chen+  void removeConnection(const TcpConnectionPtr& conn);
51048f6023SShuo Chen 
52048f6023SShuo Chen   typedef std::map<std::string, TcpConnectionPtr> ConnectionMap;
53048f6023SShuo Chen 
54048f6023SShuo Chen   EventLoop* loop_;  // the acceptor loop
55048f6023SShuo Chen   const std::string name_;
56048f6023SShuo Chen   boost::scoped_ptr<Acceptor> acceptor_; // avoid revealing Acceptor
57048f6023SShuo Chen   ConnectionCallback connectionCallback_;
58048f6023SShuo Chen   MessageCallback messageCallback_;
59048f6023SShuo Chen   bool started_;
60048f6023SShuo Chen   int nextConnId_;  // always in loop thread
61048f6023SShuo Chen   ConnectionMap connections_;
62048f6023SShuo Chen };
63048f6023SShuo Chen 
64048f6023SShuo Chen }
65048f6023SShuo Chen 
66048f6023SShuo Chen #endif  // MUDUO_NET_TCPSERVER_H
67