public virtual bool Send(BasePacket packet) { if (!Listening) { return(false); } try { var packetDeserialized = PacketSerializer.Serialize(packet); var stream = TcpClient.GetStream(); // first we write the size of the packet so we know how much to read later Int32 packetSize = (Int32)packetDeserialized.Length; var packetSizeBytes = BitConverter.GetBytes(packetSize); stream.Write(packetSizeBytes, 0, packetSizeBytes.Length); stream.Write(packetDeserialized, 0, packetDeserialized.Length); if (packet.GetType() != typeof(PingPacket)) // f*****g spam... { Log.Debug("Sent Packet " + packet.GetType().Name); } } catch (Exception e) { if (Listening) { Listening = false; DisconnectClient(); } } return(Listening); }
public virtual bool Send(BasePacket packet) { try { var packetDeserialized = PacketSerializer.Serialize(packet); var stream = TcpClient.GetStream(); // first we write the size of the packet so we know how much to read later Int32 packetSize = (Int32)packetDeserialized.Length; var packetSizeBytes = BitConverter.GetBytes(packetSize); stream.Write(packetSizeBytes, 0, packetSizeBytes.Length); stream.Write(packetDeserialized, 0, packetDeserialized.Length); if (packet.GetType() != typeof(PingPacket)) // f*****g spam... { Log.Debug("Sent Packet " + packet.GetType().Name); } } catch (Exception e) { Log.Error("Error sending packet " + e.Message); Log.Error(System.Environment.StackTrace); Listening = false; } return(Listening); }
private void Send(BasePacket packet, int player = -1) { var internalPacket = new InternalPacket(); var packetType = packet.GetType(); var packetId = _packetTypes.FirstOrDefault(x => x.Value == packetType).Key; internalPacket.Write(packetId); packet.Encode(internalPacket); internalPacket.Send(player); }
public void SignalListener(BasePacket packet) { List <PacketListener> actions = null; if (packetListeners.TryGetValue(packet.GetType(), out actions)) { // Iterate by index, as the action may add new listeners for (int i = actions.Count - 1; i >= 0; i--) { actions[i].action(packet); } } }
// use this only inside the server to send to a client public void Send(BasePacket packet) { try { var packetDeserialized = PacketSerializer.Serialize(packet); var stream = TcpClient.GetStream(); // first we write the size of the packet so we know how much to read later, its out header Int32 packetSize = (Int32)packetDeserialized.Length; var packetSizeBytes = BitConverter.GetBytes(packetSize); stream.Write(packetSizeBytes, 0, packetSizeBytes.Length); // then write the friggin packet stream.Write(packetDeserialized, 0, packetDeserialized.Length); Debug.Log("Sent Packet " + packet.GetType().Name); } catch (Exception e) { // Call in main thread Debug.Log("SOCKET ERROR"); this.Stop(); } }
//-------------------------------- deseriialize -------------------------------- public static List <BasePacket> Deserialize(byte[] bytes, int maxBufferSize, ref int amountRead) { List <BasePacket> storage = new List <BasePacket>(); using (MemoryStream stream = new MemoryStream(bytes)) { using (BinaryReader reader = new BinaryReader(stream)) { long len = maxBufferSize; // We've got no way of telling if the buffer ends with an incomplete packet, // but we want to deserialize all of the packets that are in the buffer, so // rather than leaving a bit at the end (which is what the commented out code does), // we're just going to handle the error. // TODO: Update the packet format to include information about length where appropriate. /*if(len > 512)// never read until the absolute end. * { * len = maxBufferSize - 256; * }*/ Debug.Assert(len > 0); // Catch us falling off the end of the stream - this should only happen // if we've received an incomplete packet try { bool hadParseError = false; while (reader.BaseStream.Position < len) { // Record the position before we attempt to read // a packet. If the read fails, this ensures the next time // we start the read again from the start of the packet. // critically, this must be the first thing that we do for each packet read. amountRead = (int)reader.BaseStream.Position; #if DEBUG_NETWORK_PACKETS int numBytesToRead = #endif Network.Utils.SetupRead(reader); ushort packetTypeId = reader.ReadUInt16(); var packetType = (PacketType)packetTypeId; BasePacket packet = null; // TODO: Replace this with something that looks up everything that descends from // BasePacket, and creates new instances if (listOfConstructors.ContainsKey(packetType) == true) { //packet = listOfConstructors[packetType].Invoke(); packet = TakeFromPool(packetType); } else { Console.WriteLine("Unhandled packet type received: {0}", packetTypeId); hadParseError = true; } if (packet != null) { packet.Read(reader); storage.Add(packet); #if DEBUG_NETWORK_PACKETS if (DebugLogPacket(packet)) { Console.WriteLine("Received {0}", packet.GetType()); } #endif } else { break; } } if (!hadParseError) { // We've read the whole buffer, so record the final amount read amountRead = (int)reader.BaseStream.Position; } } catch (EndOfStreamException) { // We've got an incomplete packet at the end of our buffer. // The amountRead points to the start of that incomplete packet, // so we can just continue. #if DEBUG_NETWORK_PACKETS Console.WriteLine("Incomplete packet received"); #endif } } } return(storage); }