Beispiel #1
0
            public SocketEventSession(DEServer server, IDEWebSocketScope context)
                : base(server)
            {
                this.context = context ?? throw new ArgumentNullException(nameof(context));

                synchronizationContext = SynchronizationContext.Current ?? throw new ArgumentNullException("SynchronizationContext.Current");
            }             // ctor
Beispiel #2
0
        }         // proc CleanOutdatedEvents

        #endregion

        #region -- WebSocket Events ---------------------------------------------------

        public async Task ExecuteWebSocketAsync(IDEWebSocketScope webSocket)
        {
            if (eventSessionsClosing)
            {
                return;
            }

            // create event session
            var eventSession = new SocketEventSession(this, webSocket);

            try
            {
                var ws         = webSocket.WebSocket;
                var recvOffset = 0;
                var recvBuffer = new byte[16 << 10];
                var segment    = WebSocket.CreateServerBuffer(1024);
                while (ws.State == WebSocketState.Open)
                {
                    var rest = recvBuffer.Length - recvOffset;
                    if (rest <= 0)
                    {
                        await ws.CloseAsync(WebSocketCloseStatus.ProtocolError, "Message to big", CancellationToken.None);
                    }
                    else
                    {
                        var r = await ws.ReceiveAsync(new ArraySegment <byte>(recvBuffer, recvOffset, rest), CancellationToken.None);

                        if (r.MessageType == WebSocketMessageType.Text)                         // message as relative uri
                        {
                            recvOffset += r.Count;
                            if (r.EndOfMessage)
                            {
                                try
                                {
                                    var leadingZeros = 0;
                                    var endSegment   = segment.Offset + recvOffset;
                                    for (var i = segment.Offset; i < endSegment; i++)
                                    {
                                        if (segment.Array[i] != 0)
                                        {
                                            break;
                                        }
                                        leadingZeros++;
                                    }

                                    await eventSession.ExecuteCommandAsync(Encoding.UTF8.GetString(segment.Array, leadingZeros, recvOffset - leadingZeros));
                                }
                                catch (Exception e)
                                {
                                    Log.Except("Command failed: ", e);
                                }
                                finally
                                {
                                    recvOffset = 0;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (e.InnerException is HttpListenerException le)
                {
                    if (le.NativeErrorCode != 995)
                    {
                        Log.Warn($"Event Socket failed.", le);
                    }
                }
                else
                {
                    Log.Warn("Event Socket failed.", e);
                }
            }
        }         // func AcceptWebSocket