]> git.saurik.com Git - wxWidgets.git/blob - src/common/sckint.cpp
minor 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
163 if (ret == 0)
164 // If nothing happened, we wait for 100 ms.
165 wxThread::Sleep(10);
166 else
167 wxThread::Yield();
168 #else
169 if (ret == 0)
170 // If nothing happened, we wait for 100 ms.
171 wxUsleep(10);
172 else
173 wxYield();
174 #endif
175
176 // Check whether we should exit.
177 if (TestDestroy())
178 return NULL;
179 }
180 return NULL;
181 }
182
183 // --------------------------------------------------------------
184 // --------- SocketRequester ------------------------------------
185 // --------------------------------------------------------------
186
187 SocketRequester::SocketRequester(wxSocketBase *socket,
188 wxSocketInternal *internal)
189 : m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
190 {
191 }
192
193 SocketRequester::~SocketRequester()
194 {
195 }
196
197 bool SocketRequester::WaitFor(wxSocketBase::wxRequestNotify req, int millisec)
198 {
199 int ret;
200 struct timeval tv;
201 fd_set sockrd_set, sockwr_set;
202
203 // We won't wait.
204 tv.tv_sec = millisec / 1000;
205 tv.tv_usec = (millisec % 1000) * 1000;
206
207 if ((req & READ_MASK) != 0)
208 FD_ZERO(&sockrd_set);
209 FD_ZERO(&sockwr_set);
210
211 FD_SET(m_fd, &sockrd_set);
212 FD_SET(m_fd, &sockwr_set);
213
214 m_internal->AcquireFD();
215 ret = select(FD_SETSIZE, &sockrd_set, &sockwr_set, NULL, &tv);
216 m_internal->ReleaseFD();
217
218 return (ret != 0);
219 }
220
221 void SocketRequester::ProcessReadEvent(SockRequest *req)
222 {
223 int ret;
224 size_t len;
225
226 // We'll wait for the first byte, in case a "timeout event" occurs it returns // immediately
227 if (!WaitFor(wxSocketBase::REQ_READ, req->timeout)) {
228 m_internal->EndRequest(req);
229 return;
230 }
231
232 m_internal->AcquireFD();
233 ret = recv(m_fd, req->buffer, req->size,
234 (req->type == wxSocketBase::REQ_PEEK) ? MSG_PEEK : 0);
235 m_internal->ReleaseFD();
236
237 // An error occured, we exit.
238 if (ret < 0) {
239 req->error = errno;
240 m_internal->EndRequest(req);
241 return;
242 }
243 len = ret;
244
245 // If the buffer isn't full (and we want it to be full), we don't unqueue it.
246 if ((len < req->size) && (m_socket->GetFlags() & wxSocketBase::WAITALL)) {
247 req->size -= len;
248 req->io_nbytes += len;
249 req->buffer += len;
250 return;
251 }
252 // The End.
253 req->io_nbytes += len;
254 m_internal->EndRequest(req);
255 }
256
257 void SocketRequester::ProcessWriteEvent(SockRequest *req)
258 {
259 int ret;
260 size_t len;
261
262 m_internal->AcquireFD();
263 ret = send(m_fd, req->buffer, req->size, 0);
264 m_internal->ReleaseFD();
265 if (ret < 0) {
266 req->error = errno;
267 m_internal->EndRequest(req);
268 return;
269 }
270 len = ret;
271 if ((len < req->size) && ((m_socket->GetFlags() & wxSocketBase::WAITALL) != 0)) {
272 req->size -= len;
273 req->io_nbytes += len;
274 req->buffer += len;
275 return;
276 }
277 req->io_nbytes += len;
278 m_internal->EndRequest(req);
279 }
280
281 void SocketRequester::ProcessWaitEvent(SockRequest *req)
282 {
283 if (WaitFor(req->type, req->timeout))
284 req->io_nbytes = 1; // We put 1 in the counter to tell the requester
285 // there is no timeout.
286 else
287 req->io_nbytes = 0;
288
289 m_internal->EndRequest(req);
290 }
291
292 void *SocketRequester::Entry()
293 {
294 SockRequest *req;
295
296 while (1) {
297 // Wait for a new request or a destroy message.
298 req = m_internal->WaitForReq();
299 if (TestDestroy() || req == NULL)
300 return NULL;
301
302 if ((req->type & wxSocketBase::REQ_WAIT) != 0) {
303 ProcessWaitEvent(req);
304 continue;
305 }
306
307 switch (req->type) {
308 case wxSocketBase::REQ_READ:
309 case wxSocketBase::REQ_PEEK:
310 ProcessReadEvent(req);
311 break;
312 case wxSocketBase::REQ_WRITE:
313 ProcessWriteEvent(req);
314 break;
315 }
316 }
317 return NULL;
318 }
319
320 // --------------------------------------------------------------
321 // --------- wxSocketInternal -----------------------------------
322 // --------------------------------------------------------------
323
324 wxSocketInternal::wxSocketInternal(wxSocketBase *socket)
325 {
326 m_socket = socket;
327 m_thread_waiter = new SocketWaiter(socket, this);
328 m_thread_requester = new SocketRequester(socket, this);
329 m_request_locker.Lock();
330 }
331
332 wxSocketInternal::~wxSocketInternal()
333 {
334 // wxASSERT(!m_finalized); there is no m_finalized anywhere, RR
335 m_request_locker.Unlock();
336 delete m_thread_waiter;
337 delete m_thread_requester;
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);
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 // ----------------------------------------------------------------------
385 // InitializeSocket: called by wxSocketBase to initialize the daemons with
386 // a new file descriptor and to create them
387 // ----------------------------------------------------------------------
388 void wxSocketInternal::InitializeSocket()
389 {
390 // wxASSERT( ((m_thread_waiter->IsAlive() && !m_thread_waiter->IsPaused()) ||
391 // (m_thread_requester->IsAlive() && !m_thread_requester->IsPaused())));
392
393 m_thread_waiter->m_fd = m_socket->m_fd;
394 m_thread_requester->m_fd = m_socket->m_fd;
395
396 if (m_thread_waiter->IsPaused())
397 ResumeSocket();
398 else {
399
400 if (m_thread_waiter->Create() != wxTHREAD_NO_ERROR) {
401 // Something should be done here.
402 return;
403 }
404
405 if (m_thread_requester->Create() != wxTHREAD_NO_ERROR) {
406 // Something should be done here.
407 return;
408 }
409
410 m_thread_waiter->Run();
411 m_thread_requester->Run();
412 }
413 }
414
415
416 // ----------------------------------------------------------------------
417 // InitializeSocket: called by wxSocketBase to destroy daemons
418 // ----------------------------------------------------------------------
419 void wxSocketInternal::FinalizeSocket()
420 {
421 wxASSERT( (!m_thread_waiter->IsAlive() && !m_thread_requester->IsAlive()) );
422
423 ResumeSocket();
424
425 m_thread_waiter->Delete();
426 m_socket_locker.Lock();
427 if (m_requests.Number() == 0)
428 m_socket_cond.Signal();
429 m_socket_locker.Unlock();
430
431 m_thread_requester->Delete();
432 }
433
434 void wxSocketInternal::PauseSocket()
435 {
436 if (m_thread_waiter != NULL && !m_thread_waiter->IsPaused())
437 m_thread_waiter->Pause();
438 }
439
440 void wxSocketInternal::ResumeSocket()
441 {
442 if (m_thread_waiter != NULL && m_thread_waiter->IsPaused())
443 m_thread_waiter->Resume();
444 }
445
446 void wxSocketInternal::EnableWaiter()
447 {
448 if (m_thread_waiter != NULL && m_thread_waiter->IsPaused())
449 m_thread_waiter->Resume();
450 }
451
452 void wxSocketInternal::DisableWaiter()
453 {
454 if (m_thread_waiter != NULL && !m_thread_waiter->IsPaused())
455 m_thread_waiter->Pause();
456 }
457
458 // ----------------------------------------------------------------------
459 // QueueRequest:
460 // ----------------------------------------------------------------------
461 void wxSocketInternal::QueueRequest(SockRequest *request, bool async)
462 {
463 if (async) {
464 m_request_locker.Lock();
465 request->done = FALSE;
466 m_requests.Append((wxObject *)request);
467 m_request_locker.Unlock();
468
469 // Wake up
470 m_socket_cond.Signal();
471
472 if (request->wait) {
473 if (wxThread::IsMain())
474 while (!request->done) {
475 wxYield();
476 }
477 else
478 while (!request->done) {
479 wxThread::Yield();
480 }
481 }
482
483 } else {
484 m_request_locker.Lock();
485
486 if ((request->type & wxSocketBase::REQ_WAIT) != 0) {
487 m_thread_requester->ProcessWaitEvent(request);
488 } else {
489
490 request->done = FALSE;
491
492 switch (request->type) {
493 case wxSocketBase::REQ_PEEK:
494 case wxSocketBase::REQ_READ:
495 m_thread_requester->ProcessReadEvent(request);
496 break;
497 case wxSocketBase::REQ_WRITE:
498 m_thread_requester->ProcessWriteEvent(request);
499 break;
500 }
501 }
502 request->done = TRUE;
503 m_request_locker.Unlock();
504 }
505 }
506
507 void wxSocketInternal::WaitForEnd(SockRequest *request)
508 {
509 // TODOTODO
510 }
511
512 #endif
513 // __WXSTUBS__
514
515 #endif
516 // wxUSE_SOCKETS