Skip to content

Commit 5b3f3d2

Browse files
committed
发送加密功能重构中。。。
1 parent f06cfe6 commit 5b3f3d2

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

Quick.Protocol/QpChannel_Recv.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ private async Task ReadRecvPipeAsync(PipeReader recvReader, CancellationToken to
208208
if (decryptPipe == null)
209209
{
210210
decryptPipe = new Pipe();
211-
decryptBuffer1 = new byte[1024];
212-
decryptBuffer2 = new byte[1024];
211+
decryptBuffer1 = new byte[dec.InputBlockSize];
212+
decryptBuffer2 = new byte[dec.OutputBlockSize];
213213
}
214214

215215
//写入包头

Quick.Protocol/QpChannel_Send.cs

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ protected virtual void OnWriteError(Exception exception)
2929
Disconnect();
3030
}
3131

32-
33-
private Pipe writeCompressPipe = new Pipe();
32+
//压缩相关变量
33+
private Pipe writeCompressPipe = null;
34+
//加密相关变量
35+
Pipe encryptPipe = null;
36+
byte[] encryptBuffer1 = null;
37+
byte[] encryptBuffer2 = null;
3438

3539
private async Task writePackageBuffer(Stream stream, QpPackageType packageType, ReadOnlySequence<byte> packageBodyBuffer, Action afterSendHandler)
3640
{
@@ -44,12 +48,13 @@ private async Task writePackageBuffer(Stream stream, QpPackageType packageType,
4448
//如果压缩
4549
if (options.InternalCompress)
4650
{
51+
if (writeCompressPipe == null)
52+
writeCompressPipe = new Pipe();
4753
using (var inStream = packageBodyBuffer.AsStream())
4854
using (var outStream = writeCompressPipe.Writer.AsStream(true))
4955
using (var gzStream = new GZipStream(outStream, CompressionMode.Compress, true))
5056
await inStream.CopyToAsync(gzStream).ConfigureAwait(false);
5157

52-
currentPipe = writeCompressPipe;
5358
var readRet = await writeCompressPipe.Reader.ReadAsync().ConfigureAwait(false);
5459
packageBodyBuffer = readRet.Buffer;
5560
var packageBodyLength = Convert.ToInt32(packageBodyBuffer.Length);
@@ -58,20 +63,71 @@ private async Task writePackageBuffer(Stream stream, QpPackageType packageType,
5863
//准备包头
5964
writePackageTotalLengthToBuffer(sendHeadBuffer, 0, packageTotalLength);
6065
packageHeadMemory = new Memory<byte>(sendHeadBuffer, 0, PACKAGE_TOTAL_LENGTH_LENGTH);
66+
currentPipe = writeCompressPipe;
6167
}
62-
/*
6368
//如果加密
6469
if (options.InternalEncrypt)
6570
{
66-
var retBuffer = enc.TransformFinalBlock(packageBuffer.Array, packageBuffer.Offset + PACKAGE_TOTAL_LENGTH_LENGTH, packageBuffer.Count - PACKAGE_TOTAL_LENGTH_LENGTH);
67-
var packageTotalLength = PACKAGE_TOTAL_LENGTH_LENGTH + retBuffer.Length;
68-
var currentBuffer = getFreeBuffer(packageBuffer.Array, sendBuffer, sendBuffer2);
69-
//写入包长度
70-
writePackageTotalLengthToBuffer(currentBuffer, 0, packageTotalLength);
71-
Array.Copy(retBuffer, 0, currentBuffer, PACKAGE_TOTAL_LENGTH_LENGTH, retBuffer.Length);
72-
packageBuffer = new ArraySegment<byte>(currentBuffer, 0, packageTotalLength);
71+
try
72+
{
73+
//准备管道
74+
if (encryptPipe == null)
75+
{
76+
encryptPipe = new Pipe();
77+
encryptBuffer1 = new byte[enc.InputBlockSize];
78+
encryptBuffer2 = new byte[enc.InputBlockSize * 2];
79+
}
80+
//开始加密
81+
var toEncryptedBuffer = packageBodyBuffer;
82+
var inLength = 0;
83+
while (toEncryptedBuffer.Length > 0)
84+
{
85+
inLength = Math.Min(encryptBuffer1.Length, (int)toEncryptedBuffer.Length);
86+
toEncryptedBuffer.Slice(0, inLength).CopyTo(encryptBuffer1);
87+
toEncryptedBuffer = toEncryptedBuffer.Slice(inLength);
88+
if (inLength < enc.InputBlockSize)
89+
{
90+
var v = enc.InputBlockSize - inLength;
91+
Array.Fill(encryptBuffer1, (byte)v, inLength, v);
92+
}
93+
var outLength = enc.TransformBlock(encryptBuffer1, 0, Math.Max(inLength, enc.InputBlockSize), encryptBuffer2, 0);
94+
encryptBuffer2.CopyTo(encryptPipe.Writer.GetMemory(outLength));
95+
encryptPipe.Writer.Advance(outLength);
96+
97+
packageTotalLength += outLength;
98+
}
99+
{
100+
var finnalInLength = 0;
101+
if (inLength == enc.InputBlockSize)
102+
{
103+
encryptBuffer1[0] = (byte)enc.InputBlockSize;
104+
finnalInLength = 1;
105+
}
106+
var finalData = dec.TransformFinalBlock(encryptBuffer1, 0, finnalInLength);
107+
finalData.CopyTo(encryptPipe.Writer.GetMemory(finalData.Length));
108+
encryptPipe.Writer.Advance(finalData.Length);
109+
packageTotalLength += finalData.Length;
110+
}
111+
_ = Task.Run(async () =>
112+
{
113+
await encryptPipe.Writer.FlushAsync().ConfigureAwait(false);
114+
});
115+
116+
var readRet = await encryptPipe.Reader.ReadAtLeastAsync(packageTotalLength).ConfigureAwait(false);
117+
packageBodyBuffer = readRet.Buffer;
118+
var packageBodyLength = Convert.ToInt32(packageBodyBuffer.Length);
119+
//包总长度
120+
packageTotalLength = PACKAGE_TOTAL_LENGTH_LENGTH + packageBodyLength;
121+
//准备包头
122+
writePackageTotalLengthToBuffer(sendHeadBuffer, 0, packageTotalLength);
123+
packageHeadMemory = new Memory<byte>(sendHeadBuffer, 0, PACKAGE_TOTAL_LENGTH_LENGTH);
124+
currentPipe = encryptPipe;
125+
}
126+
catch(Exception ex)
127+
{
128+
throw new IOException("发送数据加密时出错", ex);
129+
}
73130
}
74-
*/
75131
}
76132
else
77133
{

0 commit comments

Comments
 (0)