]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/gsocket.cpp
Warning fix and source cleaning.
[wxWidgets.git] / src / mac / carbon / gsocket.cpp
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
3 * Name: gsocket.cpp
4 * Copyright: (c) Guilhem Lavaux
5 * Licence: wxWindows Licence
6 * Authors: Guilhem Lavaux,
7 * Guillermo Rodriguez Garcia <guille@iies.es> (maintainer)
8 * Stefan CSomor
9 * Purpose: GSocket main mac file
10 * CVSID: $Id$
11 * -------------------------------------------------------------------------
12 */
13
14 /*
15 * PLEASE don't put C++ comments here - this is a C source file.
16 */
17
18 #ifndef __GSOCKET_STANDALONE__
19 #include "wx/setup.h"
20 #include "wx/platform.h"
21 #endif
22
23 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
24
25 #ifdef __DARWIN__
26 #include <CoreServices/CoreServices.h>
27
28 #else
29 #include <MacHeaders.c>
30 #define OTUNIXERRORS 1
31 #include <OpenTransport.h>
32 #include <OpenTransportProviders.h>
33 #include <OpenTptInternet.h>
34 #endif
35 #if TARGET_CARBON && !defined(OTAssert)
36 #define OTAssert( str , cond ) /* does not exists in Carbon */
37 #endif
38
39 #include <assert.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stddef.h>
46 #include <ctype.h>
47 #include <utime.h>
48
49 /*
50 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
51 * on all unices. INADDR_BROADCAST should be fine to indicate an error.
52 */
53 #ifndef INADDR_BROADCAST
54 #define INADDR_BROADCAST 0xFFFFFFFFUL
55 #endif
56 #ifndef INADDR_NONE
57 #define INADDR_NONE INADDR_BROADCAST
58 #endif
59 #ifndef INADDR_ANY
60 #define INADDR_ANY 0x0UL
61 #endif
62 #ifndef __GSOCKET_STANDALONE__
63
64 #include "wx/mac/macnotfy.h"
65 #include "wx/mac/gsockmac.h"
66 #include "wx/gsocket.h"
67
68 #else
69
70 #include "gsockmac.h"
71 #include "gsocket.h"
72
73 #endif /* __GSOCKET_STANDALONE__ */
74
75 #ifndef ntohl
76 #define ntohl(x) (x)
77 #define ntohs(x) (x)
78 #define htonl(x) (x)
79 #define htons(x) (x)
80 #endif
81
82 void wxCYield() ;
83 #ifdef __WXDEBUG__
84 #define qDebug 1
85 #define qDebug2 1
86 extern pascal void OTDebugStr(const char* str);
87 #endif
88 #ifndef __DARWIN__
89 #include <OTDebug.h>
90 #endif
91 InetSvcRef gInetSvcRef = 0 ;
92 int gOTInited = 0 ;
93 OTNotifyUPP gOTNotifierUPP = NULL ;
94
95 OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode);
96
97 /* Input: ep - endpointref on which to negotiate the option
98 enableReuseIPMode - desired option setting - true/false
99 Return: kOTNoError indicates that the option was successfully negotiated
100 OSStatus is an error if < 0, otherwise, the status field is
101 returned and is > 0.
102
103 IMPORTANT NOTE: The endpoint is assumed to be in synchronous more, otherwise
104 this code will not function as desired
105 */
106
107
108 OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode)
109
110 {
111 UInt8 buf[kOTFourByteOptionSize]; // define buffer for fourByte Option size
112 TOption* opt; // option ptr to make items easier to access
113 TOptMgmt req;
114 TOptMgmt ret;
115 OSStatus err;
116
117 if (!OTIsSynchronous(ep))
118 {
119 return (-1);
120 }
121 opt = (TOption*)buf; // set option ptr to buffer
122 req.opt.buf = buf;
123 req.opt.len = sizeof(buf);
124 req.flags = T_NEGOTIATE; // negotiate for option
125
126 ret.opt.buf = buf;
127 ret.opt.maxlen = kOTFourByteOptionSize;
128
129 opt->level = INET_IP; // dealing with an IP Level function
130 #ifdef __DARWIN__
131 opt->name = kIP_REUSEADDR;
132 #else
133 opt->name = IP_REUSEADDR;
134 #endif
135 opt->len = kOTFourByteOptionSize;
136 opt->status = 0;
137 *(UInt32*)opt->value = enableReuseIPMode; // set the desired option level, true or false
138
139 err = OTOptionManagement(ep, &req, &ret);
140
141 // if no error then return the option status value
142 if (err == kOTNoError)
143 {
144 if (opt->status != T_SUCCESS)
145 err = opt->status;
146 else
147 err = kOTNoError;
148 }
149
150 return err;
151 }
152
153
154 pascal void OTInetEventHandler(void*s, OTEventCode event, OTResult, void *cookie) ;
155 pascal void OTInetEventHandler(void*s, OTEventCode event, OTResult result, void *cookie)
156 {
157 int wakeUp = true ;
158 GSocket* sock = (GSocket*) s ;
159
160 if ( event == kOTSyncIdleEvent )
161 {
162 return ;
163 }
164
165 if ( s )
166 {
167 wxMacAddEvent( sock->m_mac_events , _GSocket_Internal_Proc , event , s , wakeUp ) ;
168 }
169
170 return;
171 }
172
173 static void SetDefaultEndpointModes(EndpointRef ep , void *data )
174 // This routine sets the supplied endpoint into the default
175 // mode used in this application. The specifics are:
176 // blocking, synchronous, and using synch idle events with
177 // the standard YieldingNotifier.
178 {
179 OSStatus junk = kOTNoError ;
180 OTAssert ("SetDefaultEndpointModes:invalid ref", ep != kOTInvalidEndpointRef ) ;
181 junk = OTSetAsynchronous(ep);
182 OTAssert("SetDefaultEndpointModes: Could not set asynchronous", junk == noErr);
183 /*
184 junk = OTSetBlocking(ep);
185 OTAssert("SetDefaultEndpointModes: Could not set blocking", junk == noErr);
186 junk = OTSetSynchronous(ep);
187 OTAssert("SetDefaultEndpointModes: Could not set synchronous", junk == noErr);
188 junk = OTSetBlocking(ep);
189 OTAssert("SetDefaultEndpointModes: Could not set blocking", junk == noErr);
190 */
191 junk = OTInstallNotifier(ep, gOTNotifierUPP, data);
192 OTAssert("SetDefaultEndpointModes: Could not install notifier", junk == noErr);
193 /*
194 junk = OTUseSyncIdleEvents(ep, true);
195 OTAssert("SetDefaultEndpointModes: Could not use sync idle events", junk == noErr);
196 */
197 }
198
199 /* Global initialisers */
200
201 void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable *table)
202 {
203 // do nothing, wxMac doesn't have wxBase-GUI separation yet
204 }
205
206 int GSocket_Init()
207 {
208 return 1;
209 }
210
211 bool GSocket_Verify_Inited() ;
212 bool GSocket_Verify_Inited()
213 {
214 OSStatus err ;
215 #if TARGET_CARBON
216 // Marc Newsam: added the clientcontext variable
217 // however, documentation is unclear how this works
218 OTClientContextPtr clientcontext;
219
220 if ( gInetSvcRef )
221 return true ;
222
223 InitOpenTransportInContext(kInitOTForApplicationMask, &clientcontext);
224 gOTInited = 1 ;
225 gInetSvcRef = OTOpenInternetServicesInContext(kDefaultInternetServicesPath,
226 NULL, &err, clientcontext);
227 #else
228 if ( gInetSvcRef )
229 return true ;
230
231 InitOpenTransport() ;
232 gOTInited = 1 ;
233 gInetSvcRef = OTOpenInternetServices(kDefaultInternetServicesPath, NULL, &err);
234 #endif
235 if ( gInetSvcRef == NULL || err != kOTNoError )
236 {
237 OTAssert("Could not open Inet Services", err == noErr);
238 return false ;
239 }
240 gOTNotifierUPP = NewOTNotifyUPP( OTInetEventHandler ) ;
241 return true ;
242 }
243
244 void GSocket_Cleanup()
245 {
246 if ( gOTInited != 0 )
247 {
248 if ( gInetSvcRef != NULL )
249 OTCloseProvider( gInetSvcRef );
250 #if TARGET_CARBON
251 CloseOpenTransportInContext( NULL ) ;
252 #else
253 CloseOpenTransport() ;
254 #endif
255 if ( gOTNotifierUPP )
256 DisposeOTNotifyUPP( gOTNotifierUPP ) ;
257 }
258 }
259
260 /* Constructors / Destructors for GSocket */
261
262 GSocket::GSocket()
263 {
264 int i;
265
266 m_ok = GSocket_Verify_Inited();
267
268 m_endpoint = NULL ;
269 for (i=0;i<GSOCK_MAX_EVENT;i++)
270 {
271 m_cbacks[i] = NULL;
272 }
273 m_detected = 0;
274 m_local = NULL;
275 m_peer = NULL;
276 m_error = GSOCK_NOERROR;
277 m_server = false;
278 m_stream = true;
279 m_non_blocking = false;
280 m_timeout = 1*1000;
281 /* 10 sec * 1000 millisec */
282 m_takesEvents = true ;
283 m_mac_events = wxMacGetNotifierTable() ;
284 }
285
286 GSocket::~GSocket()
287 {
288 assert(this);
289
290 /* Check that the socket is really shutdowned */
291 if (m_endpoint != kOTInvalidEndpointRef)
292 Shutdown();
293
294
295 /* Destroy private addresses */
296 if (m_local)
297 GAddress_destroy(m_local);
298
299 if (m_peer)
300 GAddress_destroy(m_peer);
301 }
302
303 /* GSocket_Shutdown:
304 * Disallow further read/write operations on this socket, close
305 * the fd and disable all callbacks.
306 */
307 void GSocket::Shutdown()
308 {
309 OSStatus err ;
310 int evt;
311
312 assert(this);
313
314 /* If socket has been created, shutdown it */
315 if (m_endpoint != kOTInvalidEndpointRef )
316 {
317 err = OTSndOrderlyDisconnect( m_endpoint ) ;
318 if ( err != kOTNoError )
319 {
320
321 }
322 err = OTRcvOrderlyDisconnect( m_endpoint ) ;
323 err = OTUnbind( m_endpoint ) ;
324 err = OTCloseProvider( m_endpoint ) ;
325 m_endpoint = kOTInvalidEndpointRef ;
326 }
327
328 /* Disable GUI callbacks */
329 for (evt = 0; evt < GSOCK_MAX_EVENT; evt++)
330 m_cbacks[evt] = NULL;
331
332 m_detected = 0;
333 Disable_Events();
334 wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable() , this ) ;
335 }
336
337
338 /* Address handling */
339
340 /* GSocket_SetLocal:
341 * GSocket_GetLocal:
342 * GSocket_SetPeer:
343 * GSocket_GetPeer:
344 * Set or get the local or peer address for this socket. The 'set'
345 * functions return GSOCK_NOERROR on success, an error code otherwise.
346 * The 'get' functions return a pointer to a GAddress object on success,
347 * or NULL otherwise, in which case they set the error code of the
348 * corresponding GSocket.
349 *
350 * Error codes:
351 * GSOCK_INVSOCK - the socket is not valid.
352 * GSOCK_INVADDR - the address is not valid.
353 */
354 GSocketError GSocket::SetLocal(GAddress *address)
355 {
356 assert(this);
357
358 /* the socket must be initialized, or it must be a server */
359 if ((m_endpoint != kOTInvalidEndpointRef && !m_server))
360 {
361 m_error = GSOCK_INVSOCK;
362 return GSOCK_INVSOCK;
363 }
364
365 /* check address */
366 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
367 {
368 m_error = GSOCK_INVADDR;
369 return GSOCK_INVADDR;
370 }
371
372 if (m_local)
373 GAddress_destroy(m_local);
374
375 m_local = GAddress_copy(address);
376
377 return GSOCK_NOERROR;
378 }
379
380 GSocketError GSocket::SetPeer(GAddress *address)
381 {
382 assert(this);
383
384 /* check address */
385 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
386 {
387 m_error = GSOCK_INVADDR;
388 return GSOCK_INVADDR;
389 }
390
391 if (m_peer)
392 GAddress_destroy(m_peer);
393
394 m_peer = GAddress_copy(address);
395
396 return GSOCK_NOERROR;
397 }
398
399 GAddress *GSocket::GetLocal()
400 {
401 GAddress *address = NULL ;
402 GSocketError err;
403 InetAddress loc ;
404
405 assert(this);
406
407 /* try to get it from the m_local var first */
408 if (m_local)
409 return GAddress_copy(m_local);
410
411 /* else, if the socket is initialized, try getsockname */
412 if (m_endpoint == kOTInvalidEndpointRef)
413 {
414 m_error = GSOCK_INVSOCK;
415 return NULL;
416 }
417
418
419 /* we do not support multihoming with this code at the moment
420 OTGetProtAddress will have to be used then - but we don't have a handy
421 method to use right now
422 */
423 {
424 InetInterfaceInfo info;
425 OTInetGetInterfaceInfo(&info, kDefaultInetInterface);
426 loc.fHost = info.fAddress ;
427 loc.fPort = 0 ;
428 loc.fAddressType = AF_INET ;
429 }
430
431 /* got a valid address from getsockname, create a GAddress object */
432 address = GAddress_new();
433 if (address == NULL)
434 {
435 m_error = GSOCK_MEMERR;
436 return NULL;
437 }
438
439 err = _GAddress_translate_from(address, &loc);
440 if (err != GSOCK_NOERROR)
441 {
442 GAddress_destroy(address);
443 m_error = err;
444 return NULL;
445 }
446
447 return address;
448 }
449
450 GAddress *GSocket::GetPeer()
451 {
452 assert(this);
453
454 /* try to get it from the m_peer var */
455 if (m_peer)
456 return GAddress_copy(m_peer);
457
458 return NULL;
459 }
460
461 /* Server specific parts */
462
463 /* GSocket_SetServer:
464 * Sets up this socket as a server. The local address must have been
465 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
466 * Returns GSOCK_NOERROR on success, one of the following otherwise:
467 *
468 * Error codes:
469 * GSOCK_INVSOCK - the socket is in use.
470 * GSOCK_INVADDR - the local address has not been set.
471 * GSOCK_IOERR - low-level error.
472 */
473 GSocketError GSocket::SetServer()
474 {
475 assert(this);
476
477 /* must not be in use */
478 if (m_endpoint != kOTInvalidEndpointRef )
479 {
480 m_error = GSOCK_INVSOCK;
481 return GSOCK_INVSOCK;
482 }
483
484 /* the local addr must have been set */
485 if (!m_local)
486 {
487 m_error = GSOCK_INVADDR;
488 return GSOCK_INVADDR;
489 }
490
491 /* Initialize all fields */
492 m_stream = true;
493 m_server = true;
494 m_oriented = true;
495
496 // TODO
497 #if 0
498 /* Create the socket */
499 m_endpoint = socket(m_local->m_realfamily, SOCK_STREAM, 0);
500 socket_set_ref( m_endpoint , (unsigned long) &gMacNetEvents , (unsigned long) this ) ;
501 if (m_endpoint == kOTInvalidEndpointRef)
502 {
503 m_error = GSOCK_IOERR;
504 return GSOCK_IOERR;
505 }
506
507 ioctl(m_endpoint, FIONBIO, &arg);
508 Enable_Events();
509
510 /* Bind to the local address,
511 * retrieve the actual address bound,
512 * and listen up to 5 connections.
513 */
514 if ((bind(m_endpoint, m_local->m_addr, m_local->m_len) != 0) ||
515 (getsockname(m_endpoint,
516 m_local->m_addr,
517 (SOCKLEN_T *) &m_local->m_len) != 0) ||
518 (listen(m_endpoint, 5) != 0))
519 {
520 close(m_endpoint);
521 m_endpoint = -1;
522 m_error = GSOCK_IOERR;
523 return GSOCK_IOERR;
524 }
525 #endif
526 return GSOCK_NOERROR;
527 }
528
529 /* GSocket_WaitConnection:
530 * Waits for an incoming client connection. Returns a pointer to
531 * a GSocket object, or NULL if there was an error, in which case
532 * the last error field will be updated for the calling GSocket.
533 *
534 * Error codes (set in the calling GSocket)
535 * GSOCK_INVSOCK - the socket is not valid or not a server.
536 * GSOCK_TIMEDOUT - timeout, no incoming connections.
537 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
538 * GSOCK_MEMERR - couldn't allocate memory.
539 * GSOCK_IOERR - low-level error.
540 */
541 GSocket *GSocket::WaitConnection()
542 {
543 GSocket *connection = NULL ;
544
545 assert(this);
546
547 /* Reenable CONNECTION events */
548 m_detected &= ~GSOCK_CONNECTION_FLAG;
549
550 /* If the socket has already been created, we exit immediately */
551 if (m_endpoint == kOTInvalidEndpointRef || !m_server)
552 {
553 m_error = GSOCK_INVSOCK;
554 return NULL;
555 }
556
557 /* Create a GSocket object for the new connection */
558 connection = GSocket_new();
559
560 if (!connection)
561 {
562 m_error = GSOCK_MEMERR;
563 return NULL;
564 }
565
566 /* Wait for a connection (with timeout) */
567 if (Input_Timeout() == GSOCK_TIMEDOUT)
568 {
569 delete connection;
570 /* m_error set by _GSocket_Input_Timeout */
571 return NULL;
572 }
573
574 // TODO
575 #if 0
576 connection->m_endpoint = accept(m_endpoint, &from, (SOCKLEN_T *) &fromlen);
577 #endif
578
579 if (connection->m_endpoint == kOTInvalidEndpointRef )
580 {
581 if (errno == EWOULDBLOCK)
582 m_error = GSOCK_WOULDBLOCK;
583 else
584 m_error = GSOCK_IOERR;
585
586 delete connection;
587 return NULL;
588 }
589
590 /* Initialize all fields */
591 connection->m_server = false;
592 connection->m_stream = true;
593 connection->m_oriented = true;
594
595 /* Setup the peer address field */
596 connection->m_peer = GAddress_new();
597 if (!connection->m_peer)
598 {
599 delete connection;
600 m_error = GSOCK_MEMERR;
601 return NULL;
602 }
603 // TODO
604 #if 0
605 err = _GAddress_translate_from(connection->m_peer, &from, fromlen);
606 if (err != GSOCK_NOERROR)
607 {
608 GAddress_destroy(connection->m_peer);
609 GSocket_destroy(connection);
610 m_error = err;
611 return NULL;
612 }
613
614 ioctl(connection->m_endpoint, FIONBIO, &arg);
615 #endif
616 connection->Enable_Events();
617
618 return connection;
619 }
620
621 /* Datagram sockets */
622
623 /* GSocket_SetNonOriented:
624 * Sets up this socket as a non-connection oriented (datagram) socket.
625 * Before using this function, the local address must have been set
626 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
627 * on success, or one of the following otherwise.
628 *
629 * Error codes:
630 * GSOCK_INVSOCK - the socket is in use.
631 * GSOCK_INVADDR - the local address has not been set.
632 * GSOCK_IOERR - low-level error.
633 */
634 GSocketError GSocket::SetNonOriented()
635 {
636 assert(this);
637
638 if (m_endpoint != kOTInvalidEndpointRef )
639 {
640 m_error = GSOCK_INVSOCK;
641 return GSOCK_INVSOCK;
642 }
643
644 if (!m_local)
645 {
646 m_error = GSOCK_INVADDR;
647 return GSOCK_INVADDR;
648 }
649
650 /* Initialize all fields */
651 m_stream = false;
652 m_server = false;
653 m_oriented = false;
654
655 /* Create the socket */
656
657 // TODO
658 #if 0
659 m_endpoint = socket(m_local->m_realfamily, SOCK_DGRAM, 0);
660 socket_set_ref( m_endpoint , (unsigned long) &gMacNetEvents , (unsigned long) this ) ;
661 #endif
662 if (m_endpoint == kOTInvalidEndpointRef )
663 {
664 m_error = GSOCK_IOERR;
665 return GSOCK_IOERR;
666 }
667
668 // TODO
669 #if 0
670 ioctl(m_endpoint, FIONBIO, &arg);
671 #endif
672 Enable_Events();
673
674 /* Bind to the local address,
675 * and retrieve the actual address bound.
676 */
677 // TODO
678 #if 0
679 if ((bind(m_endpoint, m_local->m_addr, m_local->m_len) != 0) ||
680 (getsockname(m_endpoint,
681 m_local->m_addr,
682 (SOCKLEN_T *) &m_local->m_len) != 0))
683 {
684 close(m_endpoint);
685 m_endpoint = -1;
686 m_error = GSOCK_IOERR;
687 return GSOCK_IOERR;
688 }
689 #endif
690 return GSOCK_NOERROR;
691 }
692
693 /* Client specific parts */
694
695 /* GSocket_Connect:
696 * For stream (connection oriented) sockets, GSocket_Connect() tries
697 * to establish a client connection to a server using the peer address
698 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
699 * connection has been successfully established, or one of the error
700 * codes listed below. Note that for nonblocking sockets, a return
701 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
702 * request can be completed later; you should use GSocket_Select()
703 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
704 * corresponding asynchronous events.
705 *
706 * For datagram (non connection oriented) sockets, GSocket_Connect()
707 * just sets the peer address established with GSocket_SetPeer() as
708 * default destination.
709 *
710 * Error codes:
711 * GSOCK_INVSOCK - the socket is in use or not valid.
712 * GSOCK_INVADDR - the peer address has not been established.
713 * GSOCK_TIMEDOUT - timeout, the connection failed.
714 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
715 * GSOCK_MEMERR - couldn't allocate memory.
716 * GSOCK_IOERR - low-level error.
717 */
718 GSocketError GSocket::Connect(GSocketStream stream)
719 {
720 InetAddress addr ;
721 TEndpointInfo info;
722 OSStatus err = kOTNoError;
723 TCall peer ;
724
725 assert(this);
726
727 /* Enable CONNECTION events (needed for nonblocking connections) */
728 m_detected &= ~GSOCK_CONNECTION_FLAG;
729
730 if (m_endpoint != kOTInvalidEndpointRef )
731 {
732 m_error = GSOCK_INVSOCK;
733 return GSOCK_INVSOCK;
734 }
735
736 if (!m_peer)
737 {
738 m_error = GSOCK_INVADDR;
739 return GSOCK_INVADDR;
740 }
741
742 /* Streamed or dgram socket? */
743 m_stream = (stream == GSOCK_STREAMED);
744 m_oriented = true;
745 m_server = false;
746
747 /* Create the socket */
748 #if TARGET_CARBON
749 m_endpoint =
750 OTOpenEndpointInContext( OTCreateConfiguration( kTCPName) , 0 , &info , &err , NULL ) ;
751 #else
752 m_endpoint =
753 OTOpenEndpoint( OTCreateConfiguration( kTCPName) , 0 , &info , &err ) ;
754 #endif
755 if ( m_endpoint == kOTInvalidEndpointRef || err != kOTNoError )
756 {
757 m_endpoint = kOTInvalidEndpointRef ;
758 m_error = GSOCK_IOERR;
759 return GSOCK_IOERR;
760 }
761 err = OTBind( m_endpoint , nil , nil ) ;
762 if ( err != kOTNoError )
763 {
764 return GSOCK_IOERR;
765 }
766 SetDefaultEndpointModes( m_endpoint , this ) ;
767 // TODO
768 #if 0
769 ioctl(m_endpoint, FIONBIO, &arg);
770 #endif
771 Enable_Events();
772
773 _GAddress_translate_to( m_peer , &addr ) ;
774 memset( &peer , 0 , sizeof( TCall ) ) ;
775 peer.addr.len = sizeof( InetAddress ) ;
776 peer.addr.buf = (unsigned char*) &addr ;
777 err = OTConnect( m_endpoint , &peer , nil ) ;
778 if ( err != noErr )
779 {
780 /* If connect failed with EINPROGRESS and the GSocket object
781 * is in blocking mode, we select() for the specified timeout
782 * checking for writability to see if the connection request
783 * completes.
784 */
785
786 if ((err == kOTNoDataErr ) && (!m_non_blocking))
787 {
788 if (Output_Timeout() == GSOCK_TIMEDOUT)
789 {
790 OTSndOrderlyDisconnect( m_endpoint ) ;
791 m_endpoint = kOTInvalidEndpointRef ;
792 /* m_error is set in _GSocket_Output_Timeout */
793 return GSOCK_TIMEDOUT;
794 }
795 else
796 {
797 /*
798 int error;
799 SOCKLEN_T len = sizeof(error);
800
801 getsockopt(m_endpoint, SOL_SOCKET, SO_ERROR, (void*) &error, &len);
802
803 if (!error)
804 */
805 return GSOCK_NOERROR;
806 }
807 }
808
809 /* If connect failed with EINPROGRESS and the GSocket object
810 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
811 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
812 * this way if the connection completes, a GSOCK_CONNECTION
813 * event will be generated, if enabled.
814 */
815 if ((err == kOTNoDataErr) && (m_non_blocking))
816 {
817 m_error = GSOCK_WOULDBLOCK;
818 return GSOCK_WOULDBLOCK;
819 }
820
821 /* If connect failed with an error other than EINPROGRESS,
822 * then the call to GSocket_Connect has failed.
823 */
824 OTSndOrderlyDisconnect( m_endpoint ) ;
825
826 m_endpoint = kOTInvalidEndpointRef ;
827 m_error = GSOCK_IOERR;
828 return GSOCK_IOERR;
829 }
830 // OTInetEventHandler(this, T_CONNECT , kOTNoError , NULL ) ;
831 return GSOCK_NOERROR;
832 }
833
834 /* Generic IO */
835
836 /* Like recv(), send(), ... */
837 int GSocket::Read(char *buffer, int size)
838 {
839 int ret = 0 ;
840
841 assert(this);
842
843 /* Reenable INPUT events */
844 m_detected &= ~GSOCK_INPUT_FLAG;
845
846 if (m_endpoint == kOTInvalidEndpointRef || m_server)
847 {
848 m_error = GSOCK_INVSOCK;
849 return -1;
850 }
851
852 /* If the socket is blocking, wait for data (with a timeout) */
853 if (Input_Timeout() == GSOCK_TIMEDOUT)
854 return -1;
855
856 /* Read the data */
857 if (m_stream)
858 ret = Recv_Stream(buffer, size);
859 else
860 ret = Recv_Dgram(buffer, size);
861
862 if (ret == -1)
863 {
864 if (errno == EWOULDBLOCK)
865 m_error = GSOCK_WOULDBLOCK;
866 else
867 m_error = GSOCK_IOERR;
868 }
869
870 return ret;
871 }
872
873 int GSocket::Write(const char *buffer, int size)
874 {
875 int ret;
876
877 assert(this);
878
879 if (m_endpoint == kOTInvalidEndpointRef || m_server)
880 {
881 m_error = GSOCK_INVSOCK;
882 return -1;
883 }
884
885 /* If the socket is blocking, wait for writability (with a timeout) */
886 if (Output_Timeout() == GSOCK_TIMEDOUT)
887 return -1;
888
889 /* Write the data */
890 if (m_stream)
891 ret = Send_Stream(buffer, size);
892 else
893 ret = Send_Dgram(buffer, size);
894
895 if (ret == -1)
896 {
897 if (errno == EWOULDBLOCK)
898 m_error = GSOCK_WOULDBLOCK;
899 else
900 m_error = GSOCK_IOERR;
901
902 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
903 * in MSW). Once the first OUTPUT event is received, users can assume
904 * that the socket is writable until a read operation fails. Only then
905 * will further OUTPUT events be posted.
906 */
907 m_detected &= ~GSOCK_OUTPUT_FLAG;
908 return -1;
909 }
910
911 return ret;
912 }
913
914 /* GSocket_Select:
915 * Polls the socket to determine its status. This function will
916 * check for the events specified in the 'flags' parameter, and
917 * it will return a mask indicating which operations can be
918 * performed. This function won't block, regardless of the
919 * mode (blocking | nonblocking) of the socket.
920 */
921 GSocketEventFlags GSocket::Select(GSocketEventFlags flags)
922 {
923 assert(this);
924 wxMacProcessNotifierEvents() ;
925 /*
926 state = OTGetEndpointState(m_endpoint);
927
928 if ( ( flags & GSOCK_INPUT_FLAG ) && ! ( m_detected & GSOCK_INPUT_FLAG ) )
929 {
930 size_t sz = 0 ;
931 OTCountDataBytes( m_endpoint , &sz ) ;
932 if ( state == T_INCON || sz > 0 )
933 {
934 m_detected |= GSOCK_INPUT_FLAG ;
935 (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]);
936 }
937 }
938 if ( ( flags & GSOCK_INPUT_FLAG ) && ! ( m_detected & GSOCK_OUTPUT_FLAG ) )
939 {
940 if ( state == T_DATAXFER || state == T_INREL )
941 {
942 m_detected |=GSOCK_OUTPUT_FLAG ;
943 (m_cbacks[GSOCK_OUTPUT])(this, GSOCK_OUTPUT, m_data[GSOCK_OUTPUT]);
944 }
945 }
946 */
947 return ( flags & m_detected ) ;
948 }
949
950 /* Flags */
951
952 /* GSocket_SetNonBlocking:
953 * Sets the socket to non-blocking mode. All IO calls will return
954 * immediately.
955 */
956 void GSocket::SetNonBlocking(bool non_block)
957 {
958 assert(this);
959
960 m_non_blocking = non_block;
961 }
962
963 /* GSocket_SetTimeout:
964 * Sets the timeout for blocking calls. Time is expressed in
965 * milliseconds.
966 */
967 void GSocket::SetTimeout(unsigned long millisec)
968 {
969 assert(this);
970
971 // this is usually set too high and we have not yet been able to detect a closed
972 // stream, thus we leave the 10 sec timeout
973 // m_timeout = millisec;
974 }
975
976 /* GSocket_GetError:
977 * Returns the last error which occurred for this socket. Note that successful
978 * operations do not clear this back to GSOCK_NOERROR, so use it only
979 * after an error.
980 */
981 GSocketError WXDLLIMPEXP_NET GSocket::GetError()
982 {
983 assert(this);
984
985 return m_error;
986 }
987
988 /* Callbacks */
989
990 /* GSOCK_INPUT:
991 * There is data to be read in the input buffer. If, after a read
992 * operation, there is still data available, the callback function will
993 * be called again.
994 * GSOCK_OUTPUT:
995 * The socket is available for writing. That is, the next write call
996 * won't block. This event is generated only once, when the connection is
997 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
998 * when the output buffer empties again. This means that the app should
999 * assume that it can write since the first OUTPUT event, and no more
1000 * OUTPUT events will be generated unless an error occurs.
1001 * GSOCK_CONNECTION:
1002 * Connection successfully established, for client sockets, or incoming
1003 * client connection, for server sockets. Wait for this event (also watch
1004 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1005 * GSOCK_LOST:
1006 * The connection is lost (or a connection request failed); this could
1007 * be due to a failure, or due to the peer closing it gracefully.
1008 */
1009
1010 /* GSocket_SetCallback:
1011 * Enables the callbacks specified by 'flags'. Note that 'flags'
1012 * may be a combination of flags OR'ed toghether, so the same
1013 * callback function can be made to accept different events.
1014 * The callback function must have the following prototype:
1015 *
1016 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1017 */
1018 void GSocket::SetCallback(GSocketEventFlags flags,
1019 GSocketCallback callback, char *cdata)
1020 {
1021 int count;
1022
1023 assert(this);
1024
1025 for (count = 0; count < GSOCK_MAX_EVENT; count++)
1026 {
1027 if ((flags & (1 << count)) != 0)
1028 {
1029 m_cbacks[count] = callback;
1030 m_data[count] = cdata;
1031 }
1032 }
1033 }
1034
1035 /* GSocket_UnsetCallback:
1036 * Disables all callbacks specified by 'flags', which may be a
1037 * combination of flags OR'ed toghether.
1038 */
1039 void GSocket::UnsetCallback(GSocketEventFlags flags)
1040 {
1041 int count;
1042
1043 assert(this);
1044
1045 for (count = 0; count < GSOCK_MAX_EVENT; count++)
1046 {
1047 if ((flags & (1 << count)) != 0)
1048 {
1049 m_cbacks[count] = NULL;
1050 m_data[count] = NULL;
1051 }
1052 }
1053 }
1054
1055
1056 #define CALL_CALLBACK(socket, event) { \
1057 _GSocket_Disable(socket, event); \
1058 if (socket->m_cbacks[event]) \
1059 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1060 }
1061
1062 int GSocket::Recv_Stream(char *buffer, int size)
1063 {
1064 OTFlags flags ;
1065 OTResult res ;
1066 OTByteCount sz = 0 ;
1067
1068 OTCountDataBytes( m_endpoint , &sz ) ;
1069 if ( size > (int)sz )
1070 size = sz ;
1071 res = OTRcv( m_endpoint , buffer , size , &flags ) ;
1072 if ( res < 0 )
1073 {
1074 return -1 ;
1075 }
1076
1077 // we simulate another read event if there are still bytes
1078 if ( m_takesEvents )
1079 {
1080 OTByteCount sz = 0 ;
1081 OTCountDataBytes( m_endpoint , &sz ) ;
1082 if ( sz > 0 )
1083 {
1084 m_detected |= GSOCK_INPUT_FLAG ;
1085 (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]);
1086 }
1087 }
1088 return res ;
1089 }
1090
1091 int GSocket::Recv_Dgram(char *buffer, int size)
1092 {
1093 // TODO
1094 int ret = -1;
1095 #if 0
1096 struct sockaddr from;
1097 SOCKLEN_T fromlen = sizeof(from);
1098 GSocketError err;
1099
1100 fromlen = sizeof(from);
1101
1102 ret = recvfrom(m_endpoint, buffer, size, 0, &from, (SOCKLEN_T *) &fromlen);
1103
1104 if (ret == -1)
1105 return -1;
1106
1107 /* Translate a system address into a GSocket address */
1108 if (!m_peer)
1109 {
1110 m_peer = GAddress_new();
1111 if (!m_peer)
1112 {
1113 m_error = GSOCK_MEMERR;
1114 return -1;
1115 }
1116 }
1117 err = _GAddress_translate_from(m_peer, &from, fromlen);
1118 if (err != GSOCK_NOERROR)
1119 {
1120 GAddress_destroy(m_peer);
1121 m_peer = NULL;
1122 m_error = err;
1123 return -1;
1124 }
1125 #endif
1126 return ret;
1127 }
1128
1129 int GSocket::Send_Stream(const char *buffer, int size)
1130 {
1131 OTFlags flags = 0 ;
1132 OTResult res ;
1133
1134 res = OTSnd( m_endpoint , (void*) buffer , size , flags ) ;
1135 return res ;
1136 }
1137
1138 int GSocket::Send_Dgram(const char *buffer, int size)
1139 {
1140 int ret = -1 ;
1141 // TODO
1142 #if 0
1143 struct sockaddr *addr;
1144 int len ;
1145 GSocketError err;
1146
1147 if (!m_peer)
1148 {
1149 m_error = GSOCK_INVADDR;
1150 return -1;
1151 }
1152
1153 err = _GAddress_translate_to(m_peer, &addr, &len);
1154 if (err != GSOCK_NOERROR)
1155 {
1156 m_error = err;
1157 return -1;
1158 }
1159
1160 ret = sendto(m_endpoint, buffer, size, 0, addr, len);
1161
1162 /* Frees memory allocated from _GAddress_translate_to */
1163 free(addr);
1164 #endif
1165 return ret;
1166 }
1167
1168 /* Compatibility functions for GSocket */
1169 GSocket *GSocket_new(void)
1170 {
1171 GSocket *newsocket = new GSocket();
1172 if(newsocket->IsOk())
1173 return newsocket;
1174 delete newsocket;
1175 return NULL;
1176 }
1177
1178
1179 /*
1180 * -------------------------------------------------------------------------
1181 * GAddress
1182 * -------------------------------------------------------------------------
1183 */
1184
1185 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1186 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1187 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1188 */
1189 #define CHECK_ADDRESS(address, family, retval) \
1190 { \
1191 if (address->m_family == GSOCK_NOFAMILY) \
1192 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1193 return address->m_error; \
1194 if (address->m_family != GSOCK_##family) \
1195 { \
1196 address->m_error = GSOCK_INVADDR; \
1197 return retval; \
1198 } \
1199 }
1200
1201 GAddress *GAddress_new()
1202 {
1203 GAddress *address;
1204
1205 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1206 return NULL;
1207
1208 address->m_family = GSOCK_NOFAMILY;
1209 address->m_host = INADDR_NONE ;
1210 address->m_port = 0 ;
1211
1212 return address;
1213 }
1214
1215 GAddress *GAddress_copy(GAddress *address)
1216 {
1217 GAddress *addr2;
1218
1219 assert(address != NULL);
1220
1221 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1222 return NULL;
1223
1224 memcpy(addr2, address, sizeof(GAddress));
1225 return addr2;
1226 }
1227
1228 void GAddress_destroy(GAddress *address)
1229 {
1230 assert(address != NULL);
1231
1232 free(address);
1233 }
1234
1235 void GAddress_SetFamily(GAddress *address, GAddressType type)
1236 {
1237 assert(address != NULL);
1238
1239 address->m_family = type;
1240 }
1241
1242 GAddressType GAddress_GetFamily(GAddress *address)
1243 {
1244 assert(address != NULL);
1245
1246 return address->m_family;
1247 }
1248
1249 GSocketError _GAddress_translate_from(GAddress *address,
1250 InetAddress *addr)
1251 {
1252 switch (addr->fAddressType)
1253 {
1254 case AF_INET:
1255 address->m_family = GSOCK_INET;
1256 break;
1257 #ifdef AF_INET6
1258 case AF_INET6:
1259 address->m_family = GSOCK_INET6;
1260 break;
1261 #endif
1262 default:
1263 {
1264 address->m_error = GSOCK_INVOP;
1265 return GSOCK_INVOP;
1266 }
1267 }
1268 address->m_host = addr->fHost ;
1269 address->m_port = addr->fPort ;
1270 return GSOCK_NOERROR;
1271 }
1272
1273 GSocketError _GAddress_translate_to(GAddress *address,
1274 InetAddress *addr)
1275 {
1276 if ( !GSocket_Verify_Inited() )
1277 return GSOCK_IOERR ;
1278 memset(addr, 0 , sizeof(struct InetAddress));
1279 OTInitInetAddress( addr , address->m_port , address->m_host ) ;
1280 return GSOCK_NOERROR;
1281 }
1282
1283 /*
1284 * -------------------------------------------------------------------------
1285 * Internet address family
1286 * -------------------------------------------------------------------------
1287 */
1288
1289 GSocketError _GAddress_Init_INET(GAddress *address)
1290 {
1291 address->m_family = GSOCK_INET;
1292 address->m_host = kOTAnyInetAddress ;
1293
1294 return GSOCK_NOERROR;
1295 }
1296
1297 GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
1298 {
1299 InetHostInfo hinfo ;
1300 OSStatus ret ;
1301
1302 if ( !GSocket_Verify_Inited() )
1303 return GSOCK_IOERR ;
1304
1305 assert(address != NULL);
1306
1307 CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
1308 ret = OTInetStringToAddress( gInetSvcRef , (char*) hostname , &hinfo ) ;
1309 if ( ret != kOTNoError )
1310 {
1311 address->m_host = INADDR_NONE ;
1312 address->m_error = GSOCK_NOHOST;
1313 return GSOCK_NOHOST;
1314 }
1315 address->m_host = hinfo.addrs[0] ;
1316 return GSOCK_NOERROR;
1317 }
1318
1319 GSocketError GAddress_INET_SetAnyAddress(GAddress *address)
1320 {
1321 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
1322 }
1323
1324 GSocketError GAddress_INET_SetHostAddress(GAddress *address,
1325 unsigned long hostaddr)
1326 {
1327 assert(address != NULL);
1328
1329 CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
1330
1331 address->m_host = htonl(hostaddr) ;
1332
1333 return GSOCK_NOERROR;
1334 }
1335
1336 struct service_entry
1337 {
1338 const char * name ;
1339 unsigned short port ;
1340 const char * protocol ;
1341 } ;
1342 typedef struct service_entry service_entry ;
1343
1344 service_entry gServices[] =
1345 {
1346 { "http" , 80 , "tcp" }
1347 } ;
1348
1349 GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
1350 const char *protocol)
1351 {
1352 size_t i ;
1353
1354 assert(address != NULL);
1355 CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
1356
1357 if (!port)
1358 {
1359 address->m_error = GSOCK_INVPORT;
1360 return GSOCK_INVPORT;
1361 }
1362 for ( i = 0 ; i < sizeof( gServices) / sizeof( service_entry ) ; ++i )
1363 {
1364 if ( strcmp( port , gServices[i].name ) == 0 )
1365 {
1366 if ( protocol == NULL || strcmp( protocol , gServices[i].protocol ) )
1367 {
1368 address->m_port = gServices[i].port ;
1369 return GSOCK_NOERROR;
1370 }
1371 }
1372 }
1373
1374 if (isdigit(port[0]))
1375 {
1376 address->m_port = atoi(port);
1377 return GSOCK_NOERROR;
1378 }
1379
1380 address->m_error = GSOCK_INVPORT;
1381 return GSOCK_INVPORT;
1382 }
1383
1384 GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
1385 {
1386 assert(address != NULL);
1387 CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
1388 address->m_port = port ;
1389
1390 return GSOCK_NOERROR;
1391 }
1392
1393 GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1394 {
1395 InetDomainName name ;
1396 if ( !GSocket_Verify_Inited() )
1397 return GSOCK_IOERR ;
1398
1399 assert(address != NULL);
1400 CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
1401
1402 OTInetAddressToName( gInetSvcRef , address->m_host , name ) ;
1403 strncpy( hostname , name , sbuf ) ;
1404 return GSOCK_NOERROR;
1405 }
1406
1407 unsigned long GAddress_INET_GetHostAddress(GAddress *address)
1408 {
1409 assert(address != NULL);
1410 CHECK_ADDRESS(address, INET, 0);
1411
1412 return ntohl(address->m_host);
1413 }
1414
1415 unsigned short GAddress_INET_GetPort(GAddress *address)
1416 {
1417 assert(address != NULL);
1418 CHECK_ADDRESS(address, INET, 0);
1419
1420 return address->m_port;
1421 }
1422
1423 void GSocket::Enable_Events()
1424 {
1425 if ( m_takesEvents )
1426 return ;
1427
1428 {
1429 OTResult state ;
1430 m_takesEvents = true ;
1431 state = OTGetEndpointState(m_endpoint);
1432
1433 {
1434 OTByteCount sz = 0 ;
1435 OTCountDataBytes( m_endpoint , &sz ) ;
1436 if ( state == T_INCON || sz > 0 )
1437 {
1438 m_detected |= GSOCK_INPUT_FLAG ;
1439 (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]);
1440 }
1441 }
1442 {
1443 if ( state == T_DATAXFER || state == T_INREL )
1444 {
1445 m_detected |=GSOCK_OUTPUT_FLAG ;
1446 (m_cbacks[GSOCK_OUTPUT])(this, GSOCK_OUTPUT, m_data[GSOCK_OUTPUT]);
1447 }
1448 }
1449 }
1450 }
1451
1452 void GSocket::Disable_Events()
1453 {
1454 m_takesEvents = false ;
1455 }
1456
1457 /* _GSocket_Input_Timeout:
1458 * For blocking sockets, wait until data is available or
1459 * until timeout ellapses.
1460 */
1461 GSocketError GSocket::Input_Timeout()
1462 {
1463 if ( !m_non_blocking )
1464 {
1465 UnsignedWide now , start ;
1466 bool formerTakesEvents = m_takesEvents ;
1467 Microseconds(&start);
1468 now = start ;
1469 m_takesEvents = false ;
1470
1471 while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < m_timeout * 1000.0 )
1472 {
1473 OTResult state ;
1474 OTByteCount sz = 0 ;
1475 state = OTGetEndpointState(m_endpoint);
1476
1477 OTCountDataBytes( m_endpoint , &sz ) ;
1478 if ( state == T_INCON || sz > 0 )
1479 {
1480 m_takesEvents = formerTakesEvents ;
1481 return GSOCK_NOERROR;
1482 }
1483 Microseconds(&now);
1484 }
1485 m_takesEvents = formerTakesEvents ;
1486 m_error = GSOCK_TIMEDOUT;
1487 return GSOCK_TIMEDOUT;
1488 }
1489 return GSOCK_NOERROR;
1490 }
1491
1492 /* _GSocket_Output_Timeout:
1493 * For blocking sockets, wait until data can be sent without
1494 * blocking or until timeout ellapses.
1495 */
1496 GSocketError GSocket::Output_Timeout()
1497 {
1498 if ( !m_non_blocking )
1499 {
1500 UnsignedWide now , start ;
1501 bool formerTakesEvents = m_takesEvents ;
1502 Microseconds(&start);
1503 now = start ;
1504 m_takesEvents = false ;
1505
1506 while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < m_timeout * 1000.0 )
1507 {
1508 OTResult state ;
1509 state = OTGetEndpointState(m_endpoint);
1510
1511 if ( state == T_DATAXFER || state == T_INREL )
1512 {
1513 m_takesEvents = formerTakesEvents ;
1514 return GSOCK_NOERROR;
1515 }
1516 Microseconds(&now);
1517 }
1518 m_takesEvents = formerTakesEvents ;
1519 m_error = GSOCK_TIMEDOUT;
1520 return GSOCK_TIMEDOUT;
1521 }
1522 return GSOCK_NOERROR;
1523 }
1524
1525 /* GSOCK_INPUT:
1526 * There is data to be read in the input buffer. If, after a read
1527 * operation, there is still data available, the callback function will
1528 * be called again.
1529 * GSOCK_OUTPUT:
1530 * The socket is available for writing. That is, the next write call
1531 * won't block. This event is generated only once, when the connection is
1532 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
1533 * when the output buffer empties again. This means that the app should
1534 * assume that it can write since the first OUTPUT event, and no more
1535 * OUTPUT events will be generated unless an error occurs.
1536 * GSOCK_CONNECTION:
1537 * Connection successfully established, for client sockets, or incoming
1538 * client connection, for server sockets. Wait for this event (also watch
1539 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1540 * GSOCK_LOST:
1541 * The connection is lost (or a connection request failed); this could
1542 * be due to a failure, or due to the peer closing it gracefully.
1543 */
1544
1545 void _GSocket_Internal_Proc(unsigned long e , void* d )
1546 {
1547
1548 GSocket * socket = (GSocket*) d ;
1549 OTEventCode ev = (OTEventCode) e ;
1550 GSocketEvent event;
1551 GSocketEvent event2;
1552 GSocketCallback cback;
1553 char *data;
1554 GSocketCallback cback2;
1555 char *data2;
1556
1557 if ( !socket )
1558 return ;
1559 event = GSOCK_MAX_EVENT ;
1560 event2 = GSOCK_MAX_EVENT ;
1561 cback = NULL;
1562 data = NULL;
1563 cback2 = NULL;
1564 data2 = NULL;
1565
1566 /* Check that the socket still exists (it has not been
1567 * destroyed) and for safety, check that the m_endpoint field
1568 * is what we expect it to be.
1569 */
1570 if ((socket != NULL) && (socket->m_takesEvents))
1571 {
1572 switch (ev)
1573 {
1574 case T_LISTEN :
1575 event = GSOCK_CONNECTION ;
1576 break ;
1577 case T_CONNECT :
1578 event = GSOCK_CONNECTION ;
1579 event2 = GSOCK_OUTPUT ;
1580 {
1581 TCall retCall;
1582
1583 retCall.addr.buf = NULL;
1584 retCall.addr.maxlen = 0;
1585 retCall.opt.buf = NULL;
1586 retCall.opt.maxlen = 0;
1587 retCall.udata.buf = NULL;
1588 retCall.udata.maxlen= 0;
1589 OTRcvConnect( socket->m_endpoint , &retCall ) ;
1590 }
1591 break ;
1592 case T_DISCONNECT :
1593 event = GSOCK_LOST ;
1594 break ;
1595 case T_GODATA :
1596 case T_GOEXDATA :
1597 event = GSOCK_OUTPUT ;
1598 break ;
1599 case T_DATA :
1600 event = GSOCK_INPUT ;
1601 break ;
1602 case T_EXDATA :
1603 event = GSOCK_INPUT ;
1604 break ;
1605 }
1606 if (event != GSOCK_MAX_EVENT)
1607 {
1608 cback = socket->m_cbacks[event];
1609 data = socket->m_data[event];
1610
1611 if (event == GSOCK_LOST)
1612 socket->m_detected = GSOCK_LOST_FLAG;
1613 else
1614 socket->m_detected |= (1 << event);
1615 }
1616 if (event2 != GSOCK_MAX_EVENT)
1617 {
1618 cback2 = socket->m_cbacks[event2];
1619 data2 = socket->m_data[event2];
1620
1621 if (event2 == GSOCK_LOST)
1622 socket->m_detected = GSOCK_LOST_FLAG;
1623 else
1624 socket->m_detected |= (1 << event2);
1625 }
1626 }
1627
1628 /* OK, we can now leave the critical section because we have
1629 * already obtained the callback address (we make no further
1630 * accesses to socket->whatever). However, the app should
1631 * be prepared to handle events from a socket that has just
1632 * been closed!
1633 */
1634
1635 if (cback != NULL)
1636 (cback)(socket, event, data);
1637 if (cback2 != NULL)
1638 (cback2)(socket, event2, data2);
1639
1640 }
1641
1642 /* Hack added for Mac OS X */
1643 GSocketError GAddress_UNIX_GetPath(GAddress *addr, char *path, size_t buf)
1644 {
1645 return GSOCK_INVADDR;
1646 }
1647
1648 GSocketError GAddress_UNIX_SetPath(GAddress *addr, const char *path)
1649 {
1650 return GSOCK_INVADDR;
1651 }
1652
1653 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */