Repository navigation
Expand file tree
/
Copy pathsession.cpp
More file actions
205 lines (172 loc) · 4.89 KB
/
Copy pathsession.cpp
File metadata and controls
205 lines (172 loc) · 4.89 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <memory.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "session.h"
#include "protocol.h"
#include "log.h"
#include "util.h"
#define __error_line (-12000 - __LINE__)
Session::Session(int fd, struct sockaddr_in addr, socklen_t len) : fd_(fd), len_(len)
{
memcpy(&addr_, &addr, sizeof(addr));
Alloc::Instance()->Acquire(read_buffer_, write_buffer_);
read_buffer_cursor_ = 0;
write_buffer_size_ = 0;
write_buffer_cursor_ = 0;
ip_ = inet_ntoa(addr.sin_addr);
accept_tick_ = 0;
addto_epollin_tick_ = 0;
addto_task_tick_ = 0;
dispatch_tick_ = 0;
addto_response_tick_ = 0;
addto_epollout_tick_ = 0;
rpc_start_tick_ = 0;
rpc_end_tick_ = 0;
}
Session::~Session()
{
Alloc::Instance()->Return(read_buffer_, write_buffer_);
read_buffer_ = NULL;
write_buffer_ = NULL;
close(fd_);
}
int Session::Read()
{
if (rpc_start_tick_ == 0)
{
rpc_start_tick_ = GetTick();
}
#if 1
size_t len = read(fd_, read_buffer_, SESSION_READ_BUFFER_SIZE);
if (len < 0)
{
return __error_line;
}
if (0 == len)
{
return READ_NULL;
}
read_buffer_cursor_ += len;
printf("read_buffer_:\n%s\n", read_buffer_);
return READ_DONE;
#else
if (read_buffer_cursor_ < sizeof(Header))
{
// read an intact header first
size_t to_read = sizeof(Header) - read_buffer_cursor_;
size_t len = read(fd_, read_buffer_ + read_buffer_cursor_, to_read);
if (len < 0)
{
LogErr("read %d to_read %zu len %zu err %s", fd_, to_read, len, strerror(errno));
return __error_line;
}
if (0 == len)
{
return READ_NULL;
}
read_buffer_cursor_ += len;
if (len < to_read)
{
return READ_NEED_MORE;
}
if (len > to_read)
{
LogErr("weird buffer len %zu to_read %zu", len, to_read);
return __error_line;
}
}
// here, we must have an intact header
Header* header = reinterpret_cast<Header*>(read_buffer_);
size_t to_read = header->size - read_buffer_cursor_ + sizeof(Header);
size_t len = read(fd_, read_buffer_ + read_buffer_cursor_, to_read);
if (len < 0)
{
LogErr("read %d to_read %zu len %zu err %s", fd_, to_read, len, strerror(errno));
return __error_line;
}
if (0 == len)
{
return READ_NULL;
}
read_buffer_cursor_ += len;
if (len < to_read)
{
return READ_NEED_MORE;
}
assert(read_buffer_cursor_ = sizeof(Header) + header->size);
return READ_DONE;
#endif
}
int Session::Write()
{
if (write_buffer_size_ == write_buffer_cursor_)
{
// nothing to write
return WRITE_NONE;
}
size_t len =
write(fd_, write_buffer_ + write_buffer_cursor_, write_buffer_size_ - write_buffer_cursor_);
if (len < 0)
{
LogErr("write %d size %d err %s",
fd_,
write_buffer_size_ - write_buffer_cursor_,
strerror(errno));
return __error_line;
}
write_buffer_cursor_ += len;
LogDebug("write %d cursor %d size %d", fd_, write_buffer_cursor_, write_buffer_size_);
if (write_buffer_cursor_ == write_buffer_size_)
{
rpc_end_tick_ = GetTick();
return WRITE_DONE;
}
else if (write_buffer_cursor_ < write_buffer_size_)
{
return WRITE_NEED_MORE;
}
else
{
// log
LogErr("weird buffer");
return __error_line;
}
}
void Session::Reset()
{
uint64_t now = GetTick();
uint64_t epollin_ms = (addto_task_tick_ - addto_epollin_tick_) / GetKHZ();
uint64_t task_ms = (dispatch_tick_ - addto_task_tick_) / GetKHZ();
uint64_t dispatch_ms = (addto_response_tick_ - dispatch_tick_) / GetKHZ();
uint64_t response_ms = (addto_epollout_tick_ - addto_response_tick_) / GetKHZ();
uint64_t epollout_ms = (now - addto_epollout_tick_) / GetKHZ();
uint64_t rpc_ms = (rpc_end_tick_ - rpc_start_tick_) / GetKHZ();
LogInfo("ip %s fd %d epollin %llu task %llu dispath %llu response %llu epollout %llu rpc %llu",
IP(),
fd_,
epollin_ms,
task_ms,
dispatch_ms,
response_ms,
epollout_ms,
rpc_ms);
StatusTick(Epollin, epollin_ms);
StatusTick(Task, task_ms);
StatusTick(Dispatch, dispatch_ms);
StatusTick(Response, response_ms);
StatusTick(Epollout, epollout_ms);
read_buffer_cursor_ = 0;
write_buffer_size_ = 0;
write_buffer_cursor_ = 0;
addto_epollin_tick_ = 0;
addto_task_tick_ = 0;
dispatch_tick_ = 0;
addto_response_tick_ = 0;
addto_epollout_tick_ = 0;
rpc_start_tick_ = 0;
rpc_end_tick_ = 0;
}