Пример #1
0
        /// <summary>
        /// Start receiving data from this connection
        /// </summary>
        public virtual Task StartReceiveAsync()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("this");
            }

            if (Connected)
            {
                throw new Exception("Already connected and receiving!");
            }

            if (ConnectionClient == null)
            {
                throw new NullReferenceException();
            }

            Interlocked.CompareExchange(ref _connected, 1, 0);

            ConnectionClient.NoDelay = true;

            SocketAsyncEventArgs args = Proxy.ListeningServer.SocketPool.Get();

            args.Completed += Operation_Completed;

            if (!ConnectionClient.ReceiveAsync(args))
            {
                Operation_Completed(this, args);
            }

            return(Task.FromResult(false));
        }
Пример #2
0
        protected virtual async Task ProcessAsync(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)
            {
                bool exception = false;

                try
                {
                    if (_cts.IsCancellationRequested)
                    {
                        return;
                    }

                    SocketAsyncEventArgs newArgs = Proxy.ListeningServer.SocketPool.Get();
                    newArgs.Completed += Operation_Completed;

                    if (ConnectionClient != null && !ConnectionClient.ReceiveAsync(newArgs))
                    {
                        Operation_Completed(this, newArgs);
                    }

                    await _sem.WaitAsync(_cts.Token);

                    var packets = PacketReader.Read(_processor, e.Buffer, e.Offset, e.BytesTransferred);

                    foreach (Packet packet in packets)
                    {
                        if (_cts.IsCancellationRequested)
                        {
                            return;
                        }

                        if (packet == null)
                        {
                            StarLog.DefaultLogger.Warn("Encountered null packet!");

                            continue;
                        }

                        try
                        {
                            packet.Direction = Direction;

                            Type pType = packet.GetType();
                            List <IPacketHandler> pHandlers = null;
                            if (PacketHandlers.ContainsKey(pType))
                            {
                                pHandlers = PacketHandlers[pType];

                                var tasks = new List <Task>();
                                foreach (IPacketHandler beforeHandler in pHandlers)
                                {
                                    tasks.Add(beforeHandler.HandleBeforeAsync(packet, this));
                                }

                                await Task.WhenAll(tasks);
                            }

                            EventHandler <PacketEventArgs> packetArgs = PacketReceived;
                            if (packetArgs != null)
                            {
                                packetArgs(this, new PacketEventArgs(Proxy, packet));
                            }

                            OtherConnection.FlushPacket(packet);

                            if (pHandlers != null)
                            {
                                var tasks = new List <Task>();
                                foreach (IPacketHandler sentHandler in pHandlers)
                                {
                                    tasks.Add(sentHandler.HandleAfterAsync(packet, this));
                                }

                                await Task.WhenAll(tasks);
                            }

                            EventHandler <PacketEventArgs> aPacketArgs = AfterPacketReceived;
                            if (aPacketArgs != null)
                            {
                                aPacketArgs(this, new PacketEventArgs(Proxy, packet));
                            }
                        }
                        catch (Exception ex)
                        {
                            ex.LogError();
                        }
                    }

                    _sem.Release();
                }
                catch (Exception)
                {
                    exception = true;
                }

                if (exception)
                {
                    await CloseAsync();
                }
            }
            else
            {
                await CloseAsync();
            }
        }