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

            await FlushMessage(socket, json);
            await CloseSocket(socket);
        }
예제 #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);
        }
예제 #3
0
        private static async Task ProcessRequestDocument(PostEventContext context, Document document)
        {
            Task release;

            using (await document.Semaphore.UseWaitAsync())
            {
                release = await RunEvent(context);
            }
            await release;
        }
예제 #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));
        }
예제 #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));
        }
예제 #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));
        }
예제 #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);
     }
 }
예제 #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);
            }
        }
예제 #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);
        }
예제 #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);
            }
        }
예제 #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);
            }
        }