private static async Task <ZResponse> _TryRegisterStreamAndWaitResponseAsync(ZRequest request, ZPacketsStreamCallback onPacketsReceivedCallback, ZStreamRejectedCallback streamRejectedCallback) { // create stream metadata to register it var streamMetadata = new ZStreamMetadata(request.RequestCommand, onPacketsReceivedCallback, streamRejectedCallback); // register stream to help find it and pass packets _streamsPool.Add(streamMetadata); // send open stream request var openStreamResponse = await _RegisterRequestAndWaitResponseAsync(request); // check response status code if (openStreamResponse.StatusCode != ZResponseStatusCode.Ok) { _streamsPool.Remove(streamMetadata); } return(openStreamResponse); }
public ZStreamMetadata(ZCommand streamCommand, ZPacketsStreamCallback packetsReceivedCallback, ZStreamRejectedCallback streamRejectedCallback) { StreamCommand = streamCommand; OnPacketsReceivedCallback = packetsReceivedCallback; StreamRejectedCallback = streamRejectedCallback; }
/// <summary> /// Sends request to ZClient to open packets streaming back as an asynchronous operation /// </summary> /// <param name="request">A request to open stream</param> /// <param name="onPacketsReceivedCallback">An <see cref="ZPacketsStreamCallback"/> delegate that references the method to invoke when the stream packets received</param> /// <param name="streamRejectedCallback">(Optional) An <see cref="ZStreamRejectedCallback"/> delegate that references the method to invoke when the stream was rejected</param> /// <returns>The task object representing the asynchronous operation</returns> public static async Task <ZResponse> OpenStreamAsync(ZRequest request, ZPacketsStreamCallback onPacketsReceivedCallback, ZStreamRejectedCallback streamRejectedCallback = null) { // check incoming arguments if (request == null) { throw new ArgumentNullException(nameof(request)); } if (onPacketsReceivedCallback == null) { throw new ArgumentNullException(nameof(onPacketsReceivedCallback)); } // check is it already exists in pool if (_streamsPool.Any(i => i.StreamCommand == request.RequestCommand)) { throw new ArgumentException($"This stream request is already exists in streams pool. {request}"); } // send request and get response var response = await _TryRegisterStreamAndWaitResponseAsync(request, onPacketsReceivedCallback, streamRejectedCallback); return(response); }