/// <summary> /// Returns a continuous segment of bytes until the given sequence is reached. /// </summary> /// <param name="client">The byte stream to perform the operation on.</param> /// <param name="sequence">The sequence to match to enable the read operation to complete.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The array segment that defines a continuous segment of characters that have matched the predicate.</returns> public static async Task <IReadOnlyList <ArraySegment <byte> > > ReadUntilAsync(this INetworkClient client, byte[] sequence, CancellationToken cancellationToken) { if (client == null) { throw new ArgumentNullException(nameof(client)); } var found = 0; return(await client.ReadAsync(current => { found = current == sequence[found] ? found + 1 : current == sequence[0] ? 1 : 0; return found < sequence.Length; }, cancellationToken : cancellationToken).ConfigureAwait(false)); }