Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public BaseInputPacket WaitPacket(TcpClient client, long timeoutMillis)
- {
- Stopwatch timer = Stopwatch.StartNew();
- NetworkStream stream = client.GetStream();
- BaseInputPacket packet = null;
- while (!stream.DataAvailable)
- {
- if (timer.ElapsedMilliseconds > timeoutMillis)
- {
- timer.Stop();
- return null;
- }
- }
- //reading header
- byte[] packetHeader = new byte[24];
- stream.Read(packetHeader, 0, packetHeader.Length);
- uint packetType = BitConverter.ToUInt32(packetHeader, 8);
- uint dataLength = BitConverter.ToUInt32(packetHeader, 12);
- //if header is OK, create and parse new packet
- if (mInputPackets[(PacketType) packetType] != null)
- {
- packet = (BaseInputPacket)Activator.CreateInstance(mInputPackets[(PacketType) packetType]);
- packet.Parse(stream, dataLength);
- }
- else
- {
- throw new Exception("Unknown packet received!");
- }
- }
- timer.Stop();
- return packet;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement