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
51129fe122SShuo Chen  //void send(const void* message, size_t len);
521bc8a9dfSShuo Chen  // Thread safe.
53129fe122SShuo Chen  void send(const std::string& message);
541bc8a9dfSShuo Chen  // Thread safe.
55129fe122SShuo Chen  void shutdown();
56129fe122SShuo Chen
572a18e699SShuo Chen  void setConnectionCallback(const ConnectionCallback& cb)
582a18e699SShuo Chen  { connectionCallback_ = cb; }
592a18e699SShuo Chen
602a18e699SShuo Chen  void setMessageCallback(const MessageCallback& cb)
612a18e699SShuo Chen  { messageCallback_ = cb; }
622a18e699SShuo Chen
632a18e699SShuo Chen  /// Internal use only.
642a18e699SShuo Chen  void setCloseCallback(const CloseCallback& cb)
652a18e699SShuo Chen  { closeCallback_ = cb; }
662a18e699SShuo Chen
672a18e699SShuo Chen  // called when TcpServer accepts a new connection
682a18e699SShuo Chen  void connectEstablished();   // should be called only once
692a18e699SShuo Chen  // called when TcpServer has removed me from its map
702a18e699SShuo Chen  void connectDestroyed();  // should be called only once
712a18e699SShuo Chen
722a18e699SShuo Chen private:
73129fe122SShuo Chen  enum StateE { kConnecting, kConnected, kDisconnecting, kDisconnected, };
742a18e699SShuo Chen
752a18e699SShuo Chen  void setState(StateE s) { state_ = s; }
762a18e699SShuo Chen  void handleRead(Timestamp receiveTime);
772a18e699SShuo Chen  void handleWrite();
782a18e699SShuo Chen  void handleClose();
792a18e699SShuo Chen  void handleError();
80129fe122SShuo Chen  void sendInLoop(const std::string& message);
81129fe122SShuo Chen  void shutdownInLoop();
822a18e699SShuo Chen
832a18e699SShuo Chen  EventLoop* loop_;
842a18e699SShuo Chen  std::string name_;
852a18e699SShuo Chen  StateE state_;  // FIXME: use atomic variable
862a18e699SShuo Chen  // we don't expose those classes to client.
872a18e699SShuo Chen  boost::scoped_ptr<Socket> socket_;
882a18e699SShuo Chen  boost::scoped_ptr<Channel> channel_;
892a18e699SShuo Chen  InetAddress localAddr_;
902a18e699SShuo Chen  InetAddress peerAddr_;
912a18e699SShuo Chen  ConnectionCallback connectionCallback_;
922a18e699SShuo Chen  MessageCallback messageCallback_;
932a18e699SShuo Chen  CloseCallback closeCallback_;
942a18e699SShuo Chen  Buffer inputBuffer_;
95129fe122SShuo Chen  Buffer outputBuffer_;
962a18e699SShuo Chen};
972a18e699SShuo Chen
982a18e699SShuo Chentypedef boost::shared_ptr<TcpConnection> TcpConnectionPtr;
992a18e699SShuo Chen
1002a18e699SShuo Chen}
1012a18e699SShuo Chen
1022a18e699SShuo Chen#endif  // MUDUO_NET_TCPCONNECTION_H
103