-
Notifications
You must be signed in to change notification settings - Fork 110
/
TcpClientThread.cpp
executable file
·157 lines (129 loc) · 3.76 KB
/
TcpClientThread.cpp
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
#include "TcpClientThread.h"
#include "Configure.h"
#include "LibeventLog.h"
#include "MessageHandler.h"
static void readcb(struct bufferevent *b, void *arg)
{
if (b == NULL || arg == NULL) {
return;
}
TcpClientThread *client = (TcpClientThread *)arg;
struct evbuffer *input = bufferevent_get_input(b);
if (evbuffer_get_length(input) == 0) {
LibeventLog::userLog("input data len is 0");
bufferevent_free(b);
}
char ch = 0;
int len = bufferevent_read(b, &ch, sizeof(char));
if (len != sizeof(char)) {
return;
}
Configure *cfg = Configure::readConfigFile(CONFIG_FILE_PATH);
if (ch == cfg->notify[0]) {
} else if (ch == cfg->heartbeat[0]) {
} else {
}
}
static void writecb(struct bufferevent *bev, void *arg)
{
if (bev == NULL || arg == NULL) {
return;
}
Configure *cfg = Configure::readConfigFile(CONFIG_FILE_PATH);
TcpClientThread *client = (TcpClientThread *)arg;
int ret = client->scanQueueMessage();
if (ret > 0) {
client->replyToServer(bev, cfg->notify.c_str());
client->yield();
client->milliSleep(cfg->sleep_mill);
} else {
client->replyToServer(bev, cfg->heartbeat.c_str());
client->milliSleep(cfg->sleep_mill);
}
}
static void errorcb(struct bufferevent *b, short what, void *arg)
{
TcpClientThread *client = (TcpClientThread *)arg;
if (what & BEV_EVENT_EOF) {
} else {
}
bufferevent_setcb(b, NULL, NULL, NULL, NULL);
bufferevent_disable(b, EV_READ|EV_WRITE);
bufferevent_free(b);
b = NULL;
LibeventLog::userLog("TcpClientThread errorcb called.");
//connect again
int ret = 1;
while (ret) {
client->yield();
ret = client->connectToServer();
LibeventLog::userLog("TcpClientThread reconnect server.");
}
}
int TcpClientThread::replyToServer(struct bufferevent *b, const char *str) {
int ret = bufferevent_write(b, str, MESSAGE_CHAR_LENGHT);
if (ret != 0) {
LibeventLog::userLog("bufferevent_write failed");
return -1;
}
return 0;
}
void TcpClientThread::frob_socket(evutil_socket_t sock)
{
struct linger l;
int one = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one))<0) {
LibeventLog::userLog("setsockopt(SO_REUSEADDR)");
}
l.l_onoff = 1;
l.l_linger = 0;
if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (char*)&l, sizeof(l))<0) {
LibeventLog::userLog("setsockopt(SO_LINGER)");
}
}
int TcpClientThread::connectToServer() {
evutil_socket_t sock;
struct sockaddr_in sin;
Configure *cfg = Configure::readConfigFile(CONFIG_FILE_PATH);
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(0x7f000001);
sin.sin_port = htons(cfg->heartbeat_port);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
LibeventLog::userLog("create socket failed");
return -1;
}
if (evutil_make_socket_nonblocking(sock) < 0) {
LibeventLog::userLog("evutil_make_socket_nonblocking failed");
return -1;
}
frob_socket(sock);
if (connect(sock, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
#ifdef WIN32
if ((errno == EINTR || errno == EINPROGRESS)) {
#else
if (!(errno == EINTR || errno == EINPROGRESS)) {
#endif
LibeventLog::userLog("errno == EINTR || errno == EINPROGRESS");
return -1;
}
}
bev = bufferevent_socket_new(base, sock, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev, readcb, writecb, errorcb, this);
bufferevent_enable(bev, EV_READ|EV_WRITE);
return 0;
}
int TcpClientThread::run() {
LibeventLog::userLog("TcpClientThread start ...");
int ret = connectToServer();
event_base_dispatch(base);
LibeventLog::userLog("TcpClientThread exit ...");
return ret;
}
//return count in queue
int TcpClientThread::scanQueueMessage() {
if (input != NULL) {
return input->scanQueueMessage();
}
return 0;
}