本文整理汇总了C#中NetworkStream.ReadExactBytesAsync方法的典型用法代码示例。如果您正苦于以下问题:C# NetworkStream.ReadExactBytesAsync方法的具体用法?C# NetworkStream.ReadExactBytesAsync怎么用?C# NetworkStream.ReadExactBytesAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetworkStream
的用法示例。
在下文中一共展示了NetworkStream.ReadExactBytesAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SlippiConnection
public SlippiConnection(TcpClient client, NetworkStream stream)
{
//Populated MAC and IP
DeviceIp = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
//Make new game info
GameInfo = new GameInformation();
//Handle receiving messages
Observable.Start(async () =>
{
try
{
while(client.Connected)
{
//First always start by reading the size of the message
var lengthBytes = await stream.ReadExactBytesAsync(4);
var messageLength = BitConverter.ToInt32(lengthBytes.Reverse().ToArray(), 0);
//Make sure message length isn't too long
if (messageLength > MAX_MESSAGE_LENGTH || messageLength < 0) throw new Exception("Message length was too long or too short. Length: " + messageLength);
//Read messageLength bytes
var message = await stream.ReadExactBytesAsync(messageLength);
//foreach (byte b in message) System.Diagnostics.Trace.WriteLine(string.Format("[{0:X2}]", b));
//Pass off message to GameInfo
GameInfo.ProcessMessage(message);
//var buf = new byte[32];
//var amountRead = stream.Read(buf, 0, 32);
//for (int i = 0; i < amountRead; i++) System.Diagnostics.Trace.WriteLine(string.Format("[{0:X2}]", buf[i]));
}
logger.Info("Client connected flag is no longer true.");
}
catch (Exception ex)
{
logger.Info(ex, "Connection threw an exception.");
}
finally
{
stream.Close();
Dispose();
}
}, System.Reactive.Concurrency.TaskPoolScheduler.Default);
}