Hello everybody,
Here is the next patch of my BitTorrent development sequence. This time I was inspired by Atul’s processPayload() function. I thought it could be perfectly mapped to readPayload()of the new BitTorrent::Packets::Packet. It would then call a new virtual overload (a template method), which would take a BinaryReader & as an argument. The gain is that BinaryReader configuration and hasReadAll() and !hasReadPastEnd() checks are only performed once in the base class and the derived classes only care about the actual reading. This approach further simplifies the implementation of readPayload() functions. I didn’t do this at the level of Generics::Packet because for Gnutella packet we sill need to use a different reader.
The same idea can be applied to the write functions as well. I needed to slightly extend BinaryWriter to make make it more suitable for exactly this use. I added an new function reserve(count) which makes sure the underlying buffer would fit count more bytes. This means that the function writePayload() of the derived class must call reserve() with the correct length, otherwise the check hasWrittenPastEnd() in the base class will fail. This might be trickier for Gnutella packets but let’s see what solution Atul will come with ;-).
Best regards,
Peter
revno 75 of http://bzr.calitko.org/developers/peter/calitko-BitTorrent
