/// <summary> /// Handles a request using a Unity3D coroutine. Yields waits until the response has been recieved. /// </summary> /// <typeparam name="TResponseType">The expected response type.</typeparam> /// <param name="envelope">Request envelope to send.</param> /// <param name="userFuture">User provided future (the future the user is holding)</param> /// <param name="onResponseCallback">The callback requested.</param> /// <returns>An enumerable-style collection. (Coroutine)</returns> private IEnumerator RequestHandlingCoroutine(RequestEnvelope envelope, ResponseEnvelopeAsyncRequestFuture userFuture, Action <ResponseEnvelope> onResponseCallback) { //Pass the request to the internal network service and poll the future when it's done IFuture <ResponseEnvelope> internalProvidedFuture = innerAsyncNetworkService.SendRequestAsResponseFuture(envelope, userFuture, GetRequestUrl()); //generic polling for futures and callbacks yield return(WaitForCompletedRequest(internalProvidedFuture, onResponseCallback)); }
public IFuture <ResponseEnvelope> SendRequest(RequestEnvelope envelope, Action <ResponseEnvelope> onResponse = null) { //the delegate can be null. that is fine. Throw <ArgumentNullException> .If.IsNull(envelope)?.Now(nameof(envelope), $"Cannot send a null {nameof(RequestEnvelope)} with {nameof(NetworkCoroutineService)}"); PreProcessRequestEnvelope(envelope); ResponseEnvelopeAsyncRequestFuture responseFuture = new ResponseEnvelopeAsyncRequestFuture(); StartCoroutine(RequestHandlingCoroutine(envelope, responseFuture, onResponse)); return(responseFuture); }
/// <summary> /// Tries to send the <see cref="RequestEnvelope"/> message to the network. /// Returns an <typeparamref name="TResponseType"/> when completed. /// </summary> /// <param name="envelope">Envolope to send.</param> /// <param name="onResponse">Optional delegate to invoke on response recieved.</param> /// <typeparam name="TResponseType">The response type expected back.</typeparam> /// <returns>An awaitable future result.</returns> public AsyncRequestFuture <TResponseType> SendRequest <TResponseType>(RequestEnvelope envelope, Action <TResponseType> onResponse) where TResponseType : class, IResponseMessage, IMessage, IMessage <TResponseType>, new() { //the delegate can be null. that is fine. Throw <ArgumentNullException> .If.IsNull(envelope)?.Now(nameof(envelope), $"Cannot send a null {nameof(RequestEnvelope)} with {nameof(NetworkCoroutineService)}"); PreProcessRequestEnvelope(envelope); //We need to generate the async token and pass it to the coroutine //so that we can provide it to the caller and pass it to the internal service ResponseEnvelopeAsyncRequestFuture <TResponseType> responseFuture = new ResponseEnvelopeAsyncRequestFuture <TResponseType>(); //Start the coroutine to service the request async StartCoroutine(RequestHandlingCoroutine(envelope, responseFuture, onResponse)); //Provide the user with the token (but let's be serious, they're noobs and won't even look at it) return(responseFuture); }