- * Project: GSocket (Generic Socket) for WX
- * Name: gsocket.cpp
- * Authors: Guilhem Lavaux,
- * Guillermo Rodriguez Garcia <guille@iies.es> (maintainer)
- * Stefan CSomor
- * Purpose: GSocket main mac file
- * CVSID: $Id$
+ * Project: GSocket (Generic Socket) for WX
+ * Name: gsocket.cpp
+ * Copyright: (c) Guilhem Lavaux
+ * Licence: wxWindows Licence
+ * Authors: Guilhem Lavaux,
+ * Guillermo Rodriguez Garcia <guille@iies.es> (maintainer)
+ * Stefan CSomor
+ * Purpose: GSocket main mac file
+ * CVSID: $Id$
- #include <MacHeaders.c>
- #define OTUNIXERRORS 1
- #include <OpenTransport.h>
- #include <OpenTransportProviders.h>
- #include <OpenTptInternet.h>
+ #include <MacHeaders.c>
+ #define OTUNIXERRORS 1
+ #include <OpenTransport.h>
+ #include <OpenTransportProviders.h>
+ #include <OpenTptInternet.h>
-#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"
OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode);
/* Input: ep - endpointref on which to negotiate the option
OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode);
/* Input: ep - endpointref on which to negotiate the option
- 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
- 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
+ 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;
- ret.opt.buf = buf;
- ret.opt.maxlen = kOTFourByteOptionSize;
+ 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
- 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)
{
}
pascal void OTInetEventHandler(void*s, OTEventCode event, OTResult, void *cookie) ;
pascal void OTInetEventHandler(void*s, OTEventCode event, OTResult result, void *cookie)
{
- // 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);
InitOpenTransportInContext(kInitOTForApplicationMask, &clientcontext);
gOTInited = 1 ;
gInetSvcRef = OTOpenInternetServicesInContext(kDefaultInternetServicesPath,
InitOpenTransportInContext(kInitOTForApplicationMask, &clientcontext);
gOTInited = 1 ;
gInetSvcRef = OTOpenInternetServicesInContext(kDefaultInternetServicesPath,
InitOpenTransport() ;
gOTInited = 1 ;
gInetSvcRef = OTOpenInternetServices(kDefaultInternetServicesPath, NULL, &err);
#endif
if ( gInetSvcRef == NULL || err != kOTNoError )
{
InitOpenTransport() ;
gOTInited = 1 ;
gInetSvcRef = OTOpenInternetServices(kDefaultInternetServicesPath, NULL, &err);
#endif
if ( gInetSvcRef == NULL || err != kOTNoError )
{
- /* 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 ;
+ }
- /* 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;
+ }
- /* 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;
+ }
- {
- 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;
+ }
* 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:
* 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:
* GSOCK_TIMEDOUT - timeout, no incoming connections.
* GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
* GSOCK_MEMERR - couldn't allocate memory.
* GSOCK_TIMEDOUT - timeout, no incoming connections.
* GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
* GSOCK_MEMERR - couldn't allocate memory.
* 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
* 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
* 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()
* 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()
* GSOCK_TIMEDOUT - timeout, the connection failed.
* GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
* GSOCK_MEMERR - couldn't allocate memory.
* GSOCK_TIMEDOUT - timeout, the connection failed.
* GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
* GSOCK_MEMERR - couldn't allocate memory.
OTOpenEndpointInContext( OTCreateConfiguration( kTCPName) , 0 , &info , &err , NULL ) ;
#else
OTOpenEndpointInContext( OTCreateConfiguration( kTCPName) , 0 , &info , &err , NULL ) ;
#else
OTOpenEndpoint( OTCreateConfiguration( kTCPName) , 0 , &info , &err ) ;
#endif
if ( m_endpoint == kOTInvalidEndpointRef || err != kOTNoError )
OTOpenEndpoint( OTCreateConfiguration( kTCPName) , 0 , &info , &err ) ;
#endif
if ( m_endpoint == kOTInvalidEndpointRef || err != kOTNoError )
* is in blocking mode, we select() for the specified timeout
* checking for writability to see if the connection request
* completes.
* is in blocking mode, we select() for the specified timeout
* checking for writability to see if the connection request
* completes.
ret = Recv_Stream(buffer, size);
else
ret = Recv_Dgram(buffer, size);
ret = Recv_Stream(buffer, size);
else
ret = Recv_Dgram(buffer, size);
ret = Send_Stream(buffer, size);
else
ret = Send_Dgram(buffer, size);
ret = Send_Stream(buffer, size);
else
ret = Send_Dgram(buffer, size);
* 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:
* 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:
* 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:
* 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:
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
OTInetAddressToName( gInetSvcRef , address->m_host , name ) ;
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
OTInetAddressToName( gInetSvcRef , address->m_host , name ) ;
- assert(address != NULL);
- CHECK_ADDRESS(address, INET, 0);
+ assert(address != NULL);
+ CHECK_ADDRESS(address, INET, 0);
- assert(address != NULL);
- CHECK_ADDRESS(address, INET, 0);
+ assert(address != NULL);
+ CHECK_ADDRESS(address, INET, 0);
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);
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;
}
OTCountDataBytes( m_endpoint , &sz ) ;
if ( state == T_INCON || sz > 0 )
{
m_takesEvents = formerTakesEvents ;
return GSOCK_NOERROR;
}
while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < m_timeout * 1000.0 )
{
OTResult state ;
state = OTGetEndpointState(m_endpoint);
while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < m_timeout * 1000.0 )
{
OTResult state ;
state = OTGetEndpointState(m_endpoint);
* 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:
* 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:
* 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:
* 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:
-
- 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;
+
* destroyed) and for safety, check that the m_endpoint field
* is what we expect it to be.
*/
* destroyed) and for safety, check that the m_endpoint field
* is what we expect it to be.
*/