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