public byte[] ReadPacket () {
// FIXME: Throw ClosedConnectionException () if the connection is closed
// FIXME: Throw ClosedConnectionException () if another thread closes the connection
// FIXME: Locking
byte[] header = new byte [HEADER_LENGTH];
int len = Receive (header, 0, header.Length);
if (len == 0)
return new byte [0];
if (len != HEADER_LENGTH) {
// FIXME:
throw new IOException ("Packet of length " + len + " is read.");
}
int packetLength = GetPacketLength (header);
if (packetLength < 11)
throw new IOException ("Invalid packet length.");
if (packetLength == 11) {
return header;
} else {
byte[] buf = new byte [packetLength];
for (int i = 0; i < header.Length; ++i)
buf [i] = header [i];
len = Receive (buf, header.Length, packetLength - header.Length);
if (len != packetLength - header.Length)
throw new IOException ();
return buf;
}
}