/// <summary> /// Process an incoming packet. /// </summary> bool ProcessPacket(Buffer buffer, TcpProtocol tc) { BinaryReader reader = buffer.BeginReading(); Packet request = (Packet)reader.ReadByte(); // TCP connections must be verified first to ensure that they are using the correct protocol if (tc.stage == TcpProtocol.Stage.Verifying) { if (tc.VerifyRequestID(request, reader, false)) return true; #if STANDALONE Console.WriteLine(tc.address + " has failed the verification step"); #endif return false; } switch (request) { case Packet.RequestServerList: { if (reader.ReadUInt16() != GameServer.gameID) return false; tc.data = (long)0; return true; } case Packet.RequestAddServer: { if (reader.ReadUInt16() != GameServer.gameID) return false; ServerList.Entry ent = new ServerList.Entry(); ent.ReadFrom(reader); if (ent.externalAddress.Address.Equals(IPAddress.None)) ent.externalAddress = tc.tcpEndPoint; mList.Add(ent, mTime).data = tc; mLastChange = mTime; #if STANDALONE Console.WriteLine(tc.address + " added a server (" + ent.internalAddress + ", " + ent.externalAddress + ")"); #endif return true; } case Packet.RequestRemoveServer: { if (reader.ReadUInt16() != GameServer.gameID) return false; IPEndPoint internalAddress, externalAddress; Tools.Serialize(reader, out internalAddress); Tools.Serialize(reader, out externalAddress); if (externalAddress.Address.Equals(IPAddress.None)) externalAddress = tc.tcpEndPoint; RemoveServer(internalAddress, externalAddress); #if STANDALONE Console.WriteLine(tc.address + " removed a server (" + internalAddress + ", " + externalAddress + ")"); #endif return true; } case Packet.Disconnect: { #if STANDALONE if (RemoveServer(tc)) Console.WriteLine(tc.address + " has disconnected"); #else RemoveServer(tc); #endif mTcp.Remove(tc); return true; } case Packet.RequestSaveFile: { string fileName = reader.ReadString(); byte[] data = reader.ReadBytes(reader.ReadInt32()); SaveFile(fileName, data); break; } case Packet.RequestLoadFile: { string fn = reader.ReadString(); byte[] data = LoadFile(fn); BinaryWriter writer = BeginSend(Packet.ResponseLoadFile); writer.Write(fn); if (data != null) { writer.Write(data.Length); writer.Write(data); } else writer.Write(0); EndSend(tc); break; } case Packet.RequestDeleteFile: { DeleteFile(reader.ReadString()); break; } case Packet.Error: { #if STANDALONE Console.WriteLine(tc.address + " error: " + reader.ReadString()); #endif return false; } } #if STANDALONE Console.WriteLine(tc.address + " sent a packet not handled by the lobby server: " + request); #endif return false; }
/// <summary> /// Process an incoming packet. /// </summary> bool ProcessPacket(Buffer buffer, IPEndPoint ip) { BinaryReader reader = buffer.BeginReading(); Packet request = (Packet)reader.ReadByte(); switch (request) { case Packet.RequestPing: { BeginSend(Packet.ResponsePing); EndSend(ip); break; } case Packet.RequestAddServer: { if (reader.ReadUInt16() != GameServer.gameID) { return(false); } ServerList.Entry ent = new ServerList.Entry(); ent.ReadFrom(reader); if (ent.externalAddress.Address.Equals(IPAddress.None)) { ent.externalAddress = ip; } mList.Add(ent, mTime); #if STANDALONE Tools.Print(ip + " added a server (" + ent.internalAddress + ", " + ent.externalAddress + ")"); #endif return(true); } case Packet.RequestRemoveServer: { if (reader.ReadUInt16() != GameServer.gameID) { return(false); } IPEndPoint internalAddress, externalAddress; Tools.Serialize(reader, out internalAddress); Tools.Serialize(reader, out externalAddress); if (externalAddress.Address.Equals(IPAddress.None)) { externalAddress = ip; } RemoveServer(internalAddress, externalAddress); #if STANDALONE Tools.Print(ip + " removed a server (" + internalAddress + ", " + externalAddress + ")"); #endif return(true); } case Packet.RequestServerList: { if (reader.ReadUInt16() != GameServer.gameID) { return(false); } mList.WriteTo(BeginSend(Packet.ResponseServerList)); EndSend(ip); return(true); } } return(false); }
/// <summary> /// Process an incoming packet. /// </summary> bool ProcessPacket(Buffer buffer, IPEndPoint ip) { BinaryReader reader = buffer.BeginReading(); Packet request = (Packet)reader.ReadByte(); switch (request) { case Packet.RequestAddServer: { if (reader.ReadUInt16() != GameServer.gameID) return false; ServerList.Entry ent = new ServerList.Entry(); ent.ReadFrom(reader); if (ent.externalAddress.Address.Equals(IPAddress.None)) ent.externalAddress = ip; mList.Add(ent, mTime); #if STANDALONE Console.WriteLine(ip + " added a server (" + ent.internalAddress + ", " + ent.externalAddress + ")"); #endif return true; } case Packet.RequestRemoveServer: { if (reader.ReadUInt16() != GameServer.gameID) return false; IPEndPoint internalAddress, externalAddress; Tools.Serialize(reader, out internalAddress); Tools.Serialize(reader, out externalAddress); if (externalAddress.Address.Equals(IPAddress.None)) externalAddress = ip; RemoveServer(internalAddress, externalAddress); #if STANDALONE Console.WriteLine(ip + " removed a server (" + internalAddress + ", " + externalAddress + ")"); #endif return true; } case Packet.RequestServerList: { if (reader.ReadUInt16() != GameServer.gameID) return false; mList.WriteTo(BeginSend(Packet.ResponseServerList)); EndSend(ip); return true; } } return false; }
/// <summary> /// Process an incoming packet. /// </summary> bool ProcessPacket(Buffer buffer, IPEndPoint ip) { if (mBan.Count != 0 && mBan.Contains(ip.Address.ToString())) { return(false); } var reader = buffer.BeginReading(); var request = (Packet)reader.ReadByte(); switch (request) { case Packet.RequestPing: { var writer = BeginSend(Packet.ResponsePing); writer.Write(mTime); writer.Write((ushort)mList.list.size); EndSend(ip); break; } case Packet.RequestAddServer: { if (reader.ReadUInt16() != GameServer.gameID) { return(false); } var ent = new ServerList.Entry(); ent.ReadFrom(reader); if (mBan.Count != 0 && (mBan.Contains(ent.externalAddress.Address.ToString()) || IsBanned(ent.name))) { return(false); } if (ent.externalAddress.Address.Equals(IPAddress.None) || ent.externalAddress.Address.Equals(IPAddress.IPv6None)) { ent.externalAddress = ip; } mList.Add(ent, mTime); #if STANDALONE Tools.Print(ip + " added a server (" + ent.internalAddress + ", " + ent.externalAddress + ")"); #endif return(true); } case Packet.RequestRemoveServer: { if (reader.ReadUInt16() != GameServer.gameID) { return(false); } IPEndPoint internalAddress, externalAddress; Tools.Serialize(reader, out internalAddress); Tools.Serialize(reader, out externalAddress); if (externalAddress.Address.Equals(IPAddress.None) || externalAddress.Address.Equals(IPAddress.IPv6None)) { externalAddress = ip; } RemoveServer(internalAddress, externalAddress); #if STANDALONE Tools.Print(ip + " removed a server (" + internalAddress + ", " + externalAddress + ")"); #endif return(true); } case Packet.RequestServerList: { if (reader.ReadUInt16() != GameServer.gameID) { return(false); } mList.WriteTo(BeginSend(Packet.ResponseServerList)); EndSend(ip); return(true); } } return(false); }