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_TCPCONNECTION_H 9e254a845SShuo Chen#define MUDUO_NET_TCPCONNECTION_H 10e254a845SShuo Chen 11e254a845SShuo Chen#include "Buffer.h" 12e254a845SShuo Chen#include "Callbacks.h" 13e254a845SShuo Chen#include "InetAddress.h" 14e254a845SShuo Chen 15e254a845SShuo Chen#include <boost/any.hpp> 16e254a845SShuo Chen#include <boost/enable_shared_from_this.hpp> 17e254a845SShuo Chen#include <boost/noncopyable.hpp> 18e254a845SShuo Chen#include <boost/scoped_ptr.hpp> 19e254a845SShuo Chen#include <boost/shared_ptr.hpp> 20e254a845SShuo Chen 21e254a845SShuo Chennamespace muduo 22e254a845SShuo Chen{ 23e254a845SShuo Chen 24e254a845SShuo Chenclass Channel; 25e254a845SShuo Chenclass EventLoop; 26e254a845SShuo Chenclass Socket; 27e254a845SShuo Chen 28e254a845SShuo Chen/// 29e254a845SShuo Chen/// TCP connection, for both client and server usage. 30e254a845SShuo Chen/// 31e254a845SShuo Chenclass TcpConnection : boost::noncopyable, 32e254a845SShuo Chen public boost::enable_shared_from_this<TcpConnection> 33e254a845SShuo Chen{ 34e254a845SShuo Chen public: 35e254a845SShuo Chen /// Constructs a TcpConnection with a connected sockfd 36e254a845SShuo Chen /// 37e254a845SShuo Chen /// User should not create this object. 38e254a845SShuo Chen TcpConnection(EventLoop* loop, 39e254a845SShuo Chen const std::string& name, 40e254a845SShuo Chen int sockfd, 41e254a845SShuo Chen const InetAddress& localAddr, 42e254a845SShuo Chen const InetAddress& peerAddr); 43e254a845SShuo Chen ~TcpConnection(); 44e254a845SShuo Chen 45e254a845SShuo Chen EventLoop* getLoop() const { return loop_; } 46e254a845SShuo Chen const std::string& name() const { return name_; } 47e254a845SShuo Chen const InetAddress& localAddress() { return localAddr_; } 48e254a845SShuo Chen const InetAddress& peerAddress() { return peerAddr_; } 49e254a845SShuo Chen bool connected() const { return state_ == kConnected; } 50e254a845SShuo Chen 51e254a845SShuo Chen //void send(const void* message, size_t len); 52e254a845SShuo Chen // Thread safe. 53e254a845SShuo Chen void send(const std::string& message); 54e254a845SShuo Chen // Thread safe. 55e254a845SShuo Chen void shutdown(); 56e254a845SShuo Chen void setTcpNoDelay(bool on); 57e254a845SShuo Chen 58e254a845SShuo Chen void setConnectionCallback(const ConnectionCallback& cb) 59e254a845SShuo Chen { connectionCallback_ = cb; } 60e254a845SShuo Chen 61e254a845SShuo Chen void setMessageCallback(const MessageCallback& cb) 62e254a845SShuo Chen { messageCallback_ = cb; } 63e254a845SShuo Chen 64e254a845SShuo Chen void setWriteCompleteCallback(const WriteCompleteCallback& cb) 65e254a845SShuo Chen { writeCompleteCallback_ = cb; } 66e254a845SShuo Chen 67e254a845SShuo Chen /// Internal use only. 68e254a845SShuo Chen void setCloseCallback(const CloseCallback& cb) 69e254a845SShuo Chen { closeCallback_ = cb; } 70e254a845SShuo Chen 71e254a845SShuo Chen // called when TcpServer accepts a new connection 72e254a845SShuo Chen void connectEstablished(); // should be called only once 73e254a845SShuo Chen // called when TcpServer has removed me from its map 74e254a845SShuo Chen void connectDestroyed(); // should be called only once 75e254a845SShuo Chen 76e254a845SShuo Chen private: 77e254a845SShuo Chen enum StateE { kConnecting, kConnected, kDisconnecting, kDisconnected, }; 78e254a845SShuo Chen 79e254a845SShuo Chen void setState(StateE s) { state_ = s; } 80e254a845SShuo Chen void handleRead(Timestamp receiveTime); 81e254a845SShuo Chen void handleWrite(); 82e254a845SShuo Chen void handleClose(); 83e254a845SShuo Chen void handleError(); 84e254a845SShuo Chen void sendInLoop(const std::string& message); 85e254a845SShuo Chen void shutdownInLoop(); 86e254a845SShuo Chen 87e254a845SShuo Chen EventLoop* loop_; 88e254a845SShuo Chen std::string name_; 89e254a845SShuo Chen StateE state_; // FIXME: use atomic variable 90e254a845SShuo Chen // we don't expose those classes to client. 91e254a845SShuo Chen boost::scoped_ptr<Socket> socket_; 92e254a845SShuo Chen boost::scoped_ptr<Channel> channel_; 93e254a845SShuo Chen InetAddress localAddr_; 94e254a845SShuo Chen InetAddress peerAddr_; 95e254a845SShuo Chen ConnectionCallback connectionCallback_; 96e254a845SShuo Chen MessageCallback messageCallback_; 97e254a845SShuo Chen WriteCompleteCallback writeCompleteCallback_; 98e254a845SShuo Chen CloseCallback closeCallback_; 99e254a845SShuo Chen Buffer inputBuffer_; 100e254a845SShuo Chen Buffer outputBuffer_; 101e254a845SShuo Chen}; 102e254a845SShuo Chen 103e254a845SShuo Chentypedef boost::shared_ptr<TcpConnection> TcpConnectionPtr; 104e254a845SShuo Chen 105e254a845SShuo Chen} 106e254a845SShuo Chen 107e254a845SShuo Chen#endif // MUDUO_NET_TCPCONNECTION_H 108