public void SendPacket(Packet packet) { try { if (socket.Connected) { lock (socket) { if (packet == null) { return; } int dataLength = packet.getLength(); //This is where ISSAC encryption would have been applied to each packet, so it won't ever mess up. } byte[] buffer = new byte[dataLength + (int)packet.getSize()]; buffer[0] = packet.getId(); if (packet.getSize() == Packet.Size.VariableByte) { if (dataLength > 255) //trying to send more data then we can represent with 8 bits! { Misc.WriteError("Tried to send packet length " + dataLength + " in 8 bits [pid=" + packet.getId() + "]"); } buffer[1] = ((byte)dataLength); } else if (packet.getSize() == Packet.Size.VariableShort) { if (dataLength > 65535) //trying to send more data then we can represent with 16 bits! { Misc.WriteError("Tried to send packet length " + dataLength + " in 16 bits [pid=" + packet.getId() + "]"); } //requires 3+dataLength bytes (packetId [1] + dataLength Short [2] + dataLength[x] buffer[1] = ((byte)(dataLength >> 8)); buffer[2] = ((byte)dataLength); } packet.getData().CopyTo(buffer, (int)packet.getSize()); //we use a blocking mode send, no async on the outgoing //since this is primarily a multithreaded application, shouldn't cause problems to send in blocking mode socket.Send(buffer, SocketFlags.None); } } } catch (Exception e) { Misc.WriteError(e.Message); } }
/** * Handles a packet. * @param session * @param p */ public static void handlePacket(Player player, Packet p) { if (p.getSize() != Packet.Size.Bare) { PacketHandler handler = null; if (handlers.TryGetValue(p.getPacketId(), out handler)) handler.handlePacket(player, p); else Misc.WriteError("Unhandled packet: " + p + "."); //if(p.getId() != 93 && p.getId() != 75 && p.getId() != 22) //Console.WriteLine(p.ToString()); } }
public void SendPacket(Packet packet) { try { if (socket.Connected) { lock (socket) { if (packet == null) return; int dataLength = packet.getLength(); //This is where ISSAC encryption would have been applied to each packet, so it won't ever mess up. } byte[] buffer = new byte[dataLength + (int)packet.getSize()]; buffer[0] = packet.getId(); if (packet.getSize() == Packet.Size.VariableByte) { if (dataLength > 255) //trying to send more data then we can represent with 8 bits! Misc.WriteError("Tried to send packet length " + dataLength + " in 8 bits [pid=" + packet.getId() + "]"); buffer[1] = ((byte)dataLength); } else if (packet.getSize() == Packet.Size.VariableShort) { if (dataLength > 65535) //trying to send more data then we can represent with 16 bits! Misc.WriteError("Tried to send packet length " + dataLength + " in 16 bits [pid=" + packet.getId() + "]"); //requires 3+dataLength bytes (packetId [1] + dataLength Short [2] + dataLength[x] buffer[1] = ((byte)(dataLength >> 8)); buffer[2] = ((byte)dataLength); } packet.getData().CopyTo(buffer, (int)packet.getSize()); //we use a blocking mode send, no async on the outgoing //since this is primarily a multithreaded application, shouldn't cause problems to send in blocking mode socket.Send(buffer, SocketFlags.None); } } } catch (Exception e) { Misc.WriteError(e.Message); } }