165c497a3SShuo Chen// excerpts from http://code.google.com/p/muduo/
265c497a3SShuo Chen//
365c497a3SShuo Chen// Use of this source code is governed by a BSD-style license
465c497a3SShuo Chen// that can be found in the License file.
565c497a3SShuo Chen//
665c497a3SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
765c497a3SShuo Chen
865c497a3SShuo Chen#ifndef MUDUO_NET_TCPCONNECTION_H
965c497a3SShuo Chen#define MUDUO_NET_TCPCONNECTION_H
1065c497a3SShuo Chen
11714cd85fSShuo Chen#include "Buffer.h"
1265c497a3SShuo Chen#include "Callbacks.h"
1365c497a3SShuo Chen#include "InetAddress.h"
1465c497a3SShuo Chen
1565c497a3SShuo Chen#include <boost/any.hpp>
1665c497a3SShuo Chen#include <boost/enable_shared_from_this.hpp>
1765c497a3SShuo Chen#include <boost/noncopyable.hpp>
1865c497a3SShuo Chen#include <boost/scoped_ptr.hpp>
1965c497a3SShuo Chen#include <boost/shared_ptr.hpp>
2065c497a3SShuo Chen
2165c497a3SShuo Chennamespace muduo
2265c497a3SShuo Chen{
2365c497a3SShuo Chen
2465c497a3SShuo Chenclass Channel;
2565c497a3SShuo Chenclass EventLoop;
2665c497a3SShuo Chenclass Socket;
2765c497a3SShuo Chen
2865c497a3SShuo Chen///
2965c497a3SShuo Chen/// TCP connection, for both client and server usage.
3065c497a3SShuo Chen///
3165c497a3SShuo Chenclass TcpConnection : boost::noncopyable,
3265c497a3SShuo Chen                      public boost::enable_shared_from_this<TcpConnection>
3365c497a3SShuo Chen{
3465c497a3SShuo Chen public:
3565c497a3SShuo Chen  /// Constructs a TcpConnection with a connected sockfd
3665c497a3SShuo Chen  ///
3765c497a3SShuo Chen  /// User should not create this object.
3865c497a3SShuo Chen  TcpConnection(EventLoop* loop,
3965c497a3SShuo Chen                const std::string& name,
4065c497a3SShuo Chen                int sockfd,
4165c497a3SShuo Chen                const InetAddress& localAddr,
4265c497a3SShuo Chen                const InetAddress& peerAddr);
4365c497a3SShuo Chen  ~TcpConnection();
4465c497a3SShuo Chen
4565c497a3SShuo Chen  EventLoop* getLoop() const { return loop_; }
4665c497a3SShuo Chen  const std::string& name() const { return name_; }
4765c497a3SShuo Chen  const InetAddress& localAddress() { return localAddr_; }
4865c497a3SShuo Chen  const InetAddress& peerAddress() { return peerAddr_; }
4965c497a3SShuo Chen  bool connected() const { return state_ == kConnected; }
5065c497a3SShuo Chen
5165c497a3SShuo Chen  void setConnectionCallback(const ConnectionCallback& cb)
5265c497a3SShuo Chen  { connectionCallback_ = cb; }
5365c497a3SShuo Chen
5465c497a3SShuo Chen  void setMessageCallback(const MessageCallback& cb)
5565c497a3SShuo Chen  { messageCallback_ = cb; }
5665c497a3SShuo Chen
5765c497a3SShuo Chen  /// Internal use only.
5865c497a3SShuo Chen  void setCloseCallback(const CloseCallback& cb)
5965c497a3SShuo Chen  { closeCallback_ = cb; }
6065c497a3SShuo Chen
6165c497a3SShuo Chen  // called when TcpServer accepts a new connection
6265c497a3SShuo Chen  void connectEstablished();   // should be called only once
6365c497a3SShuo Chen  // called when TcpServer has removed me from its map
6465c497a3SShuo Chen  void connectDestroyed();  // should be called only once
6565c497a3SShuo Chen
6665c497a3SShuo Chen private:
6765c497a3SShuo Chen  enum StateE { kConnecting, kConnected, kDisconnected, };
6865c497a3SShuo Chen
6965c497a3SShuo Chen  void setState(StateE s) { state_ = s; }
70714cd85fSShuo Chen  void handleRead(Timestamp receiveTime);
7165c497a3SShuo Chen  void handleWrite();
7265c497a3SShuo Chen  void handleClose();
7365c497a3SShuo Chen  void handleError();
7465c497a3SShuo Chen
7565c497a3SShuo Chen  EventLoop* loop_;
7665c497a3SShuo Chen  std::string name_;
7765c497a3SShuo Chen  StateE state_;  // FIXME: use atomic variable
7865c497a3SShuo Chen  // we don't expose those classes to client.
7965c497a3SShuo Chen  boost::scoped_ptr<Socket> socket_;
8065c497a3SShuo Chen  boost::scoped_ptr<Channel> channel_;
8165c497a3SShuo Chen  InetAddress localAddr_;
8265c497a3SShuo Chen  InetAddress peerAddr_;
8365c497a3SShuo Chen  ConnectionCallback connectionCallback_;
8465c497a3SShuo Chen  MessageCallback messageCallback_;
8565c497a3SShuo Chen  CloseCallback closeCallback_;
86714cd85fSShuo Chen  Buffer inputBuffer_;
8765c497a3SShuo Chen};
8865c497a3SShuo Chen
8965c497a3SShuo Chentypedef boost::shared_ptr<TcpConnection> TcpConnectionPtr;
9065c497a3SShuo Chen
9165c497a3SShuo Chen}
9265c497a3SShuo Chen
9365c497a3SShuo Chen#endif  // MUDUO_NET_TCPCONNECTION_H
94