📄 macsocket.cpp
字号:
break; } case T_PASSCON: { theSocketStruct->mReceivedTPassCon = true; theSocketStruct->mLocalEndIsConnected = true; theSocketStruct->mRemoteEndIsConnected = true; break; } case T_DATA: { theSocketStruct->mReadyToReadData = true; break; } case T_GODATA: { theSocketStruct->mReadyToWriteData = true; break; } case T_DISCONNECT: { theSocketStruct->mReceivedTDisconnect = true; theSocketStruct->mRemoteEndIsConnected = false; theSocketStruct->mLocalEndIsConnected = false; ::OTRcvDisconnect(theSocketStruct->mEndPointRef,nil); break; } case T_ORDREL: { theSocketStruct->mReceivedTOrdRel = true; // We can still write data, so don't clear mRemoteEndIsConnected ::OTRcvOrderlyDisconnect(theSocketStruct->mEndPointRef); break; } case T_DISCONNECTCOMPLETE: { theSocketStruct->mReceivedTDisconnectComplete = true; theSocketStruct->mRemoteEndIsConnected = false; theSocketStruct->mLocalEndIsConnected = false; break; } } }/*T_LISTEN OTListenT_CONNECT OTRcvConnectT_DATA OTRcv, OTRcvUDataT_DISCONNECT OTRcvDisconnectT_ORDREL OTRcvOrderlyDisconnectT_GODATA OTSnd, OTSndUData, OTLookT_PASSCON noneT_EXDATA OTRcvT_GOEXDATA OTSnd, OTLookT_UDERR OTRcvUDErr*/}// Initialize the main socket data structureOSErr MacSocket_Startup(void){ if (!sSocketsSetup) { for (int i = 0;i < kMaxNumSockets;i++) { InitSocket(&(sSockets[i])); } ::InitOpenTransport(); sSocketsSetup = true; } return(noErr);}// Cleanup before exitingOSErr MacSocket_Shutdown(void){ if (sSocketsSetup) { for (int i = 0;i < kMaxNumSockets;i++) { SocketStruct *theSocketStruct = &(sSockets[i]); if (theSocketStruct->mIsInUse) { if (theSocketStruct->mEndPointRef != kOTInvalidEndpointRef) { OTResult theOTResult; // Since we're killing the endpoint, I don't bother to send the disconnect (sorry!)/* if (theSocketStruct->mLocalEndIsConnected) { // This is an abortive action, so we do a hard disconnect instead of an OTSndOrderlyDisconnect theOTResult = ::OTSndDisconnect(theSocketStruct->mEndPointRef, nil); // Now we have to watch for T_DISCONNECTCOMPLETE event theSocketStruct->mLocalEndIsConnected = false; }*/ theOTResult = ::OTCloseProvider(theSocketStruct->mEndPointRef); theSocketStruct->mEndPointRef = kOTInvalidEndpointRef; } if (theSocketStruct->mBindRequestedAddrInfo != nil) { ::OTFree((void *) theSocketStruct->mBindRequestedAddrInfo,T_BIND); theSocketStruct->mBindRequestedAddrInfo = nil; } if (theSocketStruct->mAssignedAddrInfo != nil) { ::OTFree((void *) theSocketStruct->mAssignedAddrInfo,T_BIND); theSocketStruct->mAssignedAddrInfo = nil; } if (theSocketStruct->mRemoteAddrInfo != nil) { ::OTFree((void *) theSocketStruct->mRemoteAddrInfo,T_CALL); theSocketStruct->mRemoteAddrInfo = nil; } } } ::CloseOpenTransport(); sSocketsSetup = false; } return(noErr);}// Allocate a socketOSErr MacSocket_socket(int *outSocketNum,const Boolean inDoThreadSwitching,const long inTimeoutTicks,MacSocket_IdleWaitCallback inIdleWaitCallback,void *inUserRefPtr){// Gotta roll support back in for threads eventually.....#pragma unused(inDoThreadSwitching)OSErr errCode = noErr; SetErrorMessageAndBailIfNil(outSocketNum,"MacSocket_socket: Bad parameter, outSocketNum == nil"); *outSocketNum = -1; // Find an unused socket for (int i = 0;i < kMaxNumSockets;i++) { if (sSockets[i].mIsInUse == false) { OTResult theOTResult; SocketStruct *theSocketStruct = &(sSockets[i]); InitSocket(theSocketStruct); theSocketStruct->mIdleWaitCallback = inIdleWaitCallback; theSocketStruct->mUserRefPtr = inUserRefPtr; theSocketStruct->mTimeoutTicks = inTimeoutTicks; // Set up OT endpoint PrepareForAsyncOperation(theSocketStruct,T_OPENCOMPLETE); theOTResult = ::OTAsyncOpenEndpoint(OTCreateConfiguration(kTCPName),0,nil,OTNonYieldingNotifier,(void *) theSocketStruct); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_socket: Can't create OT endpoint, OTAsyncOpenEndpoint() = ",theOTResult); BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTOpenComplete))); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_socket: Can't create OT endpoint, OTAsyncOpenEndpoint() = ",theOTResult); *outSocketNum = i; errCode = noErr; theSocketStruct->mIsInUse = true; break; } else if (i == kMaxNumSockets - 1) { SetErrorMessageAndBail("MacSocket_socket: No sockets available"); } }EXITPOINT: errno = errCode; return(errCode);}OSErr MacSocket_listen(const int inSocketNum,const int inPortNum){OSErr errCode = noErr;SocketStruct *theSocketStruct = nil; if (!SocketIndexIsValid(inSocketNum)) { SetErrorMessageAndBail("MacSocket_listen: Invalid socket number specified"); } theSocketStruct = &(sSockets[inSocketNum]);OTResult theOTResult; if (theSocketStruct->mBindRequestedAddrInfo == nil) { theSocketStruct->mBindRequestedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult); SetErrorMessageAndBailIfNil(theSocketStruct->mBindRequestedAddrInfo,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() returned nil"); } if (theSocketStruct->mAssignedAddrInfo == nil) { theSocketStruct->mAssignedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult); SetErrorMessageAndBailIfNil(theSocketStruct->mAssignedAddrInfo,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() returned nil"); } if (theSocketStruct->mRemoteAddrInfo == nil) { theSocketStruct->mRemoteAddrInfo = (TCall *) ::OTAlloc(theSocketStruct->mEndPointRef,T_CALL,T_ADDR,&theOTResult); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_CALL structure, OTAlloc() = ",theOTResult); SetErrorMessageAndBailIfNil(theSocketStruct->mRemoteAddrInfo,"MacSocket_listen: Can't allocate OT T_CALL structure, OTAlloc() returned nil"); } if (!theSocketStruct->mEndpointIsBound) { InetInterfaceInfo theInetInterfaceInfo; theOTResult = ::OTInetGetInterfaceInfo(&theInetInterfaceInfo,kDefaultInetInterface); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't determine OT interface info, OTInetGetInterfaceInfo() = ",theOTResult); InetAddress *theInetAddress = (InetAddress *) theSocketStruct->mBindRequestedAddrInfo->addr.buf; // theInetAddress->fAddressType = AF_INET;// theInetAddress->fPort = inPortNum;// theInetAddress->fHost = theInetInterfaceInfo.fAddress; ::OTInitInetAddress(theInetAddress,inPortNum,theInetInterfaceInfo.fAddress); theSocketStruct->mBindRequestedAddrInfo->addr.len = sizeof(InetAddress); theSocketStruct->mBindRequestedAddrInfo->qlen = 1; theOTResult = ::OTSetSynchronous(theSocketStruct->mEndPointRef); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT endpoint mode, OTSetSynchronous() = ",theOTResult); theOTResult = NegotiateIPReuseAddrOption(theSocketStruct->mEndPointRef,true); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT IP address reuse flag, NegotiateIPReuseAddrOption() = ",theOTResult); theOTResult = ::OTSetAsynchronous(theSocketStruct->mEndPointRef); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT endpoint mode, OTSetAsynchronous() = ",theOTResult); PrepareForAsyncOperation(theSocketStruct,T_BINDCOMPLETE); theOTResult = ::OTBind(theSocketStruct->mEndPointRef,theSocketStruct->mBindRequestedAddrInfo,theSocketStruct->mAssignedAddrInfo); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't bind OT endpoint, OTBind() = ",theOTResult); BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTBindComplete))); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't bind OT endpoint, OTBind() = ",theOTResult); theSocketStruct->mEndpointIsBound = true; } PrepareForAsyncOperation(theSocketStruct,T_LISTEN); theOTResult = ::OTListen(theSocketStruct->mEndPointRef,theSocketStruct->mRemoteAddrInfo); if (theOTResult == noErr) { PrepareForAsyncOperation(theSocketStruct,T_PASSCON); theOTResult = ::OTAccept(theSocketStruct->mEndPointRef,theSocketStruct->mEndPointRef,theSocketStruct->mRemoteAddrInfo); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't begin OT accept, OTAccept() = ",theOTResult); BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTPassCon))); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't accept OT connection, OTAccept() = ",theOTResult); } else if (theOTResult == kOTNoDataErr) { theOTResult = noErr; } else { SetErrorMessageAndLongIntAndBail("MacSocket_listen: Can't begin OT listen, OTListen() = ",theOTResult); } errCode = noErr;EXITPOINT: if (theSocketStruct != nil) { theSocketStruct->mLastError = noErr; CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); if (errCode != noErr) { theSocketStruct->mLastError = errCode; CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); } } errno = errCode; return(errCode);}OSErr MacSocket_connect(const int inSocketNum,char *inTargetAddressAndPort){OSErr errCode = noErr;SocketStruct *theSocketStruct = nil; if (!SocketIndexIsValid(inSocketNum)) { SetErrorMessageAndBail("MacSocket_connect: Invalid socket number specified"); } theSocketStruct = &(sSockets[inSocketNum]); if (theSocketStruct->mEndpointIsBound) { SetErrorMessageAndBail("MacSocket_connect: Socket previously bound"); } OTResult theOTResult; theSocketStruct->mBindRequestedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult); SetErrorMessageAndBailIfNil(theSocketStruct->mBindRequestedAddrInfo,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() returned nil"); theSocketStruct->mAssignedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult); SetErrorMessageAndBailIfNil(theSocketStruct->mAssignedAddrInfo,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() returned nil"); theSocketStruct->mRemoteAddrInfo = (TCall *) ::OTAlloc(theSocketStruct->mEndPointRef,T_CALL,T_ADDR,&theOTResult); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_CALL structure, OTAlloc() = ",theOTResult); SetErrorMessageAndBailIfNil(theSocketStruct->mRemoteAddrInfo,"MacSocket_connect: Can't allocate OT T_CALL structure, OTAlloc() returned nil"); PrepareForAsyncOperation(theSocketStruct,T_BINDCOMPLETE); theOTResult = ::OTBind(theSocketStruct->mEndPointRef,nil,theSocketStruct->mAssignedAddrInfo); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't bind OT endpoint, OTBind() = ",theOTResult); BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTBindComplete))); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't bind OT endpoint, OTBind() = ",theOTResult); theSocketStruct->mEndpointIsBound = true; TCall sndCall;DNSAddress hostDNSAddress; // Set up target address sndCall.addr.buf = (UInt8 *) &hostDNSAddress; sndCall.addr.len = ::OTInitDNSAddress(&hostDNSAddress,inTargetAddressAndPort); sndCall.opt.buf = nil; sndCall.opt.len = 0; sndCall.udata.buf = nil; sndCall.udata.len = 0; sndCall.sequence = 0; // Connect! PrepareForAsyncOperation(theSocketStruct,T_CONNECT); theOTResult = ::OTConnect(theSocketStruct->mEndPointRef,&sndCall,nil); if (theOTResult == kOTNoDataErr) { theOTResult = noErr; } SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't connect OT endpoint, OTConnect() = ",theOTResult); BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTConnect))); if (theOTResult == kMacSocket_TimeoutErr) { SetErrorMessageAndBail("MacSocket_connect: Can't connect OT endpoint, OTConnect() = kMacSocket_TimeoutErr"); } else { SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't connect OT endpoint, OTConnect() = ",theOTResult); } theOTResult = ::OTRcvConnect(theSocketStruct->mEndPointRef,nil); SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't complete connect on OT endpoint, OTRcvConnect() = ",theOTResult); errCode = noErr;#ifdef MACSOCKET_DEBUG printf("MacSocket_connect: connect completed\n");#endifEXITPOINT: if (theSocketStruct != nil) { theSocketStruct->mLastError = noErr; CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); if (errCode != noErr) { theSocketStruct->mLastError = errCode; CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); } } errno = errCode; return(errCode);}// Close a connectionOSErr MacSocket_close(const int inSocketNum){OSErr errCode = noErr;SocketStruct *theSocketStruct = nil;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -