Skip to content

Commit 6be2aab

Browse files
committed
由于json-glib库的JSON序列化不支持自动序列化数组属性,移植暂停。
1 parent 57779ed commit 6be2aab

18 files changed

+413
-22
lines changed

main.vala

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
class Demo.HelloWorld : GLib.Object {
22
public static int main(string[] args) {
3-
QuickProtocol.MyLib.hello();
4-
QuickProtocol.Tcp.MyLib.hello();
3+
var obj = Quick.Protocol.Base.GetInstruction();
4+
Json.Node root = Json.gobject_serialize(obj);
5+
Json.Generator generator = new Json.Generator();
6+
generator.set_root(root);
7+
generator.set_pretty(true);
8+
string data = generator.to_data(null);
59

6-
stdout.printf("Hello, Vala\n");
10+
stdout.printf(@"$data\n");
11+
stdout.printf("Hello, QuickProtocol.\n");
712
return 0;
813
}
914
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Quick.Protocol {
2+
public abstract class AbstractQpSerializer<T>: Object, IQpSerializer, IQpModel<T> {
3+
public Object Deserialize(Type type, string value) {
4+
return Json.gobject_from_data(type, value, value.length);
5+
}
6+
7+
public string Serialize(Object obj) {
8+
size_t gobject_to_data_length;
9+
return Json.gobject_to_data(obj, out gobject_to_data_length);
10+
}
11+
12+
public T DeserializeT(string value) {
13+
return (T) Deserialize(typeof (T), value);
14+
}
15+
16+
public string SerializeT(T obj) {
17+
return Serialize((Object)obj);
18+
}
19+
}
20+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Quick.Protocol {
2+
public class Base : Object {
3+
private static QpInstruction _Instruction;
4+
public static QpInstruction GetInstruction() {
5+
if (_Instruction == null) {
6+
_Instruction = new QpInstruction();
7+
_Instruction.Id = "Quick.Protocol.Base";
8+
_Instruction.Name = "基础指令集";
9+
_Instruction.NoticeInfos = {
10+
// QpNoticeInfo.Create (new Notices.PrivateNotice ())
11+
};
12+
_Instruction.CommandInfos = {
13+
QpCommandInfo.Create<Commands.Connect.Request, Commands.Connect.Response> (),
14+
QpCommandInfo.Create<Commands.Authenticate.Request, Commands.Authenticate.Response> (),
15+
// QpCommandInfo.Create (new Commands.HandShake.Request ()),
16+
// QpCommandInfo.Create (new Commands.PrivateCommand.Request ()),
17+
// QpCommandInfo.Create (new Commands.GetQpInstructions.Request ())
18+
};
19+
}
20+
return _Instruction;
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Quick.Protocol.Commands.Authenticate
2+
{
3+
[Description (nick = "认证")]
4+
public class Request : AbstractQpSerializer<Request>, IQpCommandRequest<Request, Response>
5+
{
6+
/// 认证回答
7+
/// </summary>
8+
public string Answer { get; set; }
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Quick.Protocol.Commands.Authenticate
2+
{
3+
public class Response : AbstractQpSerializer<Response>
4+
{
5+
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Quick.Protocol.Commands.Connect
2+
{
3+
/// <summary>
4+
/// 连接请求命令
5+
/// </summary>
6+
[Description (nick = "连接")]
7+
public class Request : AbstractQpSerializer<Request>, IQpCommandRequest<Request, Response>
8+
{
9+
/// <summary>
10+
/// 指令集编号数组
11+
/// </summary>
12+
public string[] InstructionIds { get; set; }
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Quick.Protocol.Commands.Connect {
2+
/// <summary>
3+
/// 连接响应命令
4+
/// </summary>
5+
public class Response : AbstractQpSerializer<Response> {
6+
/// <summary>
7+
/// 缓存大小
8+
/// </summary>
9+
public int BufferSize { get; set; }
10+
/// <summary>
11+
/// 认证问题
12+
/// </summary>
13+
public string Question { get; set; }
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace Quick.Protocol {
2+
public interface IQpCommandRequest<TRequest, TResponse> : Object {
3+
}
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Quick.Protocol {
2+
public interface IQpSerializer : Object {
3+
public abstract string Serialize(Object obj);
4+
public abstract Object Deserialize(Type type, string value);
5+
}
6+
7+
public interface IQpModel<T>: IQpSerializer {
8+
public abstract string SerializeT(T obj);
9+
public abstract T DeserializeT(string value);
10+
}
11+
}

subprojects/quickprotocol-core/MyLib.vala

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
namespace Quick.Protocol {
2+
/// <summary>
3+
/// 命令信息
4+
/// </summary>
5+
public class QpCommandInfo : Object {
6+
/// <summary>
7+
/// 名称
8+
/// </summary>
9+
public string Name { get; set; }
10+
/// <summary>
11+
/// 描述
12+
/// </summary>
13+
public string Description { get; set; }
14+
/// <summary>
15+
/// 命令请求类型名称
16+
/// </summary>
17+
public string RequestTypeName { get; set; }
18+
/// <summary>
19+
/// 请求示例
20+
/// </summary>
21+
public string RequestTypeSchemaSample { get; set; }
22+
/// <summary>
23+
/// 命令响应类型名称
24+
/// </summary>
25+
public string ResponseTypeName { get; set; }
26+
/// <summary>
27+
/// 响应示例
28+
/// </summary>
29+
public string ResponseTypeSchemaSample { get; set; }
30+
private IQpSerializer requestSerializer;
31+
private IQpSerializer responseSerializer;
32+
33+
public IQpSerializer GetRequestSeriliazer() {
34+
return requestSerializer;
35+
}
36+
37+
public IQpSerializer GetResponseSeriliazer() {
38+
return responseSerializer;
39+
}
40+
41+
private Type requestType;
42+
private Type responseType;
43+
44+
public QpCommandInfo() {}
45+
46+
public QpCommandInfo.withParameters(string name, string description,
47+
Type requestType, Type responseType,
48+
Object defaultRequestTypeInstance, Object defaultResponseTypeInstance,
49+
IQpSerializer requestSerializer, IQpSerializer responseSerializer)
50+
{
51+
Name = name;
52+
Description = description;
53+
this.requestSerializer = requestSerializer;
54+
this.responseSerializer = responseSerializer;
55+
56+
this.requestType = requestType;
57+
requestType.name();
58+
RequestTypeName = requestType.name();
59+
size_t serialize_gobject_length;
60+
RequestTypeSchemaSample = Json.gobject_to_data(defaultRequestTypeInstance, out serialize_gobject_length);
61+
this.responseType = responseType;
62+
ResponseTypeName = responseType.name();
63+
ResponseTypeSchemaSample = Json.gobject_to_data(defaultResponseTypeInstance, out serialize_gobject_length);
64+
}
65+
66+
/// <summary>
67+
/// 获取命令请求类型
68+
/// </summary>
69+
/// <returns></returns>
70+
public Type GetRequestType() {
71+
return requestType;
72+
}
73+
74+
/// <summary>
75+
/// 获取命令响应类型
76+
/// </summary>
77+
/// <returns></returns>
78+
public Type GetResponseType() {
79+
return responseType;
80+
}
81+
82+
/// <summary>
83+
/// 创建命令信息实例
84+
/// </summary>
85+
public static QpCommandInfo Create<TRequest, TResponse> () {
86+
var requestType = typeof (TRequest);
87+
var responseType = typeof (TResponse);
88+
return CreateWithRequestAndResponse(Object.new(requestType), Object.new(responseType));
89+
}
90+
91+
public static QpCommandInfo CreateWithRequestAndResponse<TRequest, TResponse> (TRequest request, TResponse response) {
92+
var requestType = typeof (TRequest);
93+
var responseType = typeof (TResponse);
94+
string name = null;
95+
if (name == null)
96+
name = requestType.name();
97+
98+
return new QpCommandInfo.withParameters(name, "",
99+
requestType, responseType,
100+
(Object) request, (Object) response,
101+
(IQpSerializer) request, (IQpSerializer) response);
102+
}
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Quick.Protocol {
2+
public class QpConsts {
3+
public const string QUICK_PROTOCOL_NAME = "Quick.Protocol";
4+
public const string QUICK_PROTOCOL_VERSION = "2.0";
5+
public const string QuickProtocolNameAndVersion = "Quick.Protocol_2.0";
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
namespace Quick.Protocol {
2+
/// <summary>
3+
/// QP指令集
4+
/// </summary>
5+
public class QpInstruction : Object {
6+
/// <summary>
7+
/// 指令集编号
8+
/// </summary>
9+
[Description (nick = "编号")]
10+
public string Id { get; set; }
11+
/// <summary>
12+
/// 指令集名称
13+
/// </summary>
14+
[Description (nick = "名称")]
15+
public string Name { get; set; }
16+
/// <summary>
17+
/// 包含的通知信息数组
18+
/// </summary>
19+
public QpNoticeInfo[] NoticeInfos { get; set; }
20+
/// <summary>
21+
/// 包含的命令信息数组
22+
/// </summary>
23+
public QpCommandInfo[] CommandInfos { get; set; }
24+
}
25+
}
26+
/*
27+
数字采用大端字节序
28+
包长度包含4个字节的包长度数据。
29+
命令编号为16字节GUID
30+
31+
0->心跳数据包结构:
32+
[4字节] [1字节]
33+
包长度 包类型
34+
35+
1->通知数据包结构:
36+
[4字节] [1字节] [1字节] [n字节] [n字节]
37+
包长度 包类型 类名长度 类名(A.B.C) 类内容(JSON)
38+
|-------包内容----------|
39+
40+
2->命令请求结构:
41+
[4字节] [1字节] [16字节] [1字节] [n字节] [n字节]
42+
包长度 包类型 命令编号 类名长度 类名(A.B.C) 类内容(JSON)
43+
|-------包内容----------------|
44+
45+
3->命令响应结构:
46+
[4字节] [1字节] [16字节] [1字节]
47+
包长度 包类型 命令编号 返回码(0代表成功,其他代表失败)
48+
成功:[1字节] [n字节] [n字节]
49+
类名长度 类名(A.B.C) 类内容(JSON)
50+
失败:[n字节]
51+
错误消息(字符串)
52+
|-------包内容----------------------------|
53+
54+
255->拆分数据包结构:
55+
[4字节] [1字节] [4字节|0字节] [n字节]
56+
包长度 包类型 原始包长度,第一个拆分包才有4个字节的长度 类内容(Byte Array)
57+
|-------包内容----------------------------|
58+
59+
加密、压缩、拆分
60+
WcfTestClient -> QpTestClient,获取功能集列表,功能集里面的指令、通知包,测试指令调用,生成dll、jar文件。
61+
62+
连接流程:
63+
客户端 服务端
64+
连接命令请求->
65+
<-连接命令响应
66+
认证命令请求->
67+
<-认证命令响应
68+
69+
*/

0 commit comments

Comments
 (0)