Conflicts between different types of packages in OMNeT ++

I created a simulation in OMNeT ++ where I have one client and one server (both of them are UDPBasicApp modules). The client sends packets to the server. The server also sends packets to the client, which are two subclasses of cPacket.

Unfortunately, there are conflicts between these two types of packets when received by the client. Suppose the types of two packages are named FirstPacket and SecondPacket respectively (classes derived from cPacket). By running the simulation, as soon as the client receives the first packet from the server, the simulation fails and I get the following error:

"check_and_cast(): cannot cast (FirstPacket*).ClientServer.client.udpApp[0] to type SecondPacket"

      

How can I solve this problem? How can the server successfully receive both types of packets sent by the client?

+3


source to share


1 answer


You are probably using something like SecondPacket* p = check_and_cast<SecondPacket*>(pkt);

to force every incoming packet to be treated as a type SecondPacket

. OMNeT ++ check_and_cast

cancels your simulation if it is not. A simple solution is to use instead dynamic_cast

:



PacketTypeA* a = dynamic_cast<PacketTypeA*>(pkt);
PacketTypeB* b = dynamic_cast<PacketTypeB*>(pkt);
if (a) {
  printf("got packet type A: %d", a->some_field_of_a);
}
if (b) {
  printf("got packet type B: %d", b->some_field_of_b);
}

      

+4


source







All Articles