15af4b7fbSShuo Chen // excerpts from http://code.google.com/p/muduo/
25af4b7fbSShuo Chen //
35af4b7fbSShuo Chen // Use of this source code is governed by a BSD-style license
45af4b7fbSShuo Chen // that can be found in the License file.
55af4b7fbSShuo Chen //
65af4b7fbSShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
75af4b7fbSShuo Chen 
85af4b7fbSShuo Chen #ifndef MUDUO_NET_TCPSERVER_H
95af4b7fbSShuo Chen #define MUDUO_NET_TCPSERVER_H
105af4b7fbSShuo Chen 
115af4b7fbSShuo Chen #include "Callbacks.h"
125af4b7fbSShuo Chen #include "TcpConnection.h"
135af4b7fbSShuo Chen 
145af4b7fbSShuo Chen #include <map>
155af4b7fbSShuo Chen #include <boost/noncopyable.hpp>
165af4b7fbSShuo Chen #include <boost/scoped_ptr.hpp>
175af4b7fbSShuo Chen 
185af4b7fbSShuo Chen namespace muduo
195af4b7fbSShuo Chen {
205af4b7fbSShuo Chen 
215af4b7fbSShuo Chen class Acceptor;
225af4b7fbSShuo Chen class EventLoop;
235af4b7fbSShuo Chen 
245af4b7fbSShuo Chen class TcpServer : boost::noncopyable
255af4b7fbSShuo Chen {
265af4b7fbSShuo Chen  public:
275af4b7fbSShuo Chen 
285af4b7fbSShuo Chen   TcpServer(EventLoop* loop, const InetAddress& listenAddr);
295af4b7fbSShuo Chen   ~TcpServer();  // force out-line dtor, for scoped_ptr members.
305af4b7fbSShuo Chen 
315af4b7fbSShuo Chen   /// Starts the server if it's not listenning.
325af4b7fbSShuo Chen   ///
335af4b7fbSShuo Chen   /// It's harmless to call it multiple times.
345af4b7fbSShuo Chen   /// Thread safe.
355af4b7fbSShuo Chen   void start();
365af4b7fbSShuo Chen 
375af4b7fbSShuo Chen   /// Set connection callback.
385af4b7fbSShuo Chen   /// Not thread safe.
395af4b7fbSShuo Chen   void setConnectionCallback(const ConnectionCallback& cb)
405af4b7fbSShuo Chen   { connectionCallback_ = cb; }
415af4b7fbSShuo Chen 
425af4b7fbSShuo Chen   /// Set message callback.
435af4b7fbSShuo Chen   /// Not thread safe.
445af4b7fbSShuo Chen   void setMessageCallback(const MessageCallback& cb)
455af4b7fbSShuo Chen   { messageCallback_ = cb; }
465af4b7fbSShuo Chen 
475af4b7fbSShuo Chen+  /// Set write complete callback.
485af4b7fbSShuo Chen+  /// Not thread safe.
495af4b7fbSShuo Chen+  void setWriteCompleteCallback(const WriteCompleteCallback& cb)
505af4b7fbSShuo Chen+  { writeCompleteCallback_ = cb; }
515af4b7fbSShuo Chen+
525af4b7fbSShuo Chen  private:
535af4b7fbSShuo Chen   /// Not thread safe, but in loop
545af4b7fbSShuo Chen   void newConnection(int sockfd, const InetAddress& peerAddr);
555af4b7fbSShuo Chen   void removeConnection(const TcpConnectionPtr& conn);
565af4b7fbSShuo Chen 
575af4b7fbSShuo Chen   typedef std::map<std::string, TcpConnectionPtr> ConnectionMap;
585af4b7fbSShuo Chen 
595af4b7fbSShuo Chen   EventLoop* loop_;  // the acceptor loop
605af4b7fbSShuo Chen   const std::string name_;
615af4b7fbSShuo Chen   boost::scoped_ptr<Acceptor> acceptor_; // avoid revealing Acceptor
625af4b7fbSShuo Chen   ConnectionCallback connectionCallback_;
635af4b7fbSShuo Chen   MessageCallback messageCallback_;
645af4b7fbSShuo Chen+  WriteCompleteCallback writeCompleteCallback_;
655af4b7fbSShuo Chen   bool started_;
665af4b7fbSShuo Chen   int nextConnId_;  // always in loop thread
675af4b7fbSShuo Chen   ConnectionMap connections_;
685af4b7fbSShuo Chen };
695af4b7fbSShuo Chen 
705af4b7fbSShuo Chen }
715af4b7fbSShuo Chen 
725af4b7fbSShuo Chen #endif  // MUDUO_NET_TCPSERVER_H
73