]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
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 | |
a737331d GL |
7 | // Updated: April 1999 |
8 | // Copyright: (C) 1999 1998, 1997, Guilhem Lavaux | |
f4ada568 GL |
9 | // RCS_ID: $Id$ |
10 | // License: see wxWindows license | |
11 | //////////////////////////////////////////////////////////////////////////////// | |
384b4373 | 12 | #ifdef __GNUG__ |
f4ada568 | 13 | #pragma implementation "socket.h" |
f4ada568 GL |
14 | #endif |
15 | ||
17dff81c SC |
16 | #ifdef __MWERKS__ |
17 | typedef int socklen_t ; | |
18 | #endif | |
19 | ||
fcc6dddd JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
35a4dab7 GL |
27 | #if wxUSE_SOCKETS |
28 | ||
f4ada568 GL |
29 | ///////////////////////////////////////////////////////////////////////////// |
30 | // wxWindows headers | |
31 | ///////////////////////////////////////////////////////////////////////////// | |
32 | #include <wx/defs.h> | |
33 | #include <wx/object.h> | |
34 | #include <wx/string.h> | |
35 | #include <wx/timer.h> | |
36 | #include <wx/utils.h> | |
37 | ||
7f555861 JS |
38 | // Not enough OS behaviour defined for wxStubs |
39 | #ifndef __WXSTUBS__ | |
40 | ||
f4ada568 GL |
41 | #include <stdlib.h> |
42 | #include <string.h> | |
43 | #include <ctype.h> | |
44 | ||
45 | ///////////////////////////////////////////////////////////////////////////// | |
46 | // System specific headers | |
47 | ///////////////////////////////////////////////////////////////////////////// | |
17dff81c SC |
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> | |
a737331d | 54 | |
17dff81c SC |
55 | #endif |
56 | ||
f4ada568 | 57 | #if defined(__WINDOWS__) |
a737331d | 58 | #include <winsock.h> |
f4ada568 GL |
59 | #endif // __WINDOWS__ |
60 | ||
61 | #if defined(__UNIX__) | |
62 | ||
63 | #ifdef VMS | |
a737331d GL |
64 | #include <socket.h> |
65 | #else | |
66 | #include <sys/socket.h> | |
67 | #endif | |
f4ada568 GL |
68 | #include <sys/ioctl.h> |
69 | ||
70 | #include <sys/time.h> | |
71 | #include <unistd.h> | |
72 | ||
384b4373 | 73 | #ifdef sun |
a737331d | 74 | #include <sys/filio.h> |
384b4373 RD |
75 | #endif |
76 | ||
f4ada568 GL |
77 | #endif // __UNIX__ |
78 | ||
79 | #include <signal.h> | |
80 | #include <errno.h> | |
81 | ||
3f4a0c5b | 82 | #ifdef __VISUALC__ |
a737331d | 83 | #include <io.h> |
f4ada568 GL |
84 | #endif |
85 | ||
86 | ///////////////////////////////////////////////////////////////////////////// | |
87 | // wxSocket headers | |
88 | ///////////////////////////////////////////////////////////////////////////// | |
a737331d | 89 | #include <wx/module.h> |
f4ada568 | 90 | #define WXSOCK_INTERNAL |
a737331d GL |
91 | #include <wx/sckaddr.h> |
92 | #include <wx/socket.h> | |
93 | #include <wx/sckint.h> | |
57493f9f | 94 | |
a737331d GL |
95 | // ---------------------- |
96 | // Some patch ----- BEGIN | |
97 | // ---------------------- | |
f4ada568 GL |
98 | #ifdef __WINDOWS__ |
99 | #define close closesocket | |
100 | #define ioctl ioctlsocket | |
0c32066b JS |
101 | #ifdef errno |
102 | #undef errno | |
103 | #endif | |
f4ada568 GL |
104 | #define errno WSAGetLastError() |
105 | #ifdef EWOULDBLOCK | |
106 | #undef EWOULDBLOCK | |
107 | #endif | |
108 | #define EWOULDBLOCK WSAEWOULDBLOCK | |
109 | #define ETIMEDOUT WSAETIMEDOUT | |
110 | #undef EINTR | |
111 | #define EINTR WSAEINTR | |
112 | #endif | |
113 | ||
114 | #ifndef __WINDOWS__ | |
115 | #define INVALID_SOCKET -1 | |
116 | #endif | |
117 | ||
f4ada568 GL |
118 | #ifdef __WINDOWS__ |
119 | // This is an MS TCP/IP routine and is not needed here. Some WinSock | |
120 | // implementations (such as PC-NFS) will require you to include this | |
121 | // or a similar routine (see appendix in WinSock doc or help file). | |
122 | ||
3f4a0c5b | 123 | #if defined( NEED_WSAFDIsSet ) || defined( __VISUALC__ ) |
f4ada568 GL |
124 | int PASCAL FAR __WSAFDIsSet(SOCKET fd, fd_set FAR *set) |
125 | { | |
17dff81c | 126 | int i = set->fd_count; |
f4ada568 | 127 | |
17dff81c SC |
128 | while (i--) |
129 | { | |
130 | if (set->fd_array[i] == fd) | |
131 | return 1; | |
132 | } | |
f4ada568 | 133 | |
17dff81c | 134 | return 0; |
f4ada568 GL |
135 | } |
136 | #endif | |
137 | #endif | |
138 | ||
a737331d GL |
139 | // ------------------- |
140 | // Some patch ---- END | |
141 | // ------------------- | |
f4ada568 | 142 | |
ce3ed50d JS |
143 | #ifdef GetClassInfo |
144 | #undef GetClassInfo | |
145 | #endif | |
146 | ||
3b4183d8 GL |
147 | // -------------------------------------------------------------- |
148 | // Module | |
149 | // -------------------------------------------------------------- | |
db131261 RR |
150 | class wxSocketModule: public wxModule |
151 | { | |
3b4183d8 GL |
152 | DECLARE_DYNAMIC_CLASS(wxSocketModule) |
153 | public: | |
154 | wxSocketModule() {} | |
155 | bool OnInit(); | |
156 | void OnExit(); | |
157 | }; | |
158 | ||
f4ada568 GL |
159 | // -------------------------------------------------------------- |
160 | // ClassInfos | |
161 | // -------------------------------------------------------------- | |
162 | #if !USE_SHARED_LIBRARY | |
a737331d GL |
163 | IMPLEMENT_CLASS(wxSocketBase, wxObject) |
164 | IMPLEMENT_CLASS(wxSocketServer, wxSocketBase) | |
165 | IMPLEMENT_CLASS(wxSocketClient, wxSocketBase) | |
166 | IMPLEMENT_CLASS(wxSocketHandler, wxObject) | |
167 | IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent) | |
168 | IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule) | |
f4ada568 GL |
169 | #endif |
170 | ||
f4ada568 GL |
171 | // -------------------------------------------------------------- |
172 | // --------- wxSocketBase CONSTRUCTOR --------------------------- | |
173 | // -------------------------------------------------------------- | |
174 | wxSocketBase::wxSocketBase(wxSocketBase::wxSockFlags _flags, | |
17dff81c | 175 | wxSocketBase::wxSockType _type) : |
f4ada568 GL |
176 | wxEvtHandler(), |
177 | m_flags(_flags), m_type(_type), m_connected(FALSE), m_connecting(FALSE), | |
a737331d | 178 | m_fd(INVALID_SOCKET), m_id(-1), |
f4ada568 GL |
179 | m_handler(0), |
180 | m_neededreq((wxRequestNotify)(REQ_READ | REQ_LOST)), | |
a737331d | 181 | m_timeout(3600), |
f4ada568 | 182 | m_unread(NULL), m_unrd_size(0), |
a737331d GL |
183 | m_cbk(NULL), m_cdata(NULL), |
184 | m_notify_state(FALSE) | |
f4ada568 | 185 | { |
a737331d | 186 | m_internal = new wxSocketInternal(this); |
f4ada568 GL |
187 | } |
188 | ||
189 | wxSocketBase::wxSocketBase() : | |
190 | wxEvtHandler(), | |
191 | m_flags(WAITALL), m_type(SOCK_UNINIT), m_connected(FALSE), | |
a737331d | 192 | m_connecting(FALSE), m_fd(INVALID_SOCKET), |
f4ada568 GL |
193 | m_id(-1), m_handler(0), |
194 | m_neededreq((wxRequestNotify)(REQ_READ | REQ_LOST)), | |
a737331d | 195 | m_timeout(3600), |
f4ada568 | 196 | m_unread(NULL), m_unrd_size(0), |
a737331d GL |
197 | m_cbk(NULL), m_cdata(NULL), |
198 | m_notify_state(FALSE) | |
f4ada568 | 199 | { |
a737331d | 200 | m_internal = new wxSocketInternal(this); |
f4ada568 GL |
201 | } |
202 | ||
203 | // -------------------------------------------------------------- | |
8c14576d | 204 | // wxSocketBase |
f4ada568 | 205 | // -------------------------------------------------------------- |
8c14576d | 206 | |
f4ada568 GL |
207 | wxSocketBase::~wxSocketBase() |
208 | { | |
a737331d | 209 | // First, close the file descriptor. |
f4ada568 GL |
210 | Close(); |
211 | ||
a737331d GL |
212 | m_internal->FinalizeSocket(); |
213 | ||
f4ada568 GL |
214 | if (m_unread) |
215 | free(m_unread); | |
a737331d | 216 | // Unregister from the handler database. |
8c14576d | 217 | if (m_handler) |
f4ada568 | 218 | m_handler->UnRegister(this); |
a737331d GL |
219 | |
220 | // Destroy all saved states. | |
f4ada568 GL |
221 | m_states.DeleteContents(TRUE); |
222 | ||
a737331d | 223 | // Destroy the socket manager. |
f4ada568 GL |
224 | delete m_internal; |
225 | } | |
226 | ||
227 | bool wxSocketBase::Close() | |
228 | { | |
8c14576d RR |
229 | if (m_fd != INVALID_SOCKET) |
230 | { | |
a737331d GL |
231 | // Pause all running socket thread. |
232 | m_internal->PauseSocket(); | |
f4ada568 | 233 | |
a737331d | 234 | // Shutdown the connection. |
f4ada568 GL |
235 | shutdown(m_fd, 2); |
236 | close(m_fd); | |
237 | m_fd = INVALID_SOCKET; | |
238 | m_connected = FALSE; | |
239 | } | |
240 | ||
241 | return TRUE; | |
242 | } | |
243 | ||
244 | // -------------------------------------------------------------- | |
8c14576d | 245 | // wxSocketBase base IO function |
f4ada568 | 246 | // -------------------------------------------------------------- |
8c14576d | 247 | |
f4ada568 GL |
248 | wxSocketBase& wxSocketBase::Read(char* buffer, size_t nbytes) |
249 | { | |
a737331d GL |
250 | m_lcount = GetPushback(buffer, nbytes, FALSE); |
251 | nbytes -= m_lcount; | |
252 | buffer += m_lcount; | |
f4ada568 GL |
253 | |
254 | // If we have got the whole needed buffer or if we don't want to | |
255 | // wait then it returns immediately. | |
a737331d | 256 | if (!nbytes || (m_lcount && !(m_flags & WAITALL)) ) { |
f4ada568 | 257 | return *this; |
375abe3d | 258 | } |
f4ada568 GL |
259 | |
260 | WantBuffer(buffer, nbytes, EVT_READ); | |
261 | ||
262 | return *this; | |
263 | } | |
264 | ||
265 | wxSocketBase& wxSocketBase::Peek(char* buffer, size_t nbytes) | |
266 | { | |
a737331d GL |
267 | m_lcount = GetPushback(buffer, nbytes, TRUE); |
268 | if (nbytes-m_lcount == 0) | |
8c14576d | 269 | { |
f4ada568 GL |
270 | return *this; |
271 | } | |
a737331d GL |
272 | buffer += m_lcount; |
273 | nbytes -= m_lcount; | |
f4ada568 GL |
274 | |
275 | WantBuffer(buffer, nbytes, EVT_PEEK); | |
276 | ||
277 | return *this; | |
278 | } | |
279 | ||
280 | wxSocketBase& wxSocketBase::Write(const char *buffer, size_t nbytes) | |
281 | { | |
a737331d | 282 | m_lcount = 0; |
f4ada568 GL |
283 | WantBuffer((char *)buffer, nbytes, EVT_WRITE); |
284 | return *this; | |
285 | } | |
286 | ||
f4ada568 GL |
287 | wxSocketBase& wxSocketBase::Unread(const char *buffer, size_t nbytes) |
288 | { | |
289 | CreatePushbackAfter(buffer, nbytes); | |
290 | return *this; | |
291 | } | |
292 | ||
293 | bool wxSocketBase::IsData() const | |
294 | { | |
295 | struct timeval tv; | |
296 | fd_set sock_set; | |
297 | ||
298 | if (m_fd < 0) | |
299 | return FALSE; | |
300 | if (m_unrd_size > 0) | |
301 | return TRUE; | |
302 | ||
a737331d GL |
303 | m_internal->AcquireFD(); |
304 | ||
f4ada568 GL |
305 | tv.tv_sec = 0; |
306 | tv.tv_usec = 0; | |
307 | FD_ZERO(&sock_set); | |
308 | FD_SET(m_fd, &sock_set); | |
309 | select(FD_SETSIZE, &sock_set, NULL, NULL, &tv); | |
a737331d GL |
310 | |
311 | m_internal->ReleaseFD(); | |
312 | ||
0c32066b | 313 | return (FD_ISSET(m_fd, &sock_set) != 0); |
f4ada568 GL |
314 | } |
315 | ||
316 | // --------------------------------------------------------------------- | |
317 | // --------- wxSocketBase Discard(): deletes all byte in the input queue | |
318 | // --------------------------------------------------------------------- | |
319 | void wxSocketBase::Discard() | |
320 | { | |
321 | #define MAX_BUFSIZE (10*1024) | |
322 | char *my_data = new char[MAX_BUFSIZE]; | |
323 | size_t recv_size = MAX_BUFSIZE; | |
324 | ||
325 | SaveState(); | |
326 | SetFlags((wxSockFlags)(NOWAIT | SPEED)); | |
384b4373 | 327 | |
8c14576d RR |
328 | while (recv_size == MAX_BUFSIZE) |
329 | { | |
f4ada568 GL |
330 | recv_size = Read(my_data, MAX_BUFSIZE).LastCount(); |
331 | } | |
332 | ||
333 | RestoreState(); | |
334 | delete [] my_data; | |
335 | ||
336 | #undef MAX_BUFSIZE | |
337 | } | |
338 | ||
a737331d GL |
339 | // If what? Who seems to need unsigned int? |
340 | // BTW uint isn't even defined on wxMSW for VC++ for some reason. Even if it | |
341 | // were, getpeername/getsockname don't take unsigned int*, they take int*. | |
342 | // | |
343 | // Under glibc 2.0.7, socketbits.h declares socklen_t to be unsigned int | |
344 | // and it uses *socklen_t as the 3rd parameter. Robert. | |
345 | ||
346 | // JACS - How can we detect this? | |
347 | // Meanwhile, if your compiler complains about socklen_t, | |
348 | // switch lines below. | |
349 | ||
350 | #if wxHAVE_GLIBC2 | |
351 | # define wxSOCKET_INT socklen_t | |
352 | #else | |
353 | # define wxSOCKET_INT int | |
354 | #endif | |
15ed4533 | 355 | |
f4ada568 | 356 | // -------------------------------------------------------------- |
8c14576d | 357 | // wxSocketBase socket info functions |
f4ada568 | 358 | // -------------------------------------------------------------- |
8c14576d | 359 | |
f4ada568 GL |
360 | bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const |
361 | { | |
362 | struct sockaddr my_addr; | |
a737331d | 363 | wxSOCKET_INT len_addr = sizeof(my_addr); |
f4ada568 GL |
364 | |
365 | if (m_fd < 0) | |
366 | return FALSE; | |
367 | ||
a737331d GL |
368 | m_internal->AcquireFD(); |
369 | ||
370 | if (getpeername(m_fd, (struct sockaddr *)&my_addr, &len_addr) < 0) { | |
371 | m_internal->ReleaseFD(); | |
384b4373 | 372 | return FALSE; |
a737331d | 373 | } |
f4ada568 | 374 | |
a737331d | 375 | m_internal->ReleaseFD(); |
f4ada568 GL |
376 | addr_man.Disassemble(&my_addr, len_addr); |
377 | return TRUE; | |
378 | } | |
379 | ||
380 | bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const | |
381 | { | |
382 | struct sockaddr my_addr; | |
a737331d | 383 | wxSOCKET_INT len_addr = sizeof(my_addr); |
f4ada568 GL |
384 | |
385 | if (m_fd < 0) | |
386 | return FALSE; | |
387 | ||
a737331d GL |
388 | m_internal->AcquireFD(); |
389 | ||
390 | if (getsockname(m_fd, (struct sockaddr *)&my_addr, &len_addr) < 0) { | |
391 | m_internal->ReleaseFD(); | |
f4ada568 | 392 | return FALSE; |
a737331d GL |
393 | } |
394 | m_internal->ReleaseFD(); | |
f4ada568 GL |
395 | |
396 | addr_man.Disassemble(&my_addr, len_addr); | |
397 | return TRUE; | |
398 | } | |
399 | ||
400 | // -------------------------------------------------------------- | |
8c14576d | 401 | // wxSocketBase wait functions |
f4ada568 | 402 | // -------------------------------------------------------------- |
db131261 | 403 | |
f4ada568 GL |
404 | void wxSocketBase::SaveState() |
405 | { | |
a737331d | 406 | SocketState *state = new SocketState; |
f4ada568 | 407 | |
a737331d GL |
408 | state->notify_state = m_notify_state; |
409 | state->evt_notify_state = m_neededreq; | |
410 | state->socket_flags = m_flags; | |
411 | state->c_callback = m_cbk; | |
412 | state->c_callback_data = m_cdata; | |
f4ada568 | 413 | |
a737331d | 414 | m_states.Append((wxObject *)state); |
f4ada568 GL |
415 | } |
416 | ||
417 | void wxSocketBase::RestoreState() | |
418 | { | |
419 | wxNode *node; | |
a737331d | 420 | SocketState *state; |
f4ada568 GL |
421 | |
422 | node = m_states.Last(); | |
423 | if (!node) | |
424 | return; | |
425 | ||
a737331d | 426 | state = (SocketState *)node->Data(); |
384b4373 | 427 | |
a737331d GL |
428 | SetFlags(state->socket_flags); |
429 | m_neededreq = state->evt_notify_state; | |
430 | m_cbk = state->c_callback; | |
431 | m_cdata = state->c_callback_data; | |
432 | Notify(state->notify_state); | |
f4ada568 GL |
433 | |
434 | delete node; | |
435 | delete state; | |
436 | } | |
437 | ||
438 | // -------------------------------------------------------------- | |
a737331d | 439 | // --------- wxSocketBase callback functions -------------------- |
f4ada568 | 440 | // -------------------------------------------------------------- |
db131261 | 441 | |
a737331d | 442 | wxSocketBase::wxSockCbk wxSocketBase::Callback(wxSockCbk cbk_) |
f4ada568 | 443 | { |
a737331d | 444 | wxSockCbk old_cbk = cbk_; |
f4ada568 | 445 | |
a737331d GL |
446 | m_cbk = cbk_; |
447 | return old_cbk; | |
f4ada568 GL |
448 | } |
449 | ||
a737331d | 450 | char *wxSocketBase::CallbackData(char *data) |
f4ada568 | 451 | { |
a737331d | 452 | char *old_data = m_cdata; |
f4ada568 | 453 | |
a737331d GL |
454 | m_cdata = data; |
455 | return old_data; | |
f4ada568 GL |
456 | } |
457 | ||
458 | // -------------------------------------------------------------- | |
a737331d | 459 | // --------- wxSocketBase wait functions ------------------------ |
f4ada568 GL |
460 | // -------------------------------------------------------------- |
461 | ||
a737331d | 462 | bool wxSocketBase::_Wait(long seconds, long milliseconds, int type) |
375abe3d | 463 | { |
a737331d | 464 | SockRequest *req; |
375abe3d | 465 | |
a737331d GL |
466 | if ((!m_connected && !m_connecting) || m_fd < 0) |
467 | return FALSE; | |
f4ada568 | 468 | |
a737331d | 469 | req = new SockRequest; |
f4ada568 | 470 | |
a737331d GL |
471 | req->type = REQ_WAIT | type; |
472 | req->timeout = seconds * 1000 + milliseconds; | |
473 | req->done = FALSE; | |
474 | req->buffer = NULL; | |
475 | req->size = 0; | |
476 | req->error = 0; | |
477 | req->wait = TRUE; | |
478 | m_internal->QueueRequest(req, TRUE); | |
f4ada568 | 479 | |
a737331d | 480 | return (req->io_nbytes != 0); |
f4ada568 | 481 | } |
f4ada568 | 482 | |
a737331d | 483 | bool wxSocketBase::Wait(long seconds, long milliseconds) |
f4ada568 | 484 | { |
a737331d GL |
485 | return _Wait(seconds, milliseconds, REQ_ACCEPT | REQ_CONNECT | |
486 | REQ_READ | REQ_WRITE | REQ_LOST); | |
f4ada568 | 487 | } |
f4ada568 | 488 | |
a737331d | 489 | bool wxSocketBase::WaitForRead(long seconds, long milliseconds) |
f4ada568 | 490 | { |
a737331d | 491 | return _Wait(seconds, milliseconds, REQ_READ | REQ_LOST); |
f4ada568 GL |
492 | } |
493 | ||
a737331d | 494 | bool wxSocketBase::WaitForWrite(long seconds, long milliseconds) |
f4ada568 | 495 | { |
a737331d GL |
496 | return _Wait(seconds, milliseconds, REQ_WRITE); |
497 | } | |
f4ada568 | 498 | |
a737331d GL |
499 | bool wxSocketBase::WaitForLost(long seconds, long milliseconds) |
500 | { | |
501 | return _Wait(seconds, milliseconds, REQ_LOST); | |
f4ada568 | 502 | } |
a737331d GL |
503 | |
504 | // -------------------------------------------------------------- | |
505 | // --------- wxSocketBase callback management ------------------- | |
506 | // -------------------------------------------------------------- | |
f4ada568 GL |
507 | |
508 | wxSocketBase::wxRequestNotify wxSocketBase::EventToNotify(wxRequestEvent evt) | |
509 | { | |
db131261 RR |
510 | switch (evt) |
511 | { | |
f4ada568 GL |
512 | case EVT_READ: |
513 | return REQ_READ; | |
514 | case EVT_PEEK: | |
515 | return REQ_PEEK; | |
516 | case EVT_WRITE: | |
517 | return REQ_WRITE; | |
518 | case EVT_LOST: | |
519 | return REQ_LOST; | |
520 | case EVT_ACCEPT: | |
521 | return REQ_ACCEPT; | |
522 | case EVT_CONNECT: | |
523 | return REQ_CONNECT; | |
524 | } | |
525 | return 0; | |
526 | } | |
527 | ||
528 | void wxSocketBase::SetFlags(wxSockFlags _flags) | |
529 | { | |
530 | m_flags = _flags; | |
a737331d | 531 | if (_flags & SPEED) { |
f4ada568 GL |
532 | // SPEED and WAITALL are antagonists. |
533 | m_flags = (wxSockFlags)(m_flags & ~WAITALL); | |
f4ada568 GL |
534 | } |
535 | } | |
536 | ||
a737331d | 537 | wxSocketBase::wxSockFlags wxSocketBase::GetFlags() const |
f4ada568 | 538 | { |
a737331d | 539 | return m_flags; |
f4ada568 GL |
540 | } |
541 | ||
a737331d | 542 | void wxSocketBase::SetNotify(wxRequestNotify flags) |
f4ada568 | 543 | { |
a737331d GL |
544 | /* Check if server */ |
545 | if (m_type != SOCK_SERVER) | |
546 | flags &= ~REQ_ACCEPT; | |
f4ada568 | 547 | |
a737331d GL |
548 | m_neededreq = flags; |
549 | if (m_neededreq == 0) | |
550 | m_internal->DisableWaiter(); | |
8c14576d | 551 | else |
a737331d | 552 | Notify(m_notify_state); |
f4ada568 GL |
553 | } |
554 | ||
555 | void wxSocketBase::Notify(bool notify) | |
556 | { | |
f4ada568 | 557 | if (notify) |
a737331d | 558 | m_internal->EnableWaiter(); |
f4ada568 | 559 | else |
a737331d GL |
560 | m_internal->DisableWaiter(); |
561 | m_notify_state = notify; | |
f4ada568 GL |
562 | } |
563 | ||
564 | void wxSocketBase::OnRequest(wxRequestEvent req_evt) | |
565 | { | |
a737331d GL |
566 | wxSocketEvent event(m_id); |
567 | wxRequestNotify notify = EventToNotify(req_evt); | |
f4ada568 | 568 | |
a737331d GL |
569 | if ((m_neededreq & notify) == notify) { |
570 | event.m_socket = this; | |
571 | event.m_skevt = req_evt; | |
572 | ProcessEvent(event); | |
573 | // TODOTODO | |
574 | // OldOnNotify(req_evt); | |
f4ada568 | 575 | |
a737331d GL |
576 | // We disable the event reporting. |
577 | SetNotify(m_neededreq & ~notify); | |
f4ada568 | 578 | } |
f4ada568 GL |
579 | } |
580 | ||
581 | wxSocketEvent::wxSocketEvent(int id) | |
582 | : wxEvent(id) | |
583 | { | |
584 | wxEventType type = (wxEventType)wxEVT_SOCKET; | |
585 | ||
586 | SetEventType(type); | |
587 | } | |
588 | ||
a737331d | 589 | wxObject *wxSocketEvent::Clone() const |
f4ada568 | 590 | { |
a737331d | 591 | wxSocketEvent *event = (wxSocketEvent *)wxEvent::Clone(); |
f4ada568 | 592 | |
a737331d GL |
593 | event->m_skevt = m_skevt; |
594 | event->m_socket = m_socket; | |
f4ada568 | 595 | |
a737331d | 596 | return event; |
f4ada568 GL |
597 | } |
598 | ||
a737331d | 599 | void wxSocketBase::OldOnNotify(wxRequestEvent evt) |
f4ada568 | 600 | { |
f4ada568 GL |
601 | } |
602 | ||
a737331d GL |
603 | // -------------------------------------------------------------- |
604 | // --------- wxSocketBase functions [Callback, CallbackData] ---- | |
605 | // -------------------------------------------------------------- | |
f4ada568 GL |
606 | |
607 | void wxSocketBase::SetEventHandler(wxEvtHandler& h_evt, int id) | |
608 | { | |
609 | SetNextHandler(&h_evt); | |
610 | m_id = id; | |
611 | } | |
612 | ||
613 | // -------------------------------------------------------------- | |
614 | // --------- wxSocketBase pushback library ---------------------- | |
615 | // -------------------------------------------------------------- | |
db131261 | 616 | |
f4ada568 GL |
617 | void wxSocketBase::CreatePushbackAfter(const char *buffer, size_t size) |
618 | { | |
619 | char *curr_pos; | |
620 | ||
375abe3d GL |
621 | if (m_unread != NULL) |
622 | m_unread = (char *) realloc(m_unread, m_unrd_size+size); | |
623 | else | |
624 | m_unread = (char *) malloc(size); | |
f4ada568 GL |
625 | curr_pos = m_unread + m_unrd_size; |
626 | ||
627 | memcpy(curr_pos, buffer, size); | |
628 | m_unrd_size += size; | |
629 | } | |
630 | ||
631 | void wxSocketBase::CreatePushbackBefore(const char *buffer, size_t size) | |
632 | { | |
633 | char *curr_pos, *new_buf; | |
634 | ||
635 | new_buf = (char *) malloc(m_unrd_size+size); | |
636 | curr_pos = new_buf + size; | |
637 | ||
638 | memcpy(new_buf, buffer, size); | |
41895a05 GL |
639 | if (m_unrd_size != 0) { |
640 | memcpy(curr_pos, m_unread, m_unrd_size); | |
641 | free(m_unread); | |
642 | } | |
f4ada568 GL |
643 | m_unread = new_buf; |
644 | m_unrd_size += size; | |
645 | } | |
646 | ||
647 | size_t wxSocketBase::GetPushback(char *buffer, size_t size, bool peek) | |
648 | { | |
649 | if (!m_unrd_size) | |
650 | return 0; | |
651 | ||
652 | if (size > m_unrd_size) | |
653 | size = m_unrd_size; | |
654 | memcpy(buffer, m_unread, size); | |
655 | ||
656 | if (!peek) { | |
657 | m_unrd_size -= size; | |
41895a05 | 658 | if (m_unrd_size == 0) { |
f4ada568 GL |
659 | free(m_unread); |
660 | m_unread = NULL; | |
661 | } | |
662 | } | |
663 | ||
664 | return size; | |
665 | } | |
666 | ||
667 | // -------------------------------------------------------------- | |
a737331d | 668 | // --------- wxSocketBase buffer core requester ----------------- |
f4ada568 GL |
669 | // -------------------------------------------------------------- |
670 | ||
f4ada568 | 671 | void wxSocketBase::WantBuffer(char *buffer, size_t nbytes, |
17dff81c | 672 | wxRequestEvent evt) |
f4ada568 GL |
673 | { |
674 | bool buf_timed_out; | |
675 | ||
676 | if (m_fd == INVALID_SOCKET || !m_handler || !m_connected) | |
677 | return; | |
678 | ||
f4ada568 | 679 | SockRequest *buf = new SockRequest; |
f4ada568 GL |
680 | |
681 | SaveState(); | |
a737331d | 682 | m_internal->DisableWaiter(); |
f4ada568 GL |
683 | buf->buffer = buffer; |
684 | buf->size = nbytes; | |
685 | buf->done = FALSE; | |
a737331d GL |
686 | buf->type = EventToNotify(evt); |
687 | buf->io_nbytes = 0; | |
f4ada568 | 688 | buf->error = 0; |
a737331d GL |
689 | buf->wait = TRUE; |
690 | buf->timeout = 1000; | |
f4ada568 GL |
691 | buf_timed_out = FALSE; |
692 | ||
a737331d GL |
693 | if (m_flags & SPEED) |
694 | m_internal->QueueRequest(buf, FALSE); | |
695 | else | |
696 | if (m_flags & NOWAIT) | |
697 | m_internal->QueueRequest(buf, TRUE); | |
698 | else | |
699 | m_internal->QueueRequest(buf, TRUE); | |
700 | m_lcount += buf->io_nbytes; | |
f4ada568 GL |
701 | if (buf_timed_out) |
702 | m_error = ETIMEDOUT; | |
703 | else | |
704 | m_error = buf->error; | |
705 | ||
706 | delete buf; | |
707 | RestoreState(); | |
708 | } | |
709 | ||
710 | // -------------------------------------------------------------- | |
8c14576d | 711 | // wxSocketServer |
f4ada568 GL |
712 | // -------------------------------------------------------------- |
713 | ||
f4ada568 | 714 | wxSocketServer::wxSocketServer(wxSockAddress& addr_man, |
17dff81c | 715 | wxSockFlags flags) : |
f4ada568 GL |
716 | wxSocketBase(flags, SOCK_SERVER) |
717 | { | |
718 | m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0); | |
719 | ||
720 | if (m_fd == INVALID_SOCKET) | |
721 | return; | |
384b4373 | 722 | |
f4ada568 GL |
723 | int flag = 1; |
724 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(int)); | |
384b4373 | 725 | |
f4ada568 GL |
726 | struct sockaddr *myaddr; |
727 | size_t len; | |
384b4373 | 728 | |
f4ada568 GL |
729 | addr_man.Build(myaddr, len); |
730 | if (bind(m_fd, myaddr, addr_man.SockAddrLen()) < 0) | |
731 | return; | |
384b4373 | 732 | |
f4ada568 GL |
733 | if (listen(m_fd, 5) < 0) { |
734 | m_fd = INVALID_SOCKET; | |
735 | return; | |
736 | } | |
a737331d GL |
737 | |
738 | m_internal->InitializeSocket(); | |
f4ada568 GL |
739 | } |
740 | ||
741 | // -------------------------------------------------------------- | |
8c14576d | 742 | // wxSocketServer Accept |
f4ada568 | 743 | // -------------------------------------------------------------- |
8c14576d | 744 | |
f4ada568 GL |
745 | bool wxSocketServer::AcceptWith(wxSocketBase& sock) |
746 | { | |
747 | int fd2; | |
384b4373 | 748 | |
f4ada568 GL |
749 | if ((fd2 = accept(m_fd, 0, 0)) < 0) |
750 | return FALSE; | |
751 | ||
752 | struct linger linger; | |
753 | linger.l_onoff = 0; | |
754 | linger.l_linger = 1; | |
384b4373 | 755 | |
f4ada568 | 756 | setsockopt(fd2, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger)); |
384b4373 | 757 | |
f4ada568 GL |
758 | int flag = 0; |
759 | setsockopt(fd2, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int)); | |
384b4373 | 760 | |
f4ada568 GL |
761 | sock.m_type = SOCK_INTERNAL; |
762 | sock.m_fd = fd2; | |
763 | sock.m_connected = TRUE; | |
764 | ||
a737331d GL |
765 | sock.m_internal->InitializeSocket(); |
766 | ||
f4ada568 GL |
767 | return TRUE; |
768 | } | |
769 | ||
770 | wxSocketBase *wxSocketServer::Accept() | |
771 | { | |
772 | wxSocketBase* sock = new wxSocketBase(); | |
773 | ||
774 | sock->SetFlags((wxSockFlags)m_flags); | |
775 | ||
776 | if (!AcceptWith(*sock)) | |
777 | return NULL; | |
778 | ||
779 | if (m_handler) | |
780 | m_handler->Register(sock); | |
781 | ||
782 | return sock; | |
783 | } | |
784 | ||
f4ada568 | 785 | // -------------------------------------------------------------- |
8c14576d | 786 | // wxSocketClient |
f4ada568 GL |
787 | // -------------------------------------------------------------- |
788 | ||
789 | // --------- wxSocketClient CONSTRUCTOR ------------------------- | |
790 | // -------------------------------------------------------------- | |
791 | wxSocketClient::wxSocketClient(wxSockFlags _flags) : | |
17dff81c | 792 | wxSocketBase(_flags, SOCK_CLIENT) |
f4ada568 GL |
793 | { |
794 | } | |
795 | ||
796 | // -------------------------------------------------------------- | |
797 | // --------- wxSocketClient DESTRUCTOR -------------------------- | |
798 | // -------------------------------------------------------------- | |
799 | wxSocketClient::~wxSocketClient() | |
800 | { | |
801 | } | |
802 | ||
803 | // -------------------------------------------------------------- | |
804 | // --------- wxSocketClient Connect functions ------------------- | |
805 | // -------------------------------------------------------------- | |
e22036dc | 806 | bool wxSocketClient::Connect(wxSockAddress& addr_man, bool WXUNUSED(wait) ) |
f4ada568 GL |
807 | { |
808 | struct linger linger; | |
809 | ||
810 | if (IsConnected()) | |
811 | Close(); | |
812 | ||
a737331d GL |
813 | // Initializes all socket stuff ... |
814 | // -------------------------------- | |
f4ada568 | 815 | m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0); |
384b4373 | 816 | |
f4ada568 GL |
817 | if (m_fd < 0) |
818 | return FALSE; | |
384b4373 | 819 | |
f4ada568 GL |
820 | m_connected = FALSE; |
821 | ||
822 | linger.l_onoff = 1; | |
384b4373 | 823 | linger.l_linger = 5; |
f4ada568 | 824 | setsockopt(m_fd, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger)); |
384b4373 | 825 | |
f4ada568 | 826 | // Stay in touch with the state of things... |
384b4373 | 827 | |
f4ada568 GL |
828 | unsigned long flag = 1; |
829 | setsockopt(m_fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int)); | |
384b4373 | 830 | |
f4ada568 GL |
831 | // Disable the nagle algorithm, which delays sends till the |
832 | // buffer is full (or a certain time period has passed?)... | |
833 | ||
834 | #if defined(__WINDOWS__) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY)) | |
835 | flag = 1; | |
836 | setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int)); | |
837 | #endif | |
384b4373 | 838 | |
f4ada568 GL |
839 | struct sockaddr *remote; |
840 | size_t len; | |
841 | ||
842 | addr_man.Build(remote, len); | |
843 | ||
844 | if (connect(m_fd, remote, len) != 0) | |
845 | return FALSE; | |
846 | ||
a737331d GL |
847 | // Initializes the background threads ... |
848 | // -------------------------------------- | |
849 | m_internal->InitializeSocket(); | |
384b4373 | 850 | |
a737331d GL |
851 | // Enables bg events. |
852 | // ------------------ | |
f4ada568 GL |
853 | Notify(TRUE); |
854 | ||
855 | m_connected = TRUE; | |
856 | return TRUE; | |
857 | } | |
858 | ||
75ed1d15 | 859 | bool wxSocketClient::WaitOnConnect(long seconds, long microseconds) |
f4ada568 | 860 | { |
75ed1d15 | 861 | int ret = _Wait(seconds, microseconds, REQ_CONNECT | REQ_LOST); |
384b4373 | 862 | |
f4ada568 GL |
863 | if (ret) |
864 | m_connected = TRUE; | |
384b4373 RD |
865 | |
866 | return m_connected; | |
f4ada568 GL |
867 | } |
868 | ||
869 | void wxSocketClient::OnRequest(wxRequestEvent evt) | |
870 | { | |
8c14576d RR |
871 | if (evt == EVT_CONNECT) |
872 | { | |
873 | if (m_connected) | |
874 | { | |
f4ada568 GL |
875 | SetNotify(m_neededreq & ~REQ_CONNECT); |
876 | return; | |
877 | } | |
384b4373 | 878 | m_connected = TRUE; |
f4ada568 | 879 | OldOnNotify(EVT_CONNECT); |
f4ada568 GL |
880 | return; |
881 | } | |
882 | wxSocketBase::OnRequest(evt); | |
883 | } | |
884 | ||
885 | ///////////////////////////////////////////////////////////////// | |
886 | // wxSocketHandler /////////////////////////////////////////////// | |
887 | ///////////////////////////////////////////////////////////////// | |
888 | ||
889 | wxSocketHandler *wxSocketHandler::master = NULL; | |
890 | #if defined(__WINDOWS__) | |
891 | static int win_initialized = 0; | |
892 | #endif | |
893 | ||
894 | // -------------------------------------------------------------- | |
895 | // --------- wxSocketHandler CONSTRUCTOR ------------------------ | |
896 | // -------------------------------------------------------------- | |
f4ada568 GL |
897 | wxSocketHandler::wxSocketHandler() |
898 | { | |
899 | #if defined(__WINDOWS__) | |
8c14576d RR |
900 | if (!win_initialized) |
901 | { | |
f4ada568 GL |
902 | WSADATA wsaData; |
903 | ||
904 | WSAStartup((1 << 8) | 1, &wsaData); | |
905 | win_initialized = 1; | |
906 | } | |
f4ada568 GL |
907 | #endif |
908 | ||
909 | socks = new wxList; | |
910 | ||
911 | #ifndef __WINDOWS__ | |
912 | signal(SIGPIPE, SIG_IGN); | |
913 | #endif | |
914 | } | |
915 | ||
916 | // -------------------------------------------------------------- | |
917 | // --------- wxSocketHandler DESTRUCTOR ------------------------- | |
918 | // -------------------------------------------------------------- | |
919 | wxSocketHandler::~wxSocketHandler() | |
920 | { | |
921 | wxNode *next_node, *node = socks->First(); | |
922 | ||
8c14576d RR |
923 | while (node) |
924 | { | |
f4ada568 GL |
925 | wxSocketBase* sock = (wxSocketBase*)node->Data(); |
926 | ||
927 | delete sock; | |
928 | next_node = node->Next(); | |
929 | delete node; | |
930 | node = next_node; | |
931 | } | |
932 | ||
933 | delete socks; | |
934 | ||
935 | #ifdef __WINDOWS__ | |
f4ada568 GL |
936 | WSACleanup(); |
937 | win_initialized = 0; | |
f4ada568 GL |
938 | #endif |
939 | } | |
940 | ||
941 | // -------------------------------------------------------------- | |
942 | // --------- wxSocketHandler registering functions -------------- | |
943 | // -------------------------------------------------------------- | |
8c14576d | 944 | |
f4ada568 GL |
945 | void wxSocketHandler::Register(wxSocketBase* sock) |
946 | { | |
947 | wxNode *node; | |
948 | ||
8c14576d RR |
949 | for (node = socks->First(); node != NULL; node = node->Next()) |
950 | { | |
f4ada568 GL |
951 | wxSocketBase* s = (wxSocketBase*)node->Data(); |
952 | ||
953 | if (s == sock) | |
954 | return; | |
955 | } | |
956 | ||
8c14576d RR |
957 | if (sock) |
958 | { | |
f4ada568 GL |
959 | socks->Append(sock); |
960 | sock->SetHandler(this); | |
f4ada568 GL |
961 | } |
962 | } | |
963 | ||
964 | void wxSocketHandler::UnRegister(wxSocketBase* sock) | |
965 | { | |
966 | wxNode *node; | |
967 | ||
8c14576d RR |
968 | for (node = socks->First(); node; node = node->Next()) |
969 | { | |
f4ada568 | 970 | wxSocketBase* s = (wxSocketBase*)node->Data(); |
384b4373 | 971 | |
8c14576d RR |
972 | if (s == sock) |
973 | { | |
f4ada568 | 974 | delete node; |
f4ada568 GL |
975 | sock->SetHandler(NULL); |
976 | return; | |
977 | } | |
978 | } | |
979 | } | |
980 | ||
981 | unsigned long wxSocketHandler::Count() const | |
982 | { | |
983 | return socks->Number(); | |
984 | } | |
985 | ||
986 | // -------------------------------------------------------------- | |
987 | // --------- wxSocketHandler "big" wait functions --------------- | |
988 | // -------------------------------------------------------------- | |
f4ada568 GL |
989 | |
990 | int wxSocketHandler::Wait(long seconds, long microseconds) | |
991 | { | |
a737331d GL |
992 | // TODO Needs the completely asynchronous notifier. |
993 | ||
994 | /* | |
f4ada568 GL |
995 | int i; |
996 | int on_wait; | |
f4ada568 | 997 | wxNode *node; |
8c14576d RR |
998 | for (node = socks->First(), i=0; node; node = node->Next(), i++) |
999 | { | |
f4ada568 GL |
1000 | wxSocketBase *sock = (wxSocketBase *)node->Data(); |
1001 | ||
1002 | sock->SaveState(); | |
1003 | ||
1004 | sock->SetupCallbacks(); | |
1005 | ||
1006 | sock->Callback(handler_cbk); | |
1007 | sock->CallbackData((char *)&on_wait); | |
1008 | } | |
1009 | on_wait = 0; | |
1010 | if (seconds != -1) | |
1011 | s_wake.Start((seconds*1000) + (microseconds/1000), TRUE); | |
1012 | ||
1013 | while (!on_wait) | |
1014 | PROCESS_EVENTS(); | |
1015 | ||
8c14576d RR |
1016 | for (node = socks->First(), i=0; node; node = node->Next(), i++) |
1017 | { | |
f4ada568 GL |
1018 | wxSocketBase *sock = (wxSocketBase *)node->Data(); |
1019 | ||
1020 | sock->RestoreState(); | |
1021 | } | |
1022 | ||
1023 | if (on_wait == -2) | |
1024 | return 0; | |
1025 | ||
1026 | return on_wait; | |
a737331d GL |
1027 | */ |
1028 | return 0; | |
f4ada568 GL |
1029 | } |
1030 | ||
1031 | void wxSocketHandler::YieldSock() | |
1032 | { | |
1033 | wxNode *node; | |
1034 | ||
a737331d GL |
1035 | // Nothing to do anymore here except waiting for the queue emptying. |
1036 | for (node = socks->First(); node; node=node->Next()) { | |
f4ada568 GL |
1037 | wxSocketBase *sock = (wxSocketBase *)node->Data(); |
1038 | ||
a737331d | 1039 | sock->m_internal->WaitForEnd(NULL); |
f4ada568 GL |
1040 | } |
1041 | } | |
1042 | ||
1043 | // -------------------------------------------------------------- | |
1044 | // --------- wxSocketHandler: create and register the socket ---- | |
1045 | // -------------------------------------------------------------- | |
1046 | wxSocketServer *wxSocketHandler::CreateServer(wxSockAddress& addr, | |
17dff81c | 1047 | wxSocketBase::wxSockFlags flags) |
f4ada568 GL |
1048 | { |
1049 | wxSocketServer *serv = new wxSocketServer(addr, flags); | |
1050 | ||
1051 | Register(serv); | |
1052 | return serv; | |
1053 | } | |
1054 | ||
1055 | wxSocketClient *wxSocketHandler::CreateClient(wxSocketBase::wxSockFlags flags) | |
1056 | { | |
1057 | wxSocketClient *client = new wxSocketClient(flags); | |
1058 | ||
1059 | Register(client); | |
1060 | return client; | |
1061 | } | |
1062 | ||
8c14576d RR |
1063 | bool wxSocketModule::OnInit() |
1064 | { | |
3b4183d8 GL |
1065 | wxSocketHandler::master = new wxSocketHandler(); |
1066 | return TRUE; | |
1067 | } | |
1068 | ||
8c14576d RR |
1069 | void wxSocketModule::OnExit() |
1070 | { | |
3b4183d8 GL |
1071 | delete wxSocketHandler::master; |
1072 | wxSocketHandler::master = NULL; | |
1073 | } | |
1074 | ||
7f555861 JS |
1075 | #endif |
1076 | // __WXSTUBS__ | |
35a4dab7 GL |
1077 | |
1078 | #endif | |
1079 | // wxUSE_SOCKETS |