Esempio n. 1
0
        private static async Task SendSocketReply(PostEventContext post, string json)
        {
            var socket = post.GetSocket();

            await FlushMessage(socket, json);
            await CloseSocket(socket);
        }
Esempio n. 2
0
        private static async Task SendEvent(PostEventContext post, EventResultType type)
        {
            var reply = new EventResult
            {
                ResultType = type
            };
            var json = reply.ToJSON();

            await SendReply(post, json);
        }
Esempio n. 3
0
        private static async Task ProcessRequestDocument(PostEventContext context, Document document)
        {
            Task release;

            using (await document.Semaphore.UseWaitAsync())
            {
                release = await RunEvent(context);
            }
            await release;
        }
Esempio n. 4
0
        private static Task NotifyEventHandler(PostEventContext post)
        {
            var parameters = post.GetParameters();

            if (post.Element != null)
            {
                return(post.Element.NotifyEvent(parameters.EventName));
            }
            var document = post.GetDocument();

            return(document.NotifyEvent(parameters.EventName));
        }
Esempio n. 5
0
        internal static async Task <Task> RunEventHandler(PostEventContext post)
        {
            if (!await MiddlewareCommon.RunHandler(post.Http,
                                                   () => NotifyEventHandler(post)))
            {
                return(Task.CompletedTask);
            }
            var document = post.GetDocument();
            var queue    = document.FlushQueue();

            return(await SendReply(post, queue));
        }
Esempio n. 6
0
        private static async Task <Task> RunEvent(PostEventContext post)
        {
            var connection = post.GetConnection();
            var document   = post.GetDocument();
            var context    = new PageContext(post.Application, post.Http, connection)
            {
                Socket           = post.Socket,
                DocumentInternal = document
            };

            ProcessMessageIfNeeded(context, post.Parameters);
            return(await RunEventHandler(post));
        }
Esempio n. 7
0
 private static async Task ProcessRequest(PostEventContext context)
 {
     if (MiddlewareCommon.TryFindConnection(context.Application, context.Http, out var connection) &&
         context.Parameters != null &&
         connection.TryGetDocument(context.Parameters.DocumentId, out var document))
     {
         context.Connection = connection;
         context.Document   = document;
         await ProcessRequestDocument(context);
     }
     else
     {
         await SendEvent(context, EventResultType.NoSession);
     }
 }
Esempio n. 8
0
        internal static async Task ProcessAjaxRequest(Application app, HttpContext http)
        {
            if (EventParameters.TryParse(http.Request.Query, out var parameters))
            {
                await parameters.ReadAjaxMessage(http);

                var post = new PostEventContext(app, http)
                {
                    Parameters = parameters
                };
                await ProcessRequest(post);
            }
            else
            {
                await MiddlewareCommon.SendStatusReply(http, HttpStatusCode.BadRequest, Resources.BadRequest);
            }
        }
Esempio n. 9
0
        internal static async Task <Task> SendReply(PostEventContext post, string json)
        {
            var result = Task.CompletedTask;

            if (post.IsWebSocketRequest)
            {
                if (post.SocketRemainsOpen())
                {
                    var completion = await post.GetSocketCompletion();

                    return(completion.Task);
                }

                await SendSocketReply(post, json);
            }
            else
            {
                await SendAjaxReply(post.Http, json);
            }
            EventComplete?.Invoke(post.Http, _EventArgs);
            return(result);
        }
Esempio n. 10
0
        private static async Task ProcessWebSocketEvent(Application app, HttpContext http)
        {
            var socket = await http.WebSockets.AcceptWebSocketAsync();

            var result = await MiddlewareCommon.ReadWebSocketMessage <SocketEventParameters>(socket, MaxSizeBytes);

            var context = new PostEventContext(app, http)
            {
                Http       = http,
                Socket     = socket,
                Parameters = result.Item2
            };

            if (result.Item1)
            {
                await ProcessRequest(context);
            }
            else
            {
                await context.Socket.CloseAsync(WebSocketCloseStatus.InvalidPayloadData,
                                                "Bad request", CancellationToken.None);
            }
        }
Esempio n. 11
0
        internal static async Task ProcessRequestDocument(PostEventContext context)
        {
            var document   = context.GetDocument();
            var parameters = context.GetParameters();
            var proceed    = await document.WaitForTurn(parameters.EventNumber);

            if (!proceed)
            {
                await SendEvent(context, EventResultType.OutOfSequence);
            }
            else if (string.IsNullOrEmpty(parameters.ElementId))
            {
                await ProcessRequestDocument(context, document);
            }
            else if (document.TryGetElementById(parameters.ElementId, out var element))
            {
                context.Element = element;
                await ProcessRequestDocument(context, document);
            }
            else
            {
                await SendEvent(context, EventResultType.NoElement);
            }
        }