/// <summary> /// Receive a packet from the network for dissonance /// </summary> /// <param name="source">An integer identifying where this packet came from (same ID will be used for sending)</param> /// <param name="data">Packet received</param> public void NetworkReceivedPacket(TPeer source, ArraySegment <byte> data) { var reader = new PacketReader(data); var magic = reader.ReadUInt16(); if (magic != PacketWriter.Magic) { Log.Warn("Received packet with incorrect magic number. Expected {0}, got {1}", PacketWriter.Magic, magic); return; } var header = (MessageTypes)reader.ReadByte(); if (header != MessageTypes.HandshakeRequest) { var session = reader.ReadUInt32(); if (session != _sessionId) { Log.Warn("Received a packet with incorrect session ID. Expected {0}, got {1}. Resetting client.", _sessionId, session); SendErrorWrongSession(source, _sessionId); return; } } switch (header) { case MessageTypes.ClientState: RecvClientState.Update(data.Count); _clients.ProcessClientState(source, ref reader); break; case MessageTypes.PlayerRoutingUpdate: Log.Error("Received a routing update (this should only ever be received by the client)"); break; case MessageTypes.VoiceData: RecvVoiceData.Update(data.Count); _packetRouter.ProcessVoiceData(source, ref reader); break; case MessageTypes.TextData: RecvTextData.Update(data.Count); _packetRouter.ProcessTextData(ref reader); break; case MessageTypes.HandshakeRequest: RecvHandshakeRequest.Update(data.Count); ClientDisconnected(source); // disconnect existing peers on this connection ((IServer <TPeer>) this).SendReliable(source, _handshakeResponse); break; case MessageTypes.HandshakeResponse: Log.Error("Received a handshake response (this should only ever be received by the client)"); break; case MessageTypes.ErrorWrongSession: Log.Error("Received wrong session error from client (this should only ever be received by the client)"); break; default: Log.Error("Ignoring a packet with an unknown header: '{0}'", header); break; } }
private void ProcessReceivedPacket(ArraySegment <byte> data) { var reader = new PacketReader(data); var magic = reader.ReadUInt16(); if (magic != PacketWriter.Magic) { Log.Warn("Received packet with incorrect magic number. Expected {0}, got {1}", PacketWriter.Magic, magic); return; } var header = (MessageTypes)reader.ReadByte(); switch (header) { case MessageTypes.ClientState: Log.Error("Received a client state update (this should only ever be received by the server)"); break; case MessageTypes.PlayerRoutingUpdate: if (CheckSessionId(ref reader)) { RecvRoutingUpdate.Update(data.Count); _processor.ReceivePlayerRoutingUpdate(ref reader); } break; case MessageTypes.VoiceData: if (CheckSessionId(ref reader)) { RecvVoiceData.Update(data.Count); _processor.ReceiveVoiceData(ref reader); } break; case MessageTypes.TextData: if (CheckSessionId(ref reader)) { RecvTextData.Update(data.Count); _processor.ReceiveTextData(ref reader); } break; case MessageTypes.HandshakeResponse: RecvHandshakeResponse.Update(data.Count); _processor.ReceiveHandshakeResponse(ref reader); break; case MessageTypes.HandshakeRequest: Log.Error("Received a handshake request (this should only ever be received by the server)"); break; case MessageTypes.ErrorWrongSession: HandleErrorWrongSession(ref reader); break; default: Log.Error("Ignoring a packet with an unknown header: '{0}'", header); break; } }