Показать сообщение отдельно
Непрочитано 14.11.2019, 00:44   #1
Пользователь

Автор темы (Топик Стартер) Чтение пакетов

Пишу эмулятор под игру Soul Worker , но столкнулся с проблемой.
В этой игре клиент сначала отправляет клиентский пакет, затем серверный
Но вот мой сервер почему то пропускает клиентский и сразу читает серверный
К сожалению свою ошибку я не вижу
 
public ClientConnection(Socket socket)
        {
            _socket = socket;
            _socket.NoDelay = true;
 
            BeginReceive();
        }
 
        private void BeginReceive()
        {
            if (_disconnected)
                return;
 
            _socket.BeginReceive(_receiveBuffer, 0, _receiveBuffer.Length, SocketFlags.None, Receive, null);
        }
 
        private void Receive(IAsyncResult ar)
        {
 
            var receivedBytes = 0;
 
            try
            {
                receivedBytes = _socket.EndReceive(ar);
            }
            catch (Exception ex)
            {
                Disconnect($"Receive Exception: {ex}");
            }
 
            // Check if the socket was closed.
            if (receivedBytes == 0)
            {
                Disconnect();
                return;
            }
 
            using (var stream = new MemoryStream(_receiveBuffer, 0, receivedBytes))
            using (var reader = new BinaryReader(stream))
            {
                var keyIdentifier = reader.ReadInt16();
                var size = reader.ReadInt16();
                var sender = reader.ReadByte();
 
                var body = reader.ReadBytes(size);
                body = Cryptography.CryptBytes(2, body);
                using (var messageStream = new MemoryStream(body))
                using (var message = new BinaryReader(messageStream))
                {
                    var opcode = message.ReadInt16R();
                    var handler = MessageTable.FindMessageHandler((ushort)opcode);
                    if (handler != null)
                    {
                        handler.Handle(this, message);
                        log.Debug($"Received(Size:{size}, Opcodes: {opcode}, 0x{opcode:X2})");
                    }
                    else
                    {
                        log.Warn($"Unknown Message Received: Size:{size}, Opcodes: {opcode} (0x{opcode:X2})");
                        log.Warn(BufferExtensions.ToHex(body.Skip(2).ToArray()));
                    }
                }
            }
        }
 
        public void Send(IMessage message)
        {
            var messageData = MessageTable.FindMessageData(message);
 
            if (messageData == null)
                throw new Exception($"Could not find message attribute for {nameof(message)}");
 
 
            using (var bodyStream = new MemoryStream())
            using (var bodyWriter = new BinaryWriter(bodyStream))
            {
                bodyWriter.Write(new byte[] { 0x02, 0x00 });
                bodyWriter.Write((ushort)(bodyStream.Length + 5));
                bodyWriter.Write((byte)1);
                bodyWriter.Write(Cryptography.CryptBytes(2, Make(message, messageData)));
                Send(bodyStream.ToArray());
            }
        }
 
        private byte[] Make(IMessage message, MessageAttribute attribute)
        {
            using (var bodyStream = new MemoryStream())
            using (var bodyWriter = new BinaryWriter(bodyStream))
            {
                bodyWriter.WriteShortR((ushort)attribute.Opcode, true);
                message.Serialize(bodyWriter);
                return bodyStream.ToArray();
            }
        }
 
        private void Send(byte[] buffer)
        {
            try
            {
                _socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, SendCallback, null);
            }
            catch (Exception ex)
            {
                Disconnect($"Send Exception: {ex}");
            }
        }
 
        private void SendCallback(IAsyncResult ar)
        {
            try
            {
                _socket.EndSend(ar);
            }
            catch (Exception ex)
            {
                Disconnect($"Send Exception: {ex}");
            }
        }
 
Code: C#
SelloSoo вне форума Ответить с цитированием