13b467340SShuo Chen // excerpts from http://code.google.com/p/muduo/ 23b467340SShuo Chen // 33b467340SShuo Chen // Use of this source code is governed by a BSD-style license 43b467340SShuo Chen // that can be found in the License file. 53b467340SShuo Chen // 63b467340SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com) 73b467340SShuo Chen 83b467340SShuo Chen #ifndef MUDUO_NET_TCPCONNECTION_H 93b467340SShuo Chen #define MUDUO_NET_TCPCONNECTION_H 103b467340SShuo Chen 113b467340SShuo Chen+#include "Buffer.h" 123b467340SShuo Chen #include "Callbacks.h" 133b467340SShuo Chen #include "InetAddress.h" 143b467340SShuo Chen 153b467340SShuo Chen #include <boost/any.hpp> 163b467340SShuo Chen #include <boost/enable_shared_from_this.hpp> 173b467340SShuo Chen #include <boost/noncopyable.hpp> 183b467340SShuo Chen #include <boost/scoped_ptr.hpp> 193b467340SShuo Chen #include <boost/shared_ptr.hpp> 203b467340SShuo Chen 213b467340SShuo Chen namespace muduo 223b467340SShuo Chen { 233b467340SShuo Chen 243b467340SShuo Chen class Channel; 253b467340SShuo Chen class EventLoop; 263b467340SShuo Chen class Socket; 273b467340SShuo Chen 283b467340SShuo Chen /// 293b467340SShuo Chen /// TCP connection, for both client and server usage. 303b467340SShuo Chen /// 313b467340SShuo Chen class TcpConnection : boost::noncopyable, 323b467340SShuo Chen public boost::enable_shared_from_this<TcpConnection> 333b467340SShuo Chen { 343b467340SShuo Chen public: 353b467340SShuo Chen /// Constructs a TcpConnection with a connected sockfd 363b467340SShuo Chen /// 373b467340SShuo Chen /// User should not create this object. 383b467340SShuo Chen TcpConnection(EventLoop* loop, 393b467340SShuo Chen const std::string& name, 403b467340SShuo Chen int sockfd, 413b467340SShuo Chen const InetAddress& localAddr, 423b467340SShuo Chen const InetAddress& peerAddr); 433b467340SShuo Chen ~TcpConnection(); 443b467340SShuo Chen 453b467340SShuo Chen EventLoop* getLoop() const { return loop_; } 463b467340SShuo Chen const std::string& name() const { return name_; } 473b467340SShuo Chen const InetAddress& localAddress() { return localAddr_; } 483b467340SShuo Chen const InetAddress& peerAddress() { return peerAddr_; } 493b467340SShuo Chen bool connected() const { return state_ == kConnected; } 503b467340SShuo Chen 513b467340SShuo Chen void setConnectionCallback(const ConnectionCallback& cb) 523b467340SShuo Chen { connectionCallback_ = cb; } 533b467340SShuo Chen 543b467340SShuo Chen void setMessageCallback(const MessageCallback& cb) 553b467340SShuo Chen { messageCallback_ = cb; } 563b467340SShuo Chen 573b467340SShuo Chen /// Internal use only. 583b467340SShuo Chen void setCloseCallback(const CloseCallback& cb) 593b467340SShuo Chen { closeCallback_ = cb; } 603b467340SShuo Chen 613b467340SShuo Chen // called when TcpServer accepts a new connection 623b467340SShuo Chen void connectEstablished(); // should be called only once 633b467340SShuo Chen // called when TcpServer has removed me from its map 643b467340SShuo Chen void connectDestroyed(); // should be called only once 653b467340SShuo Chen 663b467340SShuo Chen private: 673b467340SShuo Chen enum StateE { kConnecting, kConnected, kDisconnected, }; 683b467340SShuo Chen 693b467340SShuo Chen void setState(StateE s) { state_ = s; } 703b467340SShuo Chen! void handleRead(Timestamp receiveTime); 713b467340SShuo Chen void handleWrite(); 723b467340SShuo Chen void handleClose(); 733b467340SShuo Chen void handleError(); 743b467340SShuo Chen 753b467340SShuo Chen EventLoop* loop_; 763b467340SShuo Chen std::string name_; 773b467340SShuo Chen StateE state_; // FIXME: use atomic variable 783b467340SShuo Chen // we don't expose those classes to client. 793b467340SShuo Chen boost::scoped_ptr<Socket> socket_; 803b467340SShuo Chen boost::scoped_ptr<Channel> channel_; 813b467340SShuo Chen InetAddress localAddr_; 823b467340SShuo Chen InetAddress peerAddr_; 833b467340SShuo Chen ConnectionCallback connectionCallback_; 843b467340SShuo Chen MessageCallback messageCallback_; 853b467340SShuo Chen CloseCallback closeCallback_; 863b467340SShuo Chen+ Buffer inputBuffer_; 873b467340SShuo Chen }; 883b467340SShuo Chen 893b467340SShuo Chen typedef boost::shared_ptr<TcpConnection> TcpConnectionPtr; 903b467340SShuo Chen 913b467340SShuo Chen } 923b467340SShuo Chen 933b467340SShuo Chen #endif // MUDUO_NET_TCPCONNECTION_H 94