X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ee6b1d97e741fda8d579fa21cbc89f0c91615cef..51a229c65f93a8fbb0c80dcec98ba3fc9c95ad2f:/src/mac/carbon/gsocket.c diff --git a/src/mac/carbon/gsocket.c b/src/mac/carbon/gsocket.c index 5b36717505..5411425556 100644 --- a/src/mac/carbon/gsocket.c +++ b/src/mac/carbon/gsocket.c @@ -19,6 +19,25 @@ #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) +#ifdef __DARWIN__ + #include + + #ifndef FALSE + #define FALSE 0 + #endif + #ifndef TRUE + #define TRUE 1 + #endif +#else + #define OTUNIXERRORS 1 + #include + #include + #include +#endif +#if TARGET_CARBON + #define OTAssert( str , cond ) /* does not exists in Carbon */ +#endif + #include #include #include @@ -27,13 +46,6 @@ #include #include #include -#define OTUNIXERRORS 1 -#include -#include -#include -#if TARGET_CARBON - #define OTAssert( str , cond ) /* does not exists in Carbon */ -#endif #include /* @@ -68,7 +80,9 @@ void wxCYield() ; #define qDebug2 1 extern pascal void OTDebugStr(const char* str); #endif -#include +#ifndef __DARWIN__ + #include +#endif InetSvcRef gInetSvcRef = 0 ; @@ -107,7 +121,11 @@ OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode) ret.opt.maxlen = kOTFourByteOptionSize; opt->level = INET_IP; // dealing with an IP Level function +#ifdef __DARWIN__ + opt->name = kIP_REUSEADDR; +#else opt->name = IP_REUSEADDR; +#endif opt->len = kOTFourByteOptionSize; opt->status = 0; *(UInt32*)opt->value = enableReuseIPMode; // set the desired option level, true or false @@ -177,19 +195,37 @@ static void SetDefaultEndpointModes(EndpointRef ep , void *data ) int GSocket_Init() { - OSStatus err ; + return TRUE; +} + +int GSocket_Verify_Inited() ; +int GSocket_Verify_Inited() +{ + OSStatus err ; #if TARGET_CARBON - InitOpenTransportInContext( kInitOTForApplicationMask , NULL ) ; + // Marc Newsam: added the clientcontext variable + // however, documentation is unclear how this works + OTClientContextPtr clientcontext; + + if ( gInetSvcRef ) + return TRUE ; + + InitOpenTransportInContext(kInitOTForApplicationMask, &clientcontext); + gInetSvcRef = OTOpenInternetServicesInContext(kDefaultInternetServicesPath, + NULL, &err, clientcontext); #else - InitOpenTransport() ; + if ( gInetSvcRef ) + return TRUE ; + + InitOpenTransport() ; + gInetSvcRef = OTOpenInternetServices(kDefaultInternetServicesPath, NULL, &err); #endif - gInetSvcRef = OTOpenInternetServices(kDefaultInternetServicesPath, NULL, &err); - if ( gInetSvcRef == NULL || err != kOTNoError ) - { - OTAssert("Could not open Inet Services", err == noErr); - return FALSE ; - } - return TRUE; + if ( gInetSvcRef == NULL || err != kOTNoError ) + { + OTAssert("Could not open Inet Services", err == noErr); + return FALSE ; + } + return TRUE ; } void GSocket_Cleanup() @@ -207,9 +243,13 @@ void GSocket_Cleanup() GSocket *GSocket_new() { + int i; GSocket *socket; + if ( GSocket_Verify_Inited() == FALSE ) + return NULL ; + socket = (GSocket *)malloc(sizeof(GSocket)); if (socket == NULL) @@ -227,8 +267,8 @@ GSocket *GSocket_new() socket->m_server = FALSE; socket->m_stream = TRUE; socket->m_non_blocking = FALSE; - socket->m_timeout = 10*60*1000; - /* 10 minutes * 60 sec * 1000 millisec */ + socket->m_timeout = 10*1000; + /* 10 sec * 1000 millisec */ socket->m_takesEvents = TRUE ; socket->m_mac_events = wxMacGetNotifierTable() ; return socket; @@ -353,7 +393,6 @@ GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address) GAddress *GSocket_GetLocal(GSocket *socket) { GAddress *address = NULL ; - InetAddress addr; GSocketError err; InetAddress loc ; @@ -935,7 +974,9 @@ void GSocket_SetTimeout(GSocket *socket, unsigned long millisec) { assert(socket != NULL); - socket->m_timeout = millisec; +// this is usually set too high and we have not yet been able to detect a closed +// stream, thus we leave the 10 sec timeout +// socket->m_timeout = millisec; } /* GSocket_GetError: @@ -1028,9 +1069,11 @@ int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size) { OTFlags flags ; OTResult res ; + OTByteCount sz = 0 ; - size_t sz = 0 ; OTCountDataBytes( socket->m_endpoint , &sz ) ; + if ( size > sz ) + size = sz ; res = OTRcv( socket->m_endpoint , buffer , size , &flags ) ; if ( res < 0 ) { @@ -1040,7 +1083,7 @@ int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size) // we simulate another read event if there are still bytes if ( socket->m_takesEvents ) { - size_t sz = 0 ; + OTByteCount sz = 0 ; OTCountDataBytes( socket->m_endpoint , &sz ) ; if ( sz > 0 ) { @@ -1226,6 +1269,8 @@ GSocketError _GAddress_translate_from(GAddress *address, GSocketError _GAddress_translate_to(GAddress *address, InetAddress *addr) { + if ( GSocket_Verify_Inited() == FALSE ) + return GSOCK_IOERR ; memset(addr, 0 , sizeof(struct InetAddress)); OTInitInetAddress( addr , address->m_port , address->m_host ) ; return GSOCK_NOERROR; @@ -1250,6 +1295,9 @@ GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) InetHostInfo hinfo ; OSStatus ret ; + if ( GSocket_Verify_Inited() == FALSE ) + return GSOCK_IOERR ; + assert(address != NULL); CHECK_ADDRESS(address, INET, GSOCK_INVADDR); @@ -1346,11 +1394,13 @@ GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) { InetDomainName name ; + if ( GSocket_Verify_Inited() == FALSE ) + return GSOCK_IOERR ; assert(address != NULL); CHECK_ADDRESS(address, INET, GSOCK_INVADDR); - OTInetAddressToName( gInetSvcRef , address->m_host , &name ) ; + OTInetAddressToName( gInetSvcRef , address->m_host , name ) ; strncpy( hostname , name , sbuf ) ; return GSOCK_NOERROR; } @@ -1382,7 +1432,7 @@ void _GSocket_Enable_Events(GSocket *socket) state = OTGetEndpointState(socket->m_endpoint); { - size_t sz = 0 ; + OTByteCount sz = 0 ; OTCountDataBytes( socket->m_endpoint , &sz ) ; if ( state == T_INCON || sz > 0 ) { @@ -1422,7 +1472,7 @@ GSocketError _GSocket_Input_Timeout(GSocket *socket) while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < socket->m_timeout * 1000.0 ) { OTResult state ; - size_t sz = 0 ; + OTByteCount sz = 0 ; state = OTGetEndpointState(socket->m_endpoint); OTCountDataBytes( socket->m_endpoint , &sz ) ; @@ -1590,4 +1640,13 @@ void _GSocket_Internal_Proc(unsigned long e , void* d ) } +/* Hack added for Mac OS X */ +GSocketError GAddress_UNIX_GetPath(GAddress *addr, char *path, size_t buf) +{ +} + +GSocketError GAddress_UNIX_SetPath(GAddress *addr, const char *path) +{ +} + #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */