00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <e32base.h>
00026
00027
00028
00029 #include "serversideasynch.h"
00030 #include "nudpconnection.h"
00031 #include "Log.h"
00032
00033
00034 CNUDPConnection * CNUDPConnection::NewL(RSocket *aSocket,
00035 CServerSession &aSession,
00036 TInt aMtu)
00037 {
00038 CNUDPConnection *con = new (ELeave) CNUDPConnection(aSocket,
00039 NULL,
00040 aSession,
00041 aMtu);
00042 CleanupStack::PushL(con);
00043 con->ConstructL(EFalse);
00044 CleanupStack::Pop();
00045 return con;
00046 }
00047
00048
00049 CNUDPConnection * CNUDPConnection::NewListeningL(RSocket *aSocket,
00050 RSocketServ *aSocketServ,
00051 CServerSession &aSession)
00052 {
00053 CNUDPConnection *con = new (ELeave) CNUDPConnection(aSocket,
00054 aSocketServ,
00055 aSession,
00056 128);
00057 CleanupStack::PushL(con);
00058 con->ConstructL(ETrue);
00059 CleanupStack::Pop();
00060
00061 return con;
00062 }
00063
00064 CNUDPConnection::CNUDPConnection(RSocket* aSocket,
00065 RSocketServ *aSocketServ,
00066 CServerSession &aSession,
00067 TInt aMtu)
00068 : CConcreteConnection(aSocket, aSocketServ, aSession, aMtu)
00069 {
00070
00071 iSrcPort = 123;
00072 iDstPort = 124;
00073 }
00074
00075 CNUDPConnection::~CNUDPConnection()
00076 {
00077
00078 }
00079
00080
00081
00082
00083 void CNUDPConnection::SendL(TDesC8 *aBuf)
00084 {
00085 Log::Print(_L("CNUDPConnection::SendL()"));
00086
00087 TInt maxPayload = GetMTU() - KNUDPHeaderSize;
00088
00089 if (aBuf->Length() > maxPayload) {
00090
00091 TInt fragmentCount = (aBuf->Length() / maxPayload) +
00092 (aBuf->Length() % maxPayload > 0 ? 1 : 0);
00093 RPointerArray<TDesC8> *fragments =
00094 new (ELeave) RPointerArray<TDesC8>(fragmentCount);
00095
00096
00097 TInt pos = 0;
00098 TInt packet = 0;
00099 while (packet++ < fragmentCount) {
00100 if (packet == 1) {
00101
00102 fragments->Append(CreateFrame(&(aBuf->Mid(0, maxPayload)),
00103 KFirstFragment,
00104 aBuf->Length()));
00105 } else if (packet == fragmentCount) {
00106
00107 fragments->Append(CreateFrame(&(aBuf->Right(pos)),
00108 KLastFragment,
00109 aBuf->Length() - pos));
00110 } else {
00111
00112 fragments->Append(CreateFrame(&(aBuf->Mid(pos, maxPayload)),
00113 KFirstFragment | KLastFragment,
00114 aBuf->Length()));
00115 }
00116 pos += maxPayload;
00117 }
00118 iSender->SendL(fragments);
00119
00120 } else {
00121
00122 iSender->SendL(CreateFrame(aBuf,
00123 KFirstFragment | KLastFragment,
00124 aBuf->Length()));
00125 }
00126 }
00127
00128 void CNUDPConnection::ReceiveL(const RMessage &aMessage)
00129 {
00130 Log::Print(_L("CNUDPConnection::Receive()"));
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 iReceiver->Receive(aMessage);
00143 }
00144
00145 TDesC8 *CNUDPConnection::CreateFrame(TDesC8 *aBuf, TUint8 aFlags,
00146 TUint16 aLen)
00147 {
00148 HBufC8 *buf = HBufC8::NewL(aBuf->Length() + KNUDPHeaderSize);
00149 TPtr8 ptr = buf->Des();
00150
00151 ptr.Append(aFlags);
00152 ptr.Append(aLen);
00153 ptr.Append(iSrcPort);
00154 ptr.Append(iDstPort);
00155
00156 ptr.Append(*aBuf);
00157
00158
00159 return buf;
00160 }