1048f6023SShuo Chen // excerpts from http://code.google.com/p/muduo/
2048f6023SShuo Chen //
3048f6023SShuo Chen // Use of this source code is governed by a BSD-style license
4048f6023SShuo Chen // that can be found in the License file.
5048f6023SShuo Chen //
6048f6023SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
7048f6023SShuo Chen 
8048f6023SShuo Chen #ifndef MUDUO_NET_TCPCONNECTION_H
9048f6023SShuo Chen #define MUDUO_NET_TCPCONNECTION_H
10048f6023SShuo Chen 
11048f6023SShuo Chen #include "Callbacks.h"
12048f6023SShuo Chen #include "InetAddress.h"
13048f6023SShuo Chen 
14048f6023SShuo Chen #include <boost/any.hpp>
15048f6023SShuo Chen #include <boost/enable_shared_from_this.hpp>
16048f6023SShuo Chen #include <boost/noncopyable.hpp>
17048f6023SShuo Chen #include <boost/scoped_ptr.hpp>
18048f6023SShuo Chen #include <boost/shared_ptr.hpp>
19048f6023SShuo Chen 
20048f6023SShuo Chen namespace muduo
21048f6023SShuo Chen {
22048f6023SShuo Chen 
23048f6023SShuo Chen class Channel;
24048f6023SShuo Chen class EventLoop;
25048f6023SShuo Chen class Socket;
26048f6023SShuo Chen 
27048f6023SShuo Chen ///
28048f6023SShuo Chen /// TCP connection, for both client and server usage.
29048f6023SShuo Chen ///
30048f6023SShuo Chen class TcpConnection : boost::noncopyable,
31048f6023SShuo Chen                       public boost::enable_shared_from_this<TcpConnection>
32048f6023SShuo Chen {
33048f6023SShuo Chen  public:
34048f6023SShuo Chen   /// Constructs a TcpConnection with a connected sockfd
35048f6023SShuo Chen   ///
36048f6023SShuo Chen   /// User should not create this object.
37048f6023SShuo Chen   TcpConnection(EventLoop* loop,
38048f6023SShuo Chen                 const std::string& name,
39048f6023SShuo Chen                 int sockfd,
40048f6023SShuo Chen                 const InetAddress& localAddr,
41048f6023SShuo Chen                 const InetAddress& peerAddr);
42048f6023SShuo Chen   ~TcpConnection();
43048f6023SShuo Chen 
44048f6023SShuo Chen   EventLoop* getLoop() const { return loop_; }
45048f6023SShuo Chen   const std::string& name() const { return name_; }
46048f6023SShuo Chen   const InetAddress& localAddress() { return localAddr_; }
47048f6023SShuo Chen   const InetAddress& peerAddress() { return peerAddr_; }
48048f6023SShuo Chen   bool connected() const { return state_ == kConnected; }
49048f6023SShuo Chen 
50048f6023SShuo Chen   void setConnectionCallback(const ConnectionCallback& cb)
51048f6023SShuo Chen   { connectionCallback_ = cb; }
52048f6023SShuo Chen 
53048f6023SShuo Chen   void setMessageCallback(const MessageCallback& cb)
54048f6023SShuo Chen   { messageCallback_ = cb; }
55048f6023SShuo Chen 
56048f6023SShuo Chen   /// Internal use only.
57048f6023SShuo Chen+  void setCloseCallback(const CloseCallback& cb)
58048f6023SShuo Chen+  { closeCallback_ = cb; }
59048f6023SShuo Chen 
60048f6023SShuo Chen   // called when TcpServer accepts a new connection
61048f6023SShuo Chen   void connectEstablished();   // should be called only once
62048f6023SShuo Chen+  // called when TcpServer has removed me from its map
63048f6023SShuo Chen+  void connectDestroyed();  // should be called only once
64048f6023SShuo Chen 
65048f6023SShuo Chen  private:
66048f6023SShuo Chen!  enum StateE { kConnecting, kConnected, kDisconnected, };
67048f6023SShuo Chen 
68048f6023SShuo Chen   void setState(StateE s) { state_ = s; }
69048f6023SShuo Chen   void handleRead();
70048f6023SShuo Chen+  void handleWrite();
71048f6023SShuo Chen+  void handleClose();
72048f6023SShuo Chen+  void handleError();
73048f6023SShuo Chen 
74048f6023SShuo Chen   EventLoop* loop_;
75048f6023SShuo Chen   std::string name_;
76048f6023SShuo Chen   StateE state_;  // FIXME: use atomic variable
77048f6023SShuo Chen   // we don't expose those classes to client.
78048f6023SShuo Chen   boost::scoped_ptr<Socket> socket_;
79048f6023SShuo Chen   boost::scoped_ptr<Channel> channel_;
80048f6023SShuo Chen   InetAddress localAddr_;
81048f6023SShuo Chen   InetAddress peerAddr_;
82048f6023SShuo Chen   ConnectionCallback connectionCallback_;
83048f6023SShuo Chen   MessageCallback messageCallback_;
84048f6023SShuo Chen+  CloseCallback closeCallback_;
85048f6023SShuo Chen };
86048f6023SShuo Chen 
87048f6023SShuo Chen typedef boost::shared_ptr<TcpConnection> TcpConnectionPtr;
88048f6023SShuo Chen 
89048f6023SShuo Chen }
90048f6023SShuo Chen 
91048f6023SShuo Chen #endif  // MUDUO_NET_TCPCONNECTION_H
92