X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3e637936e59be12938e4b020798bc1e4474cb4ba..7f65743f005572f1de2754ba8c5e7cf1eed1847e:/src/mac/carbon/gsocket.cpp diff --git a/src/mac/carbon/gsocket.cpp b/src/mac/carbon/gsocket.cpp index f7af7a1ca6..123d9cd6b5 100644 --- a/src/mac/carbon/gsocket.cpp +++ b/src/mac/carbon/gsocket.cpp @@ -1,11 +1,13 @@ /* ------------------------------------------------------------------------- - * Project: GSocket (Generic Socket) for WX - * Name: gsocket.cpp - * Authors: Guilhem Lavaux, - * Guillermo Rodriguez Garcia (maintainer) - * Stefan CSomor - * Purpose: GSocket main mac file - * CVSID: $Id$ + * Project: GSocket (Generic Socket) for WX + * Name: src/mac/carbon/gsocket.cpp + * Copyright: (c) Guilhem Lavaux + * Licence: wxWindows Licence + * Authors: Guilhem Lavaux, + * Guillermo Rodriguez Garcia (maintainer) + * Stefan CSomor + * Purpose: GSocket main mac file + * CVSID: $Id$ * ------------------------------------------------------------------------- */ @@ -14,24 +16,22 @@ */ #ifndef __GSOCKET_STANDALONE__ -#include "wx/setup.h" #include "wx/platform.h" #endif #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) #ifdef __DARWIN__ - #include - + #include #else - #include - #define OTUNIXERRORS 1 - #include - #include - #include + #include + #define OTUNIXERRORS 1 + #include + #include + #include #endif #if TARGET_CARBON && !defined(OTAssert) - #define OTAssert( str , cond ) /* does not exists in Carbon */ + #define OTAssert( str , cond ) /* does not exists in Carbon */ #endif #include @@ -59,22 +59,22 @@ #endif #ifndef __GSOCKET_STANDALONE__ -#include "wx/mac/macnotfy.h" -#include "wx/mac/gsockmac.h" -#include "wx/gsocket.h" + #include "wx/mac/macnotfy.h" + #include "wx/mac/gsockmac.h" + #include "wx/gsocket.h" #else -#include "gsockmac.h" -#include "gsocket.h" + #include "gsockmac.h" + #include "gsocket.h" #endif /* __GSOCKET_STANDALONE__ */ #ifndef ntohl -#define ntohl(x) (x) -#define ntohs(x) (x) -#define htonl(x) (x) -#define htons(x) (x) + #define ntohl(x) (x) + #define ntohs(x) (x) + #define htonl(x) (x) + #define htons(x) (x) #endif void wxCYield() ; @@ -93,120 +93,112 @@ OTNotifyUPP gOTNotifierUPP = NULL ; OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode); /* Input: ep - endpointref on which to negotiate the option - enableReuseIPMode - desired option setting - true/false + enableReuseIPMode - desired option setting - true/false Return: kOTNoError indicates that the option was successfully negotiated - OSStatus is an error if < 0, otherwise, the status field is - returned and is > 0. - - IMPORTANT NOTE: The endpoint is assumed to be in synchronous more, otherwise - this code will not function as desired + OSStatus is an error if < 0, otherwise, the status field is + returned and is > 0. + + IMPORTANT NOTE: The endpoint is assumed to be in synchronous more, otherwise + this code will not function as desired */ OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode) { - UInt8 buf[kOTFourByteOptionSize]; // define buffer for fourByte Option size - TOption* opt; // option ptr to make items easier to access - TOptMgmt req; - TOptMgmt ret; - OSStatus err; - - if (!OTIsSynchronous(ep)) - { - return (-1); - } - opt = (TOption*)buf; // set option ptr to buffer - req.opt.buf = buf; - req.opt.len = sizeof(buf); - req.flags = T_NEGOTIATE; // negotiate for option - - ret.opt.buf = buf; - ret.opt.maxlen = kOTFourByteOptionSize; - - opt->level = INET_IP; // dealing with an IP Level function + UInt8 buf[kOTFourByteOptionSize]; // define buffer for fourByte Option size + TOption* opt; // option ptr to make items easier to access + TOptMgmt req; + TOptMgmt ret; + OSStatus err; + + if (!OTIsSynchronous(ep)) + { + return (-1); + } + opt = (TOption*)buf; // set option ptr to buffer + req.opt.buf = buf; + req.opt.len = sizeof(buf); + req.flags = T_NEGOTIATE; // negotiate for option + + ret.opt.buf = buf; + ret.opt.maxlen = kOTFourByteOptionSize; + + opt->level = INET_IP; // dealing with an IP Level function #ifdef __DARWIN__ - opt->name = kIP_REUSEADDR; + opt->name = kIP_REUSEADDR; #else - opt->name = IP_REUSEADDR; + opt->name = IP_REUSEADDR; #endif - opt->len = kOTFourByteOptionSize; - opt->status = 0; - *(UInt32*)opt->value = enableReuseIPMode; // set the desired option level, true or false - - err = OTOptionManagement(ep, &req, &ret); - - // if no error then return the option status value - if (err == kOTNoError) - { - if (opt->status != T_SUCCESS) - err = opt->status; - else - err = kOTNoError; - } - - return err; + opt->len = kOTFourByteOptionSize; + opt->status = 0; + *(UInt32*)opt->value = enableReuseIPMode; // set the desired option level, true or false + + err = OTOptionManagement(ep, &req, &ret); + + // if no error then return the option status value + if (err == kOTNoError) + { + if (opt->status != T_SUCCESS) + err = opt->status; + else + err = kOTNoError; + } + + return err; } pascal void OTInetEventHandler(void*s, OTEventCode event, OTResult, void *cookie) ; pascal void OTInetEventHandler(void*s, OTEventCode event, OTResult result, void *cookie) { - int wakeUp = true ; - GSocket* sock = (GSocket*) s ; - - if ( event == kOTSyncIdleEvent ) - { - return ; - } - - if ( s ) - { - wxMacAddEvent( sock->m_mac_events , _GSocket_Internal_Proc , event , s , wakeUp ) ; - } - - return; + int wakeUp = true ; + GSocket* sock = (GSocket*) s ; + + if ( event == kOTSyncIdleEvent ) + { + return ; + } + + if ( s ) + { + wxMacAddEvent( sock->m_mac_events , _GSocket_Internal_Proc , event , s , wakeUp ) ; + } + + return; } static void SetDefaultEndpointModes(EndpointRef ep , void *data ) - // This routine sets the supplied endpoint into the default - // mode used in this application. The specifics are: - // blocking, synchronous, and using synch idle events with - // the standard YieldingNotifier. + // This routine sets the supplied endpoint into the default + // mode used in this application. The specifics are: + // blocking, synchronous, and using synch idle events with + // the standard YieldingNotifier. { - OSStatus junk = kOTNoError ; - OTAssert ("SetDefaultEndpointModes:invalid ref", ep != kOTInvalidEndpointRef ) ; - junk = OTSetAsynchronous(ep); - OTAssert("SetDefaultEndpointModes: Could not set asynchronous", junk == noErr); + OSStatus junk = kOTNoError ; + OTAssert ("SetDefaultEndpointModes:invalid ref", ep != kOTInvalidEndpointRef ) ; + junk = OTSetAsynchronous(ep); + OTAssert("SetDefaultEndpointModes: Could not set asynchronous", junk == noErr); /* - junk = OTSetBlocking(ep); - OTAssert("SetDefaultEndpointModes: Could not set blocking", junk == noErr); - junk = OTSetSynchronous(ep); - OTAssert("SetDefaultEndpointModes: Could not set synchronous", junk == noErr); - junk = OTSetBlocking(ep); - OTAssert("SetDefaultEndpointModes: Could not set blocking", junk == noErr); + junk = OTSetBlocking(ep); + OTAssert("SetDefaultEndpointModes: Could not set blocking", junk == noErr); + junk = OTSetSynchronous(ep); + OTAssert("SetDefaultEndpointModes: Could not set synchronous", junk == noErr); + junk = OTSetBlocking(ep); + OTAssert("SetDefaultEndpointModes: Could not set blocking", junk == noErr); */ - junk = OTInstallNotifier(ep, gOTNotifierUPP, data); - OTAssert("SetDefaultEndpointModes: Could not install notifier", junk == noErr); + junk = OTInstallNotifier(ep, gOTNotifierUPP, data); + OTAssert("SetDefaultEndpointModes: Could not install notifier", junk == noErr); /* - junk = OTUseSyncIdleEvents(ep, true); - OTAssert("SetDefaultEndpointModes: Could not use sync idle events", junk == noErr); + junk = OTUseSyncIdleEvents(ep, true); + OTAssert("SetDefaultEndpointModes: Could not use sync idle events", junk == noErr); */ } -/* Global initialisers */ - -void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable *table) +bool GSocket_Init() { - // do nothing, wxMac doesn't have wxBase-GUI separation yet + return true; } -int GSocket_Init() -{ - return 1; -} - -bool GSocket_Verify_Inited() ; bool GSocket_Verify_Inited() { OSStatus err ; @@ -221,19 +213,19 @@ bool GSocket_Verify_Inited() InitOpenTransportInContext(kInitOTForApplicationMask, &clientcontext); gOTInited = 1 ; gInetSvcRef = OTOpenInternetServicesInContext(kDefaultInternetServicesPath, - NULL, &err, clientcontext); -#else + NULL, &err, clientcontext); +#else if ( gInetSvcRef ) return true ; - + InitOpenTransport() ; gOTInited = 1 ; gInetSvcRef = OTOpenInternetServices(kDefaultInternetServicesPath, NULL, &err); #endif if ( gInetSvcRef == NULL || err != kOTNoError ) { - OTAssert("Could not open Inet Services", err == noErr); - return false ; + OTAssert("Could not open Inet Services", err == noErr); + return false ; } gOTNotifierUPP = NewOTNotifyUPP( OTInetEventHandler ) ; return true ; @@ -243,8 +235,8 @@ void GSocket_Cleanup() { if ( gOTInited != 0 ) { - if ( gInetSvcRef != NULL ) - OTCloseProvider( gInetSvcRef ); + if ( gInetSvcRef != NULL ) + OTCloseProvider( gInetSvcRef ); #if TARGET_CARBON CloseOpenTransportInContext( NULL ) ; #else @@ -277,25 +269,25 @@ GSocket::GSocket() m_non_blocking = false; m_timeout = 1*1000; /* 10 sec * 1000 millisec */ - m_takesEvents = true ; - m_mac_events = wxMacGetNotifierTable() ; + m_takesEvents = true ; + m_mac_events = wxMacGetNotifierTable() ; } GSocket::~GSocket() { - assert(this); + assert(this); - /* Check that the socket is really shutdowned */ - if (m_endpoint != kOTInvalidEndpointRef) - Shutdown(); + /* Check that the socket is really shutdowned */ + if (m_endpoint != kOTInvalidEndpointRef) + Shutdown(); - /* Destroy private addresses */ - if (m_local) - GAddress_destroy(m_local); + /* Destroy private addresses */ + if (m_local) + GAddress_destroy(m_local); - if (m_peer) - GAddress_destroy(m_peer); + if (m_peer) + GAddress_destroy(m_peer); } /* GSocket_Shutdown: @@ -304,32 +296,32 @@ GSocket::~GSocket() */ void GSocket::Shutdown() { - OSStatus err ; - int evt; + OSStatus err ; + int evt; - assert(this); + assert(this); - /* If socket has been created, shutdown it */ - if (m_endpoint != kOTInvalidEndpointRef ) - { - err = OTSndOrderlyDisconnect( m_endpoint ) ; - if ( err != kOTNoError ) - { - - } - err = OTRcvOrderlyDisconnect( m_endpoint ) ; - err = OTUnbind( m_endpoint ) ; - err = OTCloseProvider( m_endpoint ) ; - m_endpoint = kOTInvalidEndpointRef ; - } + /* If socket has been created, shutdown it */ + if (m_endpoint != kOTInvalidEndpointRef ) + { + err = OTSndOrderlyDisconnect( m_endpoint ) ; + if ( err != kOTNoError ) + { + } + + err = OTRcvOrderlyDisconnect( m_endpoint ) ; + err = OTUnbind( m_endpoint ) ; + err = OTCloseProvider( m_endpoint ) ; + m_endpoint = kOTInvalidEndpointRef ; + } - /* Disable GUI callbacks */ - for (evt = 0; evt < GSOCK_MAX_EVENT; evt++) - m_cbacks[evt] = NULL; + /* Disable GUI callbacks */ + for (evt = 0; evt < GSOCK_MAX_EVENT; evt++) + m_cbacks[evt] = NULL; - m_detected = 0; - Disable_Events(); - wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable() , this ) ; + m_detected = 0; + Disable_Events(); + wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable() , this ) ; } @@ -351,109 +343,109 @@ void GSocket::Shutdown() */ GSocketError GSocket::SetLocal(GAddress *address) { - assert(this); + assert(this); - /* the socket must be initialized, or it must be a server */ - if ((m_endpoint != kOTInvalidEndpointRef && !m_server)) - { - m_error = GSOCK_INVSOCK; - return GSOCK_INVSOCK; - } + /* the socket must be initialized, or it must be a server */ + if ((m_endpoint != kOTInvalidEndpointRef && !m_server)) + { + m_error = GSOCK_INVSOCK; + return GSOCK_INVSOCK; + } - /* check address */ - if (address == NULL || address->m_family == GSOCK_NOFAMILY) - { - m_error = GSOCK_INVADDR; - return GSOCK_INVADDR; - } + /* check address */ + if (address == NULL || address->m_family == GSOCK_NOFAMILY) + { + m_error = GSOCK_INVADDR; + return GSOCK_INVADDR; + } - if (m_local) - GAddress_destroy(m_local); + if (m_local) + GAddress_destroy(m_local); - m_local = GAddress_copy(address); + m_local = GAddress_copy(address); - return GSOCK_NOERROR; + return GSOCK_NOERROR; } GSocketError GSocket::SetPeer(GAddress *address) { - assert(this); + assert(this); - /* check address */ - if (address == NULL || address->m_family == GSOCK_NOFAMILY) - { - m_error = GSOCK_INVADDR; - return GSOCK_INVADDR; - } + /* check address */ + if (address == NULL || address->m_family == GSOCK_NOFAMILY) + { + m_error = GSOCK_INVADDR; + return GSOCK_INVADDR; + } - if (m_peer) - GAddress_destroy(m_peer); + if (m_peer) + GAddress_destroy(m_peer); - m_peer = GAddress_copy(address); + m_peer = GAddress_copy(address); - return GSOCK_NOERROR; + return GSOCK_NOERROR; } GAddress *GSocket::GetLocal() { - GAddress *address = NULL ; - GSocketError err; - InetAddress loc ; + GAddress *address = NULL ; + GSocketError err; + InetAddress loc ; - assert(this); + assert(this); - /* try to get it from the m_local var first */ - if (m_local) - return GAddress_copy(m_local); + /* try to get it from the m_local var first */ + if (m_local) + return GAddress_copy(m_local); + + /* else, if the socket is initialized, try getsockname */ + if (m_endpoint == kOTInvalidEndpointRef) + { + m_error = GSOCK_INVSOCK; + return NULL; + } - /* else, if the socket is initialized, try getsockname */ - if (m_endpoint == kOTInvalidEndpointRef) - { - m_error = GSOCK_INVSOCK; - return NULL; - } - /* we do not support multihoming with this code at the moment - OTGetProtAddress will have to be used then - but we don't have a handy + OTGetProtAddress will have to be used then - but we don't have a handy method to use right now */ - { - InetInterfaceInfo info; - OTInetGetInterfaceInfo(&info, kDefaultInetInterface); - loc.fHost = info.fAddress ; - loc.fPort = 0 ; - loc.fAddressType = AF_INET ; - } + { + InetInterfaceInfo info; + OTInetGetInterfaceInfo(&info, kDefaultInetInterface); + loc.fHost = info.fAddress ; + loc.fPort = 0 ; + loc.fAddressType = AF_INET ; + } - /* got a valid address from getsockname, create a GAddress object */ - address = GAddress_new(); - if (address == NULL) - { - m_error = GSOCK_MEMERR; - return NULL; - } + /* got a valid address from getsockname, create a GAddress object */ + address = GAddress_new(); + if (address == NULL) + { + m_error = GSOCK_MEMERR; + return NULL; + } - err = _GAddress_translate_from(address, &loc); - if (err != GSOCK_NOERROR) - { - GAddress_destroy(address); - m_error = err; - return NULL; - } + err = _GAddress_translate_from(address, &loc); + if (err != GSOCK_NOERROR) + { + GAddress_destroy(address); + m_error = err; + return NULL; + } - return address; + return address; } GAddress *GSocket::GetPeer() { - assert(this); + assert(this); - /* try to get it from the m_peer var */ - if (m_peer) - return GAddress_copy(m_peer); + /* try to get it from the m_peer var */ + if (m_peer) + return GAddress_copy(m_peer); - return NULL; + return NULL; } /* Server specific parts */ @@ -462,11 +454,11 @@ GAddress *GSocket::GetPeer() * Sets up this socket as a server. The local address must have been * set with GSocket_SetLocal() before GSocket_SetServer() is called. * Returns GSOCK_NOERROR on success, one of the following otherwise: - * + * * Error codes: * GSOCK_INVSOCK - the socket is in use. * GSOCK_INVADDR - the local address has not been set. - * GSOCK_IOERR - low-level error. + * GSOCK_IOERR - low-level error. */ GSocketError GSocket::SetServer() { @@ -512,7 +504,7 @@ GSocketError GSocket::SetServer() if ((bind(m_endpoint, m_local->m_addr, m_local->m_len) != 0) || (getsockname(m_endpoint, m_local->m_addr, - (SOCKLEN_T *) &m_local->m_len) != 0) || + (WX_SOCKLEN_T *) &m_local->m_len) != 0) || (listen(m_endpoint, 5) != 0)) { close(m_endpoint); @@ -534,7 +526,7 @@ GSocketError GSocket::SetServer() * GSOCK_TIMEDOUT - timeout, no incoming connections. * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. * GSOCK_MEMERR - couldn't allocate memory. - * GSOCK_IOERR - low-level error. + * GSOCK_IOERR - low-level error. */ GSocket *GSocket::WaitConnection() { @@ -571,7 +563,7 @@ GSocket *GSocket::WaitConnection() // TODO #if 0 - connection->m_endpoint = accept(m_endpoint, &from, (SOCKLEN_T *) &fromlen); + connection->m_endpoint = accept(m_endpoint, &from, (WX_SOCKLEN_T *) &fromlen); #endif if (connection->m_endpoint == kOTInvalidEndpointRef ) @@ -651,7 +643,7 @@ GSocketError GSocket::SetNonOriented() m_oriented = false; /* Create the socket */ - + // TODO #if 0 m_endpoint = socket(m_local->m_realfamily, SOCK_DGRAM, 0); @@ -677,7 +669,7 @@ GSocketError GSocket::SetNonOriented() if ((bind(m_endpoint, m_local->m_addr, m_local->m_len) != 0) || (getsockname(m_endpoint, m_local->m_addr, - (SOCKLEN_T *) &m_local->m_len) != 0)) + (WX_SOCKLEN_T *) &m_local->m_len) != 0)) { close(m_endpoint); m_endpoint = -1; @@ -694,7 +686,7 @@ GSocketError GSocket::SetNonOriented() * For stream (connection oriented) sockets, GSocket_Connect() tries * to establish a client connection to a server using the peer address * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the - * connection has been succesfully established, or one of the error + * connection has been successfully established, or one of the error * codes listed below. Note that for nonblocking sockets, a return * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection * request can be completed later; you should use GSocket_Select() @@ -711,13 +703,13 @@ GSocketError GSocket::SetNonOriented() * GSOCK_TIMEDOUT - timeout, the connection failed. * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) * GSOCK_MEMERR - couldn't allocate memory. - * GSOCK_IOERR - low-level error. + * GSOCK_IOERR - low-level error. */ GSocketError GSocket::Connect(GSocketStream stream) { InetAddress addr ; - TEndpointInfo info; - OSStatus err = kOTNoError; + TEndpointInfo info; + OSStatus err = kOTNoError; TCall peer ; assert(this); @@ -744,22 +736,22 @@ GSocketError GSocket::Connect(GSocketStream stream) /* Create the socket */ #if TARGET_CARBON - m_endpoint = - OTOpenEndpointInContext( OTCreateConfiguration( kTCPName) , 0 , &info , &err , NULL ) ; + m_endpoint = + OTOpenEndpointInContext( OTCreateConfiguration( kTCPName) , 0 , &info , &err , NULL ) ; #else - m_endpoint = - OTOpenEndpoint( OTCreateConfiguration( kTCPName) , 0 , &info , &err ) ; + m_endpoint = + OTOpenEndpoint( OTCreateConfiguration( kTCPName) , 0 , &info , &err ) ; #endif if ( m_endpoint == kOTInvalidEndpointRef || err != kOTNoError ) { - m_endpoint = kOTInvalidEndpointRef ; - m_error = GSOCK_IOERR; - return GSOCK_IOERR; + m_endpoint = kOTInvalidEndpointRef ; + m_error = GSOCK_IOERR; + return GSOCK_IOERR; } err = OTBind( m_endpoint , nil , nil ) ; if ( err != kOTNoError ) { - return GSOCK_IOERR; + return GSOCK_IOERR; } SetDefaultEndpointModes( m_endpoint , this ) ; // TODO @@ -779,13 +771,13 @@ GSocketError GSocket::Connect(GSocketStream stream) * is in blocking mode, we select() for the specified timeout * checking for writability to see if the connection request * completes. - */ - + */ + if ((err == kOTNoDataErr ) && (!m_non_blocking)) { if (Output_Timeout() == GSOCK_TIMEDOUT) { - OTSndOrderlyDisconnect( m_endpoint ) ; + OTSndOrderlyDisconnect( m_endpoint ) ; m_endpoint = kOTInvalidEndpointRef ; /* m_error is set in _GSocket_Output_Timeout */ return GSOCK_TIMEDOUT; @@ -794,7 +786,7 @@ GSocketError GSocket::Connect(GSocketStream stream) { /* int error; - SOCKLEN_T len = sizeof(error); + WX_SOCKLEN_T len = sizeof(error); getsockopt(m_endpoint, SOL_SOCKET, SO_ERROR, (void*) &error, &len); @@ -856,7 +848,7 @@ int GSocket::Read(char *buffer, int size) ret = Recv_Stream(buffer, size); else ret = Recv_Dgram(buffer, size); - + if (ret == -1) { if (errno == EWOULDBLOCK) @@ -864,12 +856,12 @@ int GSocket::Read(char *buffer, int size) else m_error = GSOCK_IOERR; } - + return ret; } int GSocket::Write(const char *buffer, int size) -{ +{ int ret; assert(this); @@ -889,7 +881,7 @@ int GSocket::Write(const char *buffer, int size) ret = Send_Stream(buffer, size); else ret = Send_Dgram(buffer, size); - + if (ret == -1) { if (errno == EWOULDBLOCK) @@ -905,7 +897,7 @@ int GSocket::Write(const char *buffer, int size) m_detected &= ~GSOCK_OUTPUT_FLAG; return -1; } - + return ret; } @@ -922,24 +914,24 @@ GSocketEventFlags GSocket::Select(GSocketEventFlags flags) wxMacProcessNotifierEvents() ; /* state = OTGetEndpointState(m_endpoint); - + if ( ( flags & GSOCK_INPUT_FLAG ) && ! ( m_detected & GSOCK_INPUT_FLAG ) ) { - size_t sz = 0 ; - OTCountDataBytes( m_endpoint , &sz ) ; - if ( state == T_INCON || sz > 0 ) - { - m_detected |= GSOCK_INPUT_FLAG ; - (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]); - } + size_t sz = 0 ; + OTCountDataBytes( m_endpoint , &sz ) ; + if ( state == T_INCON || sz > 0 ) + { + m_detected |= GSOCK_INPUT_FLAG ; + (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]); + } } if ( ( flags & GSOCK_INPUT_FLAG ) && ! ( m_detected & GSOCK_OUTPUT_FLAG ) ) { - if ( state == T_DATAXFER || state == T_INREL ) - { - m_detected |=GSOCK_OUTPUT_FLAG ; - (m_cbacks[GSOCK_OUTPUT])(this, GSOCK_OUTPUT, m_data[GSOCK_OUTPUT]); - } + if ( state == T_DATAXFER || state == T_INREL ) + { + m_detected |=GSOCK_OUTPUT_FLAG ; + (m_cbacks[GSOCK_OUTPUT])(this, GSOCK_OUTPUT, m_data[GSOCK_OUTPUT]); + } } */ return ( flags & m_detected ) ; @@ -972,7 +964,7 @@ void GSocket::SetTimeout(unsigned long millisec) } /* GSocket_GetError: - * Returns the last error occured for this socket. Note that successful + * Returns the last error which occurred for this socket. Note that successful * operations do not clear this back to GSOCK_NOERROR, so use it only * after an error. */ @@ -990,14 +982,14 @@ GSocketError WXDLLIMPEXP_NET GSocket::GetError() * operation, there is still data available, the callback function will * be called again. * GSOCK_OUTPUT: - * The socket is available for writing. That is, the next write call + * The socket is available for writing. That is, the next write call * won't block. This event is generated only once, when the connection is * first established, and then only if a call failed with GSOCK_WOULDBLOCK, * when the output buffer empties again. This means that the app should * assume that it can write since the first OUTPUT event, and no more * OUTPUT events will be generated unless an error occurs. * GSOCK_CONNECTION: - * Connection succesfully established, for client sockets, or incoming + * Connection successfully established, for client sockets, or incoming * client connection, for server sockets. Wait for this event (also watch * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. * GSOCK_LOST: @@ -1059,31 +1051,31 @@ void GSocket::UnsetCallback(GSocketEventFlags flags) int GSocket::Recv_Stream(char *buffer, int size) { - OTFlags flags ; - OTResult res ; - OTByteCount sz = 0 ; - - OTCountDataBytes( m_endpoint , &sz ) ; - if ( size > (int)sz ) - size = sz ; - res = OTRcv( m_endpoint , buffer , size , &flags ) ; - if ( res < 0 ) - { - return -1 ; - } - - // we simulate another read event if there are still bytes - if ( m_takesEvents ) - { - OTByteCount sz = 0 ; - OTCountDataBytes( m_endpoint , &sz ) ; - if ( sz > 0 ) - { - m_detected |= GSOCK_INPUT_FLAG ; - (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]); - } - } - return res ; + OTFlags flags ; + OTResult res ; + OTByteCount sz = 0 ; + + OTCountDataBytes( m_endpoint , &sz ) ; + if ( size > (int)sz ) + size = sz ; + res = OTRcv( m_endpoint , buffer , size , &flags ) ; + if ( res < 0 ) + { + return -1 ; + } + + // we simulate another read event if there are still bytes + if ( m_takesEvents ) + { + OTByteCount sz = 0 ; + OTCountDataBytes( m_endpoint , &sz ) ; + if ( sz > 0 ) + { + m_detected |= GSOCK_INPUT_FLAG ; + (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]); + } + } + return res ; } int GSocket::Recv_Dgram(char *buffer, int size) @@ -1092,12 +1084,12 @@ int GSocket::Recv_Dgram(char *buffer, int size) int ret = -1; #if 0 struct sockaddr from; - SOCKLEN_T fromlen = sizeof(from); + WX_SOCKLEN_T fromlen = sizeof(from); GSocketError err; fromlen = sizeof(from); - ret = recvfrom(m_endpoint, buffer, size, 0, &from, (SOCKLEN_T *) &fromlen); + ret = recvfrom(m_endpoint, buffer, size, 0, &from, (WX_SOCKLEN_T *) &fromlen); if (ret == -1) return -1; @@ -1126,11 +1118,11 @@ int GSocket::Recv_Dgram(char *buffer, int size) int GSocket::Send_Stream(const char *buffer, int size) { - OTFlags flags = 0 ; - OTResult res ; + OTFlags flags = 0 ; + OTResult res ; - res = OTSnd( m_endpoint , (void*) buffer , size , flags ) ; - return res ; + res = OTSnd( m_endpoint , (void*) buffer , size , flags ) ; + return res ; } int GSocket::Send_Dgram(const char *buffer, int size) @@ -1163,6 +1155,16 @@ int GSocket::Send_Dgram(const char *buffer, int size) return ret; } +/* Compatibility functions for GSocket */ +GSocket *GSocket_new(void) +{ + GSocket *newsocket = new GSocket(); + if(newsocket->IsOk()) + return newsocket; + delete newsocket; + return NULL; +} + /* * ------------------------------------------------------------------------- @@ -1261,7 +1263,7 @@ GSocketError _GAddress_translate_from(GAddress *address, GSocketError _GAddress_translate_to(GAddress *address, InetAddress *addr) { - if ( !GSocket_Verify_Inited() ) + if ( !GSocket_Verify_Inited() ) return GSOCK_IOERR ; memset(addr, 0 , sizeof(struct InetAddress)); OTInitInetAddress( addr , address->m_port , address->m_host ) ; @@ -1278,7 +1280,7 @@ GSocketError _GAddress_Init_INET(GAddress *address) { address->m_family = GSOCK_INET; address->m_host = kOTAnyInetAddress ; - + return GSOCK_NOERROR; } @@ -1296,14 +1298,19 @@ GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) ret = OTInetStringToAddress( gInetSvcRef , (char*) hostname , &hinfo ) ; if ( ret != kOTNoError ) { - address->m_host = INADDR_NONE ; + address->m_host = INADDR_NONE ; address->m_error = GSOCK_NOHOST; return GSOCK_NOHOST; - } + } address->m_host = hinfo.addrs[0] ; return GSOCK_NOERROR; } +GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address) +{ + return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST); +} + GSocketError GAddress_INET_SetAnyAddress(GAddress *address) { return GAddress_INET_SetHostAddress(address, INADDR_ANY); @@ -1321,17 +1328,17 @@ GSocketError GAddress_INET_SetHostAddress(GAddress *address, return GSOCK_NOERROR; } -struct service_entry +struct service_entry { - const char * name ; - unsigned short port ; - const char * protocol ; + const char * name ; + unsigned short port ; + const char * protocol ; } ; typedef struct service_entry service_entry ; service_entry gServices[] = { - { "http" , 80 , "tcp" } + { "http" , 80 , "tcp" } } ; GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, @@ -1349,14 +1356,14 @@ GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, } for ( i = 0 ; i < sizeof( gServices) / sizeof( service_entry ) ; ++i ) { - if ( strcmp( port , gServices[i].name ) == 0 ) - { - if ( protocol == NULL || strcmp( protocol , gServices[i].protocol ) ) - { - address->m_port = gServices[i].port ; - return GSOCK_NOERROR; - } - } + if ( strcmp( port , gServices[i].name ) == 0 ) + { + if ( protocol == NULL || strcmp( protocol , gServices[i].protocol ) ) + { + address->m_port = gServices[i].port ; + return GSOCK_NOERROR; + } + } } if (isdigit(port[0])) @@ -1374,17 +1381,17 @@ GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) assert(address != NULL); CHECK_ADDRESS(address, INET, GSOCK_INVADDR); address->m_port = port ; - + return GSOCK_NOERROR; } GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) { InetDomainName name ; - if ( !GSocket_Verify_Inited() ) + if ( !GSocket_Verify_Inited() ) return GSOCK_IOERR ; - - assert(address != NULL); + + assert(address != NULL); CHECK_ADDRESS(address, INET, GSOCK_INVADDR); OTInetAddressToName( gInetSvcRef , address->m_host , name ) ; @@ -1394,52 +1401,52 @@ GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t unsigned long GAddress_INET_GetHostAddress(GAddress *address) { - assert(address != NULL); - CHECK_ADDRESS(address, INET, 0); + assert(address != NULL); + CHECK_ADDRESS(address, INET, 0); return ntohl(address->m_host); } unsigned short GAddress_INET_GetPort(GAddress *address) { - assert(address != NULL); - CHECK_ADDRESS(address, INET, 0); + assert(address != NULL); + CHECK_ADDRESS(address, INET, 0); return address->m_port; } void GSocket::Enable_Events() { - if ( m_takesEvents ) - return ; - - { - OTResult state ; - m_takesEvents = true ; - state = OTGetEndpointState(m_endpoint); - - { - OTByteCount sz = 0 ; - OTCountDataBytes( m_endpoint , &sz ) ; - if ( state == T_INCON || sz > 0 ) - { - m_detected |= GSOCK_INPUT_FLAG ; - (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]); - } - } - { - if ( state == T_DATAXFER || state == T_INREL ) - { - m_detected |=GSOCK_OUTPUT_FLAG ; - (m_cbacks[GSOCK_OUTPUT])(this, GSOCK_OUTPUT, m_data[GSOCK_OUTPUT]); - } - } - } + if ( m_takesEvents ) + return ; + + { + OTResult state ; + m_takesEvents = true ; + state = OTGetEndpointState(m_endpoint); + + { + OTByteCount sz = 0 ; + OTCountDataBytes( m_endpoint , &sz ) ; + if ( state == T_INCON || sz > 0 ) + { + m_detected |= GSOCK_INPUT_FLAG ; + (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]); + } + } + { + if ( state == T_DATAXFER || state == T_INREL ) + { + m_detected |=GSOCK_OUTPUT_FLAG ; + (m_cbacks[GSOCK_OUTPUT])(this, GSOCK_OUTPUT, m_data[GSOCK_OUTPUT]); + } + } + } } void GSocket::Disable_Events() { - m_takesEvents = false ; + m_takesEvents = false ; } /* _GSocket_Input_Timeout: @@ -1455,20 +1462,20 @@ GSocketError GSocket::Input_Timeout() Microseconds(&start); now = start ; m_takesEvents = false ; - + while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < m_timeout * 1000.0 ) { - OTResult state ; - OTByteCount sz = 0 ; - state = OTGetEndpointState(m_endpoint); - - OTCountDataBytes( m_endpoint , &sz ) ; - if ( state == T_INCON || sz > 0 ) - { - m_takesEvents = formerTakesEvents ; - return GSOCK_NOERROR; - } - Microseconds(&now); + OTResult state ; + OTByteCount sz = 0 ; + state = OTGetEndpointState(m_endpoint); + + OTCountDataBytes( m_endpoint , &sz ) ; + if ( state == T_INCON || sz > 0 ) + { + m_takesEvents = formerTakesEvents ; + return GSOCK_NOERROR; + } + Microseconds(&now); } m_takesEvents = formerTakesEvents ; m_error = GSOCK_TIMEDOUT; @@ -1490,18 +1497,18 @@ GSocketError GSocket::Output_Timeout() Microseconds(&start); now = start ; m_takesEvents = false ; - + while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < m_timeout * 1000.0 ) { - OTResult state ; - state = OTGetEndpointState(m_endpoint); - - if ( state == T_DATAXFER || state == T_INREL ) - { - m_takesEvents = formerTakesEvents ; - return GSOCK_NOERROR; - } - Microseconds(&now); + OTResult state ; + state = OTGetEndpointState(m_endpoint); + + if ( state == T_DATAXFER || state == T_INREL ) + { + m_takesEvents = formerTakesEvents ; + return GSOCK_NOERROR; + } + Microseconds(&now); } m_takesEvents = formerTakesEvents ; m_error = GSOCK_TIMEDOUT; @@ -1515,14 +1522,14 @@ GSocketError GSocket::Output_Timeout() * operation, there is still data available, the callback function will * be called again. * GSOCK_OUTPUT: - * The socket is available for writing. That is, the next write call + * The socket is available for writing. That is, the next write call * won't block. This event is generated only once, when the connection is * first established, and then only if a call failed with GSOCK_WOULDBLOCK, * when the output buffer empties again. This means that the app should * assume that it can write since the first OUTPUT event, and no more * OUTPUT events will be generated unless an error occurs. * GSOCK_CONNECTION: - * Connection succesfully established, for client sockets, or incoming + * Connection successfully established, for client sockets, or incoming * client connection, for server sockets. Wait for this event (also watch * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. * GSOCK_LOST: @@ -1532,18 +1539,19 @@ GSocketError GSocket::Output_Timeout() void _GSocket_Internal_Proc(unsigned long e , void* d ) { - - GSocket * socket = (GSocket*) d ; - OTEventCode ev = (OTEventCode) e ; - GSocketEvent event; - GSocketEvent event2; - GSocketCallback cback; - char *data; - GSocketCallback cback2; - char *data2; - - if ( !socket ) - return ; + GSocket *socket = (GSocket*) d ; + + if ( !socket ) + return ; + + OTEventCode ev = (OTEventCode) e ; + GSocketEvent event; + GSocketEvent event2; + GSocketCallback cback; + char *data; + GSocketCallback cback2; + char *data2; + event = GSOCK_MAX_EVENT ; event2 = GSOCK_MAX_EVENT ; cback = NULL; @@ -1555,41 +1563,41 @@ void _GSocket_Internal_Proc(unsigned long e , void* d ) * destroyed) and for safety, check that the m_endpoint field * is what we expect it to be. */ - if ((socket != NULL) && (socket->m_takesEvents)) + if ( /* (socket != NULL) && */ (socket->m_takesEvents)) { - switch (ev) - { - case T_LISTEN : - event = GSOCK_CONNECTION ; - break ; - case T_CONNECT : - event = GSOCK_CONNECTION ; - event2 = GSOCK_OUTPUT ; - { - TCall retCall; - - retCall.addr.buf = NULL; - retCall.addr.maxlen = 0; - retCall.opt.buf = NULL; - retCall.opt.maxlen = 0; - retCall.udata.buf = NULL; - retCall.udata.maxlen= 0; - OTRcvConnect( socket->m_endpoint , &retCall ) ; - } - break ; - case T_DISCONNECT : - event = GSOCK_LOST ; - break ; - case T_GODATA : - case T_GOEXDATA : - event = GSOCK_OUTPUT ; - break ; - case T_DATA : - event = GSOCK_INPUT ; - break ; - case T_EXDATA : - event = GSOCK_INPUT ; - break ; + switch (ev) + { + case T_LISTEN : + event = GSOCK_CONNECTION ; + break ; + case T_CONNECT : + event = GSOCK_CONNECTION ; + event2 = GSOCK_OUTPUT ; + { + TCall retCall; + + retCall.addr.buf = NULL; + retCall.addr.maxlen = 0; + retCall.opt.buf = NULL; + retCall.opt.maxlen = 0; + retCall.udata.buf = NULL; + retCall.udata.maxlen = 0; + OTRcvConnect( socket->m_endpoint , &retCall ) ; + } + break ; + case T_DISCONNECT : + event = GSOCK_LOST ; + break ; + case T_GODATA : + case T_GOEXDATA : + event = GSOCK_OUTPUT ; + break ; + case T_DATA : + event = GSOCK_INPUT ; + break ; + case T_EXDATA : + event = GSOCK_INPUT ; + break ; } if (event != GSOCK_MAX_EVENT) {