/// <summary> /// This request is used to close the Connection object in the Phoenix query server identified by the given IDs. /// </summary> public async Task <CloseConnectionResponse> CloseConnectionRequestAsync(string connectionId, RequestOptions options) { CloseConnectionRequest req = new CloseConnectionRequest { ConnectionId = connectionId }; WireMessage msg = new WireMessage { Name = Constants.WireMessagePrefix + "CloseConnectionRequest", WrappedMessage = req.ToByteString() }; using (Response webResponse = await PostRequestAsync(msg.ToByteArray(), options)) { if (webResponse.WebResponse.StatusCode != HttpStatusCode.OK) { WireMessage output = WireMessage.Parser.ParseFrom(webResponse.WebResponse.GetResponseStream()); ErrorResponse res = ErrorResponse.Parser.ParseFrom(output.WrappedMessage); throw new WebException( string.Format( "CloseConnectionRequestAsync failed! connectionId: {0}, Response code was: {1}, Response body was: {2}", connectionId, webResponse.WebResponse.StatusCode, res.ToString())); } else { WireMessage output = WireMessage.Parser.ParseFrom(webResponse.WebResponse.GetResponseStream()); CloseConnectionResponse res = CloseConnectionResponse.Parser.ParseFrom(output.WrappedMessage); return(res); } } }
public bool HandleRequest() { // Blocking until header is received. _socket.Receive(new Span <byte>(_bufferIn, 0, MessageHeader.SizeInBytes)); var message = new Message(_bufferIn); // Request too short if (message.SizeInBytes <= MessageHeader.SizeInBytes) { Span <byte> errorBuffer = stackalloc byte[ProtocolErrorResponse.SizeInBytes]; var errorMessage = new ProtocolErrorResponse(errorBuffer, RequestId.MinRequestId, ClientId.MinClientId, ProtocolErrorStatus.RequestTooShort); Send(errorMessage.Span); return(false); } var kind = message.Header.MessageKind; var requestId = message.Header.RequestId; // Request too long if (message.SizeInBytes >= Constants.MaxRequestSize) { Span <byte> errorBuffer = stackalloc byte[ProtocolErrorResponse.SizeInBytes]; var errorMessage = new ProtocolErrorResponse(errorBuffer, requestId, ClientId.MinClientId, ProtocolErrorStatus.RequestTooLong); Send(errorMessage.Span); return(false); } // Invalid message kind if (!kind.IsDefined() || kind.IsResponse()) { // Unknown kind is invalid. // Response kind is invalid. Span <byte> buffer = stackalloc byte[ProtocolErrorResponse.SizeInBytes]; var errorMessage = new ProtocolErrorResponse( buffer, requestId, ClientId.MinClientId, ProtocolErrorStatus.InvalidMessageKind); Send(errorMessage.Span); return(false); } // Blocking until the rest of the message is received. _socket.Receive(new Span <byte>(_bufferIn, MessageHeader.SizeInBytes, message.SizeInBytes - MessageHeader.SizeInBytes)); message.Header.ClientId = _clientId; // Client request to close the connection. if (message.Header.MessageKind == MessageKind.CloseConnection) { Span <byte> buffer = stackalloc byte[CloseConnectionResponse.SizeInBytes]; var closeResponse = new CloseConnectionResponse(buffer, requestId, ClientId.MinClientId); _outbox.TryWrite(closeResponse.Span); } // Forwards the message to the dispatch controller. if (_dispatchInbox.TryWrite(message.Span)) { Interlocked.Increment(ref _requestsInProgress); return(true); } // The dispatch inbox is full. { Span <byte> errorBuffer = stackalloc byte[ProtocolErrorResponse.SizeInBytes]; var errorMessage = new ProtocolErrorResponse(errorBuffer, requestId, ClientId.MinClientId, ProtocolErrorStatus.ServerBusy); Send(errorMessage.Span); } return(false); }