Esempio n. 1
0
        public static void Identify(IConnection connection, MemoryStream stream)
        {
            //We Should move this part to a more central position
            if (gameMessages == null)
            {
                gameMessages = new Dictionary<Type, Action<GameMessage, GameBitBuffer, IConnection>>();
                var actionInfos = typeof(GameRouter).GetMethods().Where(o => o.GetParameters().Where(a => a.ParameterType == typeof(GameMessage)).Count() == 1 && 
                        o.GetParameters().Where(a => a.ParameterType == typeof(IConnection)).Count() == 1 &&
                        o.GetParameters().Where(a => a.ParameterType == typeof(GameBitBuffer)).Count() == 1
                    ).ToList();
                foreach (var actionInfo in actionInfos)
                {
                    Action<GameMessage, GameBitBuffer, IConnection> action = (Action<GameMessage, GameBitBuffer, IConnection>)Delegate.CreateDelegate(typeof(Action<GameMessage, GameBitBuffer, IConnection>), actionInfo);
                    Type typ = Type.GetType("D3Sharp.Net.Game.Packets." + actionInfo.Name);
                    gameMessages.Add(typ, action);
                }
            }
            //

            GameBitBuffer _incomingBuffer = new GameBitBuffer(512);
            GameBitBuffer _outgoingBuffer = new GameBitBuffer(ushort.MaxValue);

            _outgoingBuffer.WriteInt(32, 0);

            var header = new GameHeader(stream);
            var payload = new byte[header.Length - 6];

            stream.Read(payload, 0, (int)header.Length - 6);

            _incomingBuffer.AppendData(header.Data);
            _incomingBuffer.AppendData(payload);

            while (_incomingBuffer.IsPacketAvailable())
            {
                int end = _incomingBuffer.Position;
                end += _incomingBuffer.ReadInt(32) * 8;

                while ((end - _incomingBuffer.Position) >= 9)
                {
                    GameMessage msg = _incomingBuffer.ParseMessage();

                    Logger.LogIncoming(msg);
                    try
                    {
                        if (gameMessages.ContainsKey(GetMessageType(msg.Id)))
                            gameMessages[GetMessageType(msg.Id)](msg, _outgoingBuffer, connection);
                        else
                            Logger.Debug("Unhandled game message: 0x{0:X4} {1}", msg.Id, msg.GetType().Name);
                    }
                    catch (NotImplementedException)
                    {
                        Logger.Debug("Unhandled game message: 0x{0:X4} {1}", msg.Id, msg.GetType().Name);
                    }
                }

                _incomingBuffer.Position = end;
            }
        }