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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
* Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** \file
\ingroup realmd
*/
#include "RealmSocket.h"
#include <ace/OS_NS_string.h>
#include <ace/INET_Addr.h>
#include <ace/SString.h>
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
RealmSocket::Session::Session(void)
{
}
RealmSocket::Session::~Session(void)
{
}
RealmSocket::RealmSocket(void):
input_buffer_(4096),
session_(NULL),
remote_address_()
{
this->reference_counting_policy().value(
ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
this->msg_queue()->high_water_mark(8*1024*1024);
this->msg_queue()->low_water_mark(8*1024*1024);
}
/*virtual*/ RealmSocket::~RealmSocket(void)
{
if(this->session_)
delete this->session_;
if(this->peer().get_handle() != ACE_INVALID_HANDLE)
this->peer().close();
}
/*virtual*/ int RealmSocket::open(void * arg)
{
if(Base::open(arg) == -1)
return -1;
ACE_INET_Addr addr;
if(peer().get_remote_addr(addr) == -1)
return -1;
char address[1024];
addr.get_host_addr(address, 1024);
this->remote_address_ = address;
if(this->session_ != NULL)
{
// Prepare for upcall
this->add_reference();
ACE_Event_Handler_var guard(this);
this->session_->OnAccept();
}
this->remove_reference();
return 0;
}
const ACE_CString& RealmSocket::get_remote_address(void) const
{
return this->remote_address_;
}
size_t RealmSocket::recv_len(void) const
{
return this->input_buffer_.length();
}
bool RealmSocket::recv_soft(char *buf, size_t len)
{
if(this->input_buffer_.length() < len)
return false;
ACE_OS::memcpy(buf, this->input_buffer_.rd_ptr(), len);
return true;
}
bool RealmSocket::recv(char *buf, size_t len)
{
bool ret = this->recv_soft(buf, len);
if(ret)
this->recv_skip(len);
return ret;
}
void RealmSocket::recv_skip(size_t len)
{
this->input_buffer_.rd_ptr(len);
}
ssize_t RealmSocket::noblk_send(ACE_Message_Block &message_block)
{
const size_t len = message_block.length();
if(len == 0)
return -1;
// Try to send the message directly.
ssize_t n = this->peer().send(message_block.rd_ptr(), len, MSG_NOSIGNAL);
if(n < 0)
{
if(errno == EWOULDBLOCK)
// Blocking signal
return 0;
else
// Error happened
return -1;
}
else if(n == 0)
{
// Can this happen ?
return -1;
}
// return bytes transmitted
return n;
}
bool RealmSocket::send(const char *buf, size_t len)
{
if(buf == NULL || len == 0)
return true;
ACE_Data_Block db(
len,
ACE_Message_Block::MB_DATA,
(const char*)buf,
0,
0,
ACE_Message_Block::DONT_DELETE,
0);
ACE_Message_Block message_block(
&db,
ACE_Message_Block::DONT_DELETE,
0);
message_block.wr_ptr(len);
if(this->msg_queue()->is_empty())
{
// Try to send it directly.
ssize_t n = this->noblk_send(message_block);
if(n < 0)
return false;
else if(n == len)
return true;
// fall down
message_block.rd_ptr((size_t)n);
}
ACE_Message_Block *mb = message_block.clone();
if(this->msg_queue()->enqueue_tail(mb, (ACE_Time_Value *) &ACE_Time_Value::zero) == -1)
{
mb->release();
return false;
}
if(this->reactor()->schedule_wakeup(this, ACE_Event_Handler::WRITE_MASK) == -1)
return false;
return true;
}
/*virtual*/ int RealmSocket::handle_output(ACE_HANDLE /*= ACE_INVALID_HANDLE*/)
{
ACE_Message_Block *mb = 0;
if(this->msg_queue()->is_empty())
{
this->reactor()->cancel_wakeup(this, ACE_Event_Handler::WRITE_MASK);
return 0;
}
if(this->msg_queue()->dequeue_head(mb, (ACE_Time_Value *) &ACE_Time_Value::zero) == -1)
return -1;
ssize_t n = this->noblk_send(*mb);
if(n < 0)
{
mb->release();
return -1;
}
else if(n == mb->length())
{
mb->release();
return 1;
}
else
{
mb->rd_ptr(n);
if(this->msg_queue()->enqueue_head(mb, (ACE_Time_Value *) &ACE_Time_Value::zero) == -1)
{
mb->release();
return -1;
}
return 0;
}
ACE_NOTREACHED(return -1);
}
/*virtual*/ int RealmSocket::handle_input(ACE_HANDLE /*= ACE_INVALID_HANDLE*/)
{
const ssize_t space = this->input_buffer_.space();
ssize_t n = this->peer().recv(this->input_buffer_.wr_ptr(), space);
if(n < 0)
{
return errno == EWOULDBLOCK ? 0 : -1;
}
else if(n == 0)
{
// EOF
return -1;
}
this->input_buffer_.wr_ptr((size_t)n);
if(this->session_ != NULL)
{
// Prepare for upcall
this->add_reference();
ACE_Event_Handler_var guard(this);
this->session_->OnRead();
this->input_buffer_.crunch();
}
// return 1 in case there is more data to read from OS
return n == space ? 1 : 0;
}
/*virtual*/ int RealmSocket::handle_close(ACE_HANDLE h, ACE_Reactor_Mask m)
{
if(this->session_ != NULL)
{
// Prepare for upcall
this->add_reference();
ACE_Event_Handler_var guard(this);
this->session_->OnClose();
}
this->shutdown();
return 0;
}
void RealmSocket::set_session(Session* session)
{
if(this->session_ != NULL)
delete this->session_;
this->session_ = session;
}
int RealmSocket::close(int)
{
this->shutdown();
this->remove_reference();
return 0;
}
|