Exemple #1
0
        public override IServiceProvider GetServiceProvider(IServiceProvider serviceProvider = null)
        {
            try
            {
                if (serviceProvider == null)
                {
                    serviceProvider = this.serviceProviderFactory != null?this.serviceProviderFactory.Invoke() : serviceCollection.BuildServiceProvider();
                }

                PacketSerialiserProvider.PacketSerialiser = serviceProvider.GetService <IPacketSerialiser>();

                IPacketHandlers packetHandlers = serviceProvider.GetService <IPacketHandlers>();
                foreach (var packetHandlerModule in this.modules)
                {
                    foreach (var packetHandler in packetHandlerModule.GetPacketHandlers())
                    {
                        packetHandlers.Add(PacketSerialiserProvider.PacketSerialiser.CanReadName ? packetHandler.Key.Name : "Default",
                                           (IPacketHandler)serviceProvider.GetService(packetHandler.Value));
                    }
                }

                if (!PacketSerialiserProvider.PacketSerialiser.CanReadName && packetHandlers.GetPacketHandlers().Count > 1)
                {
                    throw new Exception("A PacketSerialiser which cannot identify a packet can only support up to one packet type");
                }
            }
            catch (Exception ex)
            {
            }

            return(serviceProvider);
        }
        protected IServiceProvider GetServiceProvider()
        {
            var serviceProvider = this.serviceProviderFactory != null?serviceProviderFactory.Invoke() : this.serviceCollection.BuildServiceProvider();

            PacketSerialiserProvider.PacketSerialiser = serviceProvider.GetService <IPacketSerialiser>();
            serviceProvider.GetService <ILogLevelProvider>().SetLogLevel(this.options.LogLevel);

            IPacketHandlers packetHandlers = serviceProvider.GetService <IPacketHandlers>();

            foreach (var packetHandlerModule in this.modules)
            {
                foreach (var packetHandler in packetHandlerModule.GetPacketHandlers())
                {
                    packetHandlers.Add(PacketSerialiserProvider.PacketSerialiser.CanReadName ? packetHandler.Key.Name : "Default",
                                       (IPacketHandler)serviceProvider.GetService(packetHandler.Value));
                }
            }

            if (!PacketSerialiserProvider.PacketSerialiser.CanReadName && packetHandlers.GetPacketHandlers().Count > 1)
            {
                throw new Exception("A PacketSerialiser which cannot identify a packet can only support up to one packet type");
            }

            return(serviceProvider);
        }
        public void ProcessFromBuffer(ISender sender,
                                      byte[] buffer,
                                      int offset = 0,
                                      int length = 0,
                                      bool isTcp = true)
        {
            var bytesRead       = 0;
            var currentPosition = offset;

            if (length == 0)
            {
                length = buffer.Length;
            }

            while (bytesRead < length)
            {
                var packetNameSize = packetSerialiser.CanReadName ? BitConverter.ToInt32(buffer, currentPosition) : 0;

                if (packetSerialiser.CanReadName)
                {
                    currentPosition += 4;
                }

                var packetSize = packetSerialiser.CanReadLength ? BitConverter.ToInt32(buffer, currentPosition) : 0;

                if (packetSerialiser.CanReadLength)
                {
                    currentPosition += 4;
                }

                try
                {
                    var packetTypeName = "Default";

                    if (packetSerialiser.CanReadName)
                    {
                        packetTypeName = Encoding.ASCII.GetString(buffer, currentPosition, packetNameSize);
                    }

                    var packetHandler = packetHandlers.GetPacketHandlers()[packetTypeName];

                    if (string.IsNullOrEmpty(packetTypeName))
                    {
                        if (isTcp)
                        {
                            serverInformation.InvalidTcpPackets++;
                        }
                        else
                        {
                            serverInformation.InvalidUdpPackets++;
                        }

                        logger.Error(new Exception("Packet was lost - Invalid"));
                        return;
                    }

                    if (packetSerialiser.CanReadName)
                    {
                        currentPosition += packetNameSize;
                    }

                    if (packetSerialiser.CanReadOffset)
                    {
                        packetHandler.Handle(buffer, currentPosition, packetSize, sender);
                    }
                    else
                    {
                        var packetBytes = new byte[packetSize];
                        Buffer.BlockCopy(buffer, currentPosition, packetBytes, 0, packetSize);
                        packetHandler.Handle(packetBytes, sender);
                    }
                }
                catch (Exception e)
                {
                    logger.Error(e);
                }

                if (packetSerialiser.CanReadLength)
                {
                    currentPosition += packetSize;
                }

                bytesRead += packetSize + packetNameSize;

                if (packetSerialiser.CanReadName)
                {
                    bytesRead += 4;
                }

                if (packetSerialiser.CanReadLength)
                {
                    bytesRead += 4;
                }

                if (isTcp)
                {
                    serverInformation.ProcessedTcpPackets++;
                }
                else
                {
                    serverInformation.ProcessedUdpPackets++;
                }
            }
        }
Exemple #4
0
        private void Process(byte[] buffer, ISender sender)
        {
            var bytesRead       = 0;
            var currentPosition = 0;

            while (bytesRead < buffer.Length)
            {
                var packetTypeNameLength = packetSerialiser.CanReadName
                    ? BitConverter.ToInt32(buffer, currentPosition)
                    : 0;

                if (packetSerialiser.CanReadName)
                {
                    currentPosition += 4;
                }

                var packetSize = packetSerialiser.CanReadLength
                    ? BitConverter.ToInt32(buffer, currentPosition)
                    : 0;

                if (packetSerialiser.CanReadLength)
                {
                    currentPosition += 4;
                }

                var packetTypeName = "Default";

                if (packetSerialiser.CanReadName)
                {
                    packetTypeName   = Encoding.ASCII.GetString(buffer, currentPosition, packetTypeNameLength);
                    currentPosition += packetTypeNameLength;

                    if (string.IsNullOrEmpty(packetTypeName))
                    {
                        //logger.Error(new Exception("Packet was lost - Invalid"));
                        return;
                    }
                }

                var packetHandler = packetHandlers.GetPacketHandlers()[packetTypeName];

                if (packetSerialiser.CanReadLength && buffer.Length - bytesRead < packetSize)
                {
                    logger.Error(new Exception("Packet was lost"));
                    return;
                }

                var context = _packetContextObjectPool.Pop();
                context.Sender = sender;

                if (packetSerialiser.CanReadOffset)
                {
                    context.PacketBytes = buffer;
                    //packetHandler.Handle(buffer, currentPosition, packetSize, context).GetAwaiter().GetResult();
                    //Not required/supported right now
                }
                else
                {
                    var packetBytes = new byte[packetSize];
                    Buffer.BlockCopy(buffer, currentPosition, packetBytes, 0, packetSize);
                    context.PacketBytes = packetBytes;
                    packetHandler.Handle(context).GetAwaiter().GetResult();
                }

                _packetContextObjectPool.Push(context);

                currentPosition += packetSize;
                bytesRead       += packetSize + packetTypeNameLength;

                if (packetSerialiser.CanReadName)
                {
                    bytesRead += 4;
                }
                if (packetSerialiser.CanReadLength)
                {
                    bytesRead += 4;
                }
            }
        }
Exemple #5
0
        public async Task ProcessFromBuffer(ISender sender,
                                            byte[] buffer,
                                            int offset = 0,
                                            int length = 0,
                                            bool isTcp = true)
        {
            var bytesRead       = 0;
            var currentPosition = offset;

            if (length == 0)
            {
                length = buffer.Length;
            }

            while (bytesRead < length)
            {
                var packetNameSize = packetSerialiser.CanReadName
                                        ? BitConverter.ToInt32(buffer, currentPosition)
                                        : 0;

                if (packetSerialiser.CanReadName)
                {
                    currentPosition += 4;
                }

                var packetSize = packetSerialiser.CanReadLength
                                        ? BitConverter.ToInt32(buffer, currentPosition)
                                        : 0;

                if (packetSerialiser.CanReadLength)
                {
                    currentPosition += 4;
                }

                try
                {
                    var packetTypeName = "Default";

                    if (packetSerialiser.CanReadName)
                    {
                        packetTypeName = Encoding.ASCII.GetString(buffer, currentPosition, packetNameSize);
                    }

                    var packetHandlerDictionary = packetHandlers.GetPacketHandlers();

                    if (!packetHandlerDictionary.ContainsKey(packetTypeName))
                    {
                        logger.LogWarning(
                            $"Could not handle packet {packetTypeName}. Make sure you have registered a handler");
                        return;
                    }

                    var packetHandler = packetHandlerDictionary[packetTypeName];

                    if (string.IsNullOrEmpty(packetTypeName))
                    {
                        if (isTcp)
                        {
                            serverInformation.InvalidTcpPackets++;
                        }
                        else
                        {
                            serverInformation.InvalidUdpPackets++;
                        }

                        logger.Error(new Exception("Packet was lost - Invalid"));
                        return;
                    }

                    if (packetSerialiser.CanReadName)
                    {
                        currentPosition += packetNameSize;
                    }

                    var packetContext = packetContextObjectPool.Pop();
                    packetContext.PacketName = packetTypeName;
                    packetContext.Sender     = sender;
                    packetContext.Serialiser = packetSerialiser;
                    packetContext.Handler    = packetHandler;

                    if (packetSerialiser.CanReadOffset)
                    {
                        //todo: Fix
                        //packetContext.PacketBytes = buffer;
                        //packetHandler.Handle(buffer, currentPosition, packetSize, packetContext).GetAwaiter().GetResult();
                    }
                    else
                    {
                        packetContext.PacketBytes = new byte[packetSize];
                        Buffer.BlockCopy(buffer, currentPosition, packetContext.PacketBytes, 0, packetSize);
                    }

                    await packetHandler.Handle(packetContext);

                    packetContextObjectPool.Push(packetContext);
                }
                catch (Exception e)
                {
                    logger.Error(e);
                }

                if (packetSerialiser.CanReadLength)
                {
                    currentPosition += packetSize;
                }

                bytesRead += packetSize + packetNameSize;

                if (packetSerialiser.CanReadName)
                {
                    bytesRead += 4;
                }

                if (packetSerialiser.CanReadLength)
                {
                    bytesRead += 4;
                }

                if (isTcp)
                {
                    serverInformation.ProcessedTcpPackets++;
                }
                else
                {
                    serverInformation.ProcessedUdpPackets++;
                }
            }
        }