Re: Buffering object
On Jan 27, 10:04 pm, Andrea Crotti <andrea.crott...@gmail.com> wrote:
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.
Not really. Or it depends on what you mean by "timer".
So well I thought to manage all this externally, in a PacketHandler
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)
I wonder if you aren't mixing issues. I suspect that the
packets should be created elsewhere; the PacketHandler should be
private to the logic which is responsible for sending them.
And also to make sure that the return type is correct
Huh? Classes don't have return types.
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.
Packets are likely represented by a class hierarchy. All your
"consumer" needs to do is to deal with the public interface,
however. (Alternatively: a Packet is nothing more than an
std::vector<char>, or something along those lines.)
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?
No. It's a very frequent pattern, and it's directly supported
in the standard with std::auto_ptr.