TcpConnection.h revision 2a18e699
12a18e699SShuo Chen// excerpts from http://code.google.com/p/muduo/ 22a18e699SShuo Chen// 32a18e699SShuo Chen// Use of this source code is governed by a BSD-style license 42a18e699SShuo Chen// that can be found in the License file. 52a18e699SShuo Chen// 62a18e699SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 72a18e699SShuo Chen 82a18e699SShuo Chen#ifndef MUDUO_NET_TCPCONNECTION_H 92a18e699SShuo Chen#define MUDUO_NET_TCPCONNECTION_H 102a18e699SShuo Chen 112a18e699SShuo Chen#include "Buffer.h" 122a18e699SShuo Chen#include "Callbacks.h" 132a18e699SShuo Chen#include "InetAddress.h" 142a18e699SShuo Chen 152a18e699SShuo Chen#include <boost/any.hpp> 162a18e699SShuo Chen#include <boost/enable_shared_from_this.hpp> 172a18e699SShuo Chen#include <boost/noncopyable.hpp> 182a18e699SShuo Chen#include <boost/scoped_ptr.hpp> 192a18e699SShuo Chen#include <boost/shared_ptr.hpp> 202a18e699SShuo Chen 212a18e699SShuo Chennamespace muduo 222a18e699SShuo Chen{ 232a18e699SShuo Chen 242a18e699SShuo Chenclass Channel; 252a18e699SShuo Chenclass EventLoop; 262a18e699SShuo Chenclass Socket; 272a18e699SShuo Chen 282a18e699SShuo Chen/// 292a18e699SShuo Chen/// TCP connection, for both client and server usage. 302a18e699SShuo Chen/// 312a18e699SShuo Chenclass TcpConnection : boost::noncopyable, 322a18e699SShuo Chen public boost::enable_shared_from_this<TcpConnection> 332a18e699SShuo Chen{ 342a18e699SShuo Chen public: 352a18e699SShuo Chen /// Constructs a TcpConnection with a connected sockfd 362a18e699SShuo Chen /// 372a18e699SShuo Chen /// User should not create this object. 382a18e699SShuo Chen TcpConnection(EventLoop* loop, 392a18e699SShuo Chen const std::string& name, 402a18e699SShuo Chen int sockfd, 412a18e699SShuo Chen const InetAddress& localAddr, 422a18e699SShuo Chen const InetAddress& peerAddr); 432a18e699SShuo Chen ~TcpConnection(); 442a18e699SShuo Chen 452a18e699SShuo Chen EventLoop* getLoop() const { return loop_; } 462a18e699SShuo Chen const std::string& name() const { return name_; } 472a18e699SShuo Chen const InetAddress& localAddress() { return localAddr_; } 482a18e699SShuo Chen const InetAddress& peerAddress() { return peerAddr_; } 492a18e699SShuo Chen bool connected() const { return state_ == kConnected; } 502a18e699SShuo Chen 512a18e699SShuo Chen void setConnectionCallback(const ConnectionCallback& cb) 522a18e699SShuo Chen { connectionCallback_ = cb; } 532a18e699SShuo Chen 542a18e699SShuo Chen void setMessageCallback(const MessageCallback& cb) 552a18e699SShuo Chen { messageCallback_ = cb; } 562a18e699SShuo Chen 572a18e699SShuo Chen /// Internal use only. 582a18e699SShuo Chen void setCloseCallback(const CloseCallback& cb) 592a18e699SShuo Chen { closeCallback_ = cb; } 602a18e699SShuo Chen 612a18e699SShuo Chen // called when TcpServer accepts a new connection 622a18e699SShuo Chen void connectEstablished(); // should be called only once 632a18e699SShuo Chen // called when TcpServer has removed me from its map 642a18e699SShuo Chen void connectDestroyed(); // should be called only once 652a18e699SShuo Chen 662a18e699SShuo Chen private: 672a18e699SShuo Chen enum StateE { kConnecting, kConnected, kDisconnected, }; 682a18e699SShuo Chen 692a18e699SShuo Chen void setState(StateE s) { state_ = s; } 702a18e699SShuo Chen void handleRead(Timestamp receiveTime); 712a18e699SShuo Chen void handleWrite(); 722a18e699SShuo Chen void handleClose(); 732a18e699SShuo Chen void handleError(); 742a18e699SShuo Chen 752a18e699SShuo Chen EventLoop* loop_; 762a18e699SShuo Chen std::string name_; 772a18e699SShuo Chen StateE state_; // FIXME: use atomic variable 782a18e699SShuo Chen // we don't expose those classes to client. 792a18e699SShuo Chen boost::scoped_ptr<Socket> socket_; 802a18e699SShuo Chen boost::scoped_ptr<Channel> channel_; 812a18e699SShuo Chen InetAddress localAddr_; 822a18e699SShuo Chen InetAddress peerAddr_; 832a18e699SShuo Chen ConnectionCallback connectionCallback_; 842a18e699SShuo Chen MessageCallback messageCallback_; 852a18e699SShuo Chen CloseCallback closeCallback_; 862a18e699SShuo Chen Buffer inputBuffer_; 872a18e699SShuo Chen}; 882a18e699SShuo Chen 892a18e699SShuo Chentypedef boost::shared_ptr<TcpConnection> TcpConnectionPtr; 902a18e699SShuo Chen 912a18e699SShuo Chen} 922a18e699SShuo Chen 932a18e699SShuo Chen#endif // MUDUO_NET_TCPCONNECTION_H 94