Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twisted Python: Max Packet Size? Flush socket?

I'm implementing a client-server solution based on Twisted for the server side and e.g. and Android phone for the client side. Because the Andoird emulator takes no TCP Packets larger then 1500b (or less?), I need to be able to chunk packets on the server side. Without flushing the socket after each "transport.write", Twisted buffers the outgoing data so the chunking would be useless without somekind of manual or automatic flushing / maxpacketsize function. How do I do this in Twisted? I'm familiar with the "reactor.doSelect(1)" function, but since I'm using the EPoll reactor (for scalability and performance reasons), I cannot use doSelect. Is it possible to change the maxPacketValue for certain connections within Twisted?

Hoping that someone can show me the light...

like image 202
Dirk Avatar asked Feb 17 '26 13:02

Dirk


2 Answers

TCP packets are automatically chunked by the OS, all the application can do is give hints when to flush. Apart from that, an application can just read and write to a stream.

The OSs of two communicating peers will automatically configure the maximum packet sizes based on the MTU on the links with Path MTU discovery. Make sure you don't block ICMP packets to get that to work.

Since it is extremely unlikely that a wrong MTU is the problem (and an MTU of 1500 or less is often set anyway), you should re-diagnose your problem, for example with a packet tracer such as wireshark.

like image 144
phihag Avatar answered Feb 19 '26 03:02

phihag


TCP is a stream-oriented protocol, not a packet-oriented protocol. When you call transport.write, that data gets appended to the TCP stream and may be sent out in any number of packets; it might be broken up, or glued together with the next or previous call to write. This is a fairly frequently asked question about Twisted, but everyone who asks it asks it slightly differently.

You want to use a protocol construction kit like AMP, or, more basically, LineReceiver, to delimit the messages within your protocol, rather than relying upon the random nature of packet boundaries.

like image 37
Glyph Avatar answered Feb 19 '26 01:02

Glyph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!