TcpConnection.h revision 354280cf
1354280cfSShuo Chen// excerpts from http://code.google.com/p/muduo/
2354280cfSShuo Chen//
3354280cfSShuo Chen// Use of this source code is governed by a BSD-style license
4354280cfSShuo Chen// that can be found in the License file.
5354280cfSShuo Chen//
6354280cfSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
7354280cfSShuo Chen
8354280cfSShuo Chen#ifndef MUDUO_NET_TCPCONNECTION_H
9354280cfSShuo Chen#define MUDUO_NET_TCPCONNECTION_H
10354280cfSShuo Chen
11354280cfSShuo Chen#include "Buffer.h"
12354280cfSShuo Chen#include "Callbacks.h"
13354280cfSShuo Chen#include "InetAddress.h"
14354280cfSShuo Chen
15354280cfSShuo Chen#include <boost/any.hpp>
16354280cfSShuo Chen#include <boost/enable_shared_from_this.hpp>
17354280cfSShuo Chen#include <boost/noncopyable.hpp>
18354280cfSShuo Chen#include <boost/scoped_ptr.hpp>
19354280cfSShuo Chen#include <boost/shared_ptr.hpp>
20354280cfSShuo Chen
21354280cfSShuo Chennamespace muduo
22354280cfSShuo Chen{
23354280cfSShuo Chen
24354280cfSShuo Chenclass Channel;
25354280cfSShuo Chenclass EventLoop;
26354280cfSShuo Chenclass Socket;
27354280cfSShuo Chen
28354280cfSShuo Chen///
29354280cfSShuo Chen/// TCP connection, for both client and server usage.
30354280cfSShuo Chen///
31354280cfSShuo Chenclass TcpConnection : boost::noncopyable,
32354280cfSShuo Chen                      public boost::enable_shared_from_this<TcpConnection>
33354280cfSShuo Chen{
34354280cfSShuo Chen public:
35354280cfSShuo Chen  /// Constructs a TcpConnection with a connected sockfd
36354280cfSShuo Chen  ///
37354280cfSShuo Chen  /// User should not create this object.
38354280cfSShuo Chen  TcpConnection(EventLoop* loop,
39354280cfSShuo Chen                const std::string& name,
40354280cfSShuo Chen                int sockfd,
41354280cfSShuo Chen                const InetAddress& localAddr,
42354280cfSShuo Chen                const InetAddress& peerAddr);
43354280cfSShuo Chen  ~TcpConnection();
44354280cfSShuo Chen
45354280cfSShuo Chen  EventLoop* getLoop() const { return loop_; }
46354280cfSShuo Chen  const std::string& name() const { return name_; }
47354280cfSShuo Chen  const InetAddress& localAddress() { return localAddr_; }
48354280cfSShuo Chen  const InetAddress& peerAddress() { return peerAddr_; }
49354280cfSShuo Chen  bool connected() const { return state_ == kConnected; }
50354280cfSShuo Chen
51354280cfSShuo Chen  //void send(const void* message, size_t len);
52354280cfSShuo Chen  // Thread safe.
53354280cfSShuo Chen  void send(const std::string& message);
54354280cfSShuo Chen  // Thread safe.
55354280cfSShuo Chen  void shutdown();
56354280cfSShuo Chen  void setTcpNoDelay(bool on);
57354280cfSShuo Chen
58354280cfSShuo Chen  void setConnectionCallback(const ConnectionCallback& cb)
59354280cfSShuo Chen  { connectionCallback_ = cb; }
60354280cfSShuo Chen
61354280cfSShuo Chen  void setMessageCallback(const MessageCallback& cb)
62354280cfSShuo Chen  { messageCallback_ = cb; }
63354280cfSShuo Chen
64354280cfSShuo Chen  void setWriteCompleteCallback(const WriteCompleteCallback& cb)
65354280cfSShuo Chen  { writeCompleteCallback_ = cb; }
66354280cfSShuo Chen
67354280cfSShuo Chen  /// Internal use only.
68354280cfSShuo Chen  void setCloseCallback(const CloseCallback& cb)
69354280cfSShuo Chen  { closeCallback_ = cb; }
70354280cfSShuo Chen
71354280cfSShuo Chen  // called when TcpServer accepts a new connection
72354280cfSShuo Chen  void connectEstablished();   // should be called only once
73354280cfSShuo Chen  // called when TcpServer has removed me from its map
74354280cfSShuo Chen  void connectDestroyed();  // should be called only once
75354280cfSShuo Chen
76354280cfSShuo Chen private:
77354280cfSShuo Chen  enum StateE { kConnecting, kConnected, kDisconnecting, kDisconnected, };
78354280cfSShuo Chen
79354280cfSShuo Chen  void setState(StateE s) { state_ = s; }
80354280cfSShuo Chen  void handleRead(Timestamp receiveTime);
81354280cfSShuo Chen  void handleWrite();
82354280cfSShuo Chen  void handleClose();
83354280cfSShuo Chen  void handleError();
84354280cfSShuo Chen  void sendInLoop(const std::string& message);
85354280cfSShuo Chen  void shutdownInLoop();
86354280cfSShuo Chen
87354280cfSShuo Chen  EventLoop* loop_;
88354280cfSShuo Chen  std::string name_;
89354280cfSShuo Chen  StateE state_;  // FIXME: use atomic variable
90354280cfSShuo Chen  // we don't expose those classes to client.
91354280cfSShuo Chen  boost::scoped_ptr<Socket> socket_;
92354280cfSShuo Chen  boost::scoped_ptr<Channel> channel_;
93354280cfSShuo Chen  InetAddress localAddr_;
94354280cfSShuo Chen  InetAddress peerAddr_;
95354280cfSShuo Chen  ConnectionCallback connectionCallback_;
96354280cfSShuo Chen  MessageCallback messageCallback_;
97354280cfSShuo Chen  WriteCompleteCallback writeCompleteCallback_;
98354280cfSShuo Chen  CloseCallback closeCallback_;
99354280cfSShuo Chen  Buffer inputBuffer_;
100354280cfSShuo Chen  Buffer outputBuffer_;
101354280cfSShuo Chen};
102354280cfSShuo Chen
103354280cfSShuo Chentypedef boost::shared_ptr<TcpConnection> TcpConnectionPtr;
104354280cfSShuo Chen
105354280cfSShuo Chen}
106354280cfSShuo Chen
107354280cfSShuo Chen#endif  // MUDUO_NET_TCPCONNECTION_H
108