📄 macsocket.cpp
字号:
if (!SocketIndexIsValid(inSocketNum)) { SetErrorMessageAndBail("MacSocket_close: Invalid socket number specified"); } theSocketStruct = &(sSockets[inSocketNum]); if (theSocketStruct->mEndPointRef != kOTInvalidEndpointRef) { OTResult theOTResult = noErr; // Try to play nice if (theSocketStruct->mReceivedTOrdRel) { // Already did an OTRcvOrderlyDisconnect() in the notifier if (theSocketStruct->mLocalEndIsConnected) { theOTResult = ::OTSndOrderlyDisconnect(theSocketStruct->mEndPointRef); theSocketStruct->mLocalEndIsConnected = false; } } else if (theSocketStruct->mLocalEndIsConnected) { theOTResult = ::OTSndOrderlyDisconnect(theSocketStruct->mEndPointRef); theSocketStruct->mLocalEndIsConnected = false; // Wait for other end to hang up too! // PrepareForAsyncOperation(theSocketStruct,T_ORDREL);//// errCode = MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTOrdRel)); } if (theOTResult != noErr) { ::OTCloseProvider(theSocketStruct->mEndPointRef); } else { theOTResult = ::OTCloseProvider(theSocketStruct->mEndPointRef); } theSocketStruct->mEndPointRef = kOTInvalidEndpointRef; errCode = theOTResult; } theSocketStruct->mIsInUse = false; 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);}// Receive some bytesint MacSocket_recv(const int inSocketNum,void *outBuff,int outBuffLength,const Boolean inBlock){OSErr errCode = noErr;int totalBytesRead = 0;SocketStruct *theSocketStruct = nil; SetErrorMessageAndBailIfNil(outBuff,"MacSocket_recv: Bad parameter, outBuff = nil"); if (outBuffLength <= 0) { SetErrorMessageAndBail("MacSocket_recv: Bad parameter, outBuffLength <= 0"); } if (!SocketIndexIsValid(inSocketNum)) { SetErrorMessageAndBail("MacSocket_recv: Invalid socket number specified"); } theSocketStruct = &(sSockets[inSocketNum]); if (!theSocketStruct->mLocalEndIsConnected) { SetErrorMessageAndBail("MacSocket_recv: Socket not connected"); } if (theSocketStruct->mReceivedTOrdRel) { totalBytesRead = 0; goto EXITPOINT; } PrepareForAsyncOperation(theSocketStruct,0); for (;;) { int bytesRead; OTResult theOTResult; theOTResult = ::OTRcv(theSocketStruct->mEndPointRef,(void *) ((unsigned long) outBuff + (unsigned long) totalBytesRead),outBuffLength - totalBytesRead,nil); if (theOTResult >= 0) { bytesRead = theOTResult; #ifdef MACSOCKET_DEBUG printf("MacSocket_recv: read %d bytes in part\n",bytesRead);#endif } else if (theOTResult == kOTNoDataErr) { bytesRead = 0; } else { SetErrorMessageAndLongIntAndBail("MacSocket_recv: Can't receive OT data, OTRcv() = ",theOTResult); } totalBytesRead += bytesRead; if (totalBytesRead <= 0) { if (theSocketStruct->mReceivedTOrdRel) { break; } // This seems pretty stupid to me now. Maybe I'll delete this blocking garbage. if (inBlock) { if (TimeoutElapsed(theSocketStruct)) { SetErrorCodeAndMessageAndBail(kMacSocket_TimeoutErr,"MacSocket_recv: Receive operation timed-out"); } if (theSocketStruct->mIdleWaitCallback != nil) { theOTResult = (*(theSocketStruct->mIdleWaitCallback))(theSocketStruct->mUserRefPtr); SetErrorMessageAndBailIfError(theOTResult,"MacSocket_recv: User cancelled operation"); } continue; } } break; } errCode = noErr;#ifdef MACSOCKET_DEBUG printf("MacSocket_recv: read %d bytes in total\n",totalBytesRead);#endif 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(totalBytesRead);}// Send some bytesint MacSocket_send(const int inSocketNum,const void *inBuff,int inBuffLength){OSErr errCode = noErr;int bytesSent = 0;SocketStruct *theSocketStruct = nil; SetErrorMessageAndBailIfNil(inBuff,"MacSocket_send: Bad parameter, inBuff = nil"); if (inBuffLength <= 0) { SetErrorMessageAndBail("MacSocket_send: Bad parameter, inBuffLength <= 0"); } if (!SocketIndexIsValid(inSocketNum)) { SetErrorMessageAndBail("MacSocket_send: Invalid socket number specified"); } theSocketStruct = &(sSockets[inSocketNum]); if (!theSocketStruct->mLocalEndIsConnected) { SetErrorMessageAndBail("MacSocket_send: Socket not connected"); }OTResult theOTResult; PrepareForAsyncOperation(theSocketStruct,0); while (bytesSent < inBuffLength) { if (theSocketStruct->mIdleWaitCallback != nil) { theOTResult = (*(theSocketStruct->mIdleWaitCallback))(theSocketStruct->mUserRefPtr); SetErrorMessageAndBailIfError(theOTResult,"MacSocket_send: User cancelled"); } theOTResult = ::OTSnd(theSocketStruct->mEndPointRef,(void *) ((unsigned long) inBuff + bytesSent),inBuffLength - bytesSent,0); if (theOTResult >= 0) { bytesSent += theOTResult; theOTResult = noErr; // Reset timer.... PrepareForAsyncOperation(theSocketStruct,0); } if (theOTResult == kOTFlowErr) { if (TimeoutElapsed(theSocketStruct)) { SetErrorCodeAndMessageAndBail(kMacSocket_TimeoutErr,"MacSocket_send: Send timed-out") } theOTResult = noErr; } SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_send: Can't send OT data, OTSnd() = ",theOTResult); } errCode = noErr;#ifdef MACSOCKET_DEBUG printf("MacSocket_send: sent %d bytes\n",bytesSent);#endif 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)); } } if (errCode != noErr) { ::SysBeep(1); } errno = errCode; return(bytesSent);}static OSStatus NegotiateIPReuseAddrOption(EndpointRef inEndpoint,const Boolean inEnableReuseIP){OSStatus errCode;UInt8 buf[kOTFourByteOptionSize];TOption* theOTOption;TOptMgmt theOTRequest;TOptMgmt theOTResult; if (!OTIsSynchronous(inEndpoint)) { SetErrorMessageAndBail("NegotiateIPReuseAddrOption: Open Transport endpoint is not synchronous"); } theOTRequest.opt.buf = buf; theOTRequest.opt.len = sizeof(buf); theOTRequest.flags = T_NEGOTIATE; theOTResult.opt.buf = buf; theOTResult.opt.maxlen = kOTFourByteOptionSize; theOTOption = (TOption *) buf; theOTOption->level = INET_IP; theOTOption->name = IP_REUSEADDR; theOTOption->len = kOTFourByteOptionSize; theOTOption->status = 0; *((UInt32 *) (theOTOption->value)) = inEnableReuseIP; errCode = ::OTOptionManagement(inEndpoint,&theOTRequest,&theOTResult); if (errCode == kOTNoError) { if (theOTOption->status != T_SUCCESS) { errCode = theOTOption->status; } else { errCode = kOTNoError; } } EXITPOINT: errno = errCode; return(errCode);}// Some rough notes....// OTAckSends(ep);// OTAckSends(ep) // enable AckSend option// ......// buf = OTAllocMem( nbytes); // Allocate nbytes of memory from OT// OTSnd(ep, buf, nbytes, 0); // send a packet// ......// NotifyProc( .... void* theParam) // Notifier Proc// case T_MEMORYRELEASED: // process event// OTFreeMem( theParam); // free up memory// break;/*struct InetInterfaceInfo{ InetHost fAddress; InetHost fNetmask; InetHost fBroadcastAddr; InetHost fDefaultGatewayAddr; InetHost fDNSAddr; UInt16 fVersion; UInt16 fHWAddrLen; UInt8* fHWAddr; UInt32 fIfMTU; UInt8* fReservedPtrs[2]; InetDomainName fDomainName; UInt32 fIPSecondaryCount; UInt8 fReserved[252]; };typedef struct InetInterfaceInfo InetInterfaceInfo;((InetAddress *) addr.buf)->fHoststruct TBind{ TNetbuf addr; OTQLen qlen;};typedef struct TBind TBind;struct TNetbuf{ size_t maxlen; size_t len; UInt8* buf;};typedef struct TNetbuf TNetbuf; struct InetAddress{ OTAddressType fAddressType; // always AF_INET InetPort fPort; // Port number InetHost fHost; // Host address in net byte order UInt8 fUnused[8]; // Traditional unused bytes};typedef struct InetAddress InetAddress;*//*static pascal void Notifier(void* context, OTEventCode event, OTResult result, void* cookie){EPInfo* epi = (EPInfo*) context; switch (event) { case T_LISTEN: { DoListenAccept(); return; } case T_ACCEPTCOMPLETE: { if (result != kOTNoError) DBAlert1("Notifier: T_ACCEPTCOMPLETE - result %d",result); return; } case T_PASSCON: { if (result != kOTNoError) { DBAlert1("Notifier: T_PASSCON result %d", result); return; } OTAtomicAdd32(1, &gCntrConnections); OTAtomicAdd32(1, &gCntrTotalConnections); OTAtomicAdd32(1, &gCntrIntervalConnects); if ( OTAtomicSetBit(&epi->stateFlags, kPassconBit) != 0 ) { ReadData(epi); } return; } case T_DATA: { if ( OTAtomicSetBit(&epi->stateFlags, kPassconBit) != 0 ) { ReadData(epi); } return; } case T_GODATA: { SendData(epi); return; } case T_DISCONNECT: { DoRcvDisconnect(epi); return; } case T_DISCONNECTCOMPLETE: { if (result != kOTNoError) DBAlert1("Notifier: T_DISCONNECT_COMPLETE result %d",result); return; } case T_MEMORYRELEASED: { OTAtomicAdd32(-1, &epi->outstandingSends); return; } default: { DBAlert1("Notifier: unknown event <%x>", event); return; } }}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -