]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckint.cpp
Compile fixes.
[wxWidgets.git] / src / common / sckint.cpp
CommitLineData
a737331d 1///////////////////////////////////////////////////////////////////////////////
9111db68
GL
2// Name: sckint.cpp
3// Purpose: Socket internal classes
4// Authors: Guilhem Lavaux
5// Created: April 1999
6// Updated:
a737331d
GL
7// Copyright: (C) 1999, 1998, 1997, Guilhem Lavaux
8// RCS_ID: $Id$
9// License: see wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11#ifdef __GNUG__
12#pragma implementation "sckint.h"
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19#pragma hdrstop
20#endif
21
22#if wxUSE_SOCKETS
23
24#define WXSOCK_INTERNAL
57dde4bd
RR
25#include "wx/object.h"
26#include "wx/list.h"
27#include "wx/socket.h"
28#include "wx/thread.h"
29#include "wx/sckint.h"
30#include "wx/sckint.h"
31#include "wx/utils.h"
a737331d 32
6b92f831
VZ
33// IRIX requires bstring.h be included to use select()
34#ifdef sgi
35 #include <bstring.h>
36#endif // IRIX
37
a737331d
GL
38#ifndef __WXSTUBS__
39
40#include <stdlib.h>
41#include <string.h>
42#include <ctype.h>
43
44// -----------------------
45// System specific headers
46// -----------------------
47
48#ifdef __WXMAC__
49// in order to avoid problems with our c library and double definitions
50#define close closesocket
51#define ioctl ioctlsocket
52
53#include <wx/mac/macsock.h>
54#endif
55
56#if defined(__WINDOWS__)
57#include <winsock.h>
58#endif // __WINDOWS__
59
60#if defined(__UNIX__)
61
62#ifdef VMS
63#include <socket.h>
64#else
65#include <sys/socket.h>
66#endif
67#include <sys/ioctl.h>
68
69#include <sys/time.h>
70#include <unistd.h>
71
72#ifdef sun
73#include <sys/filio.h>
74#endif
75
76#endif // __UNIX__
77
78#include <signal.h>
79#include <errno.h>
80
81#ifdef __VISUALC__
82#include <io.h>
83#endif
84
85// Constants
86#define READ_MASK wxSocketBase::REQ_READ | wxSocketBase::REQ_ACCEPT | wxSocketBase::REQ_LOST
87#define WRITE_MASK wxSocketBase::REQ_WRITE | wxSocketBase::REQ_CONNECT
88
89// --------------------------------------------------------------
90// --------- SocketWaiter ---------------------------------------
91// --------------------------------------------------------------
92
ba9838b9 93#if wxUSE_THREADS
a737331d
GL
94SocketWaiter::SocketWaiter(wxSocketBase *socket,
95 wxSocketInternal *internal)
9111db68
GL
96 : wxThread(),
97 m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
a737331d
GL
98{
99}
100
101SocketWaiter::~SocketWaiter()
102{
103}
104
105void SocketWaiter::ProcessReadEvent()
106{
107 int ret;
108 char c;
109
062c4861 110 m_internal->AcquireFD();
a737331d 111 ret = recv(m_fd, &c, 1, MSG_PEEK);
062c4861 112 m_internal->ReleaseFD();
a737331d
GL
113
114 // We are a server => emit a EVT_ACCEPT event.
115 if (ret == -1 && m_socket->GetType() == wxSocketBase::SOCK_SERVER) {
116 m_socket->OnRequest(wxSocketBase::EVT_ACCEPT);
117 return;
118 }
119
120 // Else, no error => there is something to be read else
121 // we've lost the connection.
9111db68 122 if (ret > 0) {
a737331d 123 m_socket->OnRequest(wxSocketBase::EVT_READ);
9111db68 124 } else {
a737331d 125 m_socket->OnRequest(wxSocketBase::EVT_LOST);
48da7d0b 126 m_internal->ReleaseData(); // In that case, we mustn't forget to unlock the mutex.
a737331d
GL
127 Exit(NULL);
128 }
129}
130
131void SocketWaiter::ProcessWriteEvent()
132{
133 if (m_socket->IsConnected())
134 m_socket->OnRequest(wxSocketBase::EVT_CONNECT);
135 else
136 m_socket->OnRequest(wxSocketBase::EVT_WRITE);
137}
138
139void *SocketWaiter::Entry()
140{
141 struct timeval tv;
142 fd_set sockrd_set, sockwr_set;
143 wxSocketEvent event;
144 int ret;
145
146 while (1) {
147 // We won't wait.
148 tv.tv_sec = 0;
149 tv.tv_usec = 0;
150
151 FD_ZERO(&sockrd_set);
152 FD_ZERO(&sockwr_set);
9111db68 153
48da7d0b
GL
154 m_internal->AcquireData();
155
a737331d
GL
156 if ((m_socket->NeededReq() & READ_MASK) != 0)
157 FD_SET(m_fd, &sockrd_set);
158 if ((m_socket->NeededReq() & WRITE_MASK) != 0)
159 FD_SET(m_fd, &sockwr_set);
160
161 m_internal->AcquireFD();
9111db68 162 ret = select(m_fd+1, &sockrd_set, &sockwr_set, NULL, &tv);
a737331d
GL
163 m_internal->ReleaseFD();
164
165 if (FD_ISSET(m_fd, &sockrd_set))
166 ProcessReadEvent();
167
168 if (FD_ISSET(m_fd, &sockwr_set))
169 ProcessWriteEvent();
170
48da7d0b
GL
171 m_internal->ReleaseData();
172
70190256
JS
173#if wxUSE_THREADS
174#ifdef Yield
175#undef Yield
176#endif
aadbdf11 177#endif
70190256 178
062c4861
GL
179 // We wait for 100 ms to prevent the CPU from burning.
180 wxUsleep(100);
a737331d
GL
181
182 // Check whether we should exit.
062c4861 183 if (TestDestroy())
a737331d 184 return NULL;
2a4f27f2 185 }
a737331d
GL
186 return NULL;
187}
188
fae05df5
GL
189#endif
190
a737331d
GL
191// --------------------------------------------------------------
192// --------- SocketRequester ------------------------------------
193// --------------------------------------------------------------
194
195SocketRequester::SocketRequester(wxSocketBase *socket,
196 wxSocketInternal *internal)
062c4861
GL
197 :
198#if wxUSE_THREADS
199 wxThread(),
200#endif
9111db68 201 m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
a737331d
GL
202{
203}
204
205SocketRequester::~SocketRequester()
206{
207}
208
209bool SocketRequester::WaitFor(wxSocketBase::wxRequestNotify req, int millisec)
210{
211 int ret;
212 struct timeval tv;
213 fd_set sockrd_set, sockwr_set;
214
215 // We won't wait.
216 tv.tv_sec = millisec / 1000;
217 tv.tv_usec = (millisec % 1000) * 1000;
218
062c4861 219 FD_ZERO(&sockrd_set);
a737331d 220 FD_ZERO(&sockwr_set);
062c4861
GL
221 if ((req & READ_MASK) != 0)
222 FD_SET(m_fd, &sockrd_set);
223 if ((req & WRITE_MASK) != 0)
224 FD_SET(m_fd, &sockwr_set);
a737331d
GL
225
226 m_internal->AcquireFD();
9111db68 227 ret = select(m_fd+1, &sockrd_set, &sockwr_set, NULL, &tv);
a737331d
GL
228 m_internal->ReleaseFD();
229
230 return (ret != 0);
231}
232
233void SocketRequester::ProcessReadEvent(SockRequest *req)
234{
235 int ret;
236 size_t len;
237
238 // We'll wait for the first byte, in case a "timeout event" occurs it returns // immediately
239 if (!WaitFor(wxSocketBase::REQ_READ, req->timeout)) {
240 m_internal->EndRequest(req);
241 return;
242 }
243
244 m_internal->AcquireFD();
245 ret = recv(m_fd, req->buffer, req->size,
246 (req->type == wxSocketBase::REQ_PEEK) ? MSG_PEEK : 0);
247 m_internal->ReleaseFD();
248
249 // An error occured, we exit.
250 if (ret < 0) {
251 req->error = errno;
252 m_internal->EndRequest(req);
253 return;
254 }
255 len = ret;
256
257 // If the buffer isn't full (and we want it to be full), we don't unqueue it.
258 if ((len < req->size) && (m_socket->GetFlags() & wxSocketBase::WAITALL)) {
259 req->size -= len;
260 req->io_nbytes += len;
261 req->buffer += len;
062c4861
GL
262
263 if (len == 0)
264 m_internal->EndRequest(req);
a737331d
GL
265 return;
266 }
267 // The End.
268 req->io_nbytes += len;
269 m_internal->EndRequest(req);
270}
271
272void SocketRequester::ProcessWriteEvent(SockRequest *req)
273{
274 int ret;
275 size_t len;
276
9111db68
GL
277 if (!WaitFor(wxSocketBase::REQ_WRITE, req->timeout)) {
278 m_internal->EndRequest(req);
279 return;
280 }
281
a737331d
GL
282 m_internal->AcquireFD();
283 ret = send(m_fd, req->buffer, req->size, 0);
284 m_internal->ReleaseFD();
285 if (ret < 0) {
286 req->error = errno;
287 m_internal->EndRequest(req);
288 return;
289 }
290 len = ret;
291 if ((len < req->size) && ((m_socket->GetFlags() & wxSocketBase::WAITALL) != 0)) {
292 req->size -= len;
293 req->io_nbytes += len;
294 req->buffer += len;
295 return;
296 }
297 req->io_nbytes += len;
298 m_internal->EndRequest(req);
299}
300
301void SocketRequester::ProcessWaitEvent(SockRequest *req)
302{
303 if (WaitFor(req->type, req->timeout))
304 req->io_nbytes = 1; // We put 1 in the counter to tell the requester
305 // there is no timeout.
306 else
307 req->io_nbytes = 0;
308
309 m_internal->EndRequest(req);
310}
311
fae05df5
GL
312
313#if wxUSE_THREADS
a737331d
GL
314void *SocketRequester::Entry()
315{
316 SockRequest *req;
317
9111db68 318 m_internal->m_request_locker.Lock();
a737331d
GL
319 while (1) {
320 // Wait for a new request or a destroy message.
321 req = m_internal->WaitForReq();
63496c98 322 m_internal->m_end_requester.Lock();
9111db68 323 if (req == NULL) {
63496c98
GL
324 m_internal->m_invalid_requester = TRUE;
325 m_internal->m_end_requester.Unlock();
9111db68 326 m_internal->m_request_locker.Unlock();
a737331d 327 return NULL;
63496c98
GL
328 }
329 m_internal->m_end_requester.Unlock();
a737331d
GL
330
331 if ((req->type & wxSocketBase::REQ_WAIT) != 0) {
332 ProcessWaitEvent(req);
333 continue;
334 }
335
336 switch (req->type) {
337 case wxSocketBase::REQ_READ:
338 case wxSocketBase::REQ_PEEK:
339 ProcessReadEvent(req);
340 break;
341 case wxSocketBase::REQ_WRITE:
342 ProcessWriteEvent(req);
343 break;
344 }
345 }
346 return NULL;
347}
ba9838b9 348#endif
a737331d
GL
349
350// --------------------------------------------------------------
351// --------- wxSocketInternal -----------------------------------
352// --------------------------------------------------------------
353
354wxSocketInternal::wxSocketInternal(wxSocketBase *socket)
355{
356 m_socket = socket;
2a4f27f2
GL
357 m_thread_requester = NULL;
358 m_thread_waiter = NULL;
63496c98 359 m_invalid_requester = TRUE;
a737331d
GL
360}
361
362wxSocketInternal::~wxSocketInternal()
363{
63496c98 364 StopRequester();
2a4f27f2 365 wxASSERT(m_thread_requester == NULL);
63496c98 366 StopWaiter();
2a4f27f2 367 wxASSERT(m_thread_waiter == NULL);
a737331d
GL
368}
369
370// ----------------------------------------------------------------------
371// WaitForReq: it is called by SocketRequester and should return the next
372// socket request if available
373// ----------------------------------------------------------------------
374SockRequest *wxSocketInternal::WaitForReq()
375{
ba9838b9 376#if wxUSE_THREADS
a737331d
GL
377 wxNode *node;
378
062c4861 379 // First try.
a737331d
GL
380 node = m_requests.First();
381 if (node == NULL) {
9111db68 382 m_socket_cond.Wait(m_request_locker, 10, 0);
a737331d 383
062c4861 384 // Second try, if it is unsuccessul we give up.
a737331d
GL
385 node = m_requests.First();
386 if (node == NULL)
387 return NULL;
388 }
389
390 return (SockRequest *)node->Data();
ba9838b9
JS
391#else
392 return NULL;
393#endif
a737331d
GL
394}
395
396// ----------------------------------------------------------------------
397// EndRequest: Should be called to finalize a request
398// ----------------------------------------------------------------------
399void wxSocketInternal::EndRequest(SockRequest *req)
400{
401 wxNode *node = NULL;
402
403 req->done = TRUE;
404
405 node = m_requests.Member((wxObject *)req);
406 if (node != NULL)
407 delete node;
408}
409
48da7d0b
GL
410void wxSocketInternal::AcquireData()
411{
412#if wxUSE_THREADS
413 m_socket_locker.Lock();
414#endif
415}
416
417void wxSocketInternal::ReleaseData()
418{
419#if wxUSE_THREADS
420 m_socket_locker.Unlock();
421#endif
422}
423
a737331d
GL
424void wxSocketInternal::AcquireFD()
425{
ba9838b9 426#if wxUSE_THREADS
a737331d 427 m_fd_locker.Lock();
ba9838b9 428#endif
a737331d
GL
429}
430
431void wxSocketInternal::ReleaseFD()
432{
ba9838b9 433#if wxUSE_THREADS
a737331d 434 m_fd_locker.Unlock();
ba9838b9 435#endif
a737331d
GL
436}
437
2a4f27f2 438void wxSocketInternal::ResumeRequester()
a737331d 439{
ba9838b9 440#if wxUSE_THREADS
2a4f27f2 441 wxThreadError err;
a737331d 442
062c4861 443 wxASSERT(m_invalid_requester);
a737331d 444
63496c98 445 m_end_requester.Lock();
062c4861
GL
446
447 if (m_thread_requester != NULL) {
448 m_thread_requester->Delete(); // We must join it.
449 delete m_thread_requester;
9111db68 450 }
062c4861
GL
451
452 m_invalid_requester = FALSE;
453
9111db68 454 m_end_requester.Unlock();
a737331d 455
9111db68 456 m_thread_requester = new SocketRequester(m_socket, this);
aadbdf11 457
9111db68
GL
458 err = m_thread_requester->Create();
459 wxASSERT(err == wxTHREAD_NO_ERROR);
63496c98 460
9111db68
GL
461 err = m_thread_requester->Run();
462 wxASSERT(err == wxTHREAD_NO_ERROR);
062c4861
GL
463#else
464 if (!m_invalid_requester)
465 return;
466 m_thread_requester = new SocketRequester(m_socket, this);
467 m_invalid_requester = FALSE;
ba9838b9 468#endif
a737331d
GL
469}
470
2a4f27f2 471void wxSocketInternal::StopRequester()
a737331d 472{
ba9838b9 473#if wxUSE_THREADS
9111db68 474 m_end_requester.Lock();
63496c98 475 if (m_invalid_requester) {
9111db68 476 m_end_requester.Unlock();
48da7d0b 477 if (m_thread_requester) {
062c4861 478 m_thread_requester->Delete();
48da7d0b
GL
479 delete m_thread_requester;
480 m_thread_requester = NULL;
481 }
482 m_invalid_requester = TRUE;
63496c98
GL
483 return;
484 }
9111db68 485 m_end_requester.Unlock();
63496c98 486
2a4f27f2 487 wxASSERT(m_thread_requester != NULL);
aadbdf11 488
48da7d0b 489 m_request_locker.Lock();
2a4f27f2
GL
490
491 // Send a signal to the requester.
9111db68 492 m_socket_cond.Signal();
2a4f27f2 493
48da7d0b 494 m_request_locker.Unlock();
a737331d 495
2a4f27f2 496 // Finish the destruction of the requester.
a737331d 497 m_thread_requester->Delete();
a737331d 498
2a4f27f2
GL
499 delete m_thread_requester;
500 m_thread_requester = NULL;
062c4861
GL
501 m_invalid_requester = TRUE;
502#else
503 delete m_thread_requester;
504 m_thread_requester = NULL;
505 m_invalid_requester = TRUE;
ba9838b9 506#endif
a737331d
GL
507}
508
2a4f27f2 509void wxSocketInternal::ResumeWaiter()
a737331d 510{
ba9838b9 511#if wxUSE_THREADS
2a4f27f2 512 wxThreadError err;
a737331d 513
2a4f27f2 514 if (m_thread_waiter != NULL)
aadbdf11
GL
515 return;
516
2a4f27f2 517 m_thread_waiter = new SocketWaiter(m_socket, this);
2a4f27f2 518
062c4861
GL
519 m_thread_waiter->SetPriority(WXTHREAD_MIN_PRIORITY);
520
2a4f27f2
GL
521 err = m_thread_waiter->Create();
522 wxASSERT(err == wxTHREAD_NO_ERROR);
523
524 err = m_thread_waiter->Run();
525 wxASSERT(err == wxTHREAD_NO_ERROR);
ba9838b9 526#endif
a737331d
GL
527}
528
2a4f27f2 529void wxSocketInternal::StopWaiter()
a737331d 530{
ba9838b9 531#if wxUSE_THREADS
2a4f27f2 532 if (m_thread_waiter == NULL)
aadbdf11
GL
533 return;
534
2a4f27f2
GL
535 m_thread_waiter->Delete();
536
537 delete m_thread_waiter;
538 m_thread_waiter = NULL;
ba9838b9 539#endif
a737331d
GL
540}
541
542// ----------------------------------------------------------------------
543// QueueRequest:
544// ----------------------------------------------------------------------
545void wxSocketInternal::QueueRequest(SockRequest *request, bool async)
546{
9111db68
GL
547 if (m_invalid_requester)
548 ResumeRequester();
9111db68 549
062c4861 550#if wxUSE_THREADS
a737331d 551 if (async) {
2a4f27f2 552
a737331d
GL
553 m_request_locker.Lock();
554 request->done = FALSE;
555 m_requests.Append((wxObject *)request);
aadbdf11 556 m_socket_cond.Signal();
a737331d
GL
557 m_request_locker.Unlock();
558
559 // Wake up
a737331d
GL
560
561 if (request->wait) {
562 if (wxThread::IsMain())
563 while (!request->done) {
564 wxYield();
565 }
566 else
567 while (!request->done) {
568 wxThread::Yield();
569 }
570 }
a737331d
GL
571 } else {
572 m_request_locker.Lock();
062c4861 573#endif
a737331d
GL
574
575 if ((request->type & wxSocketBase::REQ_WAIT) != 0) {
576 m_thread_requester->ProcessWaitEvent(request);
577 } else {
578
579 request->done = FALSE;
580
581 switch (request->type) {
582 case wxSocketBase::REQ_PEEK:
583 case wxSocketBase::REQ_READ:
584 m_thread_requester->ProcessReadEvent(request);
585 break;
586 case wxSocketBase::REQ_WRITE:
587 m_thread_requester->ProcessWriteEvent(request);
588 break;
589 }
590 }
591 request->done = TRUE;
062c4861 592#if wxUSE_THREADS
a737331d 593 m_request_locker.Unlock();
48da7d0b 594 }
ba9838b9 595#endif
a737331d
GL
596}
597
598void wxSocketInternal::WaitForEnd(SockRequest *request)
599{
ba9838b9 600#if wxUSE_THREADS
a737331d 601 // TODOTODO
ba9838b9 602#endif
a737331d
GL
603}
604
605#endif
606 // __WXSTUBS__
607
608#endif
609 // wxUSE_SOCKETS