Trying to packetize TCP with non-blocking IO is hard! Am I doing something wrong?
Posted
by Ricket
on Stack Overflow
See other posts from Stack Overflow
or by Ricket
Published on 2010-04-11T23:13:54Z
Indexed on
2010/04/11
23:23 UTC
Read the original article
Hit count: 191
Oh how I wish TCP was packet-based like UDP is! But alas, that's not the case, so I'm trying to implement my own packet layer. Here's the chain of events so far (ignoring writing packets)
Oh, and my Packets are very simply structured: two unsigned bytes for length, and then byte[length] data. (I can't imagine if they were any more complex, I'd be up to my ears in if
statements!)
Server
is in an infinite loop, accepting connections and adding them to a list ofConnection
s.PacketGatherer
(another thread) uses aSelector
to figure out whichConnection.SocketChannel
s are ready for reading.- It loops over the results and tells each
Connection
toread()
. - Each
Connection
has a partialIncomingPacket
and a list ofPacket
s which have been fully read and are waiting to be processed. - On
read()
:- Tell the partial
IncomingPacket
to read more data. (IncomingPacket.readData
below) - If it's done reading (
IncomingPacket.complete()
), make aPacket
from it and stick thePacket
into the list waiting to be processed and then replace it with a newIncomingPacket
.
- Tell the partial
There are a couple problems with this. First, only one packet is being read at a time. If the IncomingPacket
needs only one more byte, then only one byte is read this pass. This can of course be fixed with a loop but it starts to get sorta complicated and I wonder if there is a better overall way.
Second, the logic in IncomingPacket
is a little bit crazy, to be able to read the two bytes for the length and then read the actual data. Here is the code, boiled down for quick & easy reading:
int readBytes; // number of total bytes read so far
byte length1, length2; // each byte in an unsigned short int (see getLength())
public int getLength() { // will be inaccurate if readBytes < 2
return (int)(length1 << 8 | length2);
}
public void readData(SocketChannel c) {
if (readBytes < 2) { // we don't yet know the length of the actual data
ByteBuffer lengthBuffer = ByteBuffer.allocate(2 - readBytes);
numBytesRead = c.read(lengthBuffer);
if(readBytes == 0) {
if(numBytesRead >= 1)
length1 = lengthBuffer.get();
if(numBytesRead == 2)
length2 = lengthBuffer.get();
} else if(readBytes == 1) {
if(numBytesRead == 1)
length2 = lengthBuffer.get();
}
readBytes += numBytesRead;
}
if(readBytes >= 2) { // then we know we have the entire length variable
// lazily-instantiate data buffers based on getLength()
// read into data buffers, increment readBytes
// (does not read more than the amount of this packet, so it does not
// need to handle overflow into the next packet's data)
}
}
public boolean complete() {
return (readBytes > 2 && readBytes == getLength()+2);
}
Basically I need feedback on my code. Please suggest any improvements. Even overhauling my entire system would be okay, if you have suggestions for how better to implement the whole thing. Book recommendations are welcome too; I love books. I just get the feeling that something isn't quite right.
© Stack Overflow or respective owner