dndserver icon indicating copy to clipboard operation
dndserver copied to clipboard

Add Buffer to GameProtocol

Open gmjehovich opened this issue 1 year ago • 0 comments

This PR is related to Implement TCP Segmented Packets

It adds self.buffer to GameProtocol and modifies the main loop in dataReceived() to read from the buffer when there are at least 8 bytes (prefix header length) present in self.buffer. It will also break if the length of the buffer is less than the length value unpacked from the payload prefix.

After doing more research, we do not have to worry much about the actual packet segmentation/re-assembly process, as it's handled at a lower level of abstraction than the application layer. However, there is still the possibility that dataReceived() gets called when an incomplete payload is sent, as TCP does not guarantee a fixed payload will be received on each send. This PR ensures that the protocol can handle this case.

References: https://docs.twisted.org/en/stable/api/twisted.internet.protocol.Protocol.html#dataReceived (particularly the section that says "Please keep in mind that you will probably need to buffer some data, as partial (or multiple) protocol messages may be received!") https://stackoverflow.com/questions/460144/python-twisted-tcp-packet-fragmentation https://stackoverflow.com/questions/14988396/twisted-receiving-data-within-datareceived

(My discord username is Graeme)

gmjehovich avatar Apr 29 '23 03:04 gmjehovich