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