Exemplo n.º 1
0
 private async Task StreamTransfer(CancellationToken ct, Stream fromStream, Stream toStream, NSPApp nspApp)
 {
     using (fromStream)
     {
         byte[] buffer = new byte[Global.ServerTunnelBufferSize];
         try
         {
             int bytesRead;
             while ((bytesRead =
                         await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0)
             {
                 if (nspApp.IsCompress)
                 {
                     var compressBuffer = StringUtil.DecompressInSnappy(buffer, 0, bytesRead);
                     bytesRead = compressBuffer.Length;
                     await toStream.WriteAsync(compressBuffer, 0, bytesRead, ct).ConfigureAwait(false);
                 }
                 else
                 {
                     await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false);
                 }
                 ServerContext.TotalSentBytes += bytesRead; //上行
             }
         }
         catch (Exception ioe)
         {
             if (ioe is IOException)
             {
                 return;
             }                                   //Suppress this exception.
             throw;
         }
     }
 }
Exemplo n.º 2
0
        private async Task StreamTransfer(CancellationToken ct, Stream fromStream, Stream toStream, NSPApp nspApp)
        {
            using (fromStream)
            {
                byte[] buffer = new byte[81920];
                try
                {
                    int bytesRead;
                    while ((bytesRead =
                                await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0)
                    {
                        if (nspApp.IsCompressed)
                        {
                            buffer    = StringUtil.DecompressInSnappy(buffer, 0, bytesRead);
                            bytesRead = buffer.Length;
                        }

                        await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false);

                        ServerContext.TotalSentBytes += bytesRead; //上行
                    }
                }
                catch (Exception ioe)
                {
                    if (ioe is IOException)
                    {
                        return;
                    }                                   //Suppress this exception.
                    throw;
                    //Server.Logger.Info(ioe.Message);
                }
            }
            //Server.Logger.Debug($"{clientApp}对服务端传输关闭。");
        }