Example #1
0
        private void Send(Packet packet, bool encrypt)
        {
            var buffer = packet.Writer.GetBuffer();

            if (encrypt)
            {
                buffer = _crypt.Encrypt(packet.Writer.GetBuffer());
            }
            var bufferLength = buffer.Length;
            var length       = (ushort)(bufferLength + 2); // Length includes itself

            try
            {
                if (ServerMain.Instance.LogEnabled[0] && ServerMain.Instance.IPBlacklist.Contains(this?.EndPoint?.Address?.ToString()) == false)
                {
                    Log.Info("[{2}] Attempting to send {0} to {1}.", Packets.GetName(packet.Id), this?._tcp?.Client?.RemoteEndPoint?.ToString(), this?.Character?.Name);
                }
                _ns.Write(BitConverter.GetBytes(length), 0, 2);
                // Depend on what I want to do
                _ns.Write(buffer, 0, bufferLength);
                this.TimeSinceLastPacket = DateTime.Now.Ticks;
            }
            catch (Exception ex)
            {
                KillConnection(ex);
            }
        }
Example #2
0
        /// <summary>
        /// This setup the listener for any packet that we have al
        /// handled. This is a self-adding parser,
        /// as long as you setup a handler, it will automatically get added in.
        /// </summary>
        /// <param name="id">unique identifier of the packet</param>
        /// <param name="parser">the listener and processor of the packet</param>
        private void SetParser(ushort id, Action <Packet> parser)
        {
            if (_parsers.ContainsKey(id))
            {
                Log.Error("Duplicated parser for packet ({0} {1} : 0x{1:X}).", Packets.GetName(id), id);
            }
#if DEBUG
            Log.Info("Loaded ({0} {1} : 0x{1:X}).", Packets.GetName(id), id);
#endif
            _parsers[id] = parser;
        }
Example #3
0
        private void OnData(IAsyncResult result)
        {
            try
            {
                _bytesToRead -= _ns.EndRead(result);
                if (_bytesToRead > 0)
                {
                    _ns.BeginRead(_buffer, _buffer.Length - _bytesToRead, _bytesToRead, OnData, null);
                    return;
                }
                // Combine the buffer for packetId and the data because we encrypt those together, we have to combine and decrypt them together
                _buffer = packetId.Concat(_buffer).ToArray();
                // Decrypt the buffer to retrieve the actual packetId and the data
                if (_encrypt)
                {
                    _buffer = this._crypt.Decrypt(_buffer).ToArray();
                }
                var packet  = new Packet(this, BitConverter.ToUInt16(_buffer, 0), _buffer.Skip(2).ToArray());
                var hexDump = SerializeWriter.HexDump(packet.Buffer);
                if (ServerMain.Instance.LogEnabled[1] && ServerMain.Instance.IPBlacklist.Contains(this.EndPoint.Address.ToString()) == false)
                {
                    Log.Info("Handling packet {0} ({1} id {2}, 0x{2:X}) on {3} at {4}.", "",
                             Packets.GetName(packet.Id), packet.Id, this.EndPoint.Port.ToString(), DateTime.Now.ToString());
                    Log.Info("HexDump {0}:{1}{2}", packet.Id, Environment.NewLine, hexDump);
                }
                _parent.Parse(packet);

                _buffer      = new byte[4];
                _bytesToRead = _buffer.Length;
                _ns.BeginRead(_buffer, 0, 4, OnHeader, null);
            }
            catch (Exception ex)
            {
                KillConnection(ex);
            }
        }
Example #4
0
 /// <summary>
 /// Process the packet by running the action according to the packet identifier
 /// </summary>
 /// <param name="packet">byte[] object contains the identifier and the buffer</param>
 public void Parse(Packet packet)
 {
     // Handle the packet.
     if (_parsers.ContainsKey(packet.Id))
     {
         // Implementing Error Handling
         _parsers[packet.Id](packet);
     }
     else
     {
         Log.Warning("Received unhandled packet {0} (id {1}, 0x{1:X}) on {2}.", Packets.GetName(packet.Id), packet.Id, _port);
     }
 }