TcpConnection.h revision 17c057c3
1bfe73648SShuo Chen// excerpts from http://code.google.com/p/muduo/
2bfe73648SShuo Chen//
3bfe73648SShuo Chen// Use of this source code is governed by a BSD-style license
4bfe73648SShuo Chen// that can be found in the License file.
5bfe73648SShuo Chen//
6bfe73648SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
7bfe73648SShuo Chen
8bfe73648SShuo Chen#ifndef MUDUO_NET_TCPCONNECTION_H
9bfe73648SShuo Chen#define MUDUO_NET_TCPCONNECTION_H
10bfe73648SShuo Chen
11bfe73648SShuo Chen#include "Callbacks.h"
12bfe73648SShuo Chen#include "InetAddress.h"
13bfe73648SShuo Chen
14bfe73648SShuo Chen#include <boost/any.hpp>
15bfe73648SShuo Chen#include <boost/enable_shared_from_this.hpp>
16bfe73648SShuo Chen#include <boost/noncopyable.hpp>
17bfe73648SShuo Chen#include <boost/scoped_ptr.hpp>
18bfe73648SShuo Chen#include <boost/shared_ptr.hpp>
19bfe73648SShuo Chen
20bfe73648SShuo Chennamespace muduo
21bfe73648SShuo Chen{
22bfe73648SShuo Chen
23bfe73648SShuo Chenclass Channel;
24bfe73648SShuo Chenclass EventLoop;
25bfe73648SShuo Chenclass Socket;
26bfe73648SShuo Chen
27bfe73648SShuo Chen///
28bfe73648SShuo Chen/// TCP connection, for both client and server usage.
29bfe73648SShuo Chen///
30bfe73648SShuo Chenclass TcpConnection : boost::noncopyable,
31bfe73648SShuo Chen                      public boost::enable_shared_from_this<TcpConnection>
32bfe73648SShuo Chen{
33bfe73648SShuo Chen public:
34bfe73648SShuo Chen  /// Constructs a TcpConnection with a connected sockfd
35bfe73648SShuo Chen  ///
36bfe73648SShuo Chen  /// User should not create this object.
37bfe73648SShuo Chen  TcpConnection(EventLoop* loop,
38bfe73648SShuo Chen                const std::string& name,
39bfe73648SShuo Chen                int sockfd,
40bfe73648SShuo Chen                const InetAddress& localAddr,
41bfe73648SShuo Chen                const InetAddress& peerAddr);
42bfe73648SShuo Chen  ~TcpConnection();
43bfe73648SShuo Chen
44bfe73648SShuo Chen  EventLoop* getLoop() const { return loop_; }
45bfe73648SShuo Chen  const std::string& name() const { return name_; }
46bfe73648SShuo Chen  const InetAddress& localAddress() { return localAddr_; }
47bfe73648SShuo Chen  const InetAddress& peerAddress() { return peerAddr_; }
48bfe73648SShuo Chen  bool connected() const { return state_ == kConnected; }
49bfe73648SShuo Chen
50bfe73648SShuo Chen  void setConnectionCallback(const ConnectionCallback& cb)
51bfe73648SShuo Chen  { connectionCallback_ = cb; }
52bfe73648SShuo Chen
53bfe73648SShuo Chen  void setMessageCallback(const MessageCallback& cb)
54bfe73648SShuo Chen  { messageCallback_ = cb; }
55bfe73648SShuo Chen
56bfe73648SShuo Chen  /// Internal use only.
57bfe73648SShuo Chen  void setCloseCallback(const CloseCallback& cb)
58bfe73648SShuo Chen  { closeCallback_ = cb; }
59bfe73648SShuo Chen
60bfe73648SShuo Chen  // called when TcpServer accepts a new connection
61bfe73648SShuo Chen  void connectEstablished();   // should be called only once
6217c057c3SShuo Chen  // called when TcpServer has removed me from its map
6317c057c3SShuo Chen  void connectDestroyed();  // should be called only once
64bfe73648SShuo Chen
65bfe73648SShuo Chen private:
6617c057c3SShuo Chen  enum StateE { kConnecting, kConnected, kDisconnected, };
67bfe73648SShuo Chen
68bfe73648SShuo Chen  void setState(StateE s) { state_ = s; }
69bfe73648SShuo Chen  void handleRead();
7017c057c3SShuo Chen  void handleWrite();
7117c057c3SShuo Chen  void handleClose();
7217c057c3SShuo Chen  void handleError();
73bfe73648SShuo Chen
74bfe73648SShuo Chen  EventLoop* loop_;
75bfe73648SShuo Chen  std::string name_;
76bfe73648SShuo Chen  StateE state_;  // FIXME: use atomic variable
77bfe73648SShuo Chen  // we don't expose those classes to client.
78bfe73648SShuo Chen  boost::scoped_ptr<Socket> socket_;
79bfe73648SShuo Chen  boost::scoped_ptr<Channel> channel_;
80bfe73648SShuo Chen  InetAddress localAddr_;
81bfe73648SShuo Chen  InetAddress peerAddr_;
82bfe73648SShuo Chen  ConnectionCallback connectionCallback_;
83bfe73648SShuo Chen  MessageCallback messageCallback_;
84bfe73648SShuo Chen  CloseCallback closeCallback_;
85bfe73648SShuo Chen};
86bfe73648SShuo Chen
87bfe73648SShuo Chentypedef boost::shared_ptr<TcpConnection> TcpConnectionPtr;
88bfe73648SShuo Chen
89bfe73648SShuo Chen}
90bfe73648SShuo Chen
91bfe73648SShuo Chen#endif  // MUDUO_NET_TCPCONNECTION_H
92