forked from lxyfirst/queue_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_tcp_handler.cpp
More file actions
executable file
·189 lines (148 loc) · 4.12 KB
/
Copy pathclient_tcp_handler.cpp
File metadata and controls
executable file
·189 lines (148 loc) · 4.12 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
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
183
184
185
186
/*
* client_tcp_handler.cpp
*
* Created on: Oct 25, 2015
* Author: lxyfirst@163.com
*/
#include "framework/string_util.h"
#include "client_tcp_handler.h"
#include "worker_util.h"
#include "queue_processor.h"
#include "public/message.h"
using namespace framework ;
ClientTcpHandler::ClientTcpHandler()
{
m_idle_timer.set_callback(this,&ClientTcpHandler::on_timeout) ;
}
ClientTcpHandler::~ClientTcpHandler()
{
}
void ClientTcpHandler::on_timeout(timer_manager* manager)
{
int idle_time = time(0)- m_last_time ;
if(idle_time > IDLE_TIMEOUT )
{
trace_log_format(get_logger(),"timeout fd:%d",this->get_id().fd) ;
fini() ;
}
else
{
get_worker().add_timer_after(&m_idle_timer,IDLE_TIMEOUT) ;
}
}
void ClientTcpHandler::on_connected()
{
char addr[32] = {0};
this->get_remote_addr(addr,sizeof(addr)-1) ;
debug_log_format(get_logger(),
"client connected host:%s fd:%d",addr,this->get_id().fd) ;
m_last_time = time(0) ;
on_timeout(NULL) ;
}
void ClientTcpHandler::send_heartbeat()
{
int now = time(0) ;
if( now - m_last_time >= (IDLE_TIMEOUT >>1) )
{
m_last_time = now ;
SSStatusRequest heartbeat ;
this->send(&heartbeat,0) ;
}
}
int ClientTcpHandler::on_heartbeat(const framework::packet_info* pi)
{
//SSStatusResponse heartbeat ;
//this->send(&heartbeat,0) ;
return 0 ;
}
int ClientTcpHandler::get_packet_info(const char* data,int size,framework::packet_info* pi)
{
static const int MAX_SIZE = 40960 ;
if ( data[0] == '{' && size < MAX_SIZE ) //web socket
{
if( data[size-1] == '}')
{
pi->data = data ;
pi->size =size ;
pi->type = JSON_PACKET_TYPE ;
}
else
{
pi->size =size +1;
}
}
else
{
//server foward binary data
if(size < (int)sizeof(PacketHead))
{
pi->size = sizeof(PacketHead) ;
}
else
{
pi->size = decode_packet_size(data) ;
pi->type = decode_packet_msg_type(data) ;
pi->data = data ;
}
}
if( pi->size < 1 || pi->size > MAX_SIZE) return -1 ;
return 0 ;
}
int ClientTcpHandler::process_packet(const packet_info* pi)
{
m_last_time = time(0) ;
switch(pi->type)
{
case FORWARD_REQUEST:
return get_worker().process_forward_request(this,pi) ;
case FORWARD_RESPONSE:
return get_worker().process_forward_response(this,pi) ;
case JSON_PACKET_TYPE:
return process_json_request(pi) ;
case STATUS_REQUEST:
return on_heartbeat(pi) ;
case STATUS_RESPONSE :
return 0 ;
}
return -1 ;
}
int ClientTcpHandler::process_json_request(const packet_info* pi)
{
Document request ;
if(json_decode(pi->data,pi->data + pi->size,request)!=0) return -1 ;
char host[16] = {0} ;
this->get_remote_addr(host,sizeof(host)) ;
int action = json_get_value(request,FIELD_ACTION,0) ;
if(action <= 0 ) return -1 ;
debug_log_format(get_logger(),"recv host:%s action:%d size:%d",host,action,pi->size) ;
if((!is_leader() ) && action < ACTION_LOCAL_START)
{
if( is_forward_request() )
{
SourceData source ;
source.is_tcp = 1 ;
source.id = this->get_id();
return get_worker().forward_to_leader(source,pi->data,pi->size) ;
}
//cannot forward , return leader info
request.RemoveAllMembers() ;
request.AddMember(FIELD_ACTION,ACTION_GET_LEADER,request.GetAllocator() );
}
if(QueueProcessor::process(request)==0)
{
StringBuffer buffer ;
json_encode(request,buffer) ;
if( this->send(buffer.GetString(),buffer.GetSize(),0 ) !=0 ) return -1 ;
}
return 0 ;
}
void ClientTcpHandler::on_disconnect(int error_type)
{
get_worker().del_timer(&m_idle_timer) ;
debug_log_format(get_logger(),
"client closed error_type:%d fd:%d",error_type,this->get_id().fd);
}
void ClientTcpHandler::on_closed()
{
get_worker().on_client_closed(this) ;
}