Beispiel #1
0
        private async Task <bool> HttpServer_BeginRequest(HttpContext context, CancellationToken cancellationToken)
        {
            var request  = context.Request;
            var response = context.Response;

            Core.Log.LibVerbose("Request received from {0}:{1} to {2} {3}", request.RemoteAddress, request.RemotePort, request.Method, request.RawUrl);

            var clientId = Guid.NewGuid();
            var ccEvent  = ClientConnectEventArgs.Retrieve(clientId);

            OnClientConnect?.Invoke(this, ccEvent);
            ClientConnectEventArgs.Store(ccEvent);

            context.Response.ContentType = Serializer.MimeTypes[0];
            var responseBuffer = default(MultiArray <byte>);

            if (context.Request.Method == HttpMethod.GET && EnableGetDescriptors && OnGetDescriptorsRequest != null)
            {
                var eArgs = ServerDescriptorsEventArgs.Retrieve();
                OnGetDescriptorsRequest(this, eArgs);
                responseBuffer = Serializer.Serialize(eArgs.Descriptors);
                ServerDescriptorsEventArgs.Store(eArgs);
            }
            if (context.Request.Method == HttpMethod.POST && !(OnMethodCallAsync is null))
            {
                Counters.IncrementBytesReceived(context.Request.PostData.Length);
                var messageRq = Serializer.Deserialize <RPCRequestMessage>(context.Request.PostData);
                var eArgs     = MethodEventArgs.Retrieve(clientId, messageRq, cancellationToken);
                await OnMethodCallAsync.InvokeAsync(this, eArgs).ConfigureAwait(false);

                if (eArgs.Response != null)
                {
                    responseBuffer = Serializer.Serialize(eArgs.Response);
                    OnResponseSent?.Invoke(this, eArgs.Response);
                }
                MethodEventArgs.Store(eArgs);
            }
            await responseBuffer.CopyToAsync(response.OutputStream).ConfigureAwait(false);

            Counters.IncrementBytesSent(responseBuffer.Count);
            return(true);
        }
        private void HttpServer_OnBeginRequest(HttpContext context, ref bool handled, CancellationToken cancellationToken)
        {
            var request  = context.Request;
            var response = context.Response;

            Core.Log.LibVerbose("Request received from {0}:{1} to {2} {3}", request.RemoteAddress, request.RemotePort, request.Method, request.RawUrl);

            var clientId = Guid.NewGuid();

            OnClientConnect?.Invoke(this, new ClientConnectEventArgs(clientId));

            context.Response.ContentType = Serializer.MimeTypes[0];
            var responseBuffer = default(SubArray <byte>);

            if (context.Request.Method == HttpMethod.GET && EnableGetDescriptors && OnGetDescriptorsRequest != null)
            {
                var eArgs = new ServerDescriptorsEventArgs();
                OnGetDescriptorsRequest(this, eArgs);
                responseBuffer = Serializer.Serialize(eArgs.Descriptors);
            }
            if (context.Request.Method == HttpMethod.POST && OnMethodCall != null)
            {
                Counters.IncrementBytesReceived(context.Request.PostData.Length);
                var messageRq = Serializer.Deserialize <RPCRequestMessage>(context.Request.PostData);
                var eArgs     = new MethodEventArgs(clientId, messageRq, cancellationToken);
                OnMethodCall(this, eArgs);
                if (eArgs.Response != null)
                {
                    responseBuffer = Serializer.Serialize(eArgs.Response);
                    OnResponseSent?.Invoke(this, eArgs.Response);
                }
            }
            response.Write(responseBuffer.Array, responseBuffer.Offset, responseBuffer.Count);
            Counters.IncrementBytesSent(responseBuffer.Count);
            handled = true;
        }