15af4b7fbSShuo Chen // excerpts from http://code.google.com/p/muduo/
25af4b7fbSShuo Chen //
35af4b7fbSShuo Chen // Use of this source code is governed by a BSD-style license
45af4b7fbSShuo Chen // that can be found in the License file.
55af4b7fbSShuo Chen //
65af4b7fbSShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
75af4b7fbSShuo Chen 
85af4b7fbSShuo Chen #ifndef MUDUO_NET_TCPCONNECTION_H
95af4b7fbSShuo Chen #define MUDUO_NET_TCPCONNECTION_H
105af4b7fbSShuo Chen 
115af4b7fbSShuo Chen #include "Buffer.h"
125af4b7fbSShuo Chen #include "Callbacks.h"
135af4b7fbSShuo Chen #include "InetAddress.h"
145af4b7fbSShuo Chen 
155af4b7fbSShuo Chen #include <boost/any.hpp>
165af4b7fbSShuo Chen #include <boost/enable_shared_from_this.hpp>
175af4b7fbSShuo Chen #include <boost/noncopyable.hpp>
185af4b7fbSShuo Chen #include <boost/scoped_ptr.hpp>
195af4b7fbSShuo Chen #include <boost/shared_ptr.hpp>
205af4b7fbSShuo Chen 
215af4b7fbSShuo Chen namespace muduo
225af4b7fbSShuo Chen {
235af4b7fbSShuo Chen 
245af4b7fbSShuo Chen class Channel;
255af4b7fbSShuo Chen class EventLoop;
265af4b7fbSShuo Chen class Socket;
275af4b7fbSShuo Chen 
285af4b7fbSShuo Chen ///
295af4b7fbSShuo Chen /// TCP connection, for both client and server usage.
305af4b7fbSShuo Chen ///
315af4b7fbSShuo Chen class TcpConnection : boost::noncopyable,
325af4b7fbSShuo Chen                       public boost::enable_shared_from_this<TcpConnection>
335af4b7fbSShuo Chen {
345af4b7fbSShuo Chen  public:
355af4b7fbSShuo Chen   /// Constructs a TcpConnection with a connected sockfd
365af4b7fbSShuo Chen   ///
375af4b7fbSShuo Chen   /// User should not create this object.
385af4b7fbSShuo Chen   TcpConnection(EventLoop* loop,
395af4b7fbSShuo Chen                 const std::string& name,
405af4b7fbSShuo Chen                 int sockfd,
415af4b7fbSShuo Chen                 const InetAddress& localAddr,
425af4b7fbSShuo Chen                 const InetAddress& peerAddr);
435af4b7fbSShuo Chen   ~TcpConnection();
445af4b7fbSShuo Chen 
455af4b7fbSShuo Chen   EventLoop* getLoop() const { return loop_; }
465af4b7fbSShuo Chen   const std::string& name() const { return name_; }
475af4b7fbSShuo Chen   const InetAddress& localAddress() { return localAddr_; }
485af4b7fbSShuo Chen   const InetAddress& peerAddress() { return peerAddr_; }
495af4b7fbSShuo Chen   bool connected() const { return state_ == kConnected; }
505af4b7fbSShuo Chen 
515af4b7fbSShuo Chen   //void send(const void* message, size_t len);
525af4b7fbSShuo Chen   // Thread safe.
535af4b7fbSShuo Chen   void send(const std::string& message);
545af4b7fbSShuo Chen   // Thread safe.
555af4b7fbSShuo Chen   void shutdown();
565af4b7fbSShuo Chen+  void setTcpNoDelay(bool on);
575af4b7fbSShuo Chen 
585af4b7fbSShuo Chen   void setConnectionCallback(const ConnectionCallback& cb)
595af4b7fbSShuo Chen   { connectionCallback_ = cb; }
605af4b7fbSShuo Chen 
615af4b7fbSShuo Chen   void setMessageCallback(const MessageCallback& cb)
625af4b7fbSShuo Chen   { messageCallback_ = cb; }
635af4b7fbSShuo Chen 
645af4b7fbSShuo Chen+  void setWriteCompleteCallback(const WriteCompleteCallback& cb)
655af4b7fbSShuo Chen+  { writeCompleteCallback_ = cb; }
665af4b7fbSShuo Chen+
675af4b7fbSShuo Chen   /// Internal use only.
685af4b7fbSShuo Chen   void setCloseCallback(const CloseCallback& cb)
695af4b7fbSShuo Chen   { closeCallback_ = cb; }
705af4b7fbSShuo Chen 
715af4b7fbSShuo Chen   // called when TcpServer accepts a new connection
725af4b7fbSShuo Chen   void connectEstablished();   // should be called only once
735af4b7fbSShuo Chen   // called when TcpServer has removed me from its map
745af4b7fbSShuo Chen   void connectDestroyed();  // should be called only once
755af4b7fbSShuo Chen 
765af4b7fbSShuo Chen  private:
775af4b7fbSShuo Chen   enum StateE { kConnecting, kConnected, kDisconnecting, kDisconnected, };
785af4b7fbSShuo Chen 
795af4b7fbSShuo Chen   void setState(StateE s) { state_ = s; }
805af4b7fbSShuo Chen   void handleRead(Timestamp receiveTime);
815af4b7fbSShuo Chen   void handleWrite();
825af4b7fbSShuo Chen   void handleClose();
835af4b7fbSShuo Chen   void handleError();
845af4b7fbSShuo Chen   void sendInLoop(const std::string& message);
855af4b7fbSShuo Chen   void shutdownInLoop();
865af4b7fbSShuo Chen 
875af4b7fbSShuo Chen   EventLoop* loop_;
885af4b7fbSShuo Chen   std::string name_;
895af4b7fbSShuo Chen   StateE state_;  // FIXME: use atomic variable
905af4b7fbSShuo Chen   // we don't expose those classes to client.
915af4b7fbSShuo Chen   boost::scoped_ptr<Socket> socket_;
925af4b7fbSShuo Chen   boost::scoped_ptr<Channel> channel_;
935af4b7fbSShuo Chen   InetAddress localAddr_;
945af4b7fbSShuo Chen   InetAddress peerAddr_;
955af4b7fbSShuo Chen   ConnectionCallback connectionCallback_;
965af4b7fbSShuo Chen   MessageCallback messageCallback_;
975af4b7fbSShuo Chen+  WriteCompleteCallback writeCompleteCallback_;
985af4b7fbSShuo Chen   CloseCallback closeCallback_;
995af4b7fbSShuo Chen   Buffer inputBuffer_;
1005af4b7fbSShuo Chen   Buffer outputBuffer_;
1015af4b7fbSShuo Chen };
1025af4b7fbSShuo Chen 
1035af4b7fbSShuo Chen typedef boost::shared_ptr<TcpConnection> TcpConnectionPtr;
1045af4b7fbSShuo Chen 
1055af4b7fbSShuo Chen }
1065af4b7fbSShuo Chen 
1075af4b7fbSShuo Chen #endif  // MUDUO_NET_TCPCONNECTION_H
108