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