]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckint.cpp
attempts to fix wxStaticBitmap::SetBitmap() - still doesn't work
[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
189// --------------------------------------------------------------
190// --------- SocketRequester ------------------------------------
191// --------------------------------------------------------------
192
193SocketRequester::SocketRequester(wxSocketBase *socket,
194 wxSocketInternal *internal)
062c4861
GL
195 :
196#if wxUSE_THREADS
197 wxThread(),
198#endif
9111db68 199 m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
a737331d
GL
200{
201}
202
203SocketRequester::~SocketRequester()
204{
205}
206
207bool SocketRequester::WaitFor(wxSocketBase::wxRequestNotify req, int millisec)
208{
209 int ret;
210 struct timeval tv;
211 fd_set sockrd_set, sockwr_set;
212
213 // We won't wait.
214 tv.tv_sec = millisec / 1000;
215 tv.tv_usec = (millisec % 1000) * 1000;
216
062c4861 217 FD_ZERO(&sockrd_set);
a737331d 218 FD_ZERO(&sockwr_set);
062c4861
GL
219 if ((req & READ_MASK) != 0)
220 FD_SET(m_fd, &sockrd_set);
221 if ((req & WRITE_MASK) != 0)
222 FD_SET(m_fd, &sockwr_set);
a737331d
GL
223
224 m_internal->AcquireFD();
9111db68 225 ret = select(m_fd+1, &sockrd_set, &sockwr_set, NULL, &tv);
a737331d
GL
226 m_internal->ReleaseFD();
227
228 return (ret != 0);
229}
230
231void SocketRequester::ProcessReadEvent(SockRequest *req)
232{
233 int ret;
234 size_t len;
235
236 // We'll wait for the first byte, in case a "timeout event" occurs it returns // immediately
237 if (!WaitFor(wxSocketBase::REQ_READ, req->timeout)) {
238 m_internal->EndRequest(req);
239 return;
240 }
241
242 m_internal->AcquireFD();
243 ret = recv(m_fd, req->buffer, req->size,
244 (req->type == wxSocketBase::REQ_PEEK) ? MSG_PEEK : 0);
245 m_internal->ReleaseFD();
246
247 // An error occured, we exit.
248 if (ret < 0) {
249 req->error = errno;
250 m_internal->EndRequest(req);
251 return;
252 }
253 len = ret;
254
255 // If the buffer isn't full (and we want it to be full), we don't unqueue it.
256 if ((len < req->size) && (m_socket->GetFlags() & wxSocketBase::WAITALL)) {
257 req->size -= len;
258 req->io_nbytes += len;
259 req->buffer += len;
062c4861
GL
260
261 if (len == 0)
262 m_internal->EndRequest(req);
a737331d
GL
263 return;
264 }
265 // The End.
266 req->io_nbytes += len;
267 m_internal->EndRequest(req);
268}
269
270void SocketRequester::ProcessWriteEvent(SockRequest *req)
271{
272 int ret;
273 size_t len;
274
9111db68
GL
275 if (!WaitFor(wxSocketBase::REQ_WRITE, req->timeout)) {
276 m_internal->EndRequest(req);
277 return;
278 }
279
a737331d
GL
280 m_internal->AcquireFD();
281 ret = send(m_fd, req->buffer, req->size, 0);
282 m_internal->ReleaseFD();
283 if (ret < 0) {
284 req->error = errno;
285 m_internal->EndRequest(req);
286 return;
287 }
288 len = ret;
289 if ((len < req->size) && ((m_socket->GetFlags() & wxSocketBase::WAITALL) != 0)) {
290 req->size -= len;
291 req->io_nbytes += len;
292 req->buffer += len;
293 return;
294 }
295 req->io_nbytes += len;
296 m_internal->EndRequest(req);
297}
298
299void SocketRequester::ProcessWaitEvent(SockRequest *req)
300{
301 if (WaitFor(req->type, req->timeout))
302 req->io_nbytes = 1; // We put 1 in the counter to tell the requester
303 // there is no timeout.
304 else
305 req->io_nbytes = 0;
306
307 m_internal->EndRequest(req);
308}
309
310void *SocketRequester::Entry()
311{
312 SockRequest *req;
313
9111db68 314 m_internal->m_request_locker.Lock();
a737331d
GL
315 while (1) {
316 // Wait for a new request or a destroy message.
317 req = m_internal->WaitForReq();
63496c98 318 m_internal->m_end_requester.Lock();
9111db68 319 if (req == NULL) {
63496c98
GL
320 m_internal->m_invalid_requester = TRUE;
321 m_internal->m_end_requester.Unlock();
9111db68 322 m_internal->m_request_locker.Unlock();
a737331d 323 return NULL;
63496c98
GL
324 }
325 m_internal->m_end_requester.Unlock();
a737331d
GL
326
327 if ((req->type & wxSocketBase::REQ_WAIT) != 0) {
328 ProcessWaitEvent(req);
329 continue;
330 }
331
332 switch (req->type) {
333 case wxSocketBase::REQ_READ:
334 case wxSocketBase::REQ_PEEK:
335 ProcessReadEvent(req);
336 break;
337 case wxSocketBase::REQ_WRITE:
338 ProcessWriteEvent(req);
339 break;
340 }
341 }
342 return NULL;
343}
ba9838b9 344#endif
a737331d
GL
345
346// --------------------------------------------------------------
347// --------- wxSocketInternal -----------------------------------
348// --------------------------------------------------------------
349
350wxSocketInternal::wxSocketInternal(wxSocketBase *socket)
351{
352 m_socket = socket;
2a4f27f2
GL
353 m_thread_requester = NULL;
354 m_thread_waiter = NULL;
63496c98 355 m_invalid_requester = TRUE;
a737331d
GL
356}
357
358wxSocketInternal::~wxSocketInternal()
359{
63496c98 360 StopRequester();
2a4f27f2 361 wxASSERT(m_thread_requester == NULL);
63496c98 362 StopWaiter();
2a4f27f2 363 wxASSERT(m_thread_waiter == NULL);
a737331d
GL
364}
365
366// ----------------------------------------------------------------------
367// WaitForReq: it is called by SocketRequester and should return the next
368// socket request if available
369// ----------------------------------------------------------------------
370SockRequest *wxSocketInternal::WaitForReq()
371{
ba9838b9 372#if wxUSE_THREADS
a737331d
GL
373 wxNode *node;
374
062c4861 375 // First try.
a737331d
GL
376 node = m_requests.First();
377 if (node == NULL) {
9111db68 378 m_socket_cond.Wait(m_request_locker, 10, 0);
a737331d 379
062c4861 380 // Second try, if it is unsuccessul we give up.
a737331d
GL
381 node = m_requests.First();
382 if (node == NULL)
383 return NULL;
384 }
385
386 return (SockRequest *)node->Data();
ba9838b9
JS
387#else
388 return NULL;
389#endif
a737331d
GL
390}
391
392// ----------------------------------------------------------------------
393// EndRequest: Should be called to finalize a request
394// ----------------------------------------------------------------------
395void wxSocketInternal::EndRequest(SockRequest *req)
396{
397 wxNode *node = NULL;
398
399 req->done = TRUE;
400
401 node = m_requests.Member((wxObject *)req);
402 if (node != NULL)
403 delete node;
404}
405
48da7d0b
GL
406void wxSocketInternal::AcquireData()
407{
408#if wxUSE_THREADS
409 m_socket_locker.Lock();
410#endif
411}
412
413void wxSocketInternal::ReleaseData()
414{
415#if wxUSE_THREADS
416 m_socket_locker.Unlock();
417#endif
418}
419
a737331d
GL
420void wxSocketInternal::AcquireFD()
421{
ba9838b9 422#if wxUSE_THREADS
a737331d 423 m_fd_locker.Lock();
ba9838b9 424#endif
a737331d
GL
425}
426
427void wxSocketInternal::ReleaseFD()
428{
ba9838b9 429#if wxUSE_THREADS
a737331d 430 m_fd_locker.Unlock();
ba9838b9 431#endif
a737331d
GL
432}
433
2a4f27f2 434void wxSocketInternal::ResumeRequester()
a737331d 435{
ba9838b9 436#if wxUSE_THREADS
2a4f27f2 437 wxThreadError err;
a737331d 438
062c4861 439 wxASSERT(m_invalid_requester);
a737331d 440
63496c98 441 m_end_requester.Lock();
062c4861
GL
442
443 if (m_thread_requester != NULL) {
444 m_thread_requester->Delete(); // We must join it.
445 delete m_thread_requester;
9111db68 446 }
062c4861
GL
447
448 m_invalid_requester = FALSE;
449
9111db68 450 m_end_requester.Unlock();
a737331d 451
9111db68 452 m_thread_requester = new SocketRequester(m_socket, this);
aadbdf11 453
9111db68
GL
454 err = m_thread_requester->Create();
455 wxASSERT(err == wxTHREAD_NO_ERROR);
63496c98 456
9111db68
GL
457 err = m_thread_requester->Run();
458 wxASSERT(err == wxTHREAD_NO_ERROR);
062c4861
GL
459#else
460 if (!m_invalid_requester)
461 return;
462 m_thread_requester = new SocketRequester(m_socket, this);
463 m_invalid_requester = FALSE;
ba9838b9 464#endif
a737331d
GL
465}
466
2a4f27f2 467void wxSocketInternal::StopRequester()
a737331d 468{
ba9838b9 469#if wxUSE_THREADS
9111db68 470 m_end_requester.Lock();
63496c98 471 if (m_invalid_requester) {
9111db68 472 m_end_requester.Unlock();
48da7d0b 473 if (m_thread_requester) {
062c4861 474 m_thread_requester->Delete();
48da7d0b
GL
475 delete m_thread_requester;
476 m_thread_requester = NULL;
477 }
478 m_invalid_requester = TRUE;
63496c98
GL
479 return;
480 }
9111db68 481 m_end_requester.Unlock();
63496c98 482
2a4f27f2 483 wxASSERT(m_thread_requester != NULL);
aadbdf11 484
48da7d0b 485 m_request_locker.Lock();
2a4f27f2
GL
486
487 // Send a signal to the requester.
9111db68 488 m_socket_cond.Signal();
2a4f27f2 489
48da7d0b 490 m_request_locker.Unlock();
a737331d 491
2a4f27f2 492 // Finish the destruction of the requester.
a737331d 493 m_thread_requester->Delete();
a737331d 494
2a4f27f2
GL
495 delete m_thread_requester;
496 m_thread_requester = NULL;
062c4861
GL
497 m_invalid_requester = TRUE;
498#else
499 delete m_thread_requester;
500 m_thread_requester = NULL;
501 m_invalid_requester = TRUE;
ba9838b9 502#endif
a737331d
GL
503}
504
2a4f27f2 505void wxSocketInternal::ResumeWaiter()
a737331d 506{
ba9838b9 507#if wxUSE_THREADS
2a4f27f2 508 wxThreadError err;
a737331d 509
2a4f27f2 510 if (m_thread_waiter != NULL)
aadbdf11
GL
511 return;
512
2a4f27f2 513 m_thread_waiter = new SocketWaiter(m_socket, this);
2a4f27f2 514
062c4861
GL
515 m_thread_waiter->SetPriority(WXTHREAD_MIN_PRIORITY);
516
2a4f27f2
GL
517 err = m_thread_waiter->Create();
518 wxASSERT(err == wxTHREAD_NO_ERROR);
519
520 err = m_thread_waiter->Run();
521 wxASSERT(err == wxTHREAD_NO_ERROR);
ba9838b9 522#endif
a737331d
GL
523}
524
2a4f27f2 525void wxSocketInternal::StopWaiter()
a737331d 526{
ba9838b9 527#if wxUSE_THREADS
2a4f27f2 528 if (m_thread_waiter == NULL)
aadbdf11
GL
529 return;
530
2a4f27f2
GL
531 m_thread_waiter->Delete();
532
533 delete m_thread_waiter;
534 m_thread_waiter = NULL;
ba9838b9 535#endif
a737331d
GL
536}
537
538// ----------------------------------------------------------------------
539// QueueRequest:
540// ----------------------------------------------------------------------
541void wxSocketInternal::QueueRequest(SockRequest *request, bool async)
542{
9111db68
GL
543 if (m_invalid_requester)
544 ResumeRequester();
9111db68 545
062c4861 546#if wxUSE_THREADS
a737331d 547 if (async) {
2a4f27f2 548
a737331d
GL
549 m_request_locker.Lock();
550 request->done = FALSE;
551 m_requests.Append((wxObject *)request);
aadbdf11 552 m_socket_cond.Signal();
a737331d
GL
553 m_request_locker.Unlock();
554
555 // Wake up
a737331d
GL
556
557 if (request->wait) {
558 if (wxThread::IsMain())
559 while (!request->done) {
560 wxYield();
561 }
562 else
563 while (!request->done) {
564 wxThread::Yield();
565 }
566 }
a737331d
GL
567 } else {
568 m_request_locker.Lock();
062c4861 569#endif
a737331d
GL
570
571 if ((request->type & wxSocketBase::REQ_WAIT) != 0) {
572 m_thread_requester->ProcessWaitEvent(request);
573 } else {
574
575 request->done = FALSE;
576
577 switch (request->type) {
578 case wxSocketBase::REQ_PEEK:
579 case wxSocketBase::REQ_READ:
580 m_thread_requester->ProcessReadEvent(request);
581 break;
582 case wxSocketBase::REQ_WRITE:
583 m_thread_requester->ProcessWriteEvent(request);
584 break;
585 }
586 }
587 request->done = TRUE;
062c4861 588#if wxUSE_THREADS
a737331d 589 m_request_locker.Unlock();
48da7d0b 590 }
ba9838b9 591#endif
a737331d
GL
592}
593
594void wxSocketInternal::WaitForEnd(SockRequest *request)
595{
ba9838b9 596#if wxUSE_THREADS
a737331d 597 // TODOTODO
ba9838b9 598#endif
a737331d
GL
599}
600
601#endif
602 // __WXSTUBS__
603
604#endif
605 // wxUSE_SOCKETS