Exemplo n.º 1
0
        private void ReceiveComplete(object sender, IOTaskCompleteEventArgs <SocketReceiveTask> e)
        {
            if (e.Task.Exception == null)
            {
                Monitor.AddInput(e.Task.Transferred);
                OnPacketReceived(
                    e.Buffer.Buffer[e.Buffer.Offset], //id
                    e.Buffer.Buffer, e.Buffer.Offset + 1,
                    e.Task.Transferred - 1,
                    e.Task.RemoteEndPoint);

                e.Task.RemoteEndPoint = receiveEp;
                if (Socket != null)
                {
                    Socket.QueueReceive(e.Task);
                }
            }
            else
            {
                OnException(e.Task.Exception, e.Task.RemoteEndPoint);
                if (Socket != null)
                {
                    Socket.QueueReceive(e.Task);
                }
            }
        }
Exemplo n.º 2
0
        protected virtual void ReceiveCompleted2(object sender, IOTaskCompleteEventArgs <SocketReceiveTask> e)
        {
            if (e.Task.Exception == null)
            {
                Monitor.AddInput(e.Task.Transferred);

                try {
                    bool finished = true;

                    readStream.Write(e.Buffer.Buffer, e.Buffer.Offset, e.Task.Transferred);
                    readStream.Position = 0;

                    long start = 0;
                    using var reader = new PacketReader(readStream, true);

                    while (finished && reader.Remaining > 0)
                    {
                        start    = reader.Position;
                        finished = ReadPacketHeader(reader);
                    }

                    if (finished)
                    {
                        readStream.SetLength(0);
                    }
                    else
                    {
                        RepositionStream(reader, start);
                    }
                }
                catch (Exception ex) { OnException(ex); }
            }
            else
            {
                OnException(e.Task.Exception);
            }

            if (IsConnected)
            {
                ReceiveAsyncInternal(e.Task);
            }
            else
            {
                e.Task.Completed -= recvHandler;
            }
        }
Exemplo n.º 3
0
        private void ReceiveCompleted(object sender, IOTaskCompleteEventArgs <SocketReceiveTask> e)
        {
            if (e.Task.Exception == null)
            {
                if (e.Task.Transferred > 0)
                {
                    Monitor.AddInput(e.Task.Transferred);

                    if ((bool)e.Task.UserToken)
                    {
                        e.Task.UserToken = false;

                        ushort length = BitConverter.ToUInt16(e.Buffer.Buffer, e.Buffer.Offset);

                        // Ares has a hard message limit of 4k... enforce?

                        if (length > (SocketManager.BufferSize - 1))
                        {
                            throw new SocketException((int)SocketError.NoBufferSpaceAvailable);
                        }

                        e.Task.Count = length + 1;
                        socket.QueueReceive(e.Task);
                    }
                    else
                    {
                        e.Task.UserToken = true;

                        byte id = e.Buffer.Buffer[e.Buffer.Offset];
                        OnPacketReceived(id, e.Buffer.Buffer, e.Buffer.Offset + 1, e.Task.Transferred - 1);

                        e.Task.Count = 2;
                        if (Connected)
                        {
                            socket.QueueReceive(e.Task);
                        }
                    }
                }
            }
            else
            {
                OnException(e.Task.Exception);
            }
        }
Exemplo n.º 4
0
        protected virtual void ReceiveCompleted(object sender, IOTaskCompleteEventArgs <SocketReceiveTask> e)
        {
            if (e.Task.Exception == null)
            {
                if (e.Task.Transferred > 0)
                {
                    Monitor.AddInput(e.Task.Transferred);
                    try {
                        switch (State)
                        {
                        case ReceiveStatus.Header:
                            HandlePacketHeader(e);
                            break;

                        case ReceiveStatus.Request_Head:
                            HandleRequestHeader(e);
                            break;

                        case ReceiveStatus.Request_Body:
                            HandleRequestBody(e);
                            break;

                        case ReceiveStatus.Decode_Length:
                            HandleWebSocketLength(e);
                            break;

                        case ReceiveStatus.Payload: {
                            HandlePayload(e);
                            break;
                        }
                        }
                    }
                    catch (Exception ex) { OnException(ex); }
                }
            }
            else
            {
                OnException(e.Task.Exception);
            }
        }