Skip to content

Commit aa33234

Browse files
committed
No longer flush PipeStream after every write., allowing us to eliminate polling with a sleep between each poll. Fixes #21.
Always use ServiceFactory to create PipeStream instances.
1 parent 6778cce commit aa33234

File tree

2 files changed

+12
-48
lines changed

2 files changed

+12
-48
lines changed

src/Renci.SshNet/ScpClient.NET.cs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ public void Upload(FileInfo fileInfo, string path)
3131
using (var input = ServiceFactory.CreatePipeStream())
3232
using (var channel = Session.CreateChannelSession())
3333
{
34-
channel.DataReceived += delegate(object sender, ChannelDataEventArgs e)
35-
{
36-
input.Write(e.Data, 0, e.Data.Length);
37-
input.Flush();
38-
};
39-
34+
channel.DataReceived += (sender, e) => input.Write(e.Data, 0, e.Data.Length);
4035
channel.Open();
4136

4237
if (!channel.SendExecRequest(string.Format("scp -t \"{0}\"", path)))
@@ -64,15 +59,10 @@ public void Upload(DirectoryInfo directoryInfo, string path)
6459
if (string.IsNullOrEmpty(path))
6560
throw new ArgumentException("path");
6661

67-
using (var input = new PipeStream())
62+
using (var input = ServiceFactory.CreatePipeStream())
6863
using (var channel = Session.CreateChannelSession())
6964
{
70-
channel.DataReceived += delegate(object sender, ChannelDataEventArgs e)
71-
{
72-
input.Write(e.Data, 0, e.Data.Length);
73-
input.Flush();
74-
};
75-
65+
channel.DataReceived += (sender, e) => input.Write(e.Data, 0, e.Data.Length);
7666
channel.Open();
7767

7868
// Send channel command request
@@ -106,15 +96,10 @@ public void Download(string filename, FileInfo fileInfo)
10696
if (fileInfo == null)
10797
throw new ArgumentNullException("fileInfo");
10898

109-
using (var input = new PipeStream())
99+
using (var input = ServiceFactory.CreatePipeStream())
110100
using (var channel = Session.CreateChannelSession())
111101
{
112-
channel.DataReceived += delegate(object sender, ChannelDataEventArgs e)
113-
{
114-
input.Write(e.Data, 0, e.Data.Length);
115-
input.Flush();
116-
};
117-
102+
channel.DataReceived += (sender, e) => input.Write(e.Data, 0, e.Data.Length);
118103
channel.Open();
119104

120105
// Send channel command request
@@ -141,15 +126,10 @@ public void Download(string directoryName, DirectoryInfo directoryInfo)
141126
if (directoryInfo == null)
142127
throw new ArgumentNullException("directoryInfo");
143128

144-
using (var input = new PipeStream())
129+
using (var input = ServiceFactory.CreatePipeStream())
145130
using (var channel = Session.CreateChannelSession())
146131
{
147-
channel.DataReceived += delegate(object sender, ChannelDataEventArgs e)
148-
{
149-
input.Write(e.Data, 0, e.Data.Length);
150-
input.Flush();
151-
};
152-
132+
channel.DataReceived += (sender, e) => input.Write(e.Data, 0, e.Data.Length);
153133
channel.Open();
154134

155135
// Send channel command request

src/Renci.SshNet/ScpClient.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Renci.SshNet.Common;
66
using System.Text.RegularExpressions;
77
using System.Diagnostics.CodeAnalysis;
8-
using Renci.SshNet.Abstractions;
98

109
namespace Renci.SshNet
1110
{
@@ -170,12 +169,7 @@ public void Upload(Stream source, string path)
170169
using (var input = ServiceFactory.CreatePipeStream())
171170
using (var channel = Session.CreateChannelSession())
172171
{
173-
channel.DataReceived += delegate(object sender, ChannelDataEventArgs e)
174-
{
175-
input.Write(e.Data, 0, e.Data.Length);
176-
input.Flush();
177-
};
178-
172+
channel.DataReceived += (sender, e) => input.Write(e.Data, 0, e.Data.Length);
179173
channel.Open();
180174

181175
var pathEnd = path.LastIndexOfAny(new[] { '\\', '/' });
@@ -213,15 +207,10 @@ public void Download(string filename, Stream destination)
213207
if (destination == null)
214208
throw new ArgumentNullException("destination");
215209

216-
using (var input = new PipeStream())
210+
using (var input = ServiceFactory.CreatePipeStream())
217211
using (var channel = Session.CreateChannelSession())
218212
{
219-
channel.DataReceived += delegate(object sender, ChannelDataEventArgs e)
220-
{
221-
input.Write(e.Data, 0, e.Data.Length);
222-
input.Flush();
223-
};
224-
213+
channel.DataReceived += (sender, e) => input.Write(e.Data, 0, e.Data.Length);
225214
channel.Open();
226215

227216
// Send channel command request
@@ -377,13 +366,8 @@ private void SendData(IChannel channel, byte[] buffer)
377366
private static int ReadByte(Stream stream)
378367
{
379368
var b = stream.ReadByte();
380-
381-
while (b < 0)
382-
{
383-
ThreadAbstraction.Sleep(100);
384-
b = stream.ReadByte();
385-
}
386-
369+
if (b == -1)
370+
throw new SshException("Stream has been closed.");
387371
return b;
388372
}
389373

0 commit comments

Comments
 (0)