forked from kerryjiang/SuperSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixedHeaderProtocolTest.cs
More file actions
55 lines (47 loc) · 1.6 KB
/
FixedHeaderProtocolTest.cs
File metadata and controls
55 lines (47 loc) · 1.6 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
using System;
using System.Buffers;
using System.Text;
using Microsoft.Extensions.Hosting;
using SuperSocket;
using SuperSocket.ProtoBase;
using SuperSocket.Server;
using Xunit;
using Xunit.Abstractions;
namespace Tests
{
[Collection("Protocol.FixedHeader")]
public class FixedHeaderProtocolTest : ProtocolTestBase
{
public FixedHeaderProtocolTest(ITestOutputHelper outputHelper) : base(outputHelper)
{
}
class MyFixedHeaderPipelineFilter : FixedHeaderPipelineFilter<TextPackageInfo>
{
public MyFixedHeaderPipelineFilter()
: base(4)
{
}
protected override int GetBodyLengthFromHeader(ReadOnlySequence<byte> buffer)
{
var strLen = buffer.GetString(Utf8Encoding);
return int.Parse(strLen.TrimStart('0'));
}
protected override TextPackageInfo DecodePackage(ReadOnlySequence<byte> buffer)
{
return new TextPackageInfo { Text = buffer.Slice(4).GetString(Utf8Encoding) };
}
}
protected override string CreateRequest(string sourceLine)
{
return sourceLine.Length.ToString().PadLeft(4) + sourceLine;
}
protected override IServer CreateServer()
{
return CreateSocketServerBuilder<TextPackageInfo, MyFixedHeaderPipelineFilter>()
.ConfigurePackageHandler(async (s, p) =>
{
await s.SendAsync(Utf8Encoding.GetBytes(p.Text + "\r\n"));
}).BuildAsServer() as IServer;
}
}
}