]>
Commit | Line | Data |
---|---|---|
56d8adc0 | 1 | ///////////////////////////////////////////////////////////////////////////// |
f4ada568 GL |
2 | // Name: socket.cpp |
3 | // Purpose: Socket handler classes | |
56d8adc0 | 4 | // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia |
f4ada568 | 5 | // Created: April 1997 |
af2fd961 | 6 | // Updated: September 1999 |
a324a7bc | 7 | // Copyright: (C) 1999, 1998, 1997, Guilhem Lavaux |
af2fd961 | 8 | // (C) 1999, Guillermo Rodriguez Garcia |
f4ada568 GL |
9 | // RCS_ID: $Id$ |
10 | // License: see wxWindows license | |
56d8adc0 GRG |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
384b4373 | 13 | #ifdef __GNUG__ |
f4ada568 | 14 | #pragma implementation "socket.h" |
f4ada568 GL |
15 | #endif |
16 | ||
fcc6dddd JS |
17 | // For compilers that support precompilation, includes "wx.h". |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
35a4dab7 GL |
24 | #if wxUSE_SOCKETS |
25 | ||
f4ada568 GL |
26 | ///////////////////////////////////////////////////////////////////////////// |
27 | // wxWindows headers | |
28 | ///////////////////////////////////////////////////////////////////////////// | |
81b92e17 | 29 | |
56d8adc0 GRG |
30 | #include "wx/defs.h" |
31 | #include "wx/object.h" | |
32 | #include "wx/string.h" | |
33 | #include "wx/timer.h" | |
34 | #include "wx/utils.h" | |
35 | #include "wx/module.h" | |
36 | #include "wx/log.h" | |
58c837a4 | 37 | #include "wx/intl.h" |
7f555861 | 38 | |
f4ada568 GL |
39 | #include <stdlib.h> |
40 | #include <string.h> | |
41 | #include <ctype.h> | |
42 | ||
f4ada568 GL |
43 | ///////////////////////////////////////////////////////////////////////////// |
44 | // wxSocket headers | |
45 | ///////////////////////////////////////////////////////////////////////////// | |
81b92e17 | 46 | |
56d8adc0 GRG |
47 | #include "wx/sckaddr.h" |
48 | #include "wx/socket.h" | |
3b4183d8 | 49 | |
96db102a | 50 | |
81b92e17 | 51 | #define PROCESS_EVENTS() wxYield() |
96db102a | 52 | |
5c15428b | 53 | |
f4ada568 GL |
54 | // -------------------------------------------------------------- |
55 | // ClassInfos | |
56 | // -------------------------------------------------------------- | |
81b92e17 | 57 | |
a737331d GL |
58 | IMPLEMENT_CLASS(wxSocketBase, wxObject) |
59 | IMPLEMENT_CLASS(wxSocketServer, wxSocketBase) | |
60 | IMPLEMENT_CLASS(wxSocketClient, wxSocketBase) | |
a737331d | 61 | IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent) |
f4ada568 | 62 | |
56d8adc0 GRG |
63 | class wxSocketState : public wxObject |
64 | { | |
a324a7bc | 65 | public: |
81b92e17 GRG |
66 | bool m_notify_state; |
67 | GSocketEventFlags m_neededreq; | |
68 | wxSockFlags m_flags; | |
69 | wxSocketBase::wxSockCbk m_cbk; | |
70 | char *m_cdata; | |
a324a7bc GL |
71 | |
72 | public: | |
73 | wxSocketState() : wxObject() {} | |
74 | }; | |
75 | ||
f4ada568 | 76 | // -------------------------------------------------------------- |
56d8adc0 | 77 | // wxSocketBase ctor and dtor |
f4ada568 | 78 | // -------------------------------------------------------------- |
56d8adc0 | 79 | |
a64a02ef | 80 | wxSocketBase::wxSocketBase(wxSockFlags _flags, wxSockType _type) : |
f4ada568 | 81 | wxEvtHandler(), |
96db102a GRG |
82 | m_socket(NULL), m_id(-1), |
83 | m_flags(_flags), m_type(_type), | |
84 | m_neededreq(0), m_notify_state(FALSE), | |
d80d1aba | 85 | m_connected(FALSE), m_establishing(FALSE), |
96db102a GRG |
86 | m_reading(FALSE), m_writing(FALSE), |
87 | m_error(FALSE), m_lcount(0), m_timeout(600), m_states(), | |
88 | m_unread(NULL), m_unrd_size(0), m_unrd_cur(0), | |
96db102a | 89 | m_cbk(NULL), m_cdata(NULL) |
f4ada568 | 90 | { |
f4ada568 GL |
91 | } |
92 | ||
93 | wxSocketBase::wxSocketBase() : | |
94 | wxEvtHandler(), | |
96db102a GRG |
95 | m_socket(NULL), m_id(-1), |
96 | m_flags(NONE), m_type(SOCK_UNINIT), | |
97 | m_neededreq(0), m_notify_state(FALSE), | |
d80d1aba | 98 | m_connected(FALSE), m_establishing(FALSE), |
96db102a GRG |
99 | m_reading(FALSE), m_writing(FALSE), |
100 | m_error(FALSE), m_lcount(0), m_timeout(600), m_states(), | |
101 | m_unread(NULL), m_unrd_size(0), m_unrd_cur(0), | |
96db102a | 102 | m_cbk(NULL), m_cdata(NULL) |
f4ada568 | 103 | { |
f4ada568 GL |
104 | } |
105 | ||
f4ada568 GL |
106 | wxSocketBase::~wxSocketBase() |
107 | { | |
f4ada568 GL |
108 | if (m_unread) |
109 | free(m_unread); | |
a737331d | 110 | |
56d8adc0 | 111 | // Shutdown and close the socket |
a324a7bc | 112 | Close(); |
f4ada568 | 113 | |
56d8adc0 | 114 | // Destroy the GSocket object |
a324a7bc GL |
115 | if (m_socket) |
116 | GSocket_destroy(m_socket); | |
f4ada568 GL |
117 | } |
118 | ||
119 | bool wxSocketBase::Close() | |
120 | { | |
81b92e17 GRG |
121 | // Interrupt pending waits |
122 | InterruptAllWaits(); | |
123 | ||
31528cd3 | 124 | if (m_socket) |
8c14576d | 125 | { |
56d8adc0 GRG |
126 | // Disable callbacks |
127 | GSocket_UnsetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
128 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG); | |
f4ada568 | 129 | |
56d8adc0 | 130 | // Shutdown the connection |
a324a7bc | 131 | GSocket_Shutdown(m_socket); |
f4ada568 | 132 | m_connected = FALSE; |
d80d1aba | 133 | m_establishing = FALSE; |
f4ada568 GL |
134 | } |
135 | ||
136 | return TRUE; | |
137 | } | |
138 | ||
139 | // -------------------------------------------------------------- | |
56d8adc0 | 140 | // wxSocketBase basic IO operations |
f4ada568 | 141 | // -------------------------------------------------------------- |
56d8adc0 | 142 | |
96db102a GRG |
143 | // All IO operations {Read, Write, ReadMsg, WriteMsg, Peek, |
144 | // Unread, Discard} update m_error and m_lcount. | |
145 | // | |
146 | // TODO: Should Connect, Accept and AcceptWith update m_error? | |
17aa2bec | 147 | |
56d8adc0 GRG |
148 | class _wxSocketInternalTimer: public wxTimer |
149 | { | |
150 | public: | |
39b91eca | 151 | int *m_state; |
e00f35bb | 152 | unsigned long m_new_val; |
39b91eca GL |
153 | |
154 | void Notify() | |
56d8adc0 | 155 | { |
479cd5de | 156 | *m_state = (int)m_new_val; // Change the value |
56d8adc0 | 157 | } |
39b91eca | 158 | }; |
8c14576d | 159 | |
aa6d9706 | 160 | wxSocketBase& wxSocketBase::Read(char* buffer, wxUint32 nbytes) |
f4ada568 | 161 | { |
96db102a GRG |
162 | // Mask read events |
163 | m_reading = TRUE; | |
a324a7bc | 164 | |
96db102a | 165 | m_lcount = _Read(buffer, nbytes); |
17aa2bec | 166 | |
96db102a GRG |
167 | // If in WAITALL mode, all bytes should have been read. |
168 | if (m_flags & WAITALL) | |
169 | m_error = (m_lcount != nbytes); | |
170 | else | |
171 | m_error = (m_lcount == 0); | |
f4ada568 | 172 | |
96db102a GRG |
173 | // Trigger another read event if there is still data available. |
174 | m_reading = FALSE; | |
175 | // TODO: TriggerRead | |
17aa2bec | 176 | |
96db102a GRG |
177 | return *this; |
178 | } | |
179 | ||
180 | wxUint32 wxSocketBase::_Read(char* buffer, wxUint32 nbytes) | |
181 | { | |
182 | int total; | |
183 | int ret = 1; | |
a324a7bc | 184 | |
96db102a GRG |
185 | // we try this even if the connection has already been closed. |
186 | total = GetPushback(buffer, nbytes, FALSE); | |
187 | nbytes -= total; | |
188 | buffer += total; | |
189 | ||
190 | // If the socket is not connected, or we have got the whole | |
191 | // needed buffer, return immedately | |
f0a56ab0 | 192 | if (!m_connected || !m_socket || !nbytes) |
96db102a | 193 | return total; |
f4ada568 | 194 | |
af2fd961 | 195 | // Possible combinations (they are checked in this order) |
81b92e17 GRG |
196 | // wxSOCKET_NOWAIT |
197 | // wxSOCKET_WAITALL | wxSOCKET_BLOCK | |
198 | // wxSOCKET_WAITALL | |
199 | // wxSOCKET_BLOCK | |
200 | // wxSOCKET_NONE | |
af2fd961 | 201 | // |
81b92e17 | 202 | if (m_flags & wxSOCKET_NOWAIT) |
af2fd961 GRG |
203 | { |
204 | GSocket_SetNonBlocking(m_socket, TRUE); | |
205 | ret = GSocket_Read(m_socket, buffer, nbytes); | |
206 | GSocket_SetNonBlocking(m_socket, FALSE); | |
207 | ||
208 | if (ret > 0) | |
96db102a | 209 | total += ret; |
af2fd961 | 210 | } |
81b92e17 | 211 | else if (m_flags & wxSOCKET_WAITALL) // wxSOCKET_WAITALL |
56d8adc0 GRG |
212 | { |
213 | while (ret > 0 && nbytes > 0) | |
214 | { | |
81b92e17 GRG |
215 | if (!(m_flags & wxSOCKET_BLOCK) && !(WaitForRead())) |
216 | break; | |
217 | ||
a324a7bc | 218 | ret = GSocket_Read(m_socket, buffer, nbytes); |
a324a7bc | 219 | |
81b92e17 GRG |
220 | if (ret > 0) |
221 | { | |
222 | total += ret; | |
223 | buffer += ret; | |
224 | nbytes -= ret; | |
225 | } | |
226 | } | |
56d8adc0 | 227 | } |
81b92e17 | 228 | else |
56d8adc0 | 229 | { |
81b92e17 GRG |
230 | if ((m_flags & wxSOCKET_BLOCK) || WaitForRead()) |
231 | { | |
232 | ret = GSocket_Read(m_socket, buffer, nbytes); | |
a324a7bc | 233 | |
81b92e17 GRG |
234 | if (ret > 0) |
235 | total += ret; | |
236 | } | |
a324a7bc | 237 | } |
f4ada568 | 238 | |
96db102a | 239 | return total; |
f4ada568 GL |
240 | } |
241 | ||
aa6d9706 | 242 | wxSocketBase& wxSocketBase::ReadMsg(char* buffer, wxUint32 nbytes) |
062c4861 | 243 | { |
96db102a | 244 | #define MAX_DISCARD_SIZE (10 * 1024) |
17aa2bec | 245 | |
96db102a GRG |
246 | wxUint32 len, len2, sig, total; |
247 | bool error; | |
17aa2bec | 248 | int old_flags; |
17aa2bec GRG |
249 | struct |
250 | { | |
96db102a GRG |
251 | unsigned char sig[4]; |
252 | unsigned char len[4]; | |
062c4861 GL |
253 | } msg; |
254 | ||
96db102a GRG |
255 | // Mask read events |
256 | m_reading = TRUE; | |
062c4861 | 257 | |
96db102a GRG |
258 | total = 0; |
259 | error = TRUE; | |
17aa2bec | 260 | old_flags = m_flags; |
81b92e17 | 261 | SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL); |
17aa2bec | 262 | |
96db102a GRG |
263 | if (_Read((char *)&msg, sizeof(msg)) != sizeof(msg)) |
264 | goto exit; | |
062c4861 | 265 | |
96db102a GRG |
266 | sig = (wxUint32)msg.sig[0]; |
267 | sig |= (wxUint32)(msg.sig[1] << 8); | |
268 | sig |= (wxUint32)(msg.sig[2] << 16); | |
269 | sig |= (wxUint32)(msg.sig[3] << 24); | |
062c4861 GL |
270 | |
271 | if (sig != 0xfeeddead) | |
17aa2bec | 272 | { |
58c837a4 | 273 | wxLogWarning( _("TCP: invalid signature returned to ReadMsg.")); |
96db102a | 274 | goto exit; |
17aa2bec GRG |
275 | } |
276 | ||
96db102a GRG |
277 | len = (wxUint32)msg.len[0]; |
278 | len |= (wxUint32)(msg.len[1] << 8); | |
279 | len |= (wxUint32)(msg.len[2] << 16); | |
280 | len |= (wxUint32)(msg.len[3] << 24); | |
281 | ||
282 | //wxLogMessage("Readmsg: %d %d %d %d -> len == %d", | |
283 | // msg.len[0], msg.len[1], msg.len[2], msg.len[3], len); | |
062c4861 | 284 | |
17aa2bec GRG |
285 | if (len > nbytes) |
286 | { | |
062c4861 GL |
287 | len2 = len - nbytes; |
288 | len = nbytes; | |
289 | } | |
290 | else | |
291 | len2 = 0; | |
292 | ||
96db102a GRG |
293 | // This check is necessary so that we don't attemp to read if |
294 | // the msg was zero bytes long. | |
295 | if (len) | |
17aa2bec | 296 | { |
96db102a GRG |
297 | total = _Read(buffer, len); |
298 | ||
299 | if (total != len) | |
300 | goto exit; | |
17aa2bec GRG |
301 | } |
302 | if (len2) | |
303 | { | |
96db102a | 304 | char *discard_buffer = new char[MAX_DISCARD_SIZE]; |
17aa2bec GRG |
305 | long discard_len; |
306 | ||
96db102a | 307 | // NOTE: discarded bytes don't add to m_lcount. |
17aa2bec GRG |
308 | do |
309 | { | |
96db102a | 310 | discard_len = ((len2 > MAX_DISCARD_SIZE)? MAX_DISCARD_SIZE : len2); |
479cd5de VZ |
311 | discard_len = _Read(discard_buffer, (wxUint32)discard_len); |
312 | len2 -= (wxUint32)discard_len; | |
17aa2bec GRG |
313 | } |
314 | while ((discard_len > 0) && len2); | |
315 | ||
316 | delete [] discard_buffer; | |
317 | ||
318 | if (len2 != 0) | |
96db102a | 319 | goto exit; |
17aa2bec | 320 | } |
96db102a GRG |
321 | if (_Read((char *)&msg, sizeof(msg)) != sizeof(msg)) |
322 | goto exit; | |
062c4861 | 323 | |
96db102a GRG |
324 | sig = (wxUint32)msg.sig[0]; |
325 | sig |= (wxUint32)(msg.sig[1] << 8); | |
326 | sig |= (wxUint32)(msg.sig[2] << 16); | |
327 | sig |= (wxUint32)(msg.sig[3] << 24); | |
a324a7bc | 328 | |
062c4861 | 329 | if (sig != 0xdeadfeed) |
17aa2bec | 330 | { |
223d09f6 | 331 | //wxLogMessage(wxT("Warning: invalid signature returned to ReadMsg")); |
96db102a | 332 | goto exit; |
17aa2bec | 333 | } |
062c4861 | 334 | |
96db102a GRG |
335 | // everything was OK |
336 | error = FALSE; | |
337 | ||
338 | exit: | |
339 | m_error = error; | |
340 | m_lcount = total; | |
341 | m_reading = FALSE; | |
17aa2bec | 342 | SetFlags(old_flags); |
96db102a GRG |
343 | |
344 | // TODO: TriggerRead | |
062c4861 | 345 | return *this; |
17aa2bec | 346 | |
96db102a | 347 | #undef MAX_DISCARD_SIZE |
062c4861 GL |
348 | } |
349 | ||
aa6d9706 | 350 | wxSocketBase& wxSocketBase::Peek(char* buffer, wxUint32 nbytes) |
f4ada568 | 351 | { |
96db102a GRG |
352 | // Mask read events |
353 | m_reading = TRUE; | |
354 | ||
355 | m_lcount = _Read(buffer, nbytes); | |
356 | Pushback(buffer, nbytes); | |
357 | ||
81b92e17 GRG |
358 | // If in wxSOCKET_WAITALL mode, all bytes should have been read. |
359 | if (m_flags & wxSOCKET_WAITALL) | |
96db102a GRG |
360 | m_error = (m_lcount != nbytes); |
361 | else | |
362 | m_error = (m_lcount == 0); | |
363 | ||
364 | // Trigger another read event if there is still data available. | |
365 | m_reading = FALSE; | |
f4ada568 | 366 | |
96db102a | 367 | // TODO: TriggerRead |
f4ada568 GL |
368 | return *this; |
369 | } | |
370 | ||
aa6d9706 | 371 | wxSocketBase& wxSocketBase::Write(const char *buffer, wxUint32 nbytes) |
f4ada568 | 372 | { |
96db102a GRG |
373 | // Mask write events |
374 | m_writing = TRUE; | |
56d8adc0 | 375 | |
96db102a GRG |
376 | m_lcount = _Write(buffer, nbytes); |
377 | ||
81b92e17 GRG |
378 | // If in wxSOCKET_WAITALL mode, all bytes should have been written. |
379 | if (m_flags & wxSOCKET_WAITALL) | |
96db102a GRG |
380 | m_error = (m_lcount != nbytes); |
381 | else | |
382 | m_error = (m_lcount == 0); | |
383 | ||
384 | // Trigger another write event if the socket is still writable | |
385 | m_writing = FALSE; | |
386 | ||
387 | // TODO: TriggerWrite | |
388 | return *this; | |
389 | } | |
390 | ||
391 | wxUint32 wxSocketBase::_Write(const char *buffer, wxUint32 nbytes) | |
392 | { | |
393 | wxUint32 total = 0; | |
394 | int ret = 1; | |
56d8adc0 | 395 | |
17aa2bec | 396 | if (!m_connected || !m_socket) |
96db102a | 397 | return 0; |
a324a7bc | 398 | |
af2fd961 | 399 | // Possible combinations (they are checked in this order) |
81b92e17 GRG |
400 | // wxSOCKET_NOWAIT |
401 | // wxSOCKET_WAITALL | wxSOCKET_BLOCK | |
402 | // wxSOCKET_WAITALL | |
403 | // wxSOCKET_BLOCK | |
404 | // wxSOCKET_NONE | |
af2fd961 | 405 | // |
81b92e17 | 406 | if (m_flags & wxSOCKET_NOWAIT) |
af2fd961 GRG |
407 | { |
408 | GSocket_SetNonBlocking(m_socket, TRUE); | |
409 | ret = GSocket_Write(m_socket, buffer, nbytes); | |
410 | GSocket_SetNonBlocking(m_socket, FALSE); | |
411 | ||
412 | if (ret > 0) | |
96db102a | 413 | total = ret; |
af2fd961 | 414 | } |
81b92e17 | 415 | else if (m_flags & wxSOCKET_WAITALL) |
56d8adc0 GRG |
416 | { |
417 | while (ret > 0 && nbytes > 0) | |
418 | { | |
81b92e17 GRG |
419 | if (!(m_flags & wxSOCKET_BLOCK) && !(WaitForWrite())) |
420 | break; | |
421 | ||
56d8adc0 | 422 | ret = GSocket_Write(m_socket, buffer, nbytes); |
56d8adc0 | 423 | |
81b92e17 GRG |
424 | if (ret > 0) |
425 | { | |
426 | total += ret; | |
427 | buffer += ret; | |
428 | nbytes -= ret; | |
429 | } | |
430 | } | |
56d8adc0 | 431 | } |
81b92e17 | 432 | else |
56d8adc0 | 433 | { |
81b92e17 GRG |
434 | if ((m_flags & wxSOCKET_BLOCK) || WaitForWrite()) |
435 | { | |
436 | ret = GSocket_Write(m_socket, buffer, nbytes); | |
a324a7bc | 437 | |
81b92e17 GRG |
438 | if (ret > 0) |
439 | total = ret; | |
440 | } | |
56d8adc0 | 441 | } |
a324a7bc | 442 | |
96db102a | 443 | return total; |
f4ada568 GL |
444 | } |
445 | ||
aa6d9706 | 446 | wxSocketBase& wxSocketBase::WriteMsg(const char *buffer, wxUint32 nbytes) |
062c4861 | 447 | { |
96db102a GRG |
448 | wxUint32 total; |
449 | bool error; | |
17aa2bec | 450 | int old_flags; |
062c4861 | 451 | struct { |
96db102a GRG |
452 | unsigned char sig[4]; |
453 | unsigned char len[4]; | |
062c4861 GL |
454 | } msg; |
455 | ||
96db102a GRG |
456 | // Mask write events |
457 | m_writing = TRUE; | |
458 | ||
459 | error = TRUE; | |
460 | total = 0; | |
461 | old_flags = m_flags; | |
81b92e17 | 462 | SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL); |
96db102a | 463 | |
062c4861 GL |
464 | // warning about 'cast truncates constant value' |
465 | #ifdef __VISUALC__ | |
96db102a | 466 | # pragma warning(disable: 4310) |
062c4861 GL |
467 | #endif // __VISUALC__ |
468 | ||
96db102a GRG |
469 | msg.sig[0] = (unsigned char) 0xad; |
470 | msg.sig[1] = (unsigned char) 0xde; | |
471 | msg.sig[2] = (unsigned char) 0xed; | |
472 | msg.sig[3] = (unsigned char) 0xfe; | |
062c4861 | 473 | |
96db102a GRG |
474 | msg.len[0] = (unsigned char) nbytes & 0xff; |
475 | msg.len[1] = (unsigned char) (nbytes >> 8) & 0xff; | |
476 | msg.len[2] = (unsigned char) (nbytes >> 16) & 0xff; | |
477 | msg.len[3] = (unsigned char) (nbytes >> 24) & 0xff; | |
062c4861 | 478 | |
96db102a GRG |
479 | //wxLogMessage("Writemsg: %d %d %d %d -> %d", |
480 | // nbytes & 0xff, | |
481 | // (nbytes >> 8) & 0xff, | |
482 | // (nbytes >> 16) & 0xff, | |
483 | // (nbytes >> 24) & 0xff, | |
484 | // nbytes | |
485 | // ); | |
17aa2bec | 486 | |
96db102a GRG |
487 | if (_Write((char *)&msg, sizeof(msg)) < sizeof(msg)) |
488 | goto exit; | |
489 | ||
490 | total = _Write(buffer, nbytes); | |
491 | ||
492 | if (total < nbytes) | |
493 | goto exit; | |
062c4861 | 494 | |
96db102a GRG |
495 | msg.sig[0] = (unsigned char) 0xed; |
496 | msg.sig[1] = (unsigned char) 0xfe; | |
497 | msg.sig[2] = (unsigned char) 0xad; | |
498 | msg.sig[3] = (unsigned char) 0xde; | |
062c4861 | 499 | msg.len[0] = msg.len[1] = msg.len[2] = msg.len[3] = (char) 0; |
062c4861 | 500 | |
96db102a GRG |
501 | if ((_Write((char *)&msg, sizeof(msg))) < sizeof(msg)) |
502 | goto exit; | |
17aa2bec | 503 | |
96db102a GRG |
504 | // everything was OK |
505 | error = FALSE; | |
506 | ||
507 | exit: | |
508 | m_error = error; | |
509 | m_lcount = total; | |
510 | m_writing = FALSE; | |
511 | ||
512 | // TODO: TriggerWrite | |
062c4861 GL |
513 | return *this; |
514 | ||
515 | #ifdef __VISUALC__ | |
96db102a | 516 | # pragma warning(default: 4310) |
062c4861 GL |
517 | #endif // __VISUALC__ |
518 | } | |
519 | ||
aa6d9706 | 520 | wxSocketBase& wxSocketBase::Unread(const char *buffer, wxUint32 nbytes) |
f4ada568 | 521 | { |
56d8adc0 | 522 | if (nbytes != 0) |
96db102a | 523 | Pushback(buffer, nbytes); |
f4ada568 | 524 | |
96db102a GRG |
525 | m_error = FALSE; |
526 | m_lcount = nbytes; | |
f4ada568 | 527 | |
96db102a | 528 | return *this; |
a324a7bc GL |
529 | } |
530 | ||
96db102a | 531 | wxSocketBase& wxSocketBase::Discard() |
f4ada568 GL |
532 | { |
533 | #define MAX_BUFSIZE (10*1024) | |
56d8adc0 | 534 | |
96db102a | 535 | int old_flags; |
f4ada568 | 536 | char *my_data = new char[MAX_BUFSIZE]; |
aa6d9706 | 537 | wxUint32 recv_size = MAX_BUFSIZE; |
17aa2bec | 538 | wxUint32 total = 0; |
f4ada568 | 539 | |
96db102a GRG |
540 | // Mask read events |
541 | m_reading = TRUE; | |
542 | ||
543 | old_flags = m_flags; | |
81b92e17 | 544 | SetFlags(wxSOCKET_NOWAIT); |
384b4373 | 545 | |
31528cd3 | 546 | while (recv_size == MAX_BUFSIZE) |
8c14576d | 547 | { |
96db102a | 548 | recv_size = _Read(my_data, MAX_BUFSIZE); |
17aa2bec | 549 | total += recv_size; |
f4ada568 GL |
550 | } |
551 | ||
f4ada568 | 552 | delete [] my_data; |
17aa2bec GRG |
553 | m_lcount = total; |
554 | m_error = FALSE; | |
555 | ||
96db102a GRG |
556 | // Trigger another read event if there is still data available. |
557 | m_reading = FALSE; | |
558 | ||
559 | // TODO: TriggerRead | |
560 | return *this; | |
f0a56ab0 | 561 | |
f4ada568 GL |
562 | #undef MAX_BUFSIZE |
563 | } | |
564 | ||
565 | // -------------------------------------------------------------- | |
56d8adc0 | 566 | // wxSocketBase get local or peer addresses |
f4ada568 | 567 | // -------------------------------------------------------------- |
8c14576d | 568 | |
f4ada568 GL |
569 | bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const |
570 | { | |
a324a7bc | 571 | GAddress *peer; |
f4ada568 | 572 | |
a324a7bc | 573 | if (!m_socket) |
f4ada568 GL |
574 | return FALSE; |
575 | ||
a324a7bc GL |
576 | peer = GSocket_GetPeer(m_socket); |
577 | addr_man.SetAddress(peer); | |
578 | GAddress_destroy(peer); | |
a737331d | 579 | |
f4ada568 GL |
580 | return TRUE; |
581 | } | |
582 | ||
583 | bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const | |
584 | { | |
a324a7bc | 585 | GAddress *local; |
f4ada568 | 586 | |
a324a7bc | 587 | if (!m_socket) |
f4ada568 GL |
588 | return FALSE; |
589 | ||
a324a7bc GL |
590 | local = GSocket_GetLocal(m_socket); |
591 | addr_man.SetAddress(local); | |
592 | GAddress_destroy(local); | |
f4ada568 | 593 | |
f4ada568 GL |
594 | return TRUE; |
595 | } | |
596 | ||
597 | // -------------------------------------------------------------- | |
56d8adc0 | 598 | // wxSocketBase save and restore socket state |
f4ada568 | 599 | // -------------------------------------------------------------- |
db131261 | 600 | |
f4ada568 GL |
601 | void wxSocketBase::SaveState() |
602 | { | |
a324a7bc GL |
603 | wxSocketState *state; |
604 | ||
605 | state = new wxSocketState(); | |
f4ada568 | 606 | |
81b92e17 GRG |
607 | state->m_notify_state = m_notify_state; |
608 | state->m_neededreq = m_neededreq; | |
609 | state->m_flags = m_flags; | |
610 | state->m_cbk = m_cbk; | |
611 | state->m_cdata = m_cdata; | |
f4ada568 | 612 | |
a324a7bc | 613 | m_states.Append(state); |
f4ada568 GL |
614 | } |
615 | ||
616 | void wxSocketBase::RestoreState() | |
617 | { | |
618 | wxNode *node; | |
a324a7bc | 619 | wxSocketState *state; |
f4ada568 GL |
620 | |
621 | node = m_states.Last(); | |
622 | if (!node) | |
623 | return; | |
624 | ||
a324a7bc | 625 | state = (wxSocketState *)node->Data(); |
384b4373 | 626 | |
81b92e17 GRG |
627 | SetFlags(state->m_flags); |
628 | m_cbk = state->m_cbk; | |
629 | m_cdata = state->m_cdata; | |
630 | m_neededreq = state->m_neededreq; | |
631 | Notify(state->m_notify_state); | |
f4ada568 GL |
632 | |
633 | delete node; | |
634 | delete state; | |
635 | } | |
636 | ||
f4ada568 GL |
637 | |
638 | // -------------------------------------------------------------- | |
56d8adc0 | 639 | // wxSocketBase Wait functions |
f4ada568 GL |
640 | // -------------------------------------------------------------- |
641 | ||
56d8adc0 GRG |
642 | // GRG: I have completely rewritten this family of functions |
643 | // so that they don't depend on event notifications; instead, | |
644 | // they poll the socket, using GSocket_Select(), to check for | |
645 | // the specified combination of event flags, until an event | |
646 | // occurs or until the timeout ellapses. The polling loop | |
5c15428b | 647 | // calls PROCESS_EVENTS(), so this won't block the GUI. |
a324a7bc | 648 | |
56d8adc0 | 649 | bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags flags) |
375abe3d | 650 | { |
56d8adc0 | 651 | GSocketEventFlags result; |
a324a7bc | 652 | _wxSocketInternalTimer timer; |
56d8adc0 GRG |
653 | long timeout; |
654 | int state = -1; | |
375abe3d | 655 | |
81b92e17 GRG |
656 | // Set this to TRUE to interrupt ongoing waits |
657 | m_interrupt = FALSE; | |
658 | ||
56d8adc0 | 659 | // Check for valid socket |
af2fd961 GRG |
660 | if (!m_socket) |
661 | return FALSE; | |
662 | ||
663 | // If it is not a server, it must be connected or establishing connection | |
664 | if ((m_type != SOCK_SERVER) && (!m_connected && !m_establishing)) | |
a737331d | 665 | return FALSE; |
f4ada568 | 666 | |
56d8adc0 GRG |
667 | // Check for valid timeout value |
668 | if (seconds != -1) | |
669 | timeout = seconds * 1000 + milliseconds; | |
670 | else | |
671 | timeout = m_timeout * 1000; | |
a324a7bc | 672 | |
56d8adc0 | 673 | // Activate timer |
96db102a GRG |
674 | if (timeout) |
675 | { | |
676 | timer.m_state = &state; | |
677 | timer.m_new_val = 0; | |
479cd5de | 678 | timer.Start((int)timeout, TRUE); |
96db102a | 679 | } |
a324a7bc | 680 | |
56d8adc0 | 681 | // Active polling (without using events) |
af2fd961 GRG |
682 | // |
683 | // NOTE: this duplicates some of the code in OnRequest (lost | |
684 | // connection and connection establishment handling) but this | |
685 | // doesn't hurt. It has to be here because the event might | |
686 | // be a bit delayed, and it has to be in OnRequest as well | |
687 | // because maybe the WaitXXX functions are not being used. | |
688 | // | |
96db102a GRG |
689 | // Do this at least once (important if timeout == 0, when |
690 | // we are just polling) | |
81b92e17 GRG |
691 | // |
692 | while (state == -1) | |
56d8adc0 | 693 | { |
af2fd961 GRG |
694 | result = GSocket_Select(m_socket, flags | GSOCK_LOST_FLAG); |
695 | ||
696 | // Connection lost | |
697 | if (result & GSOCK_LOST_FLAG) | |
698 | { | |
699 | timer.Stop(); | |
af2fd961 GRG |
700 | Close(); |
701 | return TRUE; | |
702 | } | |
703 | ||
704 | // Incoming connection (server) or connection established (client) | |
705 | if (result & GSOCK_CONNECTION_FLAG) | |
706 | { | |
707 | timer.Stop(); | |
708 | m_connected = TRUE; | |
709 | m_establishing = FALSE; | |
710 | return TRUE; | |
711 | } | |
712 | ||
81b92e17 | 713 | if ((result & GSOCK_INPUT_FLAG) || (result & GSOCK_OUTPUT_FLAG)) |
af2fd961 | 714 | { |
81b92e17 GRG |
715 | timer.Stop(); |
716 | return TRUE; | |
af2fd961 GRG |
717 | } |
718 | ||
81b92e17 GRG |
719 | // Wait more? |
720 | if ((timeout == 0) || (m_interrupt)) | |
721 | break; | |
722 | ||
723 | PROCESS_EVENTS(); | |
56d8adc0 | 724 | } |
f4ada568 | 725 | |
a324a7bc | 726 | timer.Stop(); |
af2fd961 | 727 | return FALSE; |
f4ada568 | 728 | } |
f4ada568 | 729 | |
a737331d | 730 | bool wxSocketBase::Wait(long seconds, long milliseconds) |
f4ada568 | 731 | { |
d80d1aba GRG |
732 | return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG | |
733 | GSOCK_OUTPUT_FLAG | | |
734 | GSOCK_CONNECTION_FLAG | | |
735 | GSOCK_LOST_FLAG); | |
f4ada568 | 736 | } |
f4ada568 | 737 | |
a737331d | 738 | bool wxSocketBase::WaitForRead(long seconds, long milliseconds) |
f4ada568 | 739 | { |
96db102a GRG |
740 | // Check pushback buffer |
741 | if (m_unread) | |
742 | return TRUE; | |
743 | ||
af2fd961 | 744 | return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG); |
f4ada568 GL |
745 | } |
746 | ||
a737331d | 747 | bool wxSocketBase::WaitForWrite(long seconds, long milliseconds) |
f4ada568 | 748 | { |
af2fd961 | 749 | return _Wait(seconds, milliseconds, GSOCK_OUTPUT_FLAG); |
a737331d | 750 | } |
f4ada568 | 751 | |
a737331d GL |
752 | bool wxSocketBase::WaitForLost(long seconds, long milliseconds) |
753 | { | |
a324a7bc | 754 | return _Wait(seconds, milliseconds, GSOCK_LOST_FLAG); |
f4ada568 | 755 | } |
a737331d | 756 | |
17aa2bec GRG |
757 | void wxSocketBase::SetTimeout(long seconds) |
758 | { | |
759 | m_timeout = seconds; | |
760 | ||
761 | if (m_socket) | |
e8f4c584 | 762 | GSocket_SetTimeout(m_socket, m_timeout * 1000); |
17aa2bec GRG |
763 | } |
764 | ||
a737331d | 765 | // -------------------------------------------------------------- |
56d8adc0 | 766 | // wxSocketBase flags |
a737331d | 767 | // -------------------------------------------------------------- |
f4ada568 | 768 | |
f4ada568 GL |
769 | void wxSocketBase::SetFlags(wxSockFlags _flags) |
770 | { | |
771 | m_flags = _flags; | |
f4ada568 | 772 | } |
f0a56ab0 | 773 | |
56d8adc0 GRG |
774 | // -------------------------------------------------------------- |
775 | // wxSocketBase callback management | |
776 | // -------------------------------------------------------------- | |
777 | ||
778 | wxSocketBase::wxSockCbk wxSocketBase::Callback(wxSockCbk cbk_) | |
f4ada568 | 779 | { |
56d8adc0 | 780 | wxSockCbk old_cbk = cbk_; |
f4ada568 | 781 | |
56d8adc0 GRG |
782 | m_cbk = cbk_; |
783 | return old_cbk; | |
784 | } | |
785 | ||
786 | char *wxSocketBase::CallbackData(char *data) | |
787 | { | |
788 | char *old_data = m_cdata; | |
789 | ||
790 | m_cdata = data; | |
791 | return old_data; | |
f4ada568 GL |
792 | } |
793 | ||
a324a7bc | 794 | // -------------------------------------------------------------- |
56d8adc0 | 795 | // wxSocketBase automatic notifier |
a324a7bc GL |
796 | // -------------------------------------------------------------- |
797 | ||
96db102a GRG |
798 | // All events (INPUT, OUTPUT, CONNECTION, LOST) are now always |
799 | // internally watched; but users will only be notified of those | |
800 | // events they are interested in. | |
801 | ||
6e31e940 VZ |
802 | static void LINKAGEMODE wx_socket_callback(GSocket * WXUNUSED(socket), |
803 | GSocketEvent event, char *cdata) | |
a324a7bc GL |
804 | { |
805 | wxSocketBase *sckobj = (wxSocketBase *)cdata; | |
806 | ||
aa8fb7a0 | 807 | sckobj->OnRequest((wxSocketNotify)event); |
a324a7bc GL |
808 | } |
809 | ||
56d8adc0 | 810 | wxSocketEventFlags wxSocketBase::EventToNotify(wxSocketNotify evt) |
f4ada568 | 811 | { |
56d8adc0 GRG |
812 | switch (evt) |
813 | { | |
814 | case GSOCK_INPUT: return GSOCK_INPUT_FLAG; | |
815 | case GSOCK_OUTPUT: return GSOCK_OUTPUT_FLAG; | |
816 | case GSOCK_CONNECTION: return GSOCK_CONNECTION_FLAG; | |
817 | case GSOCK_LOST: return GSOCK_LOST_FLAG; | |
818 | } | |
819 | return 0; | |
820 | } | |
aadbdf11 | 821 | |
56d8adc0 GRG |
822 | void wxSocketBase::SetNotify(wxSocketEventFlags flags) |
823 | { | |
824 | m_neededreq = flags; | |
825 | } | |
a324a7bc | 826 | |
56d8adc0 GRG |
827 | void wxSocketBase::Notify(bool notify) |
828 | { | |
829 | m_notify_state = notify; | |
f4ada568 GL |
830 | } |
831 | ||
aa8fb7a0 | 832 | void wxSocketBase::OnRequest(wxSocketNotify req_evt) |
f4ada568 | 833 | { |
a737331d | 834 | wxSocketEvent event(m_id); |
56d8adc0 | 835 | wxSocketEventFlags flag = EventToNotify(req_evt); |
a324a7bc | 836 | |
96db102a GRG |
837 | // fprintf(stderr, "%s: Entering OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt); |
838 | ||
81b92e17 GRG |
839 | // NOTE: this duplicates some of the code in _Wait, (lost |
840 | // connections and delayed connection establishment) but | |
af2fd961 GRG |
841 | // this doesn't hurt. It has to be here because maybe the |
842 | // WaitXXX are not being used, and it has to be in _Wait | |
843 | // as well because the event might be a bit delayed. | |
844 | // | |
81b92e17 | 845 | switch (req_evt) |
d80d1aba | 846 | { |
81b92e17 | 847 | case wxSOCKET_CONNECTION : |
d80d1aba GRG |
848 | m_establishing = FALSE; |
849 | m_connected = TRUE; | |
850 | break; | |
851 | case wxSOCKET_LOST: | |
852 | Close(); | |
853 | break; | |
f4ada568 | 854 | |
96db102a GRG |
855 | // If we are in the middle of a R/W operation, do not |
856 | // propagate events to users. | |
81b92e17 GRG |
857 | // |
858 | case wxSOCKET_INPUT: | |
859 | if (m_reading) return; | |
860 | ||
861 | case wxSOCKET_OUTPUT: | |
862 | if (m_writing) return; | |
96db102a GRG |
863 | } |
864 | ||
56d8adc0 | 865 | if (((m_neededreq & flag) == flag) && m_notify_state) |
d80d1aba | 866 | { |
96db102a | 867 | // fprintf(stderr, "%s: Evt %d delivered\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt); |
a737331d | 868 | event.m_socket = this; |
de3131e7 | 869 | event.m_skevt = req_evt; |
81b92e17 | 870 | ProcessEvent(event); // XXX - should be PostEvent |
56d8adc0 | 871 | |
81b92e17 | 872 | OldOnNotify(req_evt); |
56d8adc0 GRG |
873 | if (m_cbk) |
874 | m_cbk(*this, req_evt, m_cdata); | |
81b92e17 | 875 | |
f4ada568 | 876 | } |
96db102a GRG |
877 | |
878 | // fprintf(stderr, "%s: Exiting OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt); | |
f4ada568 GL |
879 | } |
880 | ||
6e31e940 | 881 | void wxSocketBase::OldOnNotify(wxSocketNotify WXUNUSED(evt)) |
f4ada568 | 882 | { |
f4ada568 GL |
883 | } |
884 | ||
a737331d | 885 | // -------------------------------------------------------------- |
56d8adc0 | 886 | // wxSocketBase set event handler |
a737331d | 887 | // -------------------------------------------------------------- |
f4ada568 GL |
888 | |
889 | void wxSocketBase::SetEventHandler(wxEvtHandler& h_evt, int id) | |
890 | { | |
891 | SetNextHandler(&h_evt); | |
892 | m_id = id; | |
893 | } | |
894 | ||
895 | // -------------------------------------------------------------- | |
96db102a | 896 | // wxSocketBase pushback |
f4ada568 | 897 | // -------------------------------------------------------------- |
db131261 | 898 | |
96db102a | 899 | void wxSocketBase::Pushback(const char *buffer, wxUint32 size) |
f4ada568 | 900 | { |
a324a7bc GL |
901 | if (m_unread == NULL) |
902 | m_unread = (char *)malloc(size); | |
903 | else { | |
904 | char *tmp; | |
f4ada568 | 905 | |
a324a7bc GL |
906 | tmp = (char *)malloc(m_unrd_size + size); |
907 | memcpy(tmp+size, m_unread, m_unrd_size); | |
41895a05 | 908 | free(m_unread); |
a324a7bc GL |
909 | |
910 | m_unread = tmp; | |
41895a05 | 911 | } |
31528cd3 | 912 | |
f4ada568 | 913 | m_unrd_size += size; |
a324a7bc GL |
914 | |
915 | memcpy(m_unread, buffer, size); | |
f4ada568 GL |
916 | } |
917 | ||
aa6d9706 | 918 | wxUint32 wxSocketBase::GetPushback(char *buffer, wxUint32 size, bool peek) |
f4ada568 GL |
919 | { |
920 | if (!m_unrd_size) | |
921 | return 0; | |
922 | ||
a324a7bc GL |
923 | if (size > (m_unrd_size-m_unrd_cur)) |
924 | size = m_unrd_size-m_unrd_cur; | |
96db102a | 925 | |
a324a7bc | 926 | memcpy(buffer, (m_unread+m_unrd_cur), size); |
f4ada568 GL |
927 | |
928 | if (!peek) { | |
a324a7bc GL |
929 | m_unrd_cur += size; |
930 | if (m_unrd_size == m_unrd_cur) { | |
f4ada568 GL |
931 | free(m_unread); |
932 | m_unread = NULL; | |
a324a7bc GL |
933 | m_unrd_size = 0; |
934 | m_unrd_cur = 0; | |
f4ada568 GL |
935 | } |
936 | } | |
937 | ||
938 | return size; | |
939 | } | |
940 | ||
f4ada568 | 941 | // -------------------------------------------------------------- |
8c14576d | 942 | // wxSocketServer |
f4ada568 GL |
943 | // -------------------------------------------------------------- |
944 | ||
56d8adc0 GRG |
945 | // -------------------------------------------------------------- |
946 | // wxSocketServer ctor and dtor | |
947 | // -------------------------------------------------------------- | |
948 | ||
f4ada568 | 949 | wxSocketServer::wxSocketServer(wxSockAddress& addr_man, |
17dff81c | 950 | wxSockFlags flags) : |
f4ada568 GL |
951 | wxSocketBase(flags, SOCK_SERVER) |
952 | { | |
56d8adc0 | 953 | // Create the socket |
a324a7bc | 954 | m_socket = GSocket_new(); |
f4ada568 | 955 | |
a324a7bc | 956 | if (!m_socket) |
f4ada568 | 957 | return; |
384b4373 | 958 | |
56d8adc0 | 959 | // Setup the socket as server |
a324a7bc | 960 | GSocket_SetLocal(m_socket, addr_man.GetAddress()); |
56d8adc0 GRG |
961 | if (GSocket_SetServer(m_socket) != GSOCK_NOERROR) |
962 | { | |
a324a7bc GL |
963 | GSocket_destroy(m_socket); |
964 | m_socket = NULL; | |
f4ada568 GL |
965 | return; |
966 | } | |
a737331d | 967 | |
e8f4c584 | 968 | GSocket_SetTimeout(m_socket, m_timeout * 1000); |
56d8adc0 GRG |
969 | GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | |
970 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
971 | wx_socket_callback, (char *)this); | |
972 | ||
f4ada568 GL |
973 | } |
974 | ||
975 | // -------------------------------------------------------------- | |
8c14576d | 976 | // wxSocketServer Accept |
f4ada568 | 977 | // -------------------------------------------------------------- |
8c14576d | 978 | |
d80d1aba | 979 | bool wxSocketServer::AcceptWith(wxSocketBase& sock, bool wait) |
f4ada568 | 980 | { |
a324a7bc | 981 | GSocket *child_socket; |
f4ada568 | 982 | |
17aa2bec GRG |
983 | if (!m_socket) |
984 | return FALSE; | |
985 | ||
56d8adc0 GRG |
986 | // GRG: If wait == FALSE, then the call should be nonblocking. |
987 | // When we are finished, we put the socket to blocking mode | |
988 | // again. | |
d80d1aba GRG |
989 | |
990 | if (!wait) | |
991 | GSocket_SetNonBlocking(m_socket, TRUE); | |
992 | ||
a324a7bc | 993 | child_socket = GSocket_WaitConnection(m_socket); |
384b4373 | 994 | |
d80d1aba GRG |
995 | if (!wait) |
996 | GSocket_SetNonBlocking(m_socket, FALSE); | |
997 | ||
998 | // GRG: this was not being handled! | |
999 | if (child_socket == NULL) | |
1000 | return FALSE; | |
1001 | ||
f4ada568 | 1002 | sock.m_type = SOCK_INTERNAL; |
a324a7bc | 1003 | sock.m_socket = child_socket; |
f4ada568 GL |
1004 | sock.m_connected = TRUE; |
1005 | ||
e8f4c584 | 1006 | GSocket_SetTimeout(sock.m_socket, sock.m_timeout * 1000); |
56d8adc0 GRG |
1007 | GSocket_SetCallback(sock.m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | |
1008 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1009 | wx_socket_callback, (char *)&sock); | |
1010 | ||
f4ada568 GL |
1011 | return TRUE; |
1012 | } | |
1013 | ||
d80d1aba | 1014 | wxSocketBase *wxSocketServer::Accept(bool wait) |
f4ada568 GL |
1015 | { |
1016 | wxSocketBase* sock = new wxSocketBase(); | |
1017 | ||
1018 | sock->SetFlags((wxSockFlags)m_flags); | |
1019 | ||
d80d1aba | 1020 | if (!AcceptWith(*sock, wait)) |
f4ada568 GL |
1021 | return NULL; |
1022 | ||
f4ada568 GL |
1023 | return sock; |
1024 | } | |
1025 | ||
17aa2bec | 1026 | bool wxSocketServer::WaitForAccept(long seconds, long milliseconds) |
d80d1aba | 1027 | { |
af2fd961 | 1028 | return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG); |
d80d1aba GRG |
1029 | } |
1030 | ||
f4ada568 | 1031 | // -------------------------------------------------------------- |
8c14576d | 1032 | // wxSocketClient |
f4ada568 GL |
1033 | // -------------------------------------------------------------- |
1034 | ||
f4ada568 | 1035 | // -------------------------------------------------------------- |
56d8adc0 GRG |
1036 | // wxSocketClient ctor and dtor |
1037 | // -------------------------------------------------------------- | |
1038 | ||
f4ada568 | 1039 | wxSocketClient::wxSocketClient(wxSockFlags _flags) : |
17dff81c | 1040 | wxSocketBase(_flags, SOCK_CLIENT) |
f4ada568 GL |
1041 | { |
1042 | } | |
1043 | ||
f4ada568 GL |
1044 | wxSocketClient::~wxSocketClient() |
1045 | { | |
1046 | } | |
1047 | ||
1048 | // -------------------------------------------------------------- | |
56d8adc0 | 1049 | // wxSocketClient Connect functions |
f4ada568 | 1050 | // -------------------------------------------------------------- |
791b24c4 | 1051 | bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait) |
f4ada568 | 1052 | { |
791b24c4 GRG |
1053 | GSocketError err; |
1054 | ||
f4ada568 GL |
1055 | if (IsConnected()) |
1056 | Close(); | |
1057 | ||
a324a7bc GL |
1058 | // This should never happen. |
1059 | if (m_socket) | |
1060 | GSocket_destroy(m_socket); | |
1061 | ||
56d8adc0 | 1062 | // Initialize all socket stuff ... |
a324a7bc | 1063 | m_socket = GSocket_new(); |
d80d1aba GRG |
1064 | m_connected = FALSE; |
1065 | m_establishing = FALSE; | |
384b4373 | 1066 | |
a324a7bc | 1067 | if (!m_socket) |
f4ada568 | 1068 | return FALSE; |
384b4373 | 1069 | |
e8f4c584 | 1070 | GSocket_SetTimeout(m_socket, m_timeout * 1000); |
96db102a GRG |
1071 | GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | |
1072 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1073 | wx_socket_callback, (char *)this); | |
56d8adc0 GRG |
1074 | |
1075 | // GRG: If wait == FALSE, then the call should be nonblocking. | |
1076 | // When we are finished, we put the socket to blocking mode | |
1077 | // again. | |
f4ada568 | 1078 | |
791b24c4 GRG |
1079 | if (!wait) |
1080 | GSocket_SetNonBlocking(m_socket, TRUE); | |
1081 | ||
a324a7bc | 1082 | GSocket_SetPeer(m_socket, addr_man.GetAddress()); |
791b24c4 GRG |
1083 | err = GSocket_Connect(m_socket, GSOCK_STREAMED); |
1084 | ||
1085 | if (!wait) | |
1086 | GSocket_SetNonBlocking(m_socket, FALSE); | |
1087 | ||
1088 | if (err != GSOCK_NOERROR) | |
56d8adc0 GRG |
1089 | { |
1090 | if (err == GSOCK_WOULDBLOCK) | |
1091 | m_establishing = TRUE; | |
1092 | ||
f4ada568 | 1093 | return FALSE; |
56d8adc0 | 1094 | } |
9111db68 | 1095 | |
f4ada568 GL |
1096 | m_connected = TRUE; |
1097 | return TRUE; | |
1098 | } | |
1099 | ||
d80d1aba | 1100 | bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds) |
f4ada568 | 1101 | { |
af2fd961 | 1102 | if (m_connected) // Already connected |
d80d1aba GRG |
1103 | return TRUE; |
1104 | ||
af2fd961 | 1105 | if (!m_establishing || !m_socket) // No connection in progress |
d80d1aba GRG |
1106 | return FALSE; |
1107 | ||
af2fd961 | 1108 | return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG); |
f4ada568 GL |
1109 | } |
1110 | ||
f4ada568 | 1111 | // -------------------------------------------------------------- |
a324a7bc | 1112 | // wxSocketEvent |
f4ada568 | 1113 | // -------------------------------------------------------------- |
f4ada568 | 1114 | |
a324a7bc GL |
1115 | wxSocketEvent::wxSocketEvent(int id) |
1116 | : wxEvent(id) | |
f4ada568 | 1117 | { |
a324a7bc | 1118 | wxEventType type = (wxEventType)wxEVT_SOCKET; |
f4ada568 | 1119 | |
a324a7bc | 1120 | SetEventType(type); |
f4ada568 GL |
1121 | } |
1122 | ||
a324a7bc | 1123 | void wxSocketEvent::CopyObject(wxObject& obj_d) const |
f4ada568 | 1124 | { |
a324a7bc | 1125 | wxSocketEvent *event = (wxSocketEvent *)&obj_d; |
f4ada568 | 1126 | |
a324a7bc | 1127 | wxEvent::CopyObject(obj_d); |
3b4183d8 | 1128 | |
a324a7bc GL |
1129 | event->m_skevt = m_skevt; |
1130 | event->m_socket = m_socket; | |
3b4183d8 GL |
1131 | } |
1132 | ||
a58d5df4 GL |
1133 | // -------------------------------------------------------------------------- |
1134 | // wxSocketModule | |
1135 | // -------------------------------------------------------------------------- | |
1136 | class WXDLLEXPORT wxSocketModule: public wxModule { | |
1137 | DECLARE_DYNAMIC_CLASS(wxSocketModule) | |
1138 | public: | |
1139 | bool OnInit() { | |
31989b0b | 1140 | return GSocket_Init(); |
a58d5df4 GL |
1141 | } |
1142 | void OnExit() { | |
ca17eff3 | 1143 | GSocket_Cleanup(); |
a58d5df4 GL |
1144 | } |
1145 | }; | |
1146 | ||
1147 | IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule) | |
1148 | ||
35a4dab7 GL |
1149 | #endif |
1150 | // wxUSE_SOCKETS |