GenericPacketParser.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00023 #include <omnetpp.h>
00024
00025 #include "GenericPacketParser.h"
00026
00027 Define_Module(GenericPacketParser);
00028
00029 char* GenericPacketParser::encapsulatePayload(cPacket *msg, unsigned int* length)
00030 {
00031 commBuffer.reset();
00032 commBuffer.packObject(msg);
00033
00034 *length = commBuffer.getMessageSize();
00035 char* byte_buf = new char[*length];
00036 memcpy(byte_buf, commBuffer.getBuffer(), *length);
00037
00038 return byte_buf;
00039 }
00040
00041 cPacket* GenericPacketParser::decapsulatePayload(char* buf, unsigned int length)
00042 {
00043 cPacket *msg = NULL;
00044
00045 commBuffer.reset();
00046 commBuffer.allocateAtLeast(length);
00047 memcpy(commBuffer.getBuffer(), buf, length);
00048 commBuffer.setMessageSize(length);
00049
00050 try {
00051 msg = check_and_cast<cPacket*>(commBuffer.unpackObject());
00052 if (!commBuffer.isBufferEmpty()) {
00053 ev << "[GenericPacketParser::decapsulatePayload()]\n"
00054 << " Parsing of payload failed: buffer size mismatch"
00055 << endl;
00056 delete msg;
00057 return NULL;
00058 }
00059
00060
00061
00062
00063
00064 } catch (...) {
00065 ev << "[GenericPacketParser::decapsulatePayload()]\n"
00066 << " Parsing of payload failed"
00067 << endl;
00068 delete msg;
00069 return NULL;
00070 }
00071
00072 return msg;
00073 }