11bc8a9dfSShuo Chen // excerpts from http://code.google.com/p/muduo/
21bc8a9dfSShuo Chen //
31bc8a9dfSShuo Chen // Use of this source code is governed by a BSD-style license
41bc8a9dfSShuo Chen // that can be found in the License file.
51bc8a9dfSShuo Chen //
61bc8a9dfSShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
71bc8a9dfSShuo Chen 
81bc8a9dfSShuo Chen #ifndef MUDUO_NET_TCPCONNECTION_H
91bc8a9dfSShuo Chen #define MUDUO_NET_TCPCONNECTION_H
101bc8a9dfSShuo Chen 
111bc8a9dfSShuo Chen #include "Buffer.h"
121bc8a9dfSShuo Chen #include "Callbacks.h"
131bc8a9dfSShuo Chen #include "InetAddress.h"
141bc8a9dfSShuo Chen 
151bc8a9dfSShuo Chen #include <boost/any.hpp>
161bc8a9dfSShuo Chen #include <boost/enable_shared_from_this.hpp>
171bc8a9dfSShuo Chen #include <boost/noncopyable.hpp>
181bc8a9dfSShuo Chen #include <boost/scoped_ptr.hpp>
191bc8a9dfSShuo Chen #include <boost/shared_ptr.hpp>
201bc8a9dfSShuo Chen 
211bc8a9dfSShuo Chen namespace muduo
221bc8a9dfSShuo Chen {
231bc8a9dfSShuo Chen 
241bc8a9dfSShuo Chen class Channel;
251bc8a9dfSShuo Chen class EventLoop;
261bc8a9dfSShuo Chen class Socket;
271bc8a9dfSShuo Chen 
281bc8a9dfSShuo Chen ///
291bc8a9dfSShuo Chen /// TCP connection, for both client and server usage.
301bc8a9dfSShuo Chen ///
311bc8a9dfSShuo Chen class TcpConnection : boost::noncopyable,
321bc8a9dfSShuo Chen                       public boost::enable_shared_from_this<TcpConnection>
331bc8a9dfSShuo Chen {
341bc8a9dfSShuo Chen  public:
351bc8a9dfSShuo Chen   /// Constructs a TcpConnection with a connected sockfd
361bc8a9dfSShuo Chen   ///
371bc8a9dfSShuo Chen   /// User should not create this object.
381bc8a9dfSShuo Chen   TcpConnection(EventLoop* loop,
391bc8a9dfSShuo Chen                 const std::string& name,
401bc8a9dfSShuo Chen                 int sockfd,
411bc8a9dfSShuo Chen                 const InetAddress& localAddr,
421bc8a9dfSShuo Chen                 const InetAddress& peerAddr);
431bc8a9dfSShuo Chen   ~TcpConnection();
441bc8a9dfSShuo Chen 
451bc8a9dfSShuo Chen   EventLoop* getLoop() const { return loop_; }
461bc8a9dfSShuo Chen   const std::string& name() const { return name_; }
471bc8a9dfSShuo Chen   const InetAddress& localAddress() { return localAddr_; }
481bc8a9dfSShuo Chen   const InetAddress& peerAddress() { return peerAddr_; }
491bc8a9dfSShuo Chen   bool connected() const { return state_ == kConnected; }
501bc8a9dfSShuo Chen 
511bc8a9dfSShuo Chen+  //void send(const void* message, size_t len);
521bc8a9dfSShuo Chen+  // Thread safe.
531bc8a9dfSShuo Chen+  void send(const std::string& message);
541bc8a9dfSShuo Chen+  // Thread safe.
551bc8a9dfSShuo Chen+  void shutdown();
561bc8a9dfSShuo Chen+
571bc8a9dfSShuo Chen   void setConnectionCallback(const ConnectionCallback& cb)
581bc8a9dfSShuo Chen   { connectionCallback_ = cb; }
591bc8a9dfSShuo Chen 
601bc8a9dfSShuo Chen   void setMessageCallback(const MessageCallback& cb)
611bc8a9dfSShuo Chen   { messageCallback_ = cb; }
621bc8a9dfSShuo Chen 
631bc8a9dfSShuo Chen   /// Internal use only.
641bc8a9dfSShuo Chen   void setCloseCallback(const CloseCallback& cb)
651bc8a9dfSShuo Chen   { closeCallback_ = cb; }
661bc8a9dfSShuo Chen 
671bc8a9dfSShuo Chen   // called when TcpServer accepts a new connection
681bc8a9dfSShuo Chen   void connectEstablished();   // should be called only once
691bc8a9dfSShuo Chen   // called when TcpServer has removed me from its map
701bc8a9dfSShuo Chen   void connectDestroyed();  // should be called only once
711bc8a9dfSShuo Chen 
721bc8a9dfSShuo Chen  private:
731bc8a9dfSShuo Chen!  enum StateE { kConnecting, kConnected, kDisconnecting, kDisconnected, };
741bc8a9dfSShuo Chen 
751bc8a9dfSShuo Chen   void setState(StateE s) { state_ = s; }
761bc8a9dfSShuo Chen   void handleRead(Timestamp receiveTime);
771bc8a9dfSShuo Chen   void handleWrite();
781bc8a9dfSShuo Chen   void handleClose();
791bc8a9dfSShuo Chen   void handleError();
801bc8a9dfSShuo Chen+  void sendInLoop(const std::string& message);
811bc8a9dfSShuo Chen+  void shutdownInLoop();
821bc8a9dfSShuo Chen 
831bc8a9dfSShuo Chen   EventLoop* loop_;
841bc8a9dfSShuo Chen   std::string name_;
851bc8a9dfSShuo Chen   StateE state_;  // FIXME: use atomic variable
861bc8a9dfSShuo Chen   // we don't expose those classes to client.
871bc8a9dfSShuo Chen   boost::scoped_ptr<Socket> socket_;
881bc8a9dfSShuo Chen   boost::scoped_ptr<Channel> channel_;
891bc8a9dfSShuo Chen   InetAddress localAddr_;
901bc8a9dfSShuo Chen   InetAddress peerAddr_;
911bc8a9dfSShuo Chen   ConnectionCallback connectionCallback_;
921bc8a9dfSShuo Chen   MessageCallback messageCallback_;
931bc8a9dfSShuo Chen   CloseCallback closeCallback_;
941bc8a9dfSShuo Chen   Buffer inputBuffer_;
951bc8a9dfSShuo Chen+  Buffer outputBuffer_;
961bc8a9dfSShuo Chen };
971bc8a9dfSShuo Chen 
981bc8a9dfSShuo Chen typedef boost::shared_ptr<TcpConnection> TcpConnectionPtr;
991bc8a9dfSShuo Chen 
1001bc8a9dfSShuo Chen }
1011bc8a9dfSShuo Chen 
1021bc8a9dfSShuo Chen #endif  // MUDUO_NET_TCPCONNECTION_H
103