-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpConnection.h
121 lines (101 loc) · 3.6 KB
/
TcpConnection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef TCPCONNECTION_H_
#define TCPCONNECTION_H_
#include"base/noncopyable.h"
#include"Buffer.h"
#include"net/InetAddress.h"
#include<memory>
#include<atomic>
class Channel;
class EventLoop;
class TcpConnection;
typedef std::shared_ptr<TcpConnection> TcpConnectionPtr;
typedef std::function<void(const TcpConnectionPtr&)> ConnectionCallback;
typedef std::function<void(const TcpConnectionPtr&,Buffer*)> MessageCallback;
typedef std::function<void(const TcpConnectionPtr&)> CloseCallback;
typedef std::function<void(const TcpConnectionPtr&)> WriteComleteCallback;
typedef std::function<void(const TcpConnectionPtr&,size_t)> HighWaterMarkCallback;
class TcpConnection:noncopyable, public std::enable_shared_from_this<TcpConnection>
{
public:
TcpConnection(EventLoop* loop,const std::string name,int sockfd,const InetAddress& locla,const InetAddress &peer);
~TcpConnection();
EventLoop* getLoop()const
{ return loop_; }
const std::string& name()
const{return name_; }
const InetAddress& localAddress()const
{ return localAddr_; }
const InetAddress& peerAddress()const
{ return peerAddr_; }
bool connected()const
{ return state_==kConnected; }
bool disconnected()const
{ return state_==kDisconnected; }
void send(const void* message,size_t len);
void send(const std::string& message);
// void send(std::string&& message);
void send(Buffer* message);
// void send(Buffer&& mesaage);
void shutdown();
void setTcpNodelay(bool on);
void startRead();
void stopread();
bool isreading()const{ return reading_; }
void setConnectionCallback(const ConnectionCallback& cb)
{ connectionCallback_=cb ;}
void setMessageCallback(const MessageCallback& cb)
{ messageCallback_=cb; }
void setCloseCallback(const CloseCallback& cb)
{ closeCallback_=cb; }
void setWriteComleteCallback(const WriteComleteCallback& cb)
{ writeCompleteCallback_=cb; }
void setHighWaterMarkCallback(const HighWaterMarkCallback&cb,size_t high)
{ highWaterMarkCallback_=cb; highWaterMark_=high; }
Buffer* inputBuffer()
{
return &inputBuffer_;
}
Buffer* outputBuffer()
{
return &outputBuffer_;
}
//called when server accept a new conn
void connectEstablished();
void connectDestroyed();
private:
enum StateE{kDisconnected,kConnecting,kConnected,kDisconnecting};
void handleRead();
void handleWrite();
void handleClose();
void handleError();
// void sendInLoop(std::string &&message);
// void sendInLoop(const std::string& message);
void sendInLoop(const char* message,size_t len);
void sendInLoop(const std::string& mess);
// void sendInLoop(const std::shared_ptr<void> &tie,const char* message,size_t len);
void startReadInLoop();
void stopReadInLoop();
void shutdownInLoop();
void setState(StateE s){ state_ = s ;}
const char* stateToString()const;
const std::string name_;
EventLoop* loop_;
bool reading_;
// bool writting_;
std::atomic<StateE> state_;
std::unique_ptr<Channel> channel_;
std::unique_ptr<Socket> socket_;
const InetAddress localAddr_;
const InetAddress peerAddr_;
ConnectionCallback connectionCallback_;
MessageCallback messageCallback_;
CloseCallback closeCallback_;
WriteComleteCallback writeCompleteCallback_;
HighWaterMarkCallback highWaterMarkCallback_;
size_t highWaterMark_;
Buffer inputBuffer_;
Buffer outputBuffer_;
};
void defaultConnectionCallback(const TcpConnectionPtr& conn);
void defaultMessageCallback(const TcpConnectionPtr& conn,Buffer* buf);
#endif