Skip to content

Commit 76a92cf

Browse files
committed
SshData.cs:
- Remove ZeroReaderIndex from SshData; offset should instead be specified when instance is created/loaded. - LoadBytes is now private, and is always invoked when a message is loaded. - Eliminate ResetReader(). SshDataStream.cs: - Added overload taking buffer, offset and count. KeyExchange*Message.cs: - Remove ResetReader() calls. Message.cs: - Remove ZeroReaderIndex override. SftpMessage.cs: - Pass offset and count to Load overload to allow skipping byte representing the message type. - Remove ZeroReaderIndex override. SftpSession.cs: - Avoid buffering when packet contains full SFTP response message. Session.cs: - Pass offset and count to Load overload to allow skipping offset bytes. General: - Temporarily add (too) verbose tracing.
1 parent bd2fa5d commit 76a92cf

14 files changed

+230
-141
lines changed

src/Renci.SshNet/BaseClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Net.Sockets;
33
using System.Threading;
4+
using Renci.SshNet.Abstractions;
45
using Renci.SshNet.Common;
56
using Renci.SshNet.Messages.Transport;
67

@@ -221,6 +222,8 @@ public void Connect()
221222
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
222223
public void Disconnect()
223224
{
225+
DiagnosticAbstraction.Log(string.Format("{0} Disconnecting client", DateTime.Now.Ticks));
226+
224227
CheckDisposed();
225228

226229
OnDisconnecting();
@@ -317,6 +320,8 @@ private void Session_HostKeyReceived(object sender, HostKeyEventArgs e)
317320
/// </summary>
318321
public void Dispose()
319322
{
323+
DiagnosticAbstraction.Log(string.Format("{0} Disposing client", DateTime.Now.Ticks));
324+
320325
Dispose(true);
321326
GC.SuppressFinalize(this);
322327
}

src/Renci.SshNet/Common/SshData.cs

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,7 @@ protected bool IsEndOfData
4747

4848
private byte[] _loadedData;
4949
private int _offset;
50-
51-
/// <summary>
52-
/// Gets the index that represents zero in current data type.
53-
/// </summary>
54-
/// <value>
55-
/// The index of the zero reader.
56-
/// </value>
57-
protected virtual int ZeroReaderIndex
58-
{
59-
get
60-
{
61-
return 0;
62-
}
63-
}
50+
private int _count;
6451

6552
/// <summary>
6653
/// Gets the size of the message in bytes.
@@ -99,30 +86,41 @@ protected virtual void WriteBytes(SshDataStream stream)
9986
internal T OfType<T>() where T : SshData, new()
10087
{
10188
var result = new T();
102-
result.LoadBytes(_loadedData, _offset);
103-
result.LoadData();
89+
result.Load(_loadedData, _offset, _count);
10490
return result;
10591
}
10692

10793
/// <summary>
10894
/// Loads data from specified bytes.
10995
/// </summary>
110-
/// <param name="value">Bytes array.</param>
111-
/// <exception cref="ArgumentNullException"><paramref name="value"/> is <c>null</c>.</exception>
112-
public void Load(byte[] value)
96+
/// <param name="data">Bytes array.</param>
97+
/// <exception cref="ArgumentNullException"><paramref name="data"/> is <c>null</c>.</exception>
98+
public void Load(byte[] data)
11399
{
114-
Load(value, 0);
100+
if (data == null)
101+
throw new ArgumentNullException("data");
102+
103+
LoadInternal(data, 0, data.Length);
115104
}
116105

117106
/// <summary>
118107
/// Loads data from the specified buffer.
119108
/// </summary>
120-
/// <param name="value">Bytes array.</param>
121-
/// <param name="offset">The zero-based offset in <paramref name="value"/> at which to begin reading SSH data.</param>
122-
/// <exception cref="ArgumentNullException"><paramref name="value"/> is <c>null</c>.</exception>
123-
public void Load(byte[] value, int offset)
109+
/// <param name="data">Bytes array.</param>
110+
/// <param name="offset">The zero-based offset in <paramref name="data"/> at which to begin reading SSH data.</param>
111+
/// <param name="count">The number of bytes to load.</param>
112+
/// <exception cref="ArgumentNullException"><paramref name="data"/> is <c>null</c>.</exception>
113+
public void Load(byte[] data, int offset, int count)
114+
{
115+
if (data == null)
116+
throw new ArgumentNullException("data");
117+
118+
LoadInternal(data, offset, count);
119+
}
120+
121+
private void LoadInternal(byte[] value, int offset, int count)
124122
{
125-
LoadBytes(value, offset);
123+
LoadBytes(value, offset, count);
126124
LoadData();
127125
}
128126

@@ -136,40 +134,19 @@ public void Load(byte[] value, int offset)
136134
/// </summary>
137135
protected abstract void SaveData();
138136

139-
/// <summary>
140-
/// Loads data bytes into internal buffer.
141-
/// </summary>
142-
/// <param name="bytes">The bytes.</param>
143-
/// <exception cref="ArgumentNullException"><paramref name="bytes"/> is <c>null</c>.</exception>
144-
protected void LoadBytes(byte[] bytes)
145-
{
146-
LoadBytes(bytes, 0);
147-
}
148-
149137
/// <summary>
150138
/// Loads data bytes into internal buffer.
151139
/// </summary>
152140
/// <param name="bytes">The bytes.</param>
153141
/// <param name="offset">The zero-based offset in <paramref name="bytes"/> at which to begin reading SSH data.</param>
154-
/// <exception cref="ArgumentNullException"><paramref name="bytes"/> is <c>null</c>.</exception>
155-
protected void LoadBytes(byte[] bytes, int offset)
142+
/// <param name="count">The number of bytes to load.</param>
143+
private void LoadBytes(byte[] bytes, int offset, int count)
156144
{
157-
if (bytes == null)
158-
throw new ArgumentNullException("bytes");
159-
160145
_loadedData = bytes;
161146
_offset = offset;
147+
_count = count;
162148

163-
_stream = new SshDataStream(bytes);
164-
ResetReader();
165-
}
166-
167-
/// <summary>
168-
/// Resets internal data reader index.
169-
/// </summary>
170-
protected void ResetReader()
171-
{
172-
_stream.Position = ZeroReaderIndex + _offset;
149+
_stream = new SshDataStream(bytes, _offset, count);
173150
}
174151

175152
/// <summary>

src/Renci.SshNet/Common/SshDataStream.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ public SshDataStream(byte[] buffer)
3030
{
3131
}
3232

33+
/// <summary>
34+
/// Initializes a new non-resizable instance of the <see cref="SshDataStream"/> class based on the specified byte array.
35+
/// </summary>
36+
/// <param name="buffer">The array of unsigned bytes from which to create the current stream.</param>
37+
/// <param name="offset">The zero-based offset in <paramref name="buffer"/> at which to begin reading SSH data.</param>
38+
/// <param name="count">The number of bytes to load.</param>
39+
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <c>null</c>.</exception>
40+
public SshDataStream(byte[] buffer, int offset, int count)
41+
: base(buffer, offset, count)
42+
{
43+
}
44+
3345
/// <summary>
3446
/// Gets a value indicating whether all data from the SSH data stream has been read.
3547
/// </summary>

src/Renci.SshNet/Messages/Message.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@ namespace Renci.SshNet.Messages
1212
/// </summary>
1313
public abstract class Message : SshData
1414
{
15-
/// <summary>
16-
/// Gets the index that represents zero in current data type.
17-
/// </summary>
18-
/// <value>
19-
/// The index of the zero reader.
20-
/// </value>
21-
protected override int ZeroReaderIndex
22-
{
23-
get
24-
{
25-
return 1;
26-
}
27-
}
28-
2915
/// <summary>
3016
/// Gets the size of the message in bytes.
3117
/// </summary>

src/Renci.SshNet/Messages/Transport/KeyExchangeDhInitMessage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public KeyExchangeDhInitMessage(BigInteger clientExchangeValue)
4949
/// </summary>
5050
protected override void LoadData()
5151
{
52-
ResetReader();
5352
_eBytes = ReadBinary();
5453
}
5554

src/Renci.SshNet/Messages/Transport/KeyExchangeDhReplyMessage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ protected override int BufferCapacity
5656
/// </summary>
5757
protected override void LoadData()
5858
{
59-
ResetReader();
6059
HostKey = ReadBinary();
6160
_fBytes = ReadBinary();
6261
Signature = ReadBinary();

src/Renci.SshNet/Messages/Transport/KeyExchangeEcdhInitMessage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public KeyExchangeEcdhInitMessage(BigInteger d, BigInteger q)
5151
/// </summary>
5252
protected override void LoadData()
5353
{
54-
ResetReader();
5554
QC = ReadBinary();
5655
}
5756

src/Renci.SshNet/Messages/Transport/KeyExchangeEcdhReplyMessage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ protected override int BufferCapacity
4949
/// </summary>
5050
protected override void LoadData()
5151
{
52-
ResetReader();
5352
KS = ReadBinary();
5453
QS = ReadBinary();
5554
Signature = ReadBinary();

src/Renci.SshNet/Messages/Transport/KeyExchangeInitMessage.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ protected override int BufferCapacity
140140
/// </summary>
141141
protected override void LoadData()
142142
{
143-
ResetReader();
144-
145143
Cookie = ReadBytes(16);
146144
KeyExchangeAlgorithms = ReadNamesList();
147145
ServerHostKeyAlgorithms = ReadNamesList();

src/Renci.SshNet/PrivateKeyFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ private class SshDataReader : SshData
391391
{
392392
public SshDataReader(byte[] data)
393393
{
394-
LoadBytes(data);
394+
Load(data);
395395
}
396396

397397
public new uint ReadUInt32()

0 commit comments

Comments
 (0)