-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient_protobuf_impl.cc
159 lines (148 loc) · 6.51 KB
/
client_protobuf_impl.cc
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
/****************************************************************************
Copyright (c) 2013-2014 King Lee
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#if defined __LINUX || defined __MACX
#include <sys/socket.h>
#include <unistd.h>
#endif // __LINUX
#include <thread>
#include "client_protobuf_impl.h"
#include "easy_byte_buffer.h"
#include "easy_util.h"
#ifndef __READ_DIRECTLY
#define __READ_DIRECTLY
#endif //__READ_DIRECTLY
const easy_uint32 Client_Impl::max_buffer_size_ = 1024*8;
const easy_uint32 Client_Impl::max_sleep_time_ = 1000*1;
Client_Impl::Client_Impl( Reactor* __reactor,const easy_char* __host,easy_uint32 __port /*= 9876*/ ) : Event_Handle_Cli(__reactor,__host,__port) {
ring_buf_ = new easy::EasyRingbuffer<easy_uint8,easy::alloc,easy::mutex_lock>(max_buffer_size_);
// start read thread
auto __thread_ = std::thread(CC_CALLBACK_0(Client_Impl::_read_thread,this));
__thread_.detach();
// work thread can work now
star_work_thread();
}
Client_Impl::~Client_Impl() {
}
void Client_Impl::on_read( easy_int32 __fd ) {
#ifndef __READ_DIRECTLY
// the follow code is ring_buf's append function actually.
easy_ulong __usable_size = 0;
_get_usable(__fd,__usable_size);
easy_int32 __ring_buf_tail_left = ring_buf_->size() - ring_buf_->wpos();
easy_int32 __read_bytes = 0;
if(__usable_size <= __ring_buf_tail_left) {
__read_bytes = Event_Handle_Cli::read(__fd,(easy_char*)ring_buf_->buffer() + ring_buf_->wpos(),__usable_size);
if(-1 != __read_bytes && 0 != __read_bytes) {
ring_buf_->set_wpos(ring_buf_->wpos() + __read_bytes);
}
} else {
__read_bytes = Event_Handle_Cli::read(__fd,(char*)ring_buf_->buffer() + ring_buf_->wpos(),__ring_buf_tail_left);
if(-1 != __read_bytes && 0 != __read_bytes) {
ring_buf_->set_wpos(ring_buf_->wpos() + __read_bytes);
}
easy_int32 __ring_buf_head_left = ring_buf_->rpos();
easy_int32 __read_left = __usable_size - __read_bytes;
if(__ring_buf_head_left > __read_left) {
__read_bytes = Event_Handle_Cli::read(__fd,(easy_char*)ring_buf_->buffer(),__read_left);
if(-1 != __read_bytes && 0 != __read_bytes) {
ring_buf_->set_wpos(__read_bytes);
}
} else {
__read_bytes = Event_Handle_Cli::read(__fd,(easy_char*)ring_buf_->buffer(),__ring_buf_head_left);
if(-1 != __read_bytes && 0 != __read_bytes) {
ring_buf_->set_wpos(__read_bytes);
}
// not read completely from system cache, read next read event.
}
}
#else
_read_directly(__fd);
#endif
}
void Client_Impl::_read_thread() {
#ifndef __READ_DIRECTLY
static const easy_int32 __head_size = sizeof(easy_uint32);
std::string __string_packet;
while (true) {
easy_uint32 __packet_length = 0;
if(!ring_buf_->peek((unsigned char*)&__packet_length,__head_size)) {
easy::Util::sleep(max_sleep_time_);
continue;
}
easy_uint16 __real_packet_length = __packet_length & 0x0000ffff;
easy_uint16 __real_fd = __packet_length >> 16;
if(!__real_packet_length) {
printf("__packet_length error\n");
continue;
}
if(__real_packet_length > max_buffer_size_) {
printf("__packet_length error\n");
continue;
}
__string_packet.clear();
if(ring_buf_->read(__string_packet,__real_packet_length + __head_size)) {
if(is_proxy_client()) {
handle_packet(__real_fd,__string_packet.c_str() + __head_size,easy_null);
} else {
handle_packet(get_handle(),__string_packet.c_str() + __head_size,easy_null);
}
}
easy::Util::sleep(max_sleep_time_);
}
#endif //__READ_DIRECTLY
}
void Client_Impl::_read_directly(easy_int32 __fd) {
easy_uint32 __packet_length = 0;
easy_uint8 __buf[max_buffer_size_] = {};
static const easy_int32 __head_size = sizeof(easy_uint32);
while (true) {
__packet_length = 0;
easy_int32 __read_bytes = Event_Handle_Cli::read(__fd,(easy_char*)&__packet_length,__head_size,MSG_PEEK);
if( __head_size != __read_bytes) {
break;
}
easy_uint16 __real_packet_length = __packet_length & 0x0000ffff;
easy_uint16 __real_fd = __packet_length >> 16;
if(!__real_packet_length) {
printf("__packet_length error\n");
break;
}
if(__real_packet_length > max_buffer_size_) {
printf("__packet_length error\n");
break;
}
// fix the problem data dirty
__read_bytes = Event_Handle_Cli::read(__fd,(easy_char*)__buf,(__real_packet_length + __head_size),MSG_PEEK);
if (__read_bytes < (__real_packet_length + __head_size)) {
break;
}
memset(__buf,0,max_buffer_size_);
std::string __string_packet;
__read_bytes = Event_Handle_Cli::read(__fd,(easy_char*)__buf,__real_packet_length + __head_size);
if(-1 != __read_bytes && 0 != __read_bytes) {
__string_packet.insert(0,(const char*)__buf,__real_packet_length + __head_size);
if(is_proxy_client()) {
handle_packet(__real_fd,__string_packet.c_str() + __head_size,easy_null);
} else {
handle_packet(get_handle(),__string_packet.c_str() + __head_size,easy_null);
}
}
easy::Util::sleep(max_sleep_time_);
}
}