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