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