由于NetworkStream.Length
从未实施过,我怎么能看到网络流上已经有多少数据可用。
注意我不能只是Read()
在DataAvailable=True
我使用一个线程时,它在设置时继续运行一个函数DataAvailable
,那么这个函数应该切断那个批处理。
这是我的破碎功能,只是为了展示我想要实现的目标。
我所有的数据包都是用[2 Bytes Size][1 Byte Type][Optional Payload]
Public Function readPacket() As PacketReader
'This cuts the stream of bytes into readable packets.
Try
If serverStream.DataAvailable Then
If packetSize = 0 Then
packetSize = serverReader.ReadUInt16()
packetType = serverReader.ReadByte()
End If
If packetSize = 0 Then
Return New PacketReader(packetType, New MemoryStream())
End If
If serverStream.Length >= packetSize Then
Dim packet() As Byte = serverReader.ReadBytes(packetSize)
Dim stream As Stream = New MemoryStream(packet)
Dim tmpPacketSize As UShort = packetSize
packetSize = 0
Return New PacketReader(packetType, tmpPacketSize, stream)
End If
End If
Catch e As Exception
formLobby.logMsg("Exception: " + e.ToString)
End Try
Return Nothing
End Function