I am developing some software that uses a pretty ancient low-level TCP/IP protocol from (I think) the eighties. I can send messages to the server very reliably, but I am a little unsure about receiving them. I cannot predict when the server will send me a message, so I have this method in an infinite loop. GetNextMessage() blocks the thread until it receives a message, whereupon the message is acted upon, and the loop repeats.
Messages are sent at a rate of about maybe 5 or 6 a minute, and I can process them in less than a quarter of a second, so I'm not currently spinning off a new thread when I receive them.
This code fragment contains all the relevant symbols and the logic that I am unsure of:
/// this is filled earlier in the program
private Socket _socket;
private const int BufferSize = 4096;
/// <summary>
/// This byte (hex 02) indicates the start of the message.
/// </summary>
private const byte RecordStart = 2;
/// <summary>
/// This byte (hex 03) indicates the end of the message.
/// </summary>
private const byte RecordEnd = 3;
private string GetNextMessage()
{
byte[] bytesReceived = new byte[BufferSize];
// ReSharper disable PossibleNullReferenceException
_socket.Receive(bytesReceived);
// ReSharper restore PossibleNullReferenceException
if (_socket.Poll(1, SelectMode.SelectRead))
{
throw new SocketException((int)SocketError.ConnectionReset);
}
IEnumerable<byte> bytes = bytesReceived
.Where(character => character != RecordStart)
.TakeWhile(character => character != RecordEnd);
return Encoding.ASCII.GetString(bytes.ToArray());
}
My concerns:
- I am not sure if _socket.Poll() is the correct way to determine if the socket has gone away. It most certainly works and is very necessary, but I'm sure I should be doing something else.
- I once performed an operation that sent three messages immediately after each other, but I only appeared to receive two of them. It is possible that there is a bug in the product I am developing for (seems likely), but I am worried that the method I have chosen is somehow causing messages to be discarded.