Пример #1
0
        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);
            }
        }
Пример #2
0
        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);
            }
        }