TcpConnection.h revision 65c497a3
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
1165c497a3SShuo Chen#include "Callbacks.h"
1265c497a3SShuo Chen#include "InetAddress.h"
1365c497a3SShuo Chen
1465c497a3SShuo Chen#include <boost/any.hpp>
1565c497a3SShuo Chen#include <boost/enable_shared_from_this.hpp>
1665c497a3SShuo Chen#include <boost/noncopyable.hpp>
1765c497a3SShuo Chen#include <boost/scoped_ptr.hpp>
1865c497a3SShuo Chen#include <boost/shared_ptr.hpp>
1965c497a3SShuo Chen
2065c497a3SShuo Chennamespace muduo
2165c497a3SShuo Chen{
2265c497a3SShuo Chen
2365c497a3SShuo Chenclass Channel;
2465c497a3SShuo Chenclass EventLoop;
2565c497a3SShuo Chenclass Socket;
2665c497a3SShuo Chen
2765c497a3SShuo Chen///
2865c497a3SShuo Chen/// TCP connection, for both client and server usage.
2965c497a3SShuo Chen///
3065c497a3SShuo Chenclass TcpConnection : boost::noncopyable,
3165c497a3SShuo Chen                      public boost::enable_shared_from_this<TcpConnection>
3265c497a3SShuo Chen{
3365c497a3SShuo Chen public:
3465c497a3SShuo Chen  /// Constructs a TcpConnection with a connected sockfd
3565c497a3SShuo Chen  ///
3665c497a3SShuo Chen  /// User should not create this object.
3765c497a3SShuo Chen  TcpConnection(EventLoop* loop,
3865c497a3SShuo Chen                const std::string& name,
3965c497a3SShuo Chen                int sockfd,
4065c497a3SShuo Chen                const InetAddress& localAddr,
4165c497a3SShuo Chen                const InetAddress& peerAddr);
4265c497a3SShuo Chen  ~TcpConnection();
4365c497a3SShuo Chen
4465c497a3SShuo Chen  EventLoop* getLoop() const { return loop_; }
4565c497a3SShuo Chen  const std::string& name() const { return name_; }
4665c497a3SShuo Chen  const InetAddress& localAddress() { return localAddr_; }
4765c497a3SShuo Chen  const InetAddress& peerAddress() { return peerAddr_; }
4865c497a3SShuo Chen  bool connected() const { return state_ == kConnected; }
4965c497a3SShuo Chen
5065c497a3SShuo Chen  void setConnectionCallback(const ConnectionCallback& cb)
5165c497a3SShuo Chen  { connectionCallback_ = cb; }
5265c497a3SShuo Chen
5365c497a3SShuo Chen  void setMessageCallback(const MessageCallback& cb)
5465c497a3SShuo Chen  { messageCallback_ = cb; }
5565c497a3SShuo Chen
5665c497a3SShuo Chen  /// Internal use only.
5765c497a3SShuo Chen  void setCloseCallback(const CloseCallback& cb)
5865c497a3SShuo Chen  { closeCallback_ = cb; }
5965c497a3SShuo Chen
6065c497a3SShuo Chen  // called when TcpServer accepts a new connection
6165c497a3SShuo Chen  void connectEstablished();   // should be called only once
6265c497a3SShuo Chen  // called when TcpServer has removed me from its map
6365c497a3SShuo Chen  void connectDestroyed();  // should be called only once
6465c497a3SShuo Chen
6565c497a3SShuo Chen private:
6665c497a3SShuo Chen  enum StateE { kConnecting, kConnected, kDisconnected, };
6765c497a3SShuo Chen
6865c497a3SShuo Chen  void setState(StateE s) { state_ = s; }
6965c497a3SShuo Chen  void handleRead();
7065c497a3SShuo Chen  void handleWrite();
7165c497a3SShuo Chen  void handleClose();
7265c497a3SShuo Chen  void handleError();
7365c497a3SShuo Chen
7465c497a3SShuo Chen  EventLoop* loop_;
7565c497a3SShuo Chen  std::string name_;
7665c497a3SShuo Chen  StateE state_;  // FIXME: use atomic variable
7765c497a3SShuo Chen  // we don't expose those classes to client.
7865c497a3SShuo Chen  boost::scoped_ptr<Socket> socket_;
7965c497a3SShuo Chen  boost::scoped_ptr<Channel> channel_;
8065c497a3SShuo Chen  InetAddress localAddr_;
8165c497a3SShuo Chen  InetAddress peerAddr_;
8265c497a3SShuo Chen  ConnectionCallback connectionCallback_;
8365c497a3SShuo Chen  MessageCallback messageCallback_;
8465c497a3SShuo Chen  CloseCallback closeCallback_;
8565c497a3SShuo Chen};
8665c497a3SShuo Chen
8765c497a3SShuo Chentypedef boost::shared_ptr<TcpConnection> TcpConnectionPtr;
8865c497a3SShuo Chen
8965c497a3SShuo Chen}
9065c497a3SShuo Chen
9165c497a3SShuo Chen#endif  // MUDUO_NET_TCPCONNECTION_H
92