// excerpts from http://code.google.com/p/muduo/ // // Use of this source code is governed by a BSD-style license // that can be found in the License file. // // Author: Shuo Chen (chenshuo at chenshuo dot com) #ifndef MUDUO_NET_TCPCONNECTION_H #define MUDUO_NET_TCPCONNECTION_H #include "Buffer.h" #include "Callbacks.h" #include "InetAddress.h" #include #include #include #include #include namespace muduo { class Channel; class EventLoop; class Socket; /// /// TCP connection, for both client and server usage. /// class TcpConnection : boost::noncopyable, public boost::enable_shared_from_this { public: /// Constructs a TcpConnection with a connected sockfd /// /// User should not create this object. TcpConnection(EventLoop* loop, const std::string& name, int sockfd, const InetAddress& localAddr, const InetAddress& peerAddr); ~TcpConnection(); EventLoop* getLoop() const { return loop_; } const std::string& name() const { return name_; } const InetAddress& localAddress() { return localAddr_; } const InetAddress& peerAddress() { return peerAddr_; } bool connected() const { return state_ == kConnected; } void setConnectionCallback(const ConnectionCallback& cb) { connectionCallback_ = cb; } void setMessageCallback(const MessageCallback& cb) { messageCallback_ = cb; } /// Internal use only. void setCloseCallback(const CloseCallback& cb) { closeCallback_ = cb; } // called when TcpServer accepts a new connection void connectEstablished(); // should be called only once // called when TcpServer has removed me from its map void connectDestroyed(); // should be called only once private: enum StateE { kConnecting, kConnected, kDisconnected, }; void setState(StateE s) { state_ = s; } void handleRead(Timestamp receiveTime); void handleWrite(); void handleClose(); void handleError(); EventLoop* loop_; std::string name_; StateE state_; // FIXME: use atomic variable // we don't expose those classes to client. boost::scoped_ptr socket_; boost::scoped_ptr channel_; InetAddress localAddr_; InetAddress peerAddr_; ConnectionCallback connectionCallback_; MessageCallback messageCallback_; CloseCallback closeCallback_; Buffer inputBuffer_; }; typedef boost::shared_ptr TcpConnectionPtr; } #endif // MUDUO_NET_TCPCONNECTION_H