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