private byte[] ReadByte(ReadBuffer readBuffer, byte[] buffer, ref int index)
{
var currentByte = readBuffer[readBuffer.Index++];
switch (this.readState)
{
case ReadState.Data:
if (currentByte == Command.InterpretAsCommand)
{
this.readState = ReadState.Command;
}
else
{
buffer[index++] = currentByte;
}
return null;
case ReadState.Command:
switch (currentByte)
{
case Command.Will:
case Command.Wont:
case Command.Do:
case Command.Dont:
this.readState = ReadState.OptionCode;
this.readCommand = currentByte;
break;
case Command.InterpretAsCommand:
this.readState = ReadState.Data;
buffer[index++] = currentByte;
break;
default:
this.readState = ReadState.Data;
break;
}
return null;
default:
this.readState = ReadState.Data;
byte response;
if (currentByte == Option.SuppressGoAhead)
{
response = this.readCommand == Command.Do ? Command.Will : Command.Do;
}
else
{
response = this.readCommand == Command.Do ? Command.Wont : Command.Dont;
}
return new[] { Command.InterpretAsCommand, response, currentByte };
}
}