-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpConnection.h
More file actions
114 lines (93 loc) · 2.87 KB
/
Copy pathTcpConnection.h
File metadata and controls
114 lines (93 loc) · 2.87 KB
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
#ifndef TcpConnection_H
#define TcpConnection_H
#include "noncopyable.h"
#include "InetAddr.h"
#include "CallBacks.h"
#include"Timestamp.h"
#include <memory>
#include <cstring>
#include <atomic>
class Channel;
class EventLoop;
class Socket;
class TcpConnection;
using TcpConnectionPtr = std::shared_ptr<TcpConnection>;
/***
* TcpServer -> Acceptor ->有新用户accept拿到clientfd ->tcpConnect 设置回调->
* 打包成 Channel -> poller 监管(epoll)-> 事件响应 chnnel回调操作
*
*
*/
class TcpConnection : noncopyable, public std::enable_shared_from_this<TcpConnection>
{
public:
TcpConnection(EventLoop *loop, const std::string &name, int sockefd, const InetAddr &localAddr, const InetAddr &peerAddr);
~TcpConnection();
EventLoop *getLoop() const { return loop_; }
const std::string name() const { return name_; }
const InetAddr &localAddr() const { return localAddr; }
const InetAddr &peerAddr() const { return peerAddr; }
bool connected() const { return state_ == kConnected; }
bool disconnected() const { return state_ == kDisconnected; }
void send(const void *message, int len);
void send(Buffer *message);
void shutdown();
void setConnectionCallback(const ConnectionCallback &cb)
{
connectCallback_ = cb;
}
void setMessageCallback(const MessageCallback &cb)
{
messageCallback_ = cb;
}
void setWriteCompleteCallback(const WriteCompleteCallback &cb)
{
writeCompleteCallback_ = cb;
}
void setHighWaterMarkCallback(const HighWaterMarkCallback &cb, size_t highWaterMark)
{
highWriterMarkCallback_ = cb;
highWaterMark_ = highWaterMark;
}
void setCloseCallback(const CloseCallback& cb)
{ closeCallback_ = cb; }
private:
enum StatuE
{
// 已断开连接
kDisconnected,
// 正在连接
kConnecting,
// 已连接
kConnected,
// 正在断开连接
kDisconnecting
};
void handleRead(Timestamp receiveTime);
void handleWrite();
void handleClose();
void hanleError();
void sendInloop(const void* message,size_t len);
void shutdownInloop();
/* 此loop不是mainloop 是workloop tcpConnect 在wrokloop工作*/
EventLoop *loop_;
const std::string name_;
bool reading_;
std::atomic_int state_;
/**accept -> mainloop listenfd
* tcpconnect -> subloop clinetfd
*/
std::unique_ptr<Socket> socket_;
std::unique_ptr<Channel> channel_;
const InetAddr localAddr_;
const InetAddr peerAddr_;
CloseCallback closeCallback_;
WriteCompleteCallback writeCompleteCallback_;
ConnectCallback connectCallback_;
MessageCallback messageCallback_;
HighWriterMarkCallback highWriterMarkCallback_
size_t highWriterMark_; // 双方水位(流量)控制
Buffer inputBuffer_;
Buffer outputBuffer_;
};
#endif