-
-
Notifications
You must be signed in to change notification settings - Fork 985
Expand file tree
/
Copy pathSftpWriteRequestBuffer.cs
More file actions
149 lines (127 loc) · 4.4 KB
/
SftpWriteRequestBuffer.cs
File metadata and controls
149 lines (127 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#nullable enable
using System;
using System.Buffers;
using System.Buffers.Binary;
using System.Diagnostics;
namespace Renci.SshNet.Sftp.Requests
{
/// <summary>
/// A helper type that wraps a buffer for SFTP write requests.
/// </summary>
/// <remarks>
/// [Sftp packet length, SftpMessageType, RequestId, Handle length, Handle, Server offset, data length, data].
/// [ 4, 1, 4, 4, ?, 8, 4, ?].
/// </remarks>
internal sealed class SftpWriteRequestBuffer : IDisposable
{
private const int MessageTypeOffset = 4;
private const int RequestIdOffset = MessageTypeOffset + 1;
private const int HandleLengthOffset = RequestIdOffset + 4;
private const int HandleOffset = HandleLengthOffset + 4;
private readonly bool _usePool;
private byte[] _buffer;
public ArraySegment<byte> ActiveBytes
{
get
{
return new(_buffer, 0, HandleOffset + HandleLength + 8 + 4 + DataLength);
}
}
public SftpWriteRequestBuffer(ReadOnlySpan<byte> handle, int dataCapacity, bool usePool = false)
{
Debug.Assert(dataCapacity >= 0);
var totalCapacity = HandleOffset + handle.Length + 8 + 4 + dataCapacity;
_usePool = usePool;
_buffer = usePool
? ArrayPool<byte>.Shared.Rent(totalCapacity)
: new byte[totalCapacity];
_buffer[MessageTypeOffset] = (byte)SftpMessageTypes.Write;
HandleLength = handle.Length;
handle.CopyTo(_buffer.AsSpan(HandleOffset));
}
public SftpWriteRequestBuffer(ReadOnlySpan<byte> handle, ulong serverFileOffset, ReadOnlySpan<byte> data, bool usePool = false)
: this(handle, data.Length, usePool)
{
ServerFileOffset = serverFileOffset;
DataLength = data.Length;
data.CopyTo(Data);
}
public uint RequestId
{
get
{
return BinaryPrimitives.ReadUInt32BigEndian(_buffer.AsSpan(RequestIdOffset));
}
set
{
BinaryPrimitives.WriteUInt32BigEndian(_buffer.AsSpan(RequestIdOffset), value);
}
}
public int HandleLength
{
get
{
return BinaryPrimitives.ReadInt32BigEndian(_buffer.AsSpan(HandleLengthOffset));
}
private init
{
Debug.Assert(value >= 0);
BinaryPrimitives.WriteInt32BigEndian(_buffer.AsSpan(HandleLengthOffset), value);
}
}
public ReadOnlySpan<byte> Handle
{
get
{
return _buffer.AsSpan(HandleOffset, HandleLength);
}
}
public ulong ServerFileOffset
{
get
{
return BinaryPrimitives.ReadUInt64BigEndian(_buffer.AsSpan(HandleOffset + HandleLength));
}
set
{
BinaryPrimitives.WriteUInt64BigEndian(_buffer.AsSpan(HandleOffset + HandleLength), value);
}
}
public int DataLength
{
get
{
return BinaryPrimitives.ReadInt32BigEndian(_buffer.AsSpan(HandleOffset + HandleLength + 8));
}
set
{
Debug.Assert(value >= 0);
Debug.Assert(value <= _buffer.Length - (HandleOffset + HandleLength + 8 + 4));
BinaryPrimitives.WriteInt32BigEndian(_buffer.AsSpan(HandleOffset + HandleLength + 8), value);
}
}
/// <summary>
/// Gets the space available to write as file data. Does not consider <see cref="DataLength"/>.
/// </summary>
public ArraySegment<byte> Data
{
get
{
var offset = HandleOffset + HandleLength + 8 + 4;
return new ArraySegment<byte>(_buffer, offset, _buffer.Length - offset);
}
}
public void Dispose()
{
if (_usePool)
{
var buffer = _buffer;
_buffer = null!;
if (buffer is not null)
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
}
}
}