Beispiel #1
0
        private void GenerateHandlerReferences(Type type, bool isWorldServer)
        {
            IEnumerable <Type> handlerTypes = !isWorldServer?type.Assembly.GetTypes().Where(t => t.Name.Equals("LoginPacketHandler"))  // shitty but it works
                                                  : type.Assembly.GetTypes().Where(p => !p.IsInterface && type.GetInterfaces().FirstOrDefault().IsAssignableFrom(p));

            // iterate thru each type in the given assembly
            foreach (Type handlerType in handlerTypes)
            {
                IPacketHandler handler = (IPacketHandler)Activator.CreateInstance(handlerType, new object[] { this });

                // include PacketDefinition
                foreach (MethodInfo methodInfo in handlerType.GetMethods().Where(x => x.GetCustomAttributes(false).OfType <PacketAttribute>().Any() || x.GetParameters().FirstOrDefault()?.ParameterType?.BaseType == typeof(PacketDefinition)))
                {
                    PacketAttribute packetAttribute = methodInfo.GetCustomAttributes(false).OfType <PacketAttribute>().FirstOrDefault();

                    // assume PacketDefinition based handler method
                    if (packetAttribute == null)
                    {
                        HandlerMethodReference methodReference = new HandlerMethodReference(DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo), handler, methodInfo.GetParameters().FirstOrDefault()?.ParameterType);
                        HandlerMethods.Add(methodReference.Identification, methodReference);
                    }
                    else
                    {
                        // assume string based handler method
                        HandlerMethodReference methodReference = new HandlerMethodReference(DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo), handler, packetAttribute);
                        HandlerMethods.Add(methodReference.Identification, methodReference);
                    }
                }
            }
        }
Beispiel #2
0
        private bool TriggerHandler(string packetHeader, string packet, bool force)
        {
            KeyValuePair <Packet, Tuple <MethodInfo, object> > methodInfo = HandlerMethods.SingleOrDefault(h => h.Key.Header.Equals(packetHeader));

            if (methodInfo.Value != null)
            {
                if (!force && methodInfo.Key.Amount > 1 && !_waitForPacketsAmount.HasValue)
                {
                    //we need to wait for more
                    _waitForPacketsAmount = methodInfo.Key.Amount;
                    _waitForPacketList.Add(packet != String.Empty ? packet : $"1 {packetHeader} ");
                    return(false);
                }
                try
                {
                    methodInfo.Value.Item1.Invoke(methodInfo.Value.Item2, new object[] { packet });
                }
                catch (Exception ex)
                {
                    Logger.Log.Error(ex.InnerException);
                }
                return(true);
            }

            return(false);
        }
Beispiel #3
0
        private void TriggerHandler(string packetHeader, string packet, bool force)
        {
            if (!IsDisposing)
            {
                HandlerMethodReference methodReference = HandlerMethods.ContainsKey(packetHeader) ? HandlerMethods[packetHeader] : null;
                if (methodReference != null)
                {
                    if (methodReference.HandlerMethodAttribute != null && !force && methodReference.HandlerMethodAttribute.Amount > 1 && !_waitForPacketsAmount.HasValue)
                    {
                        // we need to wait for more
                        _waitForPacketsAmount = methodReference.HandlerMethodAttribute.Amount;
                        _waitForPacketList.Add(packet != string.Empty ? packet : $"1 {packetHeader} ");
                        return;
                    }
                    try
                    {
                        if (HasSelectedCharacter || methodReference.ParentHandler.GetType().Name == "CharacterScreenPacketHandler" || methodReference.ParentHandler.GetType().Name == "LoginPacketHandler")
                        {
                            // call actual handler method
                            if (methodReference.PacketDefinitionParameterType != null)
                            {
                                //check for the correct authority
                                if (!IsAuthenticated || (byte)methodReference.Authority <= (byte)Account.Authority)
                                {
                                    object deserializedPacket = PacketFactory.Deserialize(packet, methodReference.PacketDefinitionParameterType, IsAuthenticated);

                                    if (deserializedPacket != null || methodReference.PassNonParseablePacket)
                                    {
                                        methodReference.HandlerMethod(methodReference.ParentHandler, deserializedPacket);
                                    }
                                    else
                                    {
                                        Logger.Log.WarnFormat(Language.Instance.GetMessageFromKey("CORRUPT_PACKET"), packetHeader, packet);
                                    }
                                }
                            }
                            else
                            {
                                methodReference.HandlerMethod(methodReference.ParentHandler, packet);
                            }
                        }
                    }
                    catch (DivideByZeroException ex)
                    {
                        // disconnect if something unexpected happens
                        Logger.Log.Error("Handler Error SessionId: " + SessionId, ex);
                        Disconnect();
                    }
                }
                else
                {
                    Logger.Log.WarnFormat(Language.Instance.GetMessageFromKey("HANDLER_NOT_FOUND"), packetHeader);
                }
            }
            else
            {
                Logger.Log.WarnFormat(Language.Instance.GetMessageFromKey("CLIENTSESSION_DISPOSING"), packetHeader);
            }
        }
Beispiel #4
0
        private void GenerateHandlerReferences(IEnumerable <IPacketHandler> packetDictionary)
        {
            // iterate thru each type in the given assembly
            foreach (IPacketHandler handlerType in packetDictionary)
            {
                IPacketHandler handler = (IPacketHandler)Activator.CreateInstance(handlerType.GetType(), this);

                // include PacketDefinition
                foreach (MethodInfo methodInfo in handlerType.GetType().GetMethods().Where(x => x.GetParameters().FirstOrDefault()?.ParameterType.BaseType == typeof(PacketDefinition)))
                {
                    HandlerMethodReference methodReference = new HandlerMethodReference(DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo), handler, methodInfo.GetParameters().FirstOrDefault()?.ParameterType);
                    HandlerMethods.Add(methodReference.Identification, methodReference);
                }
            }
        }
Beispiel #5
0
        private bool GenerateHandlerReferences(Type handlerType)
        {
            object handler = Activator.CreateInstance(handlerType, new object[] { this });

            foreach (MethodInfo methodInfo in handlerType.GetMethods().Where(x => x.GetCustomAttributes(false).OfType <Packet>().Any()))
            {
                Packet packetAttribute = methodInfo.GetCustomAttributes(false).OfType <Packet>().SingleOrDefault();

                if (packetAttribute != null)
                {
                    HandlerMethods.Add(packetAttribute, new Tuple <MethodInfo, object>(methodInfo, handler));
                }
            }

            return(false);
        }
Beispiel #6
0
        private void GenerateHandlerReferences(Type type)
        {
            //iterate thru each type in the given assembly, the IPacketHandler is expected in the same dll
            foreach (Type handlerType in type.Assembly.GetTypes().Where(p => !p.IsInterface && type.GetInterfaces().FirstOrDefault().IsAssignableFrom(p)))
            {
                object handler = Activator.CreateInstance(handlerType, new object[] { this });

                foreach (MethodInfo methodInfo in handlerType.GetMethods().Where(x => x.GetCustomAttributes(false).OfType <Packet>().Any()))
                {
                    Packet packetAttribute = methodInfo.GetCustomAttributes(false).OfType <Packet>().FirstOrDefault();

                    if (packetAttribute != null)
                    {
                        HandlerMethods.Add(packetAttribute, new Tuple <Action <object, string>, object>(DelegateBuilder.BuildDelegate <Action <object, string> >(methodInfo), handler));
                    }
                }
            }
        }
Beispiel #7
0
        private void GenerateHandlerReferences(Type type, bool isWorldServer)
        {
            IEnumerable <Type> handlerTypes = !isWorldServer?type.Assembly.GetTypes().Where(t => t.Name.Equals("LoginPacketHandler"))  //shitty but it works
                                                  : type.Assembly.GetTypes().Where(p => !p.IsInterface && type.GetInterfaces().FirstOrDefault().IsAssignableFrom(p));

            //iterate thru each type in the given assembly, the IPacketHandler is expected in the same dll
            foreach (Type handlerType in handlerTypes)
            {
                object handler = Activator.CreateInstance(handlerType, new object[] { this });

                foreach (MethodInfo methodInfo in handlerType.GetMethods().Where(x => x.GetCustomAttributes(false).OfType <PacketAttribute>().Any()))
                {
                    PacketAttribute Packet = methodInfo.GetCustomAttributes(false).OfType <PacketAttribute>().FirstOrDefault();

                    if (Packet != null)
                    {
                        HandlerMethods.Add(Packet, new Tuple <Action <object, string>, object>(DelegateBuilder.BuildDelegate <Action <object, string> >(methodInfo), handler));
                    }
                }
            }
        }
Beispiel #8
0
        private void TriggerHandler(string packetHeader, string packet, bool force)
        {
            if (!IsDisposing)
            {
                KeyValuePair <Packet, Tuple <Action <object, string>, object> > action = HandlerMethods.FirstOrDefault(h => h.Key.Header.Equals(packetHeader));

                if (action.Value != null)
                {
                    if (!force && action.Key.Amount > 1 && !_waitForPacketsAmount.HasValue)
                    {
                        //we need to wait for more
                        _waitForPacketsAmount = action.Key.Amount;
                        _waitForPacketList.Add(packet != String.Empty ? packet : $"1 {packetHeader} ");
                        return;
                    }
                    try
                    {
                        //call actual handler method
                        action.Value.Item1(action.Value.Item2, packet);
                    }
                    catch (Exception ex)
                    {
                        Logger.Log.Error("Handler Error SessionId: " + SessionId, ex);
                    }
                }
                else
                {
                    Logger.Log.WarnFormat(Language.Instance.GetMessageFromKey("HANDLER_NOT_FOUND"), packetHeader);
                }
            }
            else
            {
                Logger.Log.WarnFormat(Language.Instance.GetMessageFromKey("CLIENTSESSION_DISPOSING"), packetHeader);
            }
        }