Beispiel #1
0
        private static void InvokeHandler(ReceiveHandler handler, Message msg, Connection connection)
        {
            bool async = handler.GetMethodInfo().GetCustomAttributes(typeof(AsyncOperationAttribute), false).Length > 0;

            if (async)
            {
                ThreadPool.QueueUserWorkItem((o) => handler.Invoke(msg, connection));
            }
            else
            {
                handler.Invoke(msg, connection);
            }
        }
Beispiel #2
0
        private void HandleReceive(NetState ns)
        {
            ByteQueue buffer = ns.Buffer;

            if (buffer == null || buffer.Length <= 0)
            {
                return;
            }
            lock (buffer) {
                int length = buffer.Length;
                while (length > 0 && ns.IsConnected)
                {
                    int packetLength = buffer.Length; // guessing here
                    try {
                        byte[] packetBuffer = _Buffers.AcquireBuffer();
                        packetLength = buffer.Dequeue(packetBuffer, 0, packetLength);
                        switch (ns.ServerPort)
                        {
                        case Config.PortHttp:
                            OnReceiveHttp?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        case Config.PortHttps:
                            OnReceiveHttps?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        case Config.PortWfc27900:
                            OnReceiveWfc27900?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        case Config.PortWfc27901:
                            OnReceiveWfc27901?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        case Config.PortWfc29900:
                            OnReceiveWfc29900?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        case Config.PortWfc29901:
                            OnReceiveWfc29901?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        default:
                            Kernel.WriteLine(TypeName, $"{ns} sent message to unhandled port {ns.ServerPort}.");
                            WritePacketToConsole(packetBuffer, packetLength);
                            break;
                        }
                        length = buffer.Length;
                        _Buffers.ReleaseBuffer(packetBuffer);
                    }
                    catch (Exception e) {
                        Kernel.WriteLine(TypeName, $"{ns} sent message that could not be decoded:{Environment.NewLine}{e}");
                        ns.Dispose();
                    }
                }
            }
        }
Beispiel #3
0
        private static void InvokeHandler(ReceiveHandler handler, Message msg, Connection.Connection connection)
        {
            if (Dispatcher.IsInitialized)
            {
                Dispatcher.RunOnMainThread(handler, msg, connection);
                return;
            }

            var async = handler.GetType().GetCustomAttributes(typeof(AsyncOperationAttribute), false).Length > 0;

            if (async)
            {
                ThreadPool.QueueUserWorkItem((o) => handler.Invoke(msg, connection));
            }
            else
            {
                handler.Invoke(msg, connection);
            }
        }
Beispiel #4
0
        private bool ProcessInputBuffer(BufferData data, SocketAsyncEventArgs args)
        {
            data.ByteCount += args.BytesTransferred;

            while (true)
            {
                var length = -1;

                if (data.ByteCount >= LengthSize)
                {
                    length = ReadSize(data);
                }

                if (length != -1)
                {
                    data.Length = length;

                    if (data.Length > data.MaxLength)
                    {
                        throw new OutOfMemoryException($"Packet is bigger than the max packet size! Packet size: {length} | Max buffer size: {data.MaxLength}");
                    }
                }

                if (length == -1 || data.ByteCount < length)
                {
                    args.SetBuffer(data.BaseOffset + data.ByteCount, data.Length - data.ByteCount);

                    ReceiveAsync(args);
                    return(true);
                }

                data.Offset = LengthSize;
                data.Length = length;

                OnDecrypt?.Invoke(data);
                OnReceive?.Invoke(data);

                if (data.ByteCount == length)
                {
                    break;
                }

                data.ByteCount  -= length;
                data.BaseOffset += length;
                data.Offset      = 0;
                data.Length      = data.MaxLength;
            }

            return(false);
        }
Beispiel #5
0
 protected virtual void OnReceiveHandler(byte[] obj)
 {
     ReceiveHandler?.Invoke(obj);
 }
Beispiel #6
0
 public void Invoke()
 {
     action.Invoke(message, connection);
 }
Beispiel #7
0
 private void OnReceiveHandler(byte[] obj)
 {
     ReceiveHandler?.Invoke(obj);
 }