📄 ot_udp.cp
字号:
/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file, are
* subject to the current version of the RealNetworks Public Source License
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
* in which case the RCSL will apply. You may also obtain the license terms
* directly from RealNetworks. You may not use this file except in
* compliance with the RPSL or, if you have a valid RCSL with RealNetworks
* applicable to this file, the RCSL. Please see the applicable RPSL or
* RCSL for the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the portions
* it created.
*
* This file, and the files included with this file, is distributed and made
* available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
#include "hxcom.h"
#include "hxbuffer.h"
#include "timebuff.h"
#include "hxtick.h"
#include "netbyte.h"
#include "OT_UDP.h"
#include "hxerrors.h"
#include "MWDebug.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "hxmm.h"
//#define USE_DEFERRED_IMPL 1
OT_UDP::OT_UDP (void)
{ /* begin OT_UDP */
m_pInBuffer = new char[TCP_BUF_SIZE];
m_bIsBroadcastEnabled = FALSE;
} /* end OT_UDP */
OT_UDP::~OT_UDP (void)
{ /* begin ~OT_UDP */
Cleanup();
} /* end ~OT_UDP */
HX_RESULT
OT_UDP::init(UINT32 local_addr, UINT16 port, UINT16 blocking)
{
HX_RESULT theErr = Create();
if(!theErr) theErr = ActiveOpen(port);
if(theErr) Cleanup();
return theErr;
}
void
OT_UDP::done(void)
{
Cleanup();
}
/*----------------------------------------------------------------------------
Create
Create a UDP endpoint.
Exit: function result = error code.
----------------------------------------------------------------------------*/
OSErr OT_UDP::Create(void)
{
OSErr theErr = HXR_OK;
// clear completion flag
mComplete = false;
// open UDP endpoint
#ifdef USE_DEFERRED_IMPL
theErr = OTAsyncOpenEndpoint(OTCreateConfiguration(kUDPName), 0, nil, UDPTCPNotifyProc, this);
#else
#ifdef _CARBON
theErr = OTAsyncOpenEndpointInContext(OTCreateConfiguration(kUDPName), 0, nil,
::NewOTNotifyUPP(UDPNotifyProc), this, NULL);
#else
theErr = OTAsyncOpenEndpoint(OTCreateConfiguration(kUDPName), 0, nil,
UDPNotifyProc, this);
#endif
#endif
// wait for completion
if(!theErr) theErr = OTWait();
// check for errors
if (theErr || mCode != T_OPENCOMPLETE)
theErr = HXR_SOCKET_CREATE;
if(!theErr)
{
mRef = (EndpointRef) mCookie; // save UDP provider reference
mDataArrived = FALSE;
mDataFlowOn = FALSE;
}
#if 0
if (!theErr)
{
UINT32 uBufDepth = 0;
// XTI_RCVBUF : size of internal buffer
// XTI_RCVLOWAT : minimum accumulated size for notification
OT_net::GetFourByteOption(mRef, XTI_GENERIC, XTI_RCVBUF, &uBufDepth);
OT_net::GetFourByteOption(mRef, XTI_GENERIC, XTI_RCVLOWAT, &uBufDepth);
OT_net::SetFourByteOption(mRef, XTI_GENERIC, XTI_RCVBUF, 64000);
OT_net::SetFourByteOption(mRef, XTI_GENERIC, XTI_RCVLOWAT, 32000);
OT_net::GetFourByteOption(mRef, XTI_GENERIC, XTI_RCVBUF, &uBufDepth);
OT_net::GetFourByteOption(mRef, XTI_GENERIC, XTI_RCVLOWAT, &uBufDepth);
OT_net::GetFourByteOption(mRef, XTI_GENERIC, XTI_RCVLOWAT, &uBufDepth);
}
#endif
if(theErr) // error occured close provider
{
if(mRef != 0) OTCloseProvider(mRef);
mRef = 0;
}
mLastError = theErr;
return theErr;
}
/*----------------------------------------------------------------------------
Release
Release a UDP stream.
Exit: function result = error code.
Any active connection is also aborted, if necessary, before releasing
the stream.
----------------------------------------------------------------------------*/
OSErr OT_UDP::Cleanup (void)
{
OSErr theErr = HXR_OK;
Boolean abort;
abort = !mOtherSideHasClosed || !mWeHaveClosed;
if(mRef)
{
OTRemoveNotifier(mRef);
theErr = OTCloseProvider(mRef);
mRef = 0;
mConnectionOpen = FALSE;
mOtherSideHasClosed = TRUE;
mWeHaveClosed = TRUE;
}
HX_VECTOR_DELETE(m_pInBuffer);
mLastError = theErr;
return theErr;
}
/*----------------------------------------------------------------------------
ActiveOpen
Open an active stream.
addr = IP address of server.
port = port number of service.
Exit: function result = error code.
----------------------------------------------------------------------------*/
OSErr OT_UDP::ActiveOpen (
UINT16 port)
{
OSErr theErr = HXR_OK;
InetAddress reqAd,retAd;
TBind req,ret;
// get Inet address of port
OTInitInetAddress(&reqAd, port, (InetHost) 0);
// bind udp to current address and requested port
req.addr.maxlen = sizeof(struct InetAddress);
req.addr.len = sizeof(struct InetAddress);
req.addr.buf = (unsigned char *) &reqAd;
req.qlen = 1; // don't care for udp
ret.addr.maxlen = sizeof(struct InetAddress);
ret.addr.len = sizeof(struct InetAddress);
ret.addr.buf = (unsigned char *) &retAd;
// clear completion flag
mComplete = false;
// bind provider to return structure
theErr = OTBind(mRef, &req, &ret);
if(theErr)
{
mLastError = theErr;
theErr = HXR_BIND;
}
// wait for bind completion
if(!theErr)
{
theErr = OTWait();
if(theErr == kOTNoDataErr) theErr = HXR_OK;
if(theErr || mCode != T_BINDCOMPLETE)
{
theErr = theErr ? theErr : HXR_BIND;
mLastError = theErr;
theErr = HXR_BIND;
}
}
// get local port
if(!theErr)
{
mLocalPort = reqAd.fPort;
mConnectionOpen = TRUE;
mOtherSideHasClosed = FALSE;
mWeHaveClosed = FALSE;
}
return theErr;
}
/*----------------------------------------------------------------------------
write
write data on a UDP stream.
Entry: data = pointer to data to send.
len = length of data to send.
Exit: function result = error code.
----------------------------------------------------------------------------*/
HX_RESULT
OT_UDP::writeto (
void *buf,
UINT16 *size,
ULONG32 addr,
UINT16 port)
{
OSStatus result;
TUnitData unitdata;
InetAddress sin;
TOption option;
HX_RESULT theErr;
::OTInitInetAddress(&sin, port, addr);
unitdata.addr.len = sizeof(struct InetAddress);
unitdata.addr.buf = (UInt8*) &sin;
unitdata.udata.len = *size;
unitdata.udata.buf = (UInt8*) buf;
if(m_bIsBroadcastEnabled)
{
unitdata.opt.len = sizeof(option);
option.len = kOTFourByteOptionSize;
option.level = INET_IP;
#ifdef _MAC_MACHO
option.name = kIP_BROADCAST;
#else
option.name = IP_BROADCAST;
#endif
option.status = 0;
option.value[0] = 1;
unitdata.opt.buf = (UInt8*) &option;
}
else
{
unitdata.opt.len = 0;
unitdata.opt.maxlen = 0;
unitdata.opt.buf = 0;
}
result = ::OTSndUData(mRef,&unitdata);
if(result == 0)
{
return HXR_OK;
}
switch(result)
{
case kOTFlowErr:
mDataFlowOn = TRUE;
theErr = HXR_WOULD_BLOCK;
break;
case kENOMEMErr:
case kEBUSYErr:
case kOTOutStateErr:
case kEWOULDBLOCKErr:
case kOTStateChangeErr:
theErr = HXR_WOULD_BLOCK;
break;
case kOTLookErr:
{
OSStatus lookErr = ::OTLook(mRef);
TUDErr UDErr;
if(lookErr == T_UDERR)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -