-
Notifications
You must be signed in to change notification settings - Fork 5
/
Net.h
executable file
·182 lines (154 loc) · 5.12 KB
/
Net.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef KHAKI_NET_H
#define KHAKI_NET_H
#include "Buffer.h"
#include "Util.h"
#include "Log.h"
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <memory>
#include <mutex>
#include <thread>
namespace khaki {
class EventLoop;
class TcpClient;
class TimeWheel;
class Channel;
class Connector;
const unsigned int MAX_READ_BUFFER_SIZE = 20480;
typedef std::weak_ptr<TcpClient> TcpWeakPtr;
typedef std::shared_ptr<TcpClient> TcpClientPtr;
typedef std::shared_ptr<Connector> TcpConnectorPtr;
typedef std::function<void(const TcpClientPtr& con)> Callback;
typedef std::shared_ptr<TimeWheel> TimeWheelPtr;
typedef std::function<void(Buffer& buf)> CallbackBuffer;
typedef std::function<void(const TcpConnectorPtr& con)> CallbackConnector;
class IpAddr {
public:
IpAddr(const struct sockaddr_in& addr);
IpAddr(std::string& host, int port);
~IpAddr();
std::string getIp() const;
int getPort() const;
uint64_t toIpPort() const;
struct sockaddr_in& getAddr();
private:
struct sockaddr_in addr_;
};
////////////////////////////////////
class TcpServer;
class TcpClient : public std::enable_shared_from_this<TcpClient> {
public:
TcpClient( EventLoop* loop, TcpServer* server, IpAddr& addr, uint32_t uniqueId);
~TcpClient();
void handleRead(const TcpClientPtr& con);
void handleWrite(const TcpClientPtr& con);
void setReadCallback(const Callback& cb) { readcb_ = cb; }
void setWriteCallback(const Callback& cb) { writecb_ = cb; }
void setCloseCallback(const Callback& cb) { closecb_ = cb; }
void send(const char* buf, int len);
void send(Buffer& buf);
void sendInLoop(Buffer& buf);
Buffer getBuf() { Buffer tmp(readBuf_); readBuf_.clear(); return tmp; }
Buffer& getReadBuf() { return readBuf_; }
void registerChannel(int fd);
void closeClient(const TcpClientPtr& con);
int getFd();
uint32_t getUniqueId() { return uniqueId_; }
uint64_t getIpPort() { return addr_.toIpPort(); }
int getLastTime();
void updateTimeWheel();
void enableRead();
private:
int directWrite(const char* buf, int len);
EventLoop* loop_;
TcpServer* server_;
IpAddr addr_;
uint32_t uniqueId_;
std::shared_ptr<Channel> channel_;
Callback readcb_, writecb_, closecb_;
int last_read_time_;
Buffer readBuf_, writeBuf_;
};
////////////////////////////////////
class TcpServer : public noncopyable {
public:
TcpServer( EventLoop* loop, std::string host, int port );
virtual ~TcpServer();
void start();
virtual EventLoop* getEventLoop() { return loop_; }
void setReadCallback(const Callback& cb) { readcb_ = cb; }
void setWriteCallback(const Callback& cb) { writecb_ = cb; }
void setConnectionCallback(const Callback& cb) { newcb_ = cb; }
void setConnCloseCallback(const Callback& cb) { closecb_ = cb; }
int getOnlineNum();
void addClient(TcpClientPtr& sp);
void removeClient(const TcpClientPtr& sp);
void delClient(int fd);
private:
void newConnect( int fd, IpAddr& addr );
void handleAccept();
EventLoop* loop_;
Channel* listen_;
IpAddr addr_;
uint32_t uniqueId_;
Callback readcb_, writecb_, newcb_, closecb_;
std::mutex mtx_;
std::map<int, std::weak_ptr<TcpClient>> sSessionList;
};
////////////////////////////////////
class Connector : public std::enable_shared_from_this<Connector> {
public:
enum {
E_CONNECT_STATUS_CLOSE = 0,
E_CONNECT_STATUS_CONN = 1,
E_CONNECT_STATUS_RUNNING = 2,
};
Connector(EventLoop* loop, std::string host, uint16_t port, int timeout_);
~Connector();
bool connectServer();
bool retryConnect();
void setConnectCallback(const CallbackConnector& cb) { newcb_ = cb; }
void setReadCallback(const CallbackConnector& cb) { readcb_ = cb; }
void setWriteCallback(const CallbackConnector& cb) { writecb_ = cb; }
void setCloseCallback(const CallbackConnector& cb) { closecb_ = cb; }
void send(const char* buff, int len);
void closeFd(int fd) { close(sockFd_); status_ = E_CONNECT_STATUS_CLOSE;}
void closeConnect();
void timeout();
int getFd() { return sockFd_; }
int getStatus() { return status_; }
Buffer& getReadBuf() { return readBuf_; }
private:
void handleRead(const TcpConnectorPtr& con);
void handleWrite(const TcpConnectorPtr& con);
bool checkConnectStatus(const TcpConnectorPtr& con);
size_t directWrite(Buffer& buffer);
void send(Buffer& buf);
void sendInLoop(Buffer& buf);
private:
EventLoop* loop_;
IpAddr addr_;
int timeout_;
int sockFd_;
int status_;
Channel* channel_;
Buffer readBuf_, writeBuf_;
CallbackConnector newcb_, readcb_, writecb_, closecb_;
};
////////////////////////////////////
class EventLoopThread;
class TcpThreadServer : public TcpServer {
public:
typedef std::shared_ptr<EventLoopThread> EventLoopThreadPtr;
TcpThreadServer( EventLoop* loop, std::string host, int port, int threadNum = 0 );
~TcpThreadServer();
virtual EventLoop* getEventLoop();
private:
int index_;
int threadNum_;
int threadSize_;
std::vector<EventLoopThreadPtr> vThreadLoop_;
};
}
#endif