#ifndef __GSOCKET_STANDALONE__
#include "wx/setup.h"
+#include "wx/platform.h"
#endif
#if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
-#define OTUNIXERRORS 1
-#ifdef __UNIX__
- #include <CarbonCore/CarbonCore.h>
- #include <OT/OpenTransport.h>
- #include <OT/OpenTransportProviders.h>
+#ifdef __DARWIN__
+ #include <CoreServices/CoreServices.h>
#ifndef FALSE
#define FALSE 0
#define TRUE 1
#endif
#else
+ #include <MacHeaders.c>
+ #define OTUNIXERRORS 1
#include <OpenTransport.h>
#include <OpenTransportProviders.h>
#include <OpenTptInternet.h>
#endif
-#if TARGET_CARBON
+#if TARGET_CARBON && !defined(OTAssert)
#define OTAssert( str , cond ) /* does not exists in Carbon */
#endif
#endif /* __GSOCKET_STANDALONE__ */
+#ifndef ntohl
+#define ntohl(x) (x)
+#define ntohs(x) (x)
+#define htonl(x) (x)
+#define htons(x) (x)
+#endif
+
void wxCYield() ;
#ifdef __WXDEBUG__
#define qDebug 1
#define qDebug2 1
extern pascal void OTDebugStr(const char* str);
#endif
-#ifndef __UNIX__
+#ifndef __DARWIN__
#include <OTDebug.h>
#endif
InetSvcRef gInetSvcRef = 0 ;
-
+int gOTInited = 0 ;
+OTNotifyUPP gOTNotifierUPP = NULL ;
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
if ( event == kOTSyncIdleEvent )
{
- YieldToAnyThread() ;
return ;
}
junk = OTSetBlocking(ep);
OTAssert("SetDefaultEndpointModes: Could not set blocking", junk == noErr);
*/
- junk = OTInstallNotifier(ep, OTInetEventHandler, data);
+ junk = OTInstallNotifier(ep, gOTNotifierUPP, data);
OTAssert("SetDefaultEndpointModes: Could not install notifier", junk == noErr);
/*
junk = OTUseSyncIdleEvents(ep, true);
/* Global initialisers */
+void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable *table)
+{
+ // do nothing, wxMac doesn't have wxBase-GUI separation yet
+}
+
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);
+ gOTInited = 1 ;
+ gInetSvcRef = OTOpenInternetServicesInContext(kDefaultInternetServicesPath,
+ NULL, &err, clientcontext);
#else
- InitOpenTransport() ;
+ if ( gInetSvcRef )
+ return TRUE ;
+
+ InitOpenTransport() ;
+ gOTInited = 1 ;
+ 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 ;
+ }
+ gOTNotifierUPP = NewOTNotifyUPP( OTInetEventHandler ) ;
+ return TRUE ;
}
void GSocket_Cleanup()
{
- if ( gInetSvcRef != NULL )
- OTCloseProvider( gInetSvcRef );
-#if TARGET_CARBON
- CloseOpenTransportInContext( NULL ) ;
-#else
- CloseOpenTransport() ;
-#endif
+ if ( gOTInited != 0 )
+ {
+ if ( gInetSvcRef != NULL )
+ OTCloseProvider( gInetSvcRef );
+ #if TARGET_CARBON
+ CloseOpenTransportInContext( NULL ) ;
+ #else
+ CloseOpenTransport() ;
+ #endif
+ if ( gOTNotifierUPP )
+ DisposeOTNotifyUPP( gOTNotifierUPP ) ;
+ }
}
/* Constructors / Destructors for GSocket */
GSocket *GSocket_new()
{
+
int i;
GSocket *socket;
+ if ( GSocket_Verify_Inited() == FALSE )
+ return NULL ;
+
socket = (GSocket *)malloc(sizeof(GSocket));
if (socket == NULL)
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 = 1*1000;
+ /* 10 sec * 1000 millisec */
socket->m_takesEvents = TRUE ;
socket->m_mac_events = wxMacGetNotifierTable() ;
return socket;
*/
GSocketError GSocket_SetServer(GSocket *sck)
{
- int type;
- int arg = 1;
-
assert(sck != NULL);
/* must not be in use */
GSocket *GSocket_WaitConnection(GSocket *socket)
{
GSocket *connection = NULL ;
- GSocketError err;
-
- int arg = 1;
assert(socket != NULL);
*/
GSocketError GSocket_SetNonOriented(GSocket *sck)
{
- int arg = 1;
-
assert(sck != NULL);
if (sck->m_endpoint != kOTInvalidEndpointRef )
*/
GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
{
- int ret;
- int arg = 1;
InetAddress addr ;
TEndpointInfo info;
- OTFlags flags = 0;
- OSStatus err = kOTNoError;
+ OSStatus err = kOTNoError;
TCall peer ;
assert(sck != NULL);
*/
GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags)
{
- OTResult state ;
assert(socket != NULL);
wxMacProcessNotifierEvents() ;
/*
{
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:
{
OTFlags flags ;
OTResult res ;
+ OTByteCount sz = 0 ;
- size_t sz = 0 ;
OTCountDataBytes( socket->m_endpoint , &sz ) ;
+ if ( size > (int)sz )
+ size = sz ;
res = OTRcv( socket->m_endpoint , buffer , size , &flags ) ;
if ( res < 0 )
{
// 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 )
{
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;
InetHostInfo hinfo ;
OSStatus ret ;
+ if ( GSocket_Verify_Inited() == FALSE )
+ return GSOCK_IOERR ;
+
assert(address != NULL);
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
GSocketError GAddress_INET_SetHostAddress(GAddress *address,
unsigned long hostaddr)
{
- struct in_addr *addr;
-
assert(address != NULL);
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
- address->m_host = hostaddr ;
+ address->m_host = htonl(hostaddr) ;
return GSOCK_NOERROR;
}
GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
const char *protocol)
{
- InetAddress *addr;
- int i ;
+ size_t i ;
assert(address != NULL);
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
{
- InetAddress *addr;
-
assert(address != NULL);
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
address->m_port = 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;
}
assert(address != NULL);
CHECK_ADDRESS(address, INET, 0);
- return address->m_host;
+ return ntohl(address->m_host);
}
unsigned short GAddress_INET_GetPort(GAddress *address)
state = OTGetEndpointState(socket->m_endpoint);
{
- size_t sz = 0 ;
+ OTByteCount sz = 0 ;
OTCountDataBytes( socket->m_endpoint , &sz ) ;
if ( state == T_INCON || sz > 0 )
{
while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < socket->m_timeout * 1000.0 )
{
OTResult state ;
- size_t sz = 0 ;
- state = OTGetEndpointState(socket->m_endpoint);
+ OTByteCount sz = 0 ;
+ state = OTGetEndpointState(socket->m_endpoint);
OTCountDataBytes( socket->m_endpoint , &sz ) ;
if ( state == T_INCON || sz > 0 )
/* Hack added for Mac OS X */
GSocketError GAddress_UNIX_GetPath(GAddress *addr, char *path, size_t buf)
{
+ return GSOCK_INVADDR;
}
GSocketError GAddress_UNIX_SetPath(GAddress *addr, const char *path)
{
+ return GSOCK_INVADDR;
}
#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */