private static async Task <Stream> StreamTransmission(SegmentedNetworkStream segmentedNetworkStream) { MemoryStream memoryStream = new MemoryStream(); int totalBytesRead = 0; while (!segmentedNetworkStream.DataComplete || segmentedNetworkStream.DataAvailable > 0) { byte[] buffer = new byte[1048576]; // Wait until (partial) data has been transmitted and is ready to be used long dataAvailable = await segmentedNetworkStream.WaitUntilDataAvailable(); int bytesRead = await segmentedNetworkStream.ReadAsync(buffer, 0, buffer.Length); totalBytesRead += bytesRead; await memoryStream.WriteAsync(buffer, 0, buffer.Length); await memoryStream.FlushAsync(); } memoryStream.Position = 0; // Send the same back return(memoryStream); }
private async Task ProtocolBeginTransmission(NetworkPacket networkPacket) { List <object> filteredParameters = new List <object>(); foreach (var parameter in networkPacket.CommandParameters) { var parameterType = parameter?.GetType(); if (parameterType != null && typeof(NetworkStreamPlaceholder).IsAssignableFrom(parameterType)) { var streamToTransmit = parameter as NetworkStreamPlaceholder; var stream = new SegmentedNetworkStream(); var id = streamToTransmit.Id; _trackedStreams.TryAdd(id, stream); filteredParameters.Add(stream); // Set promised length stream.SetPromisedLength(streamToTransmit.PromisedLength); } else { filteredParameters.Add(parameter); } } NetworkPacket localInvocationNetworkPacket = new NetworkPacket { CommandName = networkPacket.CommandName, CommandParameters = filteredParameters.ToArray(), NetworkId = networkPacket.NetworkId, PacketType = networkPacket.PacketType, RequiresAcknowledgement = true }; await _messageManager.OnNetworkMessage(localInvocationNetworkPacket); }