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