Exemplo n.º 1
0
        private void HandleDisconnected(ITcpSocket socket)
        {
            MhfConnection connection;

            lock (_lock)
            {
                if (!_connections.ContainsKey(socket))
                {
                    _logger.Error(socket, $"Disconnected client does not exist in lookup");
                    return;
                }

                connection = _connections[socket];
                _connections.Remove(socket);
                _logger.Debug($"Clients Count: {_connections.Count}");
            }

            Action <MhfConnection> onClientDisconnected = ClientDisconnected;

            if (onClientDisconnected != null)
            {
                try
                {
                    onClientDisconnected.Invoke(connection);
                }
                catch (Exception ex)
                {
                    _logger.Exception(connection, ex);
                }
            }

            _logger.Info(connection, $"Client disconnected");
        }
Exemplo n.º 2
0
        public List <MhfPacket> Read(byte[] data)
        {
            List <MhfPacket> packets = new List <MhfPacket>();

            if (_buffer == null)
            {
                _buffer = BufferProvider.Provide(data);
            }
            else
            {
                _buffer.SetPositionEnd();
                _buffer.WriteBytes(data);
            }

            _buffer.Position = _position;

            bool read = true;

            while (read)
            {
                read = false;

                if (_header == null && _buffer.Size - _buffer.Position >= PacketHeaderSize)
                {
                    byte   pf0           = _buffer.ReadByte();
                    byte   keyRotDelta   = _buffer.ReadByte();
                    ushort id            = _buffer.ReadUInt16(Endianness.Big);
                    ushort dataSize      = _buffer.ReadUInt16(Endianness.Big);
                    ushort combinedCheck = _buffer.ReadUInt16(Endianness.Big);
                    ushort check0        = _buffer.ReadUInt16(Endianness.Big);
                    ushort check1        = _buffer.ReadUInt16(Endianness.Big);
                    ushort check2        = _buffer.ReadUInt16(Endianness.Big);
                    _header = new PacketHeader(id, pf0, keyRotDelta, dataSize, combinedCheck, check0, check1, check2);
                    Console.WriteLine(_header.ToLogText());
                    // Update the rolling key index.
                    if (_header.KeyRotDelta != 0)
                    {
                        _readKeyRot = _header.KeyRotDelta * (_readKeyRot + 1);
                    }
                }

                if (_header == null && _buffer.Size - _buffer.Position == InitPacketSize)
                {
                    byte[] payload = _buffer.ReadBytes(InitPacketSize);
                    _logger.Debug($"Ignoring Data: {Util.ToHexString(payload, ' ')}");
                }

                if (_header != null && _buffer.Size - _buffer.Position >= _header.DataSize)
                {
                    byte[] packetData = _buffer.ReadBytes(_header.DataSize);
                    packetData = Decrypt(packetData, _readKeyRot,
                                         out ushort combinedCheck, out ushort check0,
                                         out ushort check1, out ushort check2);

                    IBuffer   buffer = BufferProvider.Provide(packetData);
                    MhfPacket packet = new MhfPacket(_header, buffer);
                    packets.Add(packet);
                    _header = null;
                    read    = _buffer.Position != _buffer.Size;
                }
            }

            if (_buffer.Position == _buffer.Size)
            {
                Reset();
            }
            else
            {
                _position = _buffer.Position;
            }

            return(packets);
        }