TcpServer.h revision e254a845
1e254a845SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2e254a845SShuo Chen// 3e254a845SShuo Chen// Use of this source code is governed by a BSD-style license 4e254a845SShuo Chen// that can be found in the License file. 5e254a845SShuo Chen// 6e254a845SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7e254a845SShuo Chen 8e254a845SShuo Chen#ifndef MUDUO_NET_TCPSERVER_H 9e254a845SShuo Chen#define MUDUO_NET_TCPSERVER_H 10e254a845SShuo Chen 11e254a845SShuo Chen#include "Callbacks.h" 12e254a845SShuo Chen#include "TcpConnection.h" 13e254a845SShuo Chen 14e254a845SShuo Chen#include <map> 15e254a845SShuo Chen#include <boost/noncopyable.hpp> 16e254a845SShuo Chen#include <boost/scoped_ptr.hpp> 17e254a845SShuo Chen 18e254a845SShuo Chennamespace muduo 19e254a845SShuo Chen{ 20e254a845SShuo Chen 21e254a845SShuo Chenclass Acceptor; 22e254a845SShuo Chenclass EventLoop; 23e254a845SShuo Chen 24e254a845SShuo Chenclass TcpServer : boost::noncopyable 25e254a845SShuo Chen{ 26e254a845SShuo Chen public: 27e254a845SShuo Chen 28e254a845SShuo Chen TcpServer(EventLoop* loop, const InetAddress& listenAddr); 29e254a845SShuo Chen ~TcpServer(); // force out-line dtor, for scoped_ptr members. 30e254a845SShuo Chen 31e254a845SShuo Chen /// Starts the server if it's not listenning. 32e254a845SShuo Chen /// 33e254a845SShuo Chen /// It's harmless to call it multiple times. 34e254a845SShuo Chen /// Thread safe. 35e254a845SShuo Chen void start(); 36e254a845SShuo Chen 37e254a845SShuo Chen /// Set connection callback. 38e254a845SShuo Chen /// Not thread safe. 39e254a845SShuo Chen void setConnectionCallback(const ConnectionCallback& cb) 40e254a845SShuo Chen { connectionCallback_ = cb; } 41e254a845SShuo Chen 42e254a845SShuo Chen /// Set message callback. 43e254a845SShuo Chen /// Not thread safe. 44e254a845SShuo Chen void setMessageCallback(const MessageCallback& cb) 45e254a845SShuo Chen { messageCallback_ = cb; } 46e254a845SShuo Chen 47e254a845SShuo Chen /// Set write complete callback. 48e254a845SShuo Chen /// Not thread safe. 49e254a845SShuo Chen void setWriteCompleteCallback(const WriteCompleteCallback& cb) 50e254a845SShuo Chen { writeCompleteCallback_ = cb; } 51e254a845SShuo Chen 52e254a845SShuo Chen private: 53e254a845SShuo Chen /// Not thread safe, but in loop 54e254a845SShuo Chen void newConnection(int sockfd, const InetAddress& peerAddr); 55e254a845SShuo Chen void removeConnection(const TcpConnectionPtr& conn); 56e254a845SShuo Chen 57e254a845SShuo Chen typedef std::map<std::string, TcpConnectionPtr> ConnectionMap; 58e254a845SShuo Chen 59e254a845SShuo Chen EventLoop* loop_; // the acceptor loop 60e254a845SShuo Chen const std::string name_; 61e254a845SShuo Chen boost::scoped_ptr<Acceptor> acceptor_; // avoid revealing Acceptor 62e254a845SShuo Chen ConnectionCallback connectionCallback_; 63e254a845SShuo Chen MessageCallback messageCallback_; 64e254a845SShuo Chen WriteCompleteCallback writeCompleteCallback_; 65e254a845SShuo Chen bool started_; 66e254a845SShuo Chen int nextConnId_; // always in loop thread 67e254a845SShuo Chen ConnectionMap connections_; 68e254a845SShuo Chen}; 69e254a845SShuo Chen 70e254a845SShuo Chen} 71e254a845SShuo Chen 72e254a845SShuo Chen#endif // MUDUO_NET_TCPSERVER_H 73