A
Andrea Crotti
I have to design something to approach one of the (I think) classical
problems in network.
I need to send out packets (which are objects), but I need to take care
of possible retransmissions.
Suppose I can retransmit 3 times before giving up, I need to
- store the packet somewhere until I'm sure it reached destination
- setup a timer from the first time I send it
- at every timer increase a counter
But the problem is that the timer could be different for all the
packets, so I need "n" different timers (it's an event driven simulation
model) for n different packets.
So well I thought to manage all this externally, in a PacketHandler
class.
This class should in my idea take care of:
- create the object dynamically (using new)
- setup the timers and raise the right events
- clear the memory as soon as possible (that's the most important thing)
What I don't understand is how to implement it.
I mean, every packet type has its constructor with some parameters.
So, or I have a function "makePacketX" for every type of packet OR I
have to pass the pointer already initialized into the PacketHandler.
And also to make sure that the return type is correct I thought to use a
template class, and then I need to have a PacketHandler for every Data
type, which is again a bit annoying.
So the questions would be:
1. is that bad to allocate with new something outside PacketHandler and pass
the pointer inside to be eventually freed?
2. are there other better options to accomplish what I would like to do?
// this is a sketch of a possible class
// every packet stored it's fetched by its sequential number
template<class T>
class PacketHandler
{
private:
std::map<serial_t, T *> factory;
serial_t counter;
public:
PacketHandler()
: counter(0)
{}
serial_t getCounter() { return counter; }
T *getPacket(serial_t);
};
problems in network.
I need to send out packets (which are objects), but I need to take care
of possible retransmissions.
Suppose I can retransmit 3 times before giving up, I need to
- store the packet somewhere until I'm sure it reached destination
- setup a timer from the first time I send it
- at every timer increase a counter
But the problem is that the timer could be different for all the
packets, so I need "n" different timers (it's an event driven simulation
model) for n different packets.
So well I thought to manage all this externally, in a PacketHandler
class.
This class should in my idea take care of:
- create the object dynamically (using new)
- setup the timers and raise the right events
- clear the memory as soon as possible (that's the most important thing)
What I don't understand is how to implement it.
I mean, every packet type has its constructor with some parameters.
So, or I have a function "makePacketX" for every type of packet OR I
have to pass the pointer already initialized into the PacketHandler.
And also to make sure that the return type is correct I thought to use a
template class, and then I need to have a PacketHandler for every Data
type, which is again a bit annoying.
So the questions would be:
1. is that bad to allocate with new something outside PacketHandler and pass
the pointer inside to be eventually freed?
2. are there other better options to accomplish what I would like to do?
// this is a sketch of a possible class
// every packet stored it's fetched by its sequential number
template<class T>
class PacketHandler
{
private:
std::map<serial_t, T *> factory;
serial_t counter;
public:
PacketHandler()
: counter(0)
{}
serial_t getCounter() { return counter; }
T *getPacket(serial_t);
};