Adding a new class which is responsible exclusively for keeping track of current Gnutella connections would clear up existing code and (hopefully) make it easier to add related functionality. For example, reserving slots for Calitko leaves, as well as all kind of connection preferencing could be encapsulated there.
Currently, the initial number of free slots is set in the LocalPeer constructor. Every time a slot should be allocated, the slots variable is decremented and is incremented when a slot should be freed. This system is quite simple, but requires that the slot counters are always increment and decrement. Much easier would be to have the slots automatically freed when a connection is closed.
Happily, this behavior could easily be implemented using QObject’s signal destroyed(). Connecting every HandshakingSession and PacketSession object’s destroyed() signal to the SlotAllocator’s slot should do the work.
The problem is, however, that a HandshakingSession object is created after a Connection is already established. This means that connection attempts would take handshaking slots, which is wrong! Therefore, the first step that should be done is having the HandshakeSession try to establish the connection. This sounds logical - a handshake fails if no connection can be established.
I will use the opportunity to continue to remove some old constructs from the Calitko code. There was a time when there was no Handshaker and no PacketProcessor. The code on which they are based was all in good old LocalPeer. LocalPeer maps each connection’s ID to its extrinsic state – an instance of the ConnectionData struct. This struct is actually obsolete now but it is still required as it glues the parts together. A step to get rid of it is to get rid of its Connection member. (I am thus forced to use a temporary hack in order to get the ConnectionId of the connection underlying a PacketSession.)
Attached is a patch implementing this first step (preparation for the SlotAllocator introduction).
