1e54e5389SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2e54e5389SShuo Chen// 3e54e5389SShuo Chen// Use of this source code is governed by a BSD-style license 4e54e5389SShuo Chen// that can be found in the License file. 5e54e5389SShuo Chen// 6e54e5389SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7e54e5389SShuo Chen 8e54e5389SShuo Chen#ifndef MUDUO_NET_TCPCONNECTION_H 9e54e5389SShuo Chen#define MUDUO_NET_TCPCONNECTION_H 10e54e5389SShuo Chen 11e54e5389SShuo Chen#include "Callbacks.h" 12e54e5389SShuo Chen#include "InetAddress.h" 13e54e5389SShuo Chen 14e54e5389SShuo Chen#include <boost/any.hpp> 15e54e5389SShuo Chen#include <boost/enable_shared_from_this.hpp> 16e54e5389SShuo Chen#include <boost/noncopyable.hpp> 17e54e5389SShuo Chen#include <boost/scoped_ptr.hpp> 18e54e5389SShuo Chen#include <boost/shared_ptr.hpp> 19e54e5389SShuo Chen 20e54e5389SShuo Chennamespace muduo 21e54e5389SShuo Chen{ 22e54e5389SShuo Chen 23e54e5389SShuo Chenclass Channel; 24e54e5389SShuo Chenclass EventLoop; 25e54e5389SShuo Chenclass Socket; 26e54e5389SShuo Chen 27e54e5389SShuo Chen/// 28e54e5389SShuo Chen/// TCP connection, for both client and server usage. 29e54e5389SShuo Chen/// 30e54e5389SShuo Chenclass TcpConnection : boost::noncopyable, 31e54e5389SShuo Chen public boost::enable_shared_from_this<TcpConnection> 32e54e5389SShuo Chen{ 33e54e5389SShuo Chen public: 34e54e5389SShuo Chen /// Constructs a TcpConnection with a connected sockfd 35e54e5389SShuo Chen /// 36e54e5389SShuo Chen /// User should not create this object. 37e54e5389SShuo Chen TcpConnection(EventLoop* loop, 38e54e5389SShuo Chen const std::string& name, 39e54e5389SShuo Chen int sockfd, 40e54e5389SShuo Chen const InetAddress& localAddr, 41e54e5389SShuo Chen const InetAddress& peerAddr); 42e54e5389SShuo Chen ~TcpConnection(); 43e54e5389SShuo Chen 44e54e5389SShuo Chen EventLoop* getLoop() const { return loop_; } 45e54e5389SShuo Chen const std::string& name() const { return name_; } 46e54e5389SShuo Chen const InetAddress& localAddress() { return localAddr_; } 47e54e5389SShuo Chen const InetAddress& peerAddress() { return peerAddr_; } 48e54e5389SShuo Chen bool connected() const { return state_ == kConnected; } 49e54e5389SShuo Chen 50e54e5389SShuo Chen void setConnectionCallback(const ConnectionCallback& cb) 51e54e5389SShuo Chen { connectionCallback_ = cb; } 52e54e5389SShuo Chen 53e54e5389SShuo Chen void setMessageCallback(const MessageCallback& cb) 54e54e5389SShuo Chen { messageCallback_ = cb; } 55e54e5389SShuo Chen 56e54e5389SShuo Chen /// Internal use only. 57e54e5389SShuo Chen 58e54e5389SShuo Chen // called when TcpServer accepts a new connection 59e54e5389SShuo Chen void connectEstablished(); // should be called only once 60e54e5389SShuo Chen 61e54e5389SShuo Chen private: 6217c057c3SShuo Chen enum StateE { kConnecting, kConnected, }; 63e54e5389SShuo Chen 64e54e5389SShuo Chen void setState(StateE s) { state_ = s; } 65e54e5389SShuo Chen void handleRead(); 66e54e5389SShuo Chen 67e54e5389SShuo Chen EventLoop* loop_; 68e54e5389SShuo Chen std::string name_; 69e54e5389SShuo Chen StateE state_; // FIXME: use atomic variable 70e54e5389SShuo Chen // we don't expose those classes to client. 71e54e5389SShuo Chen boost::scoped_ptr<Socket> socket_; 72e54e5389SShuo Chen boost::scoped_ptr<Channel> channel_; 73e54e5389SShuo Chen InetAddress localAddr_; 74e54e5389SShuo Chen InetAddress peerAddr_; 75e54e5389SShuo Chen ConnectionCallback connectionCallback_; 76e54e5389SShuo Chen MessageCallback messageCallback_; 77e54e5389SShuo Chen}; 78e54e5389SShuo Chen 79e54e5389SShuo Chentypedef boost::shared_ptr<TcpConnection> TcpConnectionPtr; 80e54e5389SShuo Chen 81e54e5389SShuo Chen} 82e54e5389SShuo Chen 83e54e5389SShuo Chen#endif // MUDUO_NET_TCPCONNECTION_H 84