FP1的UDP接收问题,内有源码
这个类在FP2模拟器上运行良好,但是在FP1的模拟器上会卡死,只能关闭模拟器。我测试过了,是卡在接收这个活动对象里,只要我不调用接收的活动对象,就没有问题。
udpsocket.h
#ifndef UDPSOCKET_H
#define UDPSOCKET_H
// INCLUDES
#include <e32std.h>
#include <e32base.h>
#include <es_sock.h>
#include <in_sock.h>
#include "SocketDgram.h"
// CLASS DECLARATION
class CUdpReader;
class CUdpWriter;
/**
* CUDPSocket
*
*/
class CUDPSocket: public CUdpReader::MObserver, public CUdpWriter::MObserver
{
public:
///Rtp package Observer
class MAppObserver
{
public:
virtual void OnRtpPacketReceived(const TDesC8 &aPayloadData, TInt aRtpType) = 0;
};
public:
// Constructors and destructor
/**
* Constructor for performing 1st stage construction
*/
CUDPSocket(MAppObserver& aPacketObserver,RSocketServ& aSocketServer,
RConnection& aConnection,TInetAddr& aRtpAddr,TInetAddr& aRtcpAddr);
/**
* Destructor.
*/
virtual ~CUDPSocket();
/**
* Two-phased constructor.
*/
static CUDPSocket* NewL(MAppObserver& aPacketObserver,RSocketServ& aSocketServer,
RConnection& aConnection,TInetAddr& aRtpAddr,TInetAddr& aRtcpAddr);
/**
* Two-phased constructor.
*/
static CUDPSocket* NewLC(MAppObserver& aPacketObserver,RSocketServ& aSocketServer,
RConnection& aConnection,TInetAddr& aRtpAddr,TInetAddr& aRtcpAddr);
/**
* EPOC default constructor for performing 2nd stage construction
*/
void ConstructL();
public:
/* Close existing RTP session
*
*/
void CloseSession();
/*!
@function StartRTPConnection
@Initializes RTP streams.
*/
void StartRTPConnectionL(TUint aLocalPort = 5000, TBool aEnableRtcp = ETrue, TInt aRtpType = 0);
/* Send rtp packet
*
*/
void SendRtpPacketL(TDesC8& aBuffer);
/*!
@function StartSending
@Starts sending the RTP packets to Remote IP address.
*/
void StartSending();
/*!
@function StopSending
@Starts sending the RTP packets to Remote IP address.
*/
void StopSending();
void ErrorNotify(TInt aErrCode);
//From MTimerNotify
void TimerExpired(TInt aError);
// From MRtpObserver
//void RtpPacketReceived(TRtpId aStreamId,const TRtpRecvHeader & aHeaderInfo, const TDesC8 & aPayloadData);
//From MRtcpObserver
//void SdesReceived(TRtpSSRC aSSRC, const TRtpSdesParams& aParams);
//void ByeReceived(TRtpId aStreamId, TRtpSSRC aSSRC, const TDesC8& aReason);
//void AppReceived(TRtpId aStreamId, TRtpSSRC aSSRC, const TRtcpApp& aApp);
//void SrReceived(TRtpId aStreamId, TRtpSSRC aSSRC,const TTimeStamps& aTimeStamps);
//void RrReceived(TRtpId aStreamId, TRtpSSRC aSSRC);
TInt SendPackage(TDesC8& aBuf);//发送常规数据(非RTP包)
// from CUdpReader::MObServer
void OnReceiveL(const TDesC& aData, TInt aError);
// from CUdpWriter::MObServer
void OnSendL(const TDesC& aMsg, TInt aError);
void ReceiveL();
private:
MAppObserver& iPacketObserver;//观察者
RSocketServ& iSocketServer;//连接系统socket服务提供者
RConnection& iConnection;//选择接入点
TInetAddr& iRtpAddr;//视频RTP地址端口
TInetAddr& iRtcpAddr;//视频RTCP地址端口
TBool iEnableRtcp;
TInt iRtpType;
TInt iLastQueueId;
CUdpReader *iReciever;
CUdpWriter *iSender;
RSocketiRtp;
RSocketiRtcp;
TInetAddr iLocalAddr;
};
#endif // UDPSOCKET_H
#include "UDPSocket.h"
#include "SocketDgram.h"
CUDPSocket::CUDPSocket(MAppObserver& aPacketObserver,RSocketServ& aSocketServer,
RConnection& aConnection,TInetAddr& aRtpAddr,TInetAddr& aRtcpAddr):
iPacketObserver(aPacketObserver),iSocketServer(aSocketServer),iConnection(aConnection),
iRtpAddr(aRtpAddr),iRtcpAddr(aRtcpAddr)
{
// No implementation required
}
CUDPSocket::~CUDPSocket()
{
}
void CUDPSocket::CloseSession()
{
}
CUDPSocket* CUDPSocket::NewLC(MAppObserver& aPacketObserver,RSocketServ& aSocketServer,
RConnection& aConnection,TInetAddr& aRtpAddr,TInetAddr& aRtcpAddr)
{
CUDPSocket* self = new (ELeave) CUDPSocket(aPacketObserver,aSocketServer,
aConnection,aRtpAddr,aRtcpAddr);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CUDPSocket* CUDPSocket::NewL(MAppObserver& aPacketObserver,RSocketServ& aSocketServer,
RConnection& aConnection,TInetAddr& aRtpAddr,TInetAddr& aRtcpAddr)
{
CUDPSocket* self = CUDPSocket::NewLC(aPacketObserver,aSocketServer,
aConnection,aRtpAddr,aRtcpAddr);
CleanupStack::Pop(); // self;
return self;
}
void CUDPSocket::ConstructL()
{
User::LeaveIfError(iRtp.Open(iSocketServer, KAfInet, KSockDatagram,KProtocolInetUdp));
//User::LeaveIfError(iRtcp.Open(iSocketServer, KAfInet, KSockDatagram,KProtocolInetUdp));
iReciever = new (ELeave) CUdpReader(*this,iRtp,iRtpAddr);
iSender = new (ELeave) CUdpWriter(*this,iRtp,iRtpAddr);
}
void CUDPSocket::OnReceiveL(const TDesC& aData, TInt aError)
{
const TUint8* ptr = reinterpret_cast<const TUint8*> (aData.Ptr());
TPtrC8 msg(ptr+12, aData.Size()-12);
iPacketObserver.OnRtpPacketReceived(msg,iRtpType);
//TBuf<32> buf;
//buf.AppendNum(aData.Length());
//User::InfoPrint(buf);
ReceiveL();
}
void CUDPSocket::OnSendL(const TDesC& aMsg, TInt aError)
{
ReceiveL();
}
void CUDPSocket::StartRTPConnectionL(TUint aLocalPort, TBool aEnableRtcp, TInt aRtpType)
{
if(aEnableRtcp)
{
}
iLocalAddr.SetPort(aLocalPort);
iLocalAddr.Input(_L("0.0.0.0"));
iRtp.Bind(iLocalAddr);
iRtpType = aRtpType;
}
void CUDPSocket::ReceiveL()
{
if (iReciever->IsActive())
{
User::Leave(KErrGeneral);
}
iReciever->RecvPackage();
}
TInt CUDPSocket::SendPackage(TDesC8& aBuf)
{
const TUint16* ptr = reinterpret_cast<const TUint16*> (aBuf.Ptr());
TPtrC msg(ptr, aBuf.Length()/2);
iSender->SendPackage(msg);
return 0;
}
#ifndef SOCKETDGRAM_H
#define SOCKETDGRAM_H
// INCLUDES
#include <e32std.h>
#include <e32base.h>
#include <es_sock.h>
#include <in_sock.h>
// CLASS DECLARATION
/////////////////////////////////////////////////////////////////////////////////
////////reader
class CUdpReader: public CActive
{
public:
// Observer interface
class MObserver
{
public:
virtual void OnReceiveL(const TDesC& aData, TInt aError) = 0;
};
public:
// Constructors and destructor
/**
* EPOC default constructor.
*/
CUdpReader(MObserver& aObserver, RSocket& aSocket, TInetAddr &aRemote);
/**
* Destructor.
*/
virtual ~CUdpReader();
public:
// New functions
void RecvPackage();
protected:
// Functions from CActive
void DoCancel();
void RunL();
private:
// member variables
MObserver& iObserver;
RSocket& iSocket;
TSockAddr& iRemoteAddr;
TBuf8<2000> iBuffer;
TSockXfrLength iRecvLen;
TInetAddr iAddr;
};
////////////////////////////////////////////////////////////////////////////
////////writer
class CUdpWriter: public CActive
{
public:
// Observer interface
class MObserver
{
public:
virtual void OnSendL(const TDesC& aMsg, TInt aError) = 0;
};
public:
// Constructors and destructor
/**
* EPOC default constructor.
*/
CUdpWriter(MObserver& aObserver, RSocket& aSocket, TInetAddr &aRemote);
/**
* Destructor.
*/
virtual ~CUdpWriter();
public:
// New functions
void SendPackage(const TDesC& aMsg);
protected:
// Functions from CActive
void DoCancel();
void RunL();
private:
// member variables
MObserver& iObserver;
RSocket& iSocket;
TSockAddr& iRemoteAddr;
TBuf8<2000> iBuffer;
};
#endif // SOCKETDGRAM_H
#include "SocketDgram.h"
///////////////////////////////////////////////////////////
//reader
///////////////////////////////////////////////////////////
// constructor
CUdpReader::CUdpReader(MObserver& aObserver, RSocket& aSocket, TInetAddr &aRemote) :
CActive(CActive::EPriorityStandard), iObserver(aObserver), iSocket(aSocket), iRemoteAddr(aRemote)
{
CActiveScheduler::Add(this);
}
// destructor
CUdpReader::~CUdpReader()
{
Cancel();
}
void CUdpReader::RecvPackage()
{
//iSocket.RecvOneOrMore(iBuffer, 0, iStatus, iRecvLen);//接收从指定地址发送来的数据
iSocket.RecvFrom(iBuffer, iAddr, 0, iStatus);
SetActive();
}
void CUdpReader::DoCancel()
{
iSocket.CancelAll();
}
void CUdpReader::RunL()
{
const TUint16* ptr = reinterpret_cast<const TUint16*> (iBuffer.Ptr());
int len = iBuffer.Length();
if(len%2!=0)
{
len+=1;
}
TPtrC msg(ptr, len / 2);
iObserver.OnReceiveL(msg, iStatus.Int());//通知观察者(CAppSocketEngine类)
// if(iStatus == KErrNone)
// {
// User::InfoPrint(_L("Receive!!"));
// }
// else
// {
// User::InfoPrint(_L("NOReceive!!"));
// }
}
//////////////////////////////////////////////////////////
//writer
///////////////////////////////////////////////////////////
// constructor
CUdpWriter::CUdpWriter(MObserver& aObserver, RSocket& aSocket, TInetAddr &aRemote) :
CActive(CActive::EPriorityStandard), iObserver(aObserver), iSocket(aSocket), iRemoteAddr(aRemote)
{
CActiveScheduler::Add(this);
}
// destructor
CUdpWriter::~CUdpWriter()
{
Cancel();
}
void CUdpWriter::SendPackage(const TDesC& aMsg)
{
//将16位的描述符转换成8位的描述符以便发送
const TUint8* ptr = reinterpret_cast<const TUint8*> (aMsg.Ptr());
iBuffer.Copy(ptr, aMsg.Size());
//iSocket.Send(iBuffer, 0, iStatus);//向指定的地址发送数据
iSocket.SendTo(iBuffer,iRemoteAddr,0,iStatus);
SetActive();
}
void CUdpWriter::DoCancel()
{
iSocket.CancelAll();
}
void CUdpWriter::RunL()
{
const TUint16* ptr = reinterpret_cast<const TUint16*> (iBuffer.Ptr());
TPtrC msg(ptr, iBuffer.Length() / 2);
iObserver.OnSendL(msg, iStatus.Int());
//if (iStatus == KErrNone)
//{
//User::InfoPrint(_L("Send!!"));
//}
//else
//{
//User::InfoPrint(_L("NOSend!!"));
//}
}