Skip to content

Commit 0a8742c

Browse files
committed
Add IQpSerializer and AbstractQpSerializer
1 parent 7546cfe commit 0a8742c

File tree

26 files changed

+139
-156
lines changed

26 files changed

+139
-156
lines changed

QpTestClient/Controls/CommandInfoControl.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
using Quick.Protocol;
2-
using Quick.Protocol.Utils;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.ComponentModel;
6-
using System.Data;
7-
using System.Drawing;
82
using System.Text;
93
using System.Windows.Forms;
104

QpTestClient/Forms/CommandTestForm.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
using Quick.Protocol;
22
using Quick.Protocol.Utils;
33
using System;
4-
using System.Collections.Generic;
5-
using System.ComponentModel;
6-
using System.Data;
7-
using System.Drawing;
8-
using System.Text;
94
using System.Windows.Forms;
105

116
namespace QpTestClient.Forms
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization.Metadata;
3+
4+
namespace Quick.Protocol
5+
{
6+
public abstract class AbstractQpSerializer<T> : IQpSerializer, IQpModel<T>
7+
{
8+
protected abstract JsonTypeInfo<T> GetTypeInfo();
9+
10+
public T Deserialize(string value)
11+
{
12+
return JsonSerializer.Deserialize(value, GetTypeInfo());
13+
}
14+
15+
public string Serialize(T obj)
16+
{
17+
return JsonSerializer.Serialize(obj, GetTypeInfo());
18+
}
19+
20+
object IQpSerializer.Deserialize(string value)
21+
{
22+
return Deserialize(value);
23+
}
24+
25+
string IQpSerializer.Serialize(object obj)
26+
{
27+
return Serialize((T)obj);
28+
}
29+
}
30+
}

Quick.Protocol/Base.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,15 @@ public class Base
88
Name = "基础指令集",
99
NoticeInfos = new QpNoticeInfo[]
1010
{
11-
QpNoticeInfo.Create<Notices.PrivateNotice>(Notices.NoticesSerializerContext.Default)
11+
QpNoticeInfo.Create(new Notices.PrivateNotice())
1212
},
1313
CommandInfos = new QpCommandInfo[]
1414
{
15-
QpCommandInfo.Create(
16-
new Commands.Connect.Request(),
17-
Commands.ConnectCommandSerializerContext.Default),
18-
QpCommandInfo.Create(
19-
new Commands.Authenticate.Request(),
20-
Commands.AuthenticateCommandSerializerContext.Default),
21-
QpCommandInfo.Create(
22-
new Commands.HandShake.Request(),
23-
Commands.HandShakeCommandSerializerContext.Default),
24-
QpCommandInfo.Create(
25-
new Commands.PrivateCommand.Request(),
26-
Commands.PrivateCommandCommandSerializerContext.Default),
27-
QpCommandInfo.Create(
28-
new Commands.GetQpInstructions.Request(),
29-
Commands.GetQpInstructionsCommandSerializerContext.Default)
15+
QpCommandInfo.Create(new Commands.Connect.Request()),
16+
QpCommandInfo.Create(new Commands.Authenticate.Request()),
17+
QpCommandInfo.Create(new Commands.HandShake.Request()),
18+
QpCommandInfo.Create(new Commands.PrivateCommand.Request()),
19+
QpCommandInfo.Create(new Commands.GetQpInstructions.Request())
3020
}
3121
};
3222
}

Quick.Protocol/CommandExecuterManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void Register<TCmdRequest, TCmdResponse>(Func<QpChannel, TCmdRequest, TCm
2929
}
3030

3131
public void Register<TCmdRequest, TCmdResponse>(TCmdRequest request, Func<QpChannel, TCmdRequest, TCmdResponse> commandExecuter)
32-
where TCmdRequest : class, IQpCommandRequest<TCmdResponse>, new()
32+
where TCmdRequest : class, IQpCommandRequest<TCmdRequest, TCmdResponse>, new()
3333
where TCmdResponse : class, new()
3434
{
3535
var cmdRequestTypeName = request.GetType().FullName;

Quick.Protocol/Commands/Authenticate/Request.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using Quick.Protocol.Model;
2-
using System.ComponentModel;
1+
using System.ComponentModel;
32
using System.Text.Json.Serialization.Metadata;
43

54
namespace Quick.Protocol.Commands.Authenticate
65
{
76
[DisplayName("认证")]
8-
public class Request : AbstractQpModel<Request>, IQpCommandRequest<Response>
7+
public class Request : AbstractQpSerializer<Request>, IQpCommandRequest<Request, Response>
98
{
10-
protected override JsonTypeInfo<Request> TypeInfo => AuthenticateCommandSerializerContext.Default.Request;
9+
protected override JsonTypeInfo<Request> GetTypeInfo() => AuthenticateCommandSerializerContext.Default.Request;
1110

1211
/// 认证回答
1312
/// </summary>
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using Quick.Protocol.Model;
2-
using System.Text.Json.Serialization.Metadata;
1+
using System.Text.Json.Serialization.Metadata;
32

43
namespace Quick.Protocol.Commands.Authenticate
54
{
6-
public class Response : AbstractQpModel<Response>
5+
public class Response : AbstractQpSerializer<Response>
76
{
8-
protected override JsonTypeInfo<Response> TypeInfo => AuthenticateCommandSerializerContext.Default.Response;
7+
protected override JsonTypeInfo<Response> GetTypeInfo() => AuthenticateCommandSerializerContext.Default.Response;
98

109
}
1110
}

Quick.Protocol/Commands/Connect/Request.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Quick.Protocol.Model;
2-
using System.ComponentModel;
1+
using System.ComponentModel;
32
using System.Text.Json.Serialization.Metadata;
43

54
namespace Quick.Protocol.Commands.Connect
@@ -8,9 +7,9 @@ namespace Quick.Protocol.Commands.Connect
87
/// 连接请求命令
98
/// </summary>
109
[DisplayName("连接")]
11-
public class Request : AbstractQpModel<Request>, IQpCommandRequest<Response>
10+
public class Request : AbstractQpSerializer<Request>, IQpCommandRequest<Request, Response>
1211
{
13-
protected override JsonTypeInfo<Request> TypeInfo => ConnectCommandSerializerContext.Default.Request;
12+
protected override JsonTypeInfo<Request> GetTypeInfo() => ConnectCommandSerializerContext.Default.Request;
1413
/// <summary>
1514
/// 指令集编号数组
1615
/// </summary>

Quick.Protocol/Commands/Connect/Response.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
using Quick.Protocol.Model;
2-
using System.Text.Json.Serialization.Metadata;
1+
using System.Text.Json.Serialization.Metadata;
32

43
namespace Quick.Protocol.Commands.Connect
54
{
65
/// <summary>
76
/// 连接响应命令
87
/// </summary>
9-
public class Response : AbstractQpModel<Response>
8+
public class Response : AbstractQpSerializer<Response>
109
{
11-
protected override JsonTypeInfo<Response> TypeInfo => ConnectCommandSerializerContext.Default.Response;
10+
protected override JsonTypeInfo<Response> GetTypeInfo() => ConnectCommandSerializerContext.Default.Response;
1211

1312
/// <summary>
1413
/// 缓存大小
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Quick.Protocol.Model;
2-
using System.ComponentModel;
1+
using System.ComponentModel;
32
using System.Text.Json.Serialization.Metadata;
43

54
namespace Quick.Protocol.Commands.GetQpInstructions
@@ -8,8 +7,8 @@ namespace Quick.Protocol.Commands.GetQpInstructions
87
/// 获取全部指令集信息请求
98
/// </summary>
109
[DisplayName("获取全部指令集信息")]
11-
public class Request : AbstractQpModel<Request>, IQpCommandRequest<Response>
10+
public class Request : AbstractQpSerializer<Request>, IQpCommandRequest<Request, Response>
1211
{
13-
protected override JsonTypeInfo<Request> TypeInfo => GetQpInstructionsCommandSerializerContext.Default.Request;
12+
protected override JsonTypeInfo<Request> GetTypeInfo() => GetQpInstructionsCommandSerializerContext.Default.Request;
1413
}
1514
}

0 commit comments

Comments
 (0)