public static int GetLength(this NetworkStream stream) { byte[] length_buffer = new byte[2]; int byteCount = stream.Read(length_buffer, 0, 2); if (byteCount != 2) { return(0); } return(BitConverterX.ToInt16(length_buffer, 0)); }
public static async Task <int> GetLengthAsync(this NetworkStream stream) { byte[] length_buffer = new byte[2]; int byteCount = stream.ReadAsync(length_buffer, 0, 2).Result; if (byteCount != 2) { return(0); } return(BitConverterX.ToInt16(length_buffer, 0)); }
internal byte[] GetGatewayResponse() { int?bytesReceived = 0; byte[] buffer = new byte[2048]; int position = 0; int messageLength = 0; var t = DateTime.Now; int length; do { if (messageLength == 0) { byte[] lengthBuffer = new byte[2]; length = sslStream.ReadAsync(lengthBuffer, 0, lengthBuffer.Length).Result; if (length == 2) { messageLength = BitConverterX.ToInt16(lengthBuffer, 0) - 2; } } if (messageLength != 0) { int currLength = sslStream.ReadAsync(buffer, position, messageLength).Result; if (currLength == messageLength) { bytesReceived = messageLength; } else { position += currLength; } } } while ((DateTime.Now - t).TotalMilliseconds <= 20000 && bytesReceived == 0); if (bytesReceived > 0) { byte[] rec_buffer = new byte[(int)bytesReceived]; Array.Copy(buffer, 0, rec_buffer, 0, (int)bytesReceived); return(rec_buffer); } throw new GatewayTimeoutException(); }