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