예제 #1
0
        private void DataReceived(object sender, SocketAsyncEventArgs socketAsyncEventArgs)
        {
            try
            {
                if (!_socket.Connected)
                {
                    return;
                }

                var numReceivedBytes = socketAsyncEventArgs.BytesTransferred;

                if (numReceivedBytes > 0)
                {
                    var bytes = ByteUtil.ChompBytes(socketAsyncEventArgs.Buffer, 0, numReceivedBytes);

                    var data = Encoding.UTF8.GetString(bytes);

                    data = _serverType switch
                    {
                        ServerEnum.Chat => CompressionUtil.DecompressChat(data),
                        ServerEnum.Game => CompressionUtil.DecompressGame(data),
                        _ => data
                    };

                    _receivedMessage += data;

                    if (_receivedMessage.Contains((char)0x00))
                    {
                        var messages = _receivedMessage.Split((char)0x00);

                        foreach (var message in messages)
                        {
                            if (message.Length == 0)
                            {
                                continue;
                            }

                            var packet = new XmlRead("<root>" + message + "</root>");

                            foreach (var incoming in packet.Children)
                            {
                                var packetType =
                                    Neptunium.PacketManager.GetPacketHandler(incoming.XmlName, _serverType);

                                if (packetType == null)
                                {
                                    Log.Debug("[{0}] [{1}] [{2}] [{3}] [{4}] {@attributes} {@child}", "IN", _serverType,
                                              Bot?.Login,
                                              Bot?.Login, incoming.XmlName,
                                              incoming.Attributes,
                                              incoming.Children);
                                    continue;
                                }

                                try
                                {
                                    Log.Verbose("[{0}] [{1}] [{2}] [{3}] [{4}] {@attributes} {@child}", "IN",
                                                _serverType,
                                                Bot?.Login,
                                                Bot?.Login, incoming.XmlName,
                                                incoming.Attributes,
                                                incoming.Children);
                                    var packetIn = (PacketIn)Activator.CreateInstance(packetType);
                                    packetIn.Read(this, incoming);
                                    packetIn.Handle();
                                }
                                catch (Exception ex)
                                {
                                    Log.Error(ex, "Packet Error");
                                }
                            }
                        }

                        _receivedMessage = messages[^ 1];