]>
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 |
d3ea6527 GRG |
6 | // Copyright: (C) 1999-1997, Guilhem Lavaux |
7 | // (C) 2000-1999, Guillermo Rodriguez Garcia | |
f4ada568 GL |
8 | // RCS_ID: $Id$ |
9 | // License: see wxWindows license | |
56d8adc0 GRG |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
bffc1eaa GRG |
12 | // ========================================================================== |
13 | // Declarations | |
14 | // ========================================================================== | |
15 | ||
384b4373 | 16 | #ifdef __GNUG__ |
f4ada568 | 17 | #pragma implementation "socket.h" |
f4ada568 GL |
18 | #endif |
19 | ||
fcc6dddd JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
35a4dab7 GL |
27 | #if wxUSE_SOCKETS |
28 | ||
dc5c1114 | 29 | #include "wx/app.h" |
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" |
f187448d | 38 | #include "wx/event.h" |
52a07708 VZ |
39 | |
40 | #if wxUSE_GUI | |
f187448d | 41 | #include "wx/gdicmn.h" // for wxPendingDelete |
52a07708 | 42 | #endif // wxUSE_GUI |
81b92e17 | 43 | |
56d8adc0 GRG |
44 | #include "wx/sckaddr.h" |
45 | #include "wx/socket.h" | |
3b4183d8 | 46 | |
bffc1eaa GRG |
47 | // -------------------------------------------------------------------------- |
48 | // macros and constants | |
49 | // -------------------------------------------------------------------------- | |
ef57d866 | 50 | |
dc5c1114 GRG |
51 | // discard buffer |
52 | #define MAX_DISCARD_SIZE (10 * 1024) | |
96db102a | 53 | |
2f10f423 VZ |
54 | // what to do within waits: in wxBase we don't do anything as we don't have |
55 | // the event loop anyhow (for now). In GUI apps we have 2 cases: from the main | |
56 | // thread itself we have to call wxYield() to let the events (including the | |
57 | // GUI events and the low-level (not wxWindows) events from GSocket) be | |
58 | // processed. From another thread it is enough to just call wxThread::Yield() | |
59 | // which will give away the rest of our time slice: the explanation is that | |
60 | // the events will be processed by the main thread anyhow, without calling | |
61 | // wxYield(), but we don't want to eat the CPU time uselessly while sitting | |
62 | // in the loop waiting for the data | |
f187448d | 63 | #if wxUSE_GUI |
2f10f423 VZ |
64 | #if wxUSE_THREADS |
65 | #define PROCESS_EVENTS() \ | |
66 | { \ | |
67 | if ( wxThread::IsMain() ) \ | |
68 | wxYield(); \ | |
69 | else \ | |
70 | wxThread::Yield(); \ | |
71 | } | |
72 | #else // !wxUSE_THREADS | |
73 | #define PROCESS_EVENTS() wxYield() | |
74 | #endif // wxUSE_THREADS/!wxUSE_THREADS | |
75 | #else // !wxUSE_GUI | |
76 | #define PROCESS_EVENTS() | |
77 | #endif // wxUSE_GUI/!wxUSE_GUI | |
96db102a | 78 | |
ef57d866 | 79 | // -------------------------------------------------------------------------- |
bffc1eaa | 80 | // wxWin macros |
ef57d866 | 81 | // -------------------------------------------------------------------------- |
81b92e17 | 82 | |
a737331d GL |
83 | IMPLEMENT_CLASS(wxSocketBase, wxObject) |
84 | IMPLEMENT_CLASS(wxSocketServer, wxSocketBase) | |
85 | IMPLEMENT_CLASS(wxSocketClient, wxSocketBase) | |
dc5c1114 | 86 | IMPLEMENT_CLASS(wxDatagramSocket, wxSocketBase) |
a737331d | 87 | IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent) |
f4ada568 | 88 | |
bffc1eaa GRG |
89 | // -------------------------------------------------------------------------- |
90 | // private classes | |
91 | // -------------------------------------------------------------------------- | |
92 | ||
56d8adc0 GRG |
93 | class wxSocketState : public wxObject |
94 | { | |
a324a7bc | 95 | public: |
71622a7a | 96 | wxSocketFlags m_flags; |
bffc1eaa GRG |
97 | wxSocketEventFlags m_eventmask; |
98 | bool m_notify; | |
99 | void *m_clientData; | |
100 | #if WXWIN_COMPATIBILITY | |
81b92e17 GRG |
101 | wxSocketBase::wxSockCbk m_cbk; |
102 | char *m_cdata; | |
bffc1eaa | 103 | #endif // WXWIN_COMPATIBILITY |
a324a7bc GL |
104 | |
105 | public: | |
106 | wxSocketState() : wxObject() {} | |
107 | }; | |
108 | ||
ef57d866 GRG |
109 | // ========================================================================== |
110 | // wxSocketBase | |
111 | // ========================================================================== | |
112 | ||
113 | // -------------------------------------------------------------------------- | |
114 | // Ctor and dtor | |
115 | // -------------------------------------------------------------------------- | |
56d8adc0 | 116 | |
71622a7a | 117 | void wxSocketBase::Init() |
f4ada568 | 118 | { |
71622a7a GRG |
119 | m_socket = NULL; |
120 | m_type = wxSOCKET_UNINIT; | |
121 | ||
122 | // state | |
123 | m_flags = 0; | |
124 | m_connected = | |
125 | m_establishing = | |
126 | m_reading = | |
127 | m_writing = | |
128 | m_error = FALSE; | |
129 | m_lcount = 0; | |
130 | m_timeout = 600; | |
131 | m_beingDeleted = FALSE; | |
132 | ||
133 | // pushback buffer | |
134 | m_unread = NULL; | |
135 | m_unrd_size = 0; | |
136 | m_unrd_cur = 0; | |
137 | ||
138 | // events | |
71622a7a | 139 | m_id = -1; |
f187448d GRG |
140 | m_handler = NULL; |
141 | m_clientData = NULL; | |
bffc1eaa GRG |
142 | m_notify = FALSE; |
143 | m_eventmask = 0; | |
144 | #if WXWIN_COMPATIBILITY | |
71622a7a GRG |
145 | m_cbk = NULL; |
146 | m_cdata = NULL; | |
bffc1eaa | 147 | #endif // WXWIN_COMPATIBILITY |
f4ada568 GL |
148 | } |
149 | ||
f187448d GRG |
150 | wxSocketBase::wxSocketBase() |
151 | { | |
152 | Init(); | |
153 | } | |
71622a7a GRG |
154 | |
155 | wxSocketBase::wxSocketBase(wxSocketFlags flags, wxSocketType type) | |
f4ada568 | 156 | { |
71622a7a GRG |
157 | Init(); |
158 | ||
159 | m_flags = flags; | |
160 | m_type = type; | |
f4ada568 GL |
161 | } |
162 | ||
f4ada568 GL |
163 | wxSocketBase::~wxSocketBase() |
164 | { | |
9181a383 GRG |
165 | // Just in case the app called Destroy() *and* then deleted |
166 | // the socket immediately: don't leave dangling pointers. | |
167 | #if wxUSE_GUI | |
168 | wxPendingDelete.DeleteObject(this); | |
169 | #endif | |
a737331d | 170 | |
56d8adc0 | 171 | // Shutdown and close the socket |
9181a383 GRG |
172 | if (!m_beingDeleted) |
173 | Close(); | |
f4ada568 | 174 | |
56d8adc0 | 175 | // Destroy the GSocket object |
a324a7bc GL |
176 | if (m_socket) |
177 | GSocket_destroy(m_socket); | |
9181a383 GRG |
178 | |
179 | // Free the pushback buffer | |
180 | if (m_unread) | |
181 | free(m_unread); | |
f4ada568 GL |
182 | } |
183 | ||
ef57d866 GRG |
184 | bool wxSocketBase::Destroy() |
185 | { | |
186 | // Delayed destruction: the socket will be deleted during the next | |
187 | // idle loop iteration. This ensures that all pending events have | |
188 | // been processed. | |
ef57d866 | 189 | m_beingDeleted = TRUE; |
9181a383 GRG |
190 | |
191 | // Shutdown and close the socket | |
ef57d866 GRG |
192 | Close(); |
193 | ||
bffc1eaa GRG |
194 | // Supress events from now on |
195 | Notify(FALSE); | |
196 | ||
9181a383 | 197 | #if wxUSE_GUI |
bffc1eaa GRG |
198 | if ( !wxPendingDelete.Member(this) ) |
199 | wxPendingDelete.Append(this); | |
9181a383 GRG |
200 | #else |
201 | delete this; | |
202 | #endif | |
ef57d866 GRG |
203 | |
204 | return TRUE; | |
205 | } | |
9181a383 | 206 | |
ef57d866 | 207 | // -------------------------------------------------------------------------- |
bffc1eaa | 208 | // Basic IO calls |
ef57d866 GRG |
209 | // -------------------------------------------------------------------------- |
210 | ||
211 | // The following IO operations update m_error and m_lcount: | |
212 | // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard} | |
213 | // | |
214 | // TODO: Should Connect, Accept and AcceptWith update m_error? | |
215 | ||
f4ada568 GL |
216 | bool wxSocketBase::Close() |
217 | { | |
81b92e17 | 218 | // Interrupt pending waits |
02d15cad | 219 | InterruptWait(); |
81b92e17 | 220 | |
31528cd3 | 221 | if (m_socket) |
8c14576d | 222 | { |
56d8adc0 GRG |
223 | // Disable callbacks |
224 | GSocket_UnsetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
225 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG); | |
f4ada568 | 226 | |
56d8adc0 | 227 | // Shutdown the connection |
a324a7bc | 228 | GSocket_Shutdown(m_socket); |
f4ada568 GL |
229 | } |
230 | ||
ef57d866 | 231 | m_connected = FALSE; |
04e1eb03 | 232 | m_establishing = FALSE; |
f4ada568 GL |
233 | return TRUE; |
234 | } | |
235 | ||
f187448d | 236 | wxSocketBase& wxSocketBase::Read(void* buffer, wxUint32 nbytes) |
f4ada568 | 237 | { |
96db102a GRG |
238 | // Mask read events |
239 | m_reading = TRUE; | |
a324a7bc | 240 | |
96db102a | 241 | m_lcount = _Read(buffer, nbytes); |
17aa2bec | 242 | |
d3ea6527 GRG |
243 | // If in wxSOCKET_WAITALL mode, all bytes should have been read. |
244 | if (m_flags & wxSOCKET_WAITALL) | |
96db102a GRG |
245 | m_error = (m_lcount != nbytes); |
246 | else | |
247 | m_error = (m_lcount == 0); | |
f4ada568 | 248 | |
dc5c1114 | 249 | // Allow read events from now on |
96db102a | 250 | m_reading = FALSE; |
17aa2bec | 251 | |
96db102a GRG |
252 | return *this; |
253 | } | |
254 | ||
f187448d | 255 | wxUint32 wxSocketBase::_Read(void* buffer, wxUint32 nbytes) |
96db102a GRG |
256 | { |
257 | int total; | |
258 | int ret = 1; | |
a324a7bc | 259 | |
04e1eb03 | 260 | // Try the pushback buffer first |
96db102a GRG |
261 | total = GetPushback(buffer, nbytes, FALSE); |
262 | nbytes -= total; | |
f187448d | 263 | buffer = (char *)buffer + total; |
96db102a | 264 | |
5f3629ba GRG |
265 | // Return now in one of the following cases: |
266 | // - the socket is invalid, | |
267 | // - we got all the data, | |
268 | // - we got *some* data and we are not using wxSOCKET_WAITALL. | |
269 | if ( !m_socket || | |
270 | !nbytes || | |
271 | ((total != 0) && !(m_flags & wxSOCKET_WAITALL)) ) | |
96db102a | 272 | return total; |
f4ada568 | 273 | |
af2fd961 | 274 | // Possible combinations (they are checked in this order) |
81b92e17 | 275 | // wxSOCKET_NOWAIT |
5c9eff30 | 276 | // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK) |
81b92e17 GRG |
277 | // wxSOCKET_BLOCK |
278 | // wxSOCKET_NONE | |
af2fd961 | 279 | // |
81b92e17 | 280 | if (m_flags & wxSOCKET_NOWAIT) |
af2fd961 | 281 | { |
5c9eff30 | 282 | GSocket_SetNonBlocking(m_socket, 1); |
f187448d | 283 | ret = GSocket_Read(m_socket, (char *)buffer, nbytes); |
5c9eff30 | 284 | GSocket_SetNonBlocking(m_socket, 0); |
af2fd961 GRG |
285 | |
286 | if (ret > 0) | |
96db102a | 287 | total += ret; |
af2fd961 | 288 | } |
5c9eff30 | 289 | else |
56d8adc0 | 290 | { |
5c9eff30 GRG |
291 | bool more = TRUE; |
292 | ||
293 | while (more) | |
56d8adc0 | 294 | { |
5c9eff30 GRG |
295 | if ( !(m_flags & wxSOCKET_BLOCK) && !WaitForRead() ) |
296 | break; | |
81b92e17 | 297 | |
f187448d | 298 | ret = GSocket_Read(m_socket, (char *)buffer, nbytes); |
a324a7bc | 299 | |
81b92e17 GRG |
300 | if (ret > 0) |
301 | { | |
302 | total += ret; | |
81b92e17 | 303 | nbytes -= ret; |
f187448d | 304 | buffer = (char *)buffer + ret; |
81b92e17 | 305 | } |
a324a7bc | 306 | |
5c9eff30 GRG |
307 | // If we got here and wxSOCKET_WAITALL is not set, we can leave |
308 | // now. Otherwise, wait until we recv all the data or until there | |
309 | // is an error. | |
310 | // | |
311 | more = (ret > 0 && nbytes > 0 && (m_flags & wxSOCKET_WAITALL)); | |
81b92e17 | 312 | } |
a324a7bc | 313 | } |
f4ada568 | 314 | |
96db102a | 315 | return total; |
f4ada568 GL |
316 | } |
317 | ||
f187448d | 318 | wxSocketBase& wxSocketBase::ReadMsg(void* buffer, wxUint32 nbytes) |
062c4861 | 319 | { |
96db102a GRG |
320 | wxUint32 len, len2, sig, total; |
321 | bool error; | |
17aa2bec | 322 | int old_flags; |
17aa2bec GRG |
323 | struct |
324 | { | |
96db102a GRG |
325 | unsigned char sig[4]; |
326 | unsigned char len[4]; | |
062c4861 GL |
327 | } msg; |
328 | ||
96db102a GRG |
329 | // Mask read events |
330 | m_reading = TRUE; | |
062c4861 | 331 | |
96db102a GRG |
332 | total = 0; |
333 | error = TRUE; | |
17aa2bec | 334 | old_flags = m_flags; |
81b92e17 | 335 | SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL); |
17aa2bec | 336 | |
f187448d | 337 | if (_Read(&msg, sizeof(msg)) != sizeof(msg)) |
96db102a | 338 | goto exit; |
062c4861 | 339 | |
f187448d | 340 | sig = (wxUint32)msg.sig[0]; |
96db102a GRG |
341 | sig |= (wxUint32)(msg.sig[1] << 8); |
342 | sig |= (wxUint32)(msg.sig[2] << 16); | |
343 | sig |= (wxUint32)(msg.sig[3] << 24); | |
062c4861 GL |
344 | |
345 | if (sig != 0xfeeddead) | |
17aa2bec | 346 | { |
5c9eff30 | 347 | wxLogWarning(_("wxSocket: invalid signature in ReadMsg.")); |
96db102a | 348 | goto exit; |
17aa2bec GRG |
349 | } |
350 | ||
96db102a GRG |
351 | len = (wxUint32)msg.len[0]; |
352 | len |= (wxUint32)(msg.len[1] << 8); | |
353 | len |= (wxUint32)(msg.len[2] << 16); | |
354 | len |= (wxUint32)(msg.len[3] << 24); | |
355 | ||
17aa2bec GRG |
356 | if (len > nbytes) |
357 | { | |
062c4861 GL |
358 | len2 = len - nbytes; |
359 | len = nbytes; | |
360 | } | |
361 | else | |
362 | len2 = 0; | |
363 | ||
04e1eb03 | 364 | // Don't attemp to read if the msg was zero bytes long. |
96db102a | 365 | if (len) |
17aa2bec | 366 | { |
96db102a GRG |
367 | total = _Read(buffer, len); |
368 | ||
369 | if (total != len) | |
370 | goto exit; | |
17aa2bec GRG |
371 | } |
372 | if (len2) | |
373 | { | |
96db102a | 374 | char *discard_buffer = new char[MAX_DISCARD_SIZE]; |
17aa2bec GRG |
375 | long discard_len; |
376 | ||
96db102a | 377 | // NOTE: discarded bytes don't add to m_lcount. |
17aa2bec GRG |
378 | do |
379 | { | |
96db102a | 380 | discard_len = ((len2 > MAX_DISCARD_SIZE)? MAX_DISCARD_SIZE : len2); |
479cd5de VZ |
381 | discard_len = _Read(discard_buffer, (wxUint32)discard_len); |
382 | len2 -= (wxUint32)discard_len; | |
17aa2bec GRG |
383 | } |
384 | while ((discard_len > 0) && len2); | |
385 | ||
386 | delete [] discard_buffer; | |
387 | ||
388 | if (len2 != 0) | |
96db102a | 389 | goto exit; |
17aa2bec | 390 | } |
f187448d | 391 | if (_Read(&msg, sizeof(msg)) != sizeof(msg)) |
96db102a | 392 | goto exit; |
062c4861 | 393 | |
96db102a GRG |
394 | sig = (wxUint32)msg.sig[0]; |
395 | sig |= (wxUint32)(msg.sig[1] << 8); | |
396 | sig |= (wxUint32)(msg.sig[2] << 16); | |
397 | sig |= (wxUint32)(msg.sig[3] << 24); | |
a324a7bc | 398 | |
062c4861 | 399 | if (sig != 0xdeadfeed) |
17aa2bec | 400 | { |
5c9eff30 | 401 | wxLogWarning(_("wxSocket: invalid signature in ReadMsg.")); |
96db102a | 402 | goto exit; |
17aa2bec | 403 | } |
062c4861 | 404 | |
96db102a GRG |
405 | // everything was OK |
406 | error = FALSE; | |
407 | ||
408 | exit: | |
409 | m_error = error; | |
410 | m_lcount = total; | |
411 | m_reading = FALSE; | |
17aa2bec | 412 | SetFlags(old_flags); |
96db102a | 413 | |
062c4861 GL |
414 | return *this; |
415 | } | |
416 | ||
f187448d | 417 | wxSocketBase& wxSocketBase::Peek(void* buffer, wxUint32 nbytes) |
f4ada568 | 418 | { |
96db102a GRG |
419 | // Mask read events |
420 | m_reading = TRUE; | |
421 | ||
422 | m_lcount = _Read(buffer, nbytes); | |
dc5c1114 | 423 | Pushback(buffer, m_lcount); |
96db102a | 424 | |
81b92e17 GRG |
425 | // If in wxSOCKET_WAITALL mode, all bytes should have been read. |
426 | if (m_flags & wxSOCKET_WAITALL) | |
96db102a GRG |
427 | m_error = (m_lcount != nbytes); |
428 | else | |
429 | m_error = (m_lcount == 0); | |
430 | ||
dc5c1114 | 431 | // Allow read events again |
96db102a | 432 | m_reading = FALSE; |
f4ada568 GL |
433 | |
434 | return *this; | |
435 | } | |
436 | ||
f187448d | 437 | wxSocketBase& wxSocketBase::Write(const void *buffer, wxUint32 nbytes) |
f4ada568 | 438 | { |
96db102a GRG |
439 | // Mask write events |
440 | m_writing = TRUE; | |
56d8adc0 | 441 | |
96db102a GRG |
442 | m_lcount = _Write(buffer, nbytes); |
443 | ||
81b92e17 GRG |
444 | // If in wxSOCKET_WAITALL mode, all bytes should have been written. |
445 | if (m_flags & wxSOCKET_WAITALL) | |
96db102a GRG |
446 | m_error = (m_lcount != nbytes); |
447 | else | |
448 | m_error = (m_lcount == 0); | |
449 | ||
dc5c1114 | 450 | // Allow write events again |
96db102a GRG |
451 | m_writing = FALSE; |
452 | ||
96db102a GRG |
453 | return *this; |
454 | } | |
455 | ||
f187448d | 456 | wxUint32 wxSocketBase::_Write(const void *buffer, wxUint32 nbytes) |
96db102a GRG |
457 | { |
458 | wxUint32 total = 0; | |
459 | int ret = 1; | |
56d8adc0 | 460 | |
5c9eff30 GRG |
461 | // If the socket is invalid or parameters are ill, return immediately |
462 | if (!m_socket || !buffer || !nbytes) | |
96db102a | 463 | return 0; |
a324a7bc | 464 | |
af2fd961 | 465 | // Possible combinations (they are checked in this order) |
81b92e17 | 466 | // wxSOCKET_NOWAIT |
5c9eff30 | 467 | // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK) |
81b92e17 GRG |
468 | // wxSOCKET_BLOCK |
469 | // wxSOCKET_NONE | |
af2fd961 | 470 | // |
81b92e17 | 471 | if (m_flags & wxSOCKET_NOWAIT) |
af2fd961 | 472 | { |
5c9eff30 | 473 | GSocket_SetNonBlocking(m_socket, 1); |
f187448d | 474 | ret = GSocket_Write(m_socket, (const char *)buffer, nbytes); |
5c9eff30 | 475 | GSocket_SetNonBlocking(m_socket, 0); |
af2fd961 GRG |
476 | |
477 | if (ret > 0) | |
96db102a | 478 | total = ret; |
af2fd961 | 479 | } |
5c9eff30 | 480 | else |
56d8adc0 | 481 | { |
5c9eff30 GRG |
482 | bool more = TRUE; |
483 | ||
c3e646b4 | 484 | while (more) |
56d8adc0 | 485 | { |
5c9eff30 GRG |
486 | if ( !(m_flags & wxSOCKET_BLOCK) && !WaitForWrite() ) |
487 | break; | |
81b92e17 | 488 | |
f187448d | 489 | ret = GSocket_Write(m_socket, (const char *)buffer, nbytes); |
5c9eff30 | 490 | |
81b92e17 GRG |
491 | if (ret > 0) |
492 | { | |
493 | total += ret; | |
81b92e17 | 494 | nbytes -= ret; |
f187448d | 495 | buffer = (const char *)buffer + ret; |
81b92e17 | 496 | } |
a324a7bc | 497 | |
5c9eff30 GRG |
498 | // If we got here and wxSOCKET_WAITALL is not set, we can leave |
499 | // now. Otherwise, wait until we send all the data or until there | |
500 | // is an error. | |
501 | // | |
502 | more = (ret > 0 && nbytes > 0 && (m_flags & wxSOCKET_WAITALL)); | |
81b92e17 | 503 | } |
56d8adc0 | 504 | } |
a324a7bc | 505 | |
96db102a | 506 | return total; |
f4ada568 GL |
507 | } |
508 | ||
f187448d | 509 | wxSocketBase& wxSocketBase::WriteMsg(const void *buffer, wxUint32 nbytes) |
062c4861 | 510 | { |
96db102a GRG |
511 | wxUint32 total; |
512 | bool error; | |
17aa2bec | 513 | int old_flags; |
f187448d GRG |
514 | struct |
515 | { | |
96db102a GRG |
516 | unsigned char sig[4]; |
517 | unsigned char len[4]; | |
062c4861 GL |
518 | } msg; |
519 | ||
96db102a GRG |
520 | // Mask write events |
521 | m_writing = TRUE; | |
522 | ||
523 | error = TRUE; | |
524 | total = 0; | |
525 | old_flags = m_flags; | |
81b92e17 | 526 | SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL); |
96db102a | 527 | |
96db102a GRG |
528 | msg.sig[0] = (unsigned char) 0xad; |
529 | msg.sig[1] = (unsigned char) 0xde; | |
530 | msg.sig[2] = (unsigned char) 0xed; | |
531 | msg.sig[3] = (unsigned char) 0xfe; | |
062c4861 | 532 | |
04e1eb03 GRG |
533 | msg.len[0] = (unsigned char) (nbytes & 0xff); |
534 | msg.len[1] = (unsigned char) ((nbytes >> 8) & 0xff); | |
535 | msg.len[2] = (unsigned char) ((nbytes >> 16) & 0xff); | |
536 | msg.len[3] = (unsigned char) ((nbytes >> 24) & 0xff); | |
17aa2bec | 537 | |
f187448d | 538 | if (_Write(&msg, sizeof(msg)) < sizeof(msg)) |
96db102a GRG |
539 | goto exit; |
540 | ||
541 | total = _Write(buffer, nbytes); | |
542 | ||
543 | if (total < nbytes) | |
544 | goto exit; | |
062c4861 | 545 | |
96db102a GRG |
546 | msg.sig[0] = (unsigned char) 0xed; |
547 | msg.sig[1] = (unsigned char) 0xfe; | |
548 | msg.sig[2] = (unsigned char) 0xad; | |
549 | msg.sig[3] = (unsigned char) 0xde; | |
062c4861 | 550 | msg.len[0] = msg.len[1] = msg.len[2] = msg.len[3] = (char) 0; |
062c4861 | 551 | |
f187448d | 552 | if ((_Write(&msg, sizeof(msg))) < sizeof(msg)) |
96db102a | 553 | goto exit; |
17aa2bec | 554 | |
96db102a GRG |
555 | // everything was OK |
556 | error = FALSE; | |
557 | ||
558 | exit: | |
559 | m_error = error; | |
560 | m_lcount = total; | |
561 | m_writing = FALSE; | |
562 | ||
062c4861 | 563 | return *this; |
062c4861 GL |
564 | } |
565 | ||
f187448d | 566 | wxSocketBase& wxSocketBase::Unread(const void *buffer, wxUint32 nbytes) |
f4ada568 | 567 | { |
56d8adc0 | 568 | if (nbytes != 0) |
96db102a | 569 | Pushback(buffer, nbytes); |
f4ada568 | 570 | |
96db102a GRG |
571 | m_error = FALSE; |
572 | m_lcount = nbytes; | |
f4ada568 | 573 | |
96db102a | 574 | return *this; |
a324a7bc GL |
575 | } |
576 | ||
96db102a | 577 | wxSocketBase& wxSocketBase::Discard() |
f4ada568 | 578 | { |
96db102a | 579 | int old_flags; |
04e1eb03 GRG |
580 | char *buffer = new char[MAX_DISCARD_SIZE]; |
581 | wxUint32 ret; | |
17aa2bec | 582 | wxUint32 total = 0; |
f4ada568 | 583 | |
96db102a GRG |
584 | // Mask read events |
585 | m_reading = TRUE; | |
586 | ||
587 | old_flags = m_flags; | |
81b92e17 | 588 | SetFlags(wxSOCKET_NOWAIT); |
384b4373 | 589 | |
04e1eb03 | 590 | do |
8c14576d | 591 | { |
04e1eb03 GRG |
592 | ret = _Read(buffer, MAX_DISCARD_SIZE); |
593 | total += ret; | |
f4ada568 | 594 | } |
04e1eb03 | 595 | while (ret == MAX_DISCARD_SIZE); |
f4ada568 | 596 | |
04e1eb03 | 597 | delete[] buffer; |
17aa2bec | 598 | m_lcount = total; |
04e1eb03 | 599 | m_error = FALSE; |
17aa2bec | 600 | |
dc5c1114 | 601 | // Allow read events again |
96db102a GRG |
602 | m_reading = FALSE; |
603 | ||
96db102a | 604 | return *this; |
f4ada568 GL |
605 | } |
606 | ||
ef57d866 GRG |
607 | // -------------------------------------------------------------------------- |
608 | // Wait functions | |
609 | // -------------------------------------------------------------------------- | |
f4ada568 | 610 | |
ef57d866 GRG |
611 | // All Wait functions poll the socket using GSocket_Select() to |
612 | // check for the specified combination of conditions, until one | |
613 | // of these conditions become true, an error occurs, or the | |
614 | // timeout elapses. The polling loop calls PROCESS_EVENTS(), so | |
615 | // this won't block the GUI. | |
f4ada568 | 616 | |
bffc1eaa GRG |
617 | bool wxSocketBase::_Wait(long seconds, |
618 | long milliseconds, | |
d3ea6527 | 619 | wxSocketEventFlags flags) |
375abe3d | 620 | { |
56d8adc0 | 621 | GSocketEventFlags result; |
56d8adc0 | 622 | long timeout; |
375abe3d | 623 | |
81b92e17 GRG |
624 | // Set this to TRUE to interrupt ongoing waits |
625 | m_interrupt = FALSE; | |
626 | ||
ef57d866 | 627 | // Check for valid socket |
af2fd961 GRG |
628 | if (!m_socket) |
629 | return FALSE; | |
630 | ||
71622a7a | 631 | // Check for valid timeout value. |
56d8adc0 GRG |
632 | if (seconds != -1) |
633 | timeout = seconds * 1000 + milliseconds; | |
634 | else | |
635 | timeout = m_timeout * 1000; | |
a324a7bc | 636 | |
bffc1eaa | 637 | // Wait in an active polling loop. |
af2fd961 | 638 | // |
bffc1eaa GRG |
639 | // NOTE: We duplicate some of the code in OnRequest, but this doesn't |
640 | // hurt. It has to be here because the (GSocket) event might arrive | |
641 | // a bit delayed, and it has to be in OnRequest as well because we | |
642 | // don't know whether the Wait functions are being used. | |
af2fd961 | 643 | // |
96db102a | 644 | // Do this at least once (important if timeout == 0, when |
d3ea6527 | 645 | // we are just polling). Also, if just polling, do not yield. |
ef57d866 | 646 | |
71622a7a GRG |
647 | wxStopWatch chrono; |
648 | bool done = FALSE; | |
649 | ||
650 | while (!done) | |
56d8adc0 | 651 | { |
af2fd961 GRG |
652 | result = GSocket_Select(m_socket, flags | GSOCK_LOST_FLAG); |
653 | ||
af2fd961 GRG |
654 | // Incoming connection (server) or connection established (client) |
655 | if (result & GSOCK_CONNECTION_FLAG) | |
656 | { | |
af2fd961 GRG |
657 | m_connected = TRUE; |
658 | m_establishing = FALSE; | |
659 | return TRUE; | |
660 | } | |
661 | ||
04e1eb03 | 662 | // Data available or output buffer ready |
81b92e17 | 663 | if ((result & GSOCK_INPUT_FLAG) || (result & GSOCK_OUTPUT_FLAG)) |
af2fd961 | 664 | { |
81b92e17 | 665 | return TRUE; |
af2fd961 GRG |
666 | } |
667 | ||
c0043a50 GRG |
668 | // Connection lost |
669 | if (result & GSOCK_LOST_FLAG) | |
670 | { | |
ef57d866 | 671 | m_connected = FALSE; |
04e1eb03 | 672 | m_establishing = FALSE; |
378d2bd3 | 673 | return (flags & GSOCK_LOST_FLAG) != 0; |
c0043a50 GRG |
674 | } |
675 | ||
81b92e17 | 676 | // Wait more? |
71622a7a GRG |
677 | if ((!timeout) || (chrono.Time() > timeout) || (m_interrupt)) |
678 | done = TRUE; | |
d3ea6527 GRG |
679 | else |
680 | PROCESS_EVENTS(); | |
56d8adc0 | 681 | } |
f4ada568 | 682 | |
af2fd961 | 683 | return FALSE; |
f4ada568 | 684 | } |
f4ada568 | 685 | |
a737331d | 686 | bool wxSocketBase::Wait(long seconds, long milliseconds) |
f4ada568 | 687 | { |
d80d1aba GRG |
688 | return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG | |
689 | GSOCK_OUTPUT_FLAG | | |
690 | GSOCK_CONNECTION_FLAG | | |
691 | GSOCK_LOST_FLAG); | |
f4ada568 | 692 | } |
f4ada568 | 693 | |
a737331d | 694 | bool wxSocketBase::WaitForRead(long seconds, long milliseconds) |
f4ada568 | 695 | { |
ef57d866 | 696 | // Check pushback buffer before entering _Wait |
96db102a GRG |
697 | if (m_unread) |
698 | return TRUE; | |
699 | ||
ef57d866 GRG |
700 | // Note that GSOCK_INPUT_LOST has to be explicitly passed to |
701 | // _Wait becuase of the semantics of WaitForRead: a return | |
702 | // value of TRUE means that a GSocket_Read call will return | |
703 | // immediately, not that there is actually data to read. | |
704 | ||
705 | return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG | | |
706 | GSOCK_LOST_FLAG); | |
f4ada568 GL |
707 | } |
708 | ||
a737331d | 709 | bool wxSocketBase::WaitForWrite(long seconds, long milliseconds) |
f4ada568 | 710 | { |
af2fd961 | 711 | return _Wait(seconds, milliseconds, GSOCK_OUTPUT_FLAG); |
a737331d | 712 | } |
f4ada568 | 713 | |
a737331d GL |
714 | bool wxSocketBase::WaitForLost(long seconds, long milliseconds) |
715 | { | |
a324a7bc | 716 | return _Wait(seconds, milliseconds, GSOCK_LOST_FLAG); |
f4ada568 | 717 | } |
a737331d | 718 | |
ef57d866 GRG |
719 | // -------------------------------------------------------------------------- |
720 | // Miscellaneous | |
721 | // -------------------------------------------------------------------------- | |
722 | ||
723 | // | |
724 | // Get local or peer address | |
725 | // | |
726 | ||
727 | bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const | |
728 | { | |
729 | GAddress *peer; | |
730 | ||
731 | if (!m_socket) | |
732 | return FALSE; | |
733 | ||
734 | peer = GSocket_GetPeer(m_socket); | |
735 | addr_man.SetAddress(peer); | |
736 | GAddress_destroy(peer); | |
737 | ||
738 | return TRUE; | |
739 | } | |
740 | ||
741 | bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const | |
742 | { | |
743 | GAddress *local; | |
744 | ||
745 | if (!m_socket) | |
746 | return FALSE; | |
747 | ||
748 | local = GSocket_GetLocal(m_socket); | |
749 | addr_man.SetAddress(local); | |
750 | GAddress_destroy(local); | |
751 | ||
752 | return TRUE; | |
753 | } | |
754 | ||
755 | // | |
756 | // Save and restore socket state | |
757 | // | |
758 | ||
759 | void wxSocketBase::SaveState() | |
760 | { | |
761 | wxSocketState *state; | |
762 | ||
763 | state = new wxSocketState(); | |
764 | ||
bffc1eaa GRG |
765 | state->m_flags = m_flags; |
766 | state->m_notify = m_notify; | |
767 | state->m_eventmask = m_eventmask; | |
768 | state->m_clientData = m_clientData; | |
769 | #if WXWIN_COMPATIBILITY | |
770 | state->m_cbk = m_cbk; | |
771 | state->m_cdata = m_cdata; | |
772 | #endif // WXWIN_COMPATIBILITY | |
ef57d866 GRG |
773 | |
774 | m_states.Append(state); | |
775 | } | |
776 | ||
777 | void wxSocketBase::RestoreState() | |
778 | { | |
779 | wxNode *node; | |
780 | wxSocketState *state; | |
781 | ||
782 | node = m_states.Last(); | |
783 | if (!node) | |
784 | return; | |
785 | ||
786 | state = (wxSocketState *)node->Data(); | |
787 | ||
bffc1eaa GRG |
788 | m_flags = state->m_flags; |
789 | m_notify = state->m_notify; | |
790 | m_eventmask = state->m_eventmask; | |
791 | m_clientData = state->m_clientData; | |
792 | #if WXWIN_COMPATIBILITY | |
793 | m_cbk = state->m_cbk; | |
794 | m_cdata = state->m_cdata; | |
795 | #endif // WXWIN_COMPATIBILITY | |
ef57d866 GRG |
796 | |
797 | delete node; | |
798 | delete state; | |
799 | } | |
800 | ||
801 | // | |
802 | // Timeout and flags | |
803 | // | |
804 | ||
17aa2bec GRG |
805 | void wxSocketBase::SetTimeout(long seconds) |
806 | { | |
807 | m_timeout = seconds; | |
808 | ||
809 | if (m_socket) | |
e8f4c584 | 810 | GSocket_SetTimeout(m_socket, m_timeout * 1000); |
17aa2bec GRG |
811 | } |
812 | ||
71622a7a | 813 | void wxSocketBase::SetFlags(wxSocketFlags flags) |
f4ada568 | 814 | { |
71622a7a | 815 | m_flags = flags; |
f4ada568 | 816 | } |
f0a56ab0 | 817 | |
f187448d | 818 | |
ef57d866 GRG |
819 | // -------------------------------------------------------------------------- |
820 | // Callbacks (now obsolete - use events instead) | |
821 | // -------------------------------------------------------------------------- | |
56d8adc0 | 822 | |
bffc1eaa GRG |
823 | #if WXWIN_COMPATIBILITY |
824 | ||
56d8adc0 | 825 | wxSocketBase::wxSockCbk wxSocketBase::Callback(wxSockCbk cbk_) |
f4ada568 | 826 | { |
56d8adc0 | 827 | wxSockCbk old_cbk = cbk_; |
f4ada568 | 828 | |
56d8adc0 GRG |
829 | m_cbk = cbk_; |
830 | return old_cbk; | |
831 | } | |
832 | ||
833 | char *wxSocketBase::CallbackData(char *data) | |
834 | { | |
835 | char *old_data = m_cdata; | |
836 | ||
837 | m_cdata = data; | |
838 | return old_data; | |
f4ada568 GL |
839 | } |
840 | ||
bffc1eaa GRG |
841 | #endif // WXWIN_COMPATIBILITY |
842 | ||
ef57d866 | 843 | // -------------------------------------------------------------------------- |
f187448d | 844 | // Event handling |
ef57d866 | 845 | // -------------------------------------------------------------------------- |
a324a7bc | 846 | |
bffc1eaa GRG |
847 | // A note on how events are processed, which is probably the most |
848 | // difficult thing to get working right while keeping the same API | |
849 | // and functionality for all platforms. | |
850 | // | |
851 | // When GSocket detects an event, it calls wx_socket_callback, which in | |
852 | // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket | |
853 | // object. OnRequest does some housekeeping, and if the event is to be | |
854 | // propagated to the user, it creates a new wxSocketEvent object and | |
855 | // posts it. The event is not processed immediately, but delayed with | |
856 | // AddPendingEvent instead. This is necessary in order to decouple the | |
857 | // event processing from wx_socket_callback; otherwise, subsequent IO | |
858 | // calls made from the user event handler would fail, as gtk callbacks | |
859 | // are not reentrant. | |
860 | // | |
861 | // Note that, unlike events, user callbacks (now deprecated) are _not_ | |
862 | // decoupled from wx_socket_callback and thus they suffer from a variety | |
863 | // of problems. Avoid them where possible and use events instead. | |
96db102a | 864 | |
6e31e940 | 865 | static void LINKAGEMODE wx_socket_callback(GSocket * WXUNUSED(socket), |
bffc1eaa | 866 | GSocketEvent notification, |
ef57d866 | 867 | char *cdata) |
a324a7bc GL |
868 | { |
869 | wxSocketBase *sckobj = (wxSocketBase *)cdata; | |
870 | ||
bffc1eaa | 871 | sckobj->OnRequest((wxSocketNotify) notification); |
56d8adc0 | 872 | } |
a324a7bc | 873 | |
bffc1eaa | 874 | void wxSocketBase::OnRequest(wxSocketNotify notification) |
f4ada568 | 875 | { |
bffc1eaa GRG |
876 | // NOTE: We duplicate some of the code in _Wait, but this doesn't |
877 | // hurt. It has to be here because the (GSocket) event might arrive | |
878 | // a bit delayed, and it has to be in _Wait as well because we don't | |
879 | // know whether the Wait functions are being used. | |
ef57d866 | 880 | |
bffc1eaa | 881 | switch(notification) |
d80d1aba | 882 | { |
04e1eb03 GRG |
883 | case wxSOCKET_CONNECTION: |
884 | m_establishing = FALSE; | |
885 | m_connected = TRUE; | |
886 | break; | |
887 | ||
d3ea6527 GRG |
888 | // If we are in the middle of a R/W operation, do not |
889 | // propagate events to users. Also, filter 'late' events | |
890 | // which are no longer valid. | |
ef57d866 | 891 | |
04e1eb03 GRG |
892 | case wxSOCKET_INPUT: |
893 | if (m_reading || !GSocket_Select(m_socket, GSOCK_INPUT_FLAG)) | |
894 | return; | |
895 | break; | |
896 | ||
897 | case wxSOCKET_OUTPUT: | |
898 | if (m_writing || !GSocket_Select(m_socket, GSOCK_OUTPUT_FLAG)) | |
899 | return; | |
900 | break; | |
901 | ||
902 | case wxSOCKET_LOST: | |
903 | m_connected = FALSE; | |
904 | m_establishing = FALSE; | |
905 | break; | |
906 | ||
907 | default: | |
908 | break; | |
96db102a GRG |
909 | } |
910 | ||
f187448d GRG |
911 | // Schedule the event |
912 | ||
52fbdb58 | 913 | wxSocketEventFlags flag = 0; |
bffc1eaa | 914 | switch (notification) |
f187448d GRG |
915 | { |
916 | case GSOCK_INPUT: flag = GSOCK_INPUT_FLAG; break; | |
917 | case GSOCK_OUTPUT: flag = GSOCK_OUTPUT_FLAG; break; | |
918 | case GSOCK_CONNECTION: flag = GSOCK_CONNECTION_FLAG; break; | |
919 | case GSOCK_LOST: flag = GSOCK_LOST_FLAG; break; | |
52fbdb58 | 920 | default: |
5c9eff30 | 921 | wxLogWarning(_("wxSocket: unknown event!.")); |
52fbdb58 | 922 | return; |
f187448d GRG |
923 | } |
924 | ||
bffc1eaa | 925 | if (((m_eventmask & flag) == flag) && m_notify) |
d80d1aba | 926 | { |
f187448d | 927 | if (m_handler) |
bffc1eaa GRG |
928 | { |
929 | wxSocketEvent event(m_id); | |
930 | event.m_event = notification; | |
931 | event.m_clientData = m_clientData; | |
932 | event.SetEventObject(this); | |
933 | ||
f187448d | 934 | m_handler->AddPendingEvent(event); |
bffc1eaa | 935 | } |
56d8adc0 | 936 | |
bffc1eaa | 937 | #if WXWIN_COMPATIBILITY |
56d8adc0 | 938 | if (m_cbk) |
bffc1eaa GRG |
939 | m_cbk(*this, notification, m_cdata); |
940 | #endif // WXWIN_COMPATIBILITY | |
f4ada568 | 941 | } |
f4ada568 GL |
942 | } |
943 | ||
f187448d GRG |
944 | void wxSocketBase::Notify(bool notify) |
945 | { | |
bffc1eaa | 946 | m_notify = notify; |
f187448d GRG |
947 | } |
948 | ||
949 | void wxSocketBase::SetNotify(wxSocketEventFlags flags) | |
950 | { | |
bffc1eaa | 951 | m_eventmask = flags; |
f187448d GRG |
952 | } |
953 | ||
71622a7a | 954 | void wxSocketBase::SetEventHandler(wxEvtHandler& handler, int id) |
f4ada568 | 955 | { |
71622a7a GRG |
956 | m_handler = &handler; |
957 | m_id = id; | |
f4ada568 GL |
958 | } |
959 | ||
ef57d866 GRG |
960 | // -------------------------------------------------------------------------- |
961 | // Pushback buffer | |
962 | // -------------------------------------------------------------------------- | |
db131261 | 963 | |
f187448d | 964 | void wxSocketBase::Pushback(const void *buffer, wxUint32 size) |
f4ada568 | 965 | { |
dc5c1114 GRG |
966 | if (!size) return; |
967 | ||
a324a7bc | 968 | if (m_unread == NULL) |
f187448d | 969 | m_unread = malloc(size); |
dc5c1114 GRG |
970 | else |
971 | { | |
f187448d | 972 | void *tmp; |
f4ada568 | 973 | |
f187448d GRG |
974 | tmp = malloc(m_unrd_size + size); |
975 | memcpy((char *)tmp + size, m_unread, m_unrd_size); | |
41895a05 | 976 | free(m_unread); |
a324a7bc GL |
977 | |
978 | m_unread = tmp; | |
41895a05 | 979 | } |
31528cd3 | 980 | |
f4ada568 | 981 | m_unrd_size += size; |
a324a7bc GL |
982 | |
983 | memcpy(m_unread, buffer, size); | |
f4ada568 GL |
984 | } |
985 | ||
f187448d | 986 | wxUint32 wxSocketBase::GetPushback(void *buffer, wxUint32 size, bool peek) |
f4ada568 GL |
987 | { |
988 | if (!m_unrd_size) | |
989 | return 0; | |
990 | ||
a324a7bc GL |
991 | if (size > (m_unrd_size-m_unrd_cur)) |
992 | size = m_unrd_size-m_unrd_cur; | |
96db102a | 993 | |
f187448d | 994 | memcpy(buffer, (char *)m_unread + m_unrd_cur, size); |
f4ada568 | 995 | |
dc5c1114 GRG |
996 | if (!peek) |
997 | { | |
a324a7bc | 998 | m_unrd_cur += size; |
dc5c1114 GRG |
999 | if (m_unrd_size == m_unrd_cur) |
1000 | { | |
f4ada568 GL |
1001 | free(m_unread); |
1002 | m_unread = NULL; | |
a324a7bc GL |
1003 | m_unrd_size = 0; |
1004 | m_unrd_cur = 0; | |
f4ada568 GL |
1005 | } |
1006 | } | |
1007 | ||
1008 | return size; | |
1009 | } | |
1010 | ||
f187448d | 1011 | |
ef57d866 | 1012 | // ========================================================================== |
c3e646b4 | 1013 | // wxSocketServer |
ef57d866 | 1014 | // ========================================================================== |
f4ada568 | 1015 | |
ef57d866 GRG |
1016 | // -------------------------------------------------------------------------- |
1017 | // Ctor | |
1018 | // -------------------------------------------------------------------------- | |
56d8adc0 | 1019 | |
f4ada568 | 1020 | wxSocketServer::wxSocketServer(wxSockAddress& addr_man, |
71622a7a GRG |
1021 | wxSocketFlags flags) |
1022 | : wxSocketBase(flags, wxSOCKET_SERVER) | |
f4ada568 | 1023 | { |
56d8adc0 | 1024 | // Create the socket |
a324a7bc | 1025 | m_socket = GSocket_new(); |
f4ada568 | 1026 | |
a324a7bc | 1027 | if (!m_socket) |
f4ada568 | 1028 | return; |
384b4373 | 1029 | |
56d8adc0 | 1030 | // Setup the socket as server |
a324a7bc | 1031 | GSocket_SetLocal(m_socket, addr_man.GetAddress()); |
56d8adc0 GRG |
1032 | if (GSocket_SetServer(m_socket) != GSOCK_NOERROR) |
1033 | { | |
a324a7bc GL |
1034 | GSocket_destroy(m_socket); |
1035 | m_socket = NULL; | |
f4ada568 GL |
1036 | return; |
1037 | } | |
a737331d | 1038 | |
e8f4c584 | 1039 | GSocket_SetTimeout(m_socket, m_timeout * 1000); |
56d8adc0 GRG |
1040 | GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | |
1041 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1042 | wx_socket_callback, (char *)this); | |
1043 | ||
f4ada568 GL |
1044 | } |
1045 | ||
ef57d866 GRG |
1046 | // -------------------------------------------------------------------------- |
1047 | // Accept | |
1048 | // -------------------------------------------------------------------------- | |
8c14576d | 1049 | |
d80d1aba | 1050 | bool wxSocketServer::AcceptWith(wxSocketBase& sock, bool wait) |
f4ada568 | 1051 | { |
a324a7bc | 1052 | GSocket *child_socket; |
f4ada568 | 1053 | |
17aa2bec GRG |
1054 | if (!m_socket) |
1055 | return FALSE; | |
1056 | ||
04e1eb03 | 1057 | // If wait == FALSE, then the call should be nonblocking. |
56d8adc0 GRG |
1058 | // When we are finished, we put the socket to blocking mode |
1059 | // again. | |
d80d1aba GRG |
1060 | |
1061 | if (!wait) | |
5c9eff30 | 1062 | GSocket_SetNonBlocking(m_socket, 1); |
d80d1aba | 1063 | |
a324a7bc | 1064 | child_socket = GSocket_WaitConnection(m_socket); |
384b4373 | 1065 | |
d80d1aba | 1066 | if (!wait) |
5c9eff30 | 1067 | GSocket_SetNonBlocking(m_socket, 0); |
d80d1aba | 1068 | |
04e1eb03 | 1069 | if (!child_socket) |
d80d1aba GRG |
1070 | return FALSE; |
1071 | ||
71622a7a | 1072 | sock.m_type = wxSOCKET_BASE; |
a324a7bc | 1073 | sock.m_socket = child_socket; |
f4ada568 GL |
1074 | sock.m_connected = TRUE; |
1075 | ||
e8f4c584 | 1076 | GSocket_SetTimeout(sock.m_socket, sock.m_timeout * 1000); |
56d8adc0 GRG |
1077 | GSocket_SetCallback(sock.m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | |
1078 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1079 | wx_socket_callback, (char *)&sock); | |
1080 | ||
f4ada568 GL |
1081 | return TRUE; |
1082 | } | |
1083 | ||
d80d1aba | 1084 | wxSocketBase *wxSocketServer::Accept(bool wait) |
f4ada568 GL |
1085 | { |
1086 | wxSocketBase* sock = new wxSocketBase(); | |
1087 | ||
71622a7a | 1088 | sock->SetFlags(m_flags); |
f4ada568 | 1089 | |
d80d1aba | 1090 | if (!AcceptWith(*sock, wait)) |
c3e646b4 VZ |
1091 | { |
1092 | sock->Destroy(); | |
1093 | sock = NULL; | |
1094 | } | |
f4ada568 | 1095 | |
f4ada568 GL |
1096 | return sock; |
1097 | } | |
1098 | ||
17aa2bec | 1099 | bool wxSocketServer::WaitForAccept(long seconds, long milliseconds) |
d80d1aba | 1100 | { |
af2fd961 | 1101 | return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG); |
d80d1aba GRG |
1102 | } |
1103 | ||
ef57d866 | 1104 | // ========================================================================== |
8c14576d | 1105 | // wxSocketClient |
ef57d866 | 1106 | // ========================================================================== |
f4ada568 | 1107 | |
ef57d866 GRG |
1108 | // -------------------------------------------------------------------------- |
1109 | // Ctor and dtor | |
1110 | // -------------------------------------------------------------------------- | |
56d8adc0 | 1111 | |
71622a7a GRG |
1112 | wxSocketClient::wxSocketClient(wxSocketFlags flags) |
1113 | : wxSocketBase(flags, wxSOCKET_CLIENT) | |
f4ada568 GL |
1114 | { |
1115 | } | |
1116 | ||
f4ada568 GL |
1117 | wxSocketClient::~wxSocketClient() |
1118 | { | |
1119 | } | |
1120 | ||
ef57d866 GRG |
1121 | // -------------------------------------------------------------------------- |
1122 | // Connect | |
1123 | // -------------------------------------------------------------------------- | |
dc5c1114 | 1124 | |
791b24c4 | 1125 | bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait) |
f4ada568 | 1126 | { |
791b24c4 GRG |
1127 | GSocketError err; |
1128 | ||
ef57d866 | 1129 | if (m_socket) |
04e1eb03 GRG |
1130 | { |
1131 | // Shutdown and destroy the socket | |
f4ada568 | 1132 | Close(); |
a324a7bc | 1133 | GSocket_destroy(m_socket); |
04e1eb03 | 1134 | } |
a324a7bc | 1135 | |
a324a7bc | 1136 | m_socket = GSocket_new(); |
d80d1aba GRG |
1137 | m_connected = FALSE; |
1138 | m_establishing = FALSE; | |
384b4373 | 1139 | |
a324a7bc | 1140 | if (!m_socket) |
f4ada568 | 1141 | return FALSE; |
384b4373 | 1142 | |
e8f4c584 | 1143 | GSocket_SetTimeout(m_socket, m_timeout * 1000); |
96db102a GRG |
1144 | GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | |
1145 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1146 | wx_socket_callback, (char *)this); | |
56d8adc0 | 1147 | |
04e1eb03 | 1148 | // If wait == FALSE, then the call should be nonblocking. |
56d8adc0 GRG |
1149 | // When we are finished, we put the socket to blocking mode |
1150 | // again. | |
f4ada568 | 1151 | |
791b24c4 | 1152 | if (!wait) |
5c9eff30 | 1153 | GSocket_SetNonBlocking(m_socket, 1); |
791b24c4 | 1154 | |
a324a7bc | 1155 | GSocket_SetPeer(m_socket, addr_man.GetAddress()); |
791b24c4 GRG |
1156 | err = GSocket_Connect(m_socket, GSOCK_STREAMED); |
1157 | ||
1158 | if (!wait) | |
5c9eff30 | 1159 | GSocket_SetNonBlocking(m_socket, 0); |
791b24c4 GRG |
1160 | |
1161 | if (err != GSOCK_NOERROR) | |
56d8adc0 GRG |
1162 | { |
1163 | if (err == GSOCK_WOULDBLOCK) | |
1164 | m_establishing = TRUE; | |
1165 | ||
f4ada568 | 1166 | return FALSE; |
56d8adc0 | 1167 | } |
9111db68 | 1168 | |
f4ada568 GL |
1169 | m_connected = TRUE; |
1170 | return TRUE; | |
1171 | } | |
1172 | ||
d80d1aba | 1173 | bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds) |
f4ada568 | 1174 | { |
af2fd961 | 1175 | if (m_connected) // Already connected |
d80d1aba GRG |
1176 | return TRUE; |
1177 | ||
af2fd961 | 1178 | if (!m_establishing || !m_socket) // No connection in progress |
d80d1aba GRG |
1179 | return FALSE; |
1180 | ||
2b396caa GRG |
1181 | return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG | |
1182 | GSOCK_LOST_FLAG); | |
f4ada568 GL |
1183 | } |
1184 | ||
ef57d866 | 1185 | // ========================================================================== |
dc5c1114 | 1186 | // wxDatagramSocket |
ef57d866 | 1187 | // ========================================================================== |
dc5c1114 GRG |
1188 | |
1189 | /* NOTE: experimental stuff - might change */ | |
1190 | ||
71622a7a GRG |
1191 | wxDatagramSocket::wxDatagramSocket( wxSockAddress& addr, |
1192 | wxSocketFlags flags ) | |
1193 | : wxSocketBase( flags, wxSOCKET_DATAGRAM ) | |
dc5c1114 GRG |
1194 | { |
1195 | // Create the socket | |
1196 | m_socket = GSocket_new(); | |
1197 | ||
1198 | if(!m_socket) | |
1199 | return; | |
1200 | ||
1201 | // Setup the socket as non connection oriented | |
1202 | GSocket_SetLocal(m_socket, addr.GetAddress()); | |
1203 | if( GSocket_SetNonOriented(m_socket) != GSOCK_NOERROR ) | |
1204 | { | |
1205 | GSocket_destroy(m_socket); | |
1206 | m_socket = NULL; | |
1207 | return; | |
1208 | } | |
1209 | ||
1210 | // Initialize all stuff | |
1211 | m_connected = FALSE; | |
1212 | m_establishing = FALSE; | |
1213 | GSocket_SetTimeout( m_socket, m_timeout ); | |
1214 | GSocket_SetCallback( m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
1215 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1216 | wx_socket_callback, (char*)this ); | |
1217 | ||
1218 | } | |
1219 | ||
1220 | wxDatagramSocket& wxDatagramSocket::RecvFrom( wxSockAddress& addr, | |
f187448d | 1221 | void* buf, |
dc5c1114 GRG |
1222 | wxUint32 nBytes ) |
1223 | { | |
1224 | Read(buf, nBytes); | |
1225 | GetPeer(addr); | |
1226 | return (*this); | |
1227 | } | |
1228 | ||
1229 | wxDatagramSocket& wxDatagramSocket::SendTo( wxSockAddress& addr, | |
f187448d | 1230 | const void* buf, |
dc5c1114 GRG |
1231 | wxUint32 nBytes ) |
1232 | { | |
1233 | GSocket_SetPeer(m_socket, addr.GetAddress()); | |
1234 | Write(buf, nBytes); | |
1235 | return (*this); | |
1236 | } | |
1237 | ||
ef57d866 | 1238 | // ========================================================================== |
a324a7bc | 1239 | // wxSocketEvent |
ef57d866 | 1240 | // ========================================================================== |
f4ada568 | 1241 | |
71622a7a | 1242 | wxSocketEvent::wxSocketEvent(int id) : wxEvent(id) |
f4ada568 | 1243 | { |
71622a7a | 1244 | SetEventType( (wxEventType)wxEVT_SOCKET ); |
f4ada568 GL |
1245 | } |
1246 | ||
71622a7a | 1247 | void wxSocketEvent::CopyObject(wxObject& object_dest) const |
f4ada568 | 1248 | { |
71622a7a | 1249 | wxSocketEvent *event = (wxSocketEvent *)&object_dest; |
f4ada568 | 1250 | |
71622a7a | 1251 | wxEvent::CopyObject(object_dest); |
3b4183d8 | 1252 | |
f187448d GRG |
1253 | event->m_event = m_event; |
1254 | event->m_clientData = m_clientData; | |
3b4183d8 GL |
1255 | } |
1256 | ||
ef57d866 | 1257 | // ========================================================================== |
a58d5df4 | 1258 | // wxSocketModule |
ef57d866 | 1259 | // ========================================================================== |
dc5c1114 | 1260 | |
bffc1eaa | 1261 | class WXDLLEXPORT wxSocketModule : public wxModule |
dc5c1114 | 1262 | { |
a58d5df4 | 1263 | DECLARE_DYNAMIC_CLASS(wxSocketModule) |
04e1eb03 | 1264 | |
dc5c1114 | 1265 | public: |
1f0500b3 | 1266 | bool OnInit() { return GSocket_Init() != 0; } |
04e1eb03 | 1267 | void OnExit() { GSocket_Cleanup(); } |
a58d5df4 GL |
1268 | }; |
1269 | ||
1270 | IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule) | |
1271 | ||
35a4dab7 GL |
1272 | #endif |
1273 | // wxUSE_SOCKETS |