-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathMessage.cs
130 lines (110 loc) · 4.97 KB
/
Message.cs
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
namespace KBEngine
{
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using MessageID = System.UInt16;
/*
消息模块
客户端与服务端交互基于消息通讯, 任何一个行为一条指令都是以一个消息包来描述
*/
public class Message
{
public MessageID id = 0;
public string name;
public Int16 msglen = -1;
public System.Reflection.MethodInfo handler = null;
public KBEDATATYPE_BASE[] argtypes = null;
public sbyte argsType = 0;
public static Dictionary<MessageID, Message> loginappMessages = new Dictionary<MessageID, Message>();
public static Dictionary<MessageID, Message> baseappMessages = new Dictionary<MessageID, Message>();
public static Dictionary<MessageID, Message> clientMessages = new Dictionary<MessageID, Message>();
public static Dictionary<string, Message> messages = new Dictionary<string, Message>();
public static void clear()
{
loginappMessages = new Dictionary<MessageID, Message>();
baseappMessages = new Dictionary<MessageID, Message>();
clientMessages = new Dictionary<MessageID, Message>();
messages = new Dictionary<string, Message>();
bindFixedMessage();
}
/*
提前约定一些固定的协议
这样可以在没有从服务端导入协议之前就能与服务端进行握手等交互。
*/
public static void bindFixedMessage()
{
// 引擎协议说明参见: http://www.kbengine.org/cn/docs/programming/clientsdkprogramming.html
Message.messages["Loginapp_importClientMessages"] = new Message(5, "importClientMessages", 0, 0, new List<Byte>(), null);
Message.messages["Loginapp_hello"] = new Message(4, "hello", -1, -1, new List<Byte>(), null);
Message.messages["Baseapp_importClientMessages"] = new Message(207, "importClientMessages", 0, 0, new List<Byte>(), null);
Message.messages["Baseapp_importClientEntityDef"] = new Message(208, "importClientMessages", 0, 0, new List<Byte>(), null);
Message.messages["Baseapp_hello"] = new Message(200, "hello", -1, -1, new List<Byte>(), null);
Message.messages["Client_onHelloCB"] = new Message(521, "Client_onHelloCB", -1, -1, new List<Byte>(),
KBEngineApp.app.GetType().GetMethod("Client_onHelloCB"));
Message.clientMessages[Message.messages["Client_onHelloCB"].id] = Message.messages["Client_onHelloCB"];
Message.messages["Client_onScriptVersionNotMatch"] = new Message(522, "Client_onScriptVersionNotMatch", -1, -1, new List<Byte>(),
KBEngineApp.app.GetType().GetMethod("Client_onScriptVersionNotMatch"));
Message.clientMessages[Message.messages["Client_onScriptVersionNotMatch"].id] = Message.messages["Client_onScriptVersionNotMatch"];
Message.messages["Client_onVersionNotMatch"] = new Message(523, "Client_onVersionNotMatch", -1, -1, new List<Byte>(),
KBEngineApp.app.GetType().GetMethod("Client_onVersionNotMatch"));
Message.clientMessages[Message.messages["Client_onVersionNotMatch"].id] = Message.messages["Client_onVersionNotMatch"];
Message.messages["Client_onImportClientMessages"] = new Message(518, "Client_onImportClientMessages", -1, -1, new List<Byte>(),
KBEngineApp.app.GetType().GetMethod("Client_onImportClientMessages"));
Message.clientMessages[Message.messages["Client_onImportClientMessages"].id] = Message.messages["Client_onImportClientMessages"];
}
public Message(MessageID msgid, string msgname, Int16 length, sbyte argstype, List<Byte> msgargtypes, System.Reflection.MethodInfo msghandler)
{
id = msgid;
name = msgname;
msglen = length;
handler = msghandler;
argsType = argstype;
// 对该消息的所有参数绑定反序列化方法,该方法能够将二进制流转化为参数需要的值
// 在服务端下发消息数据时会用到
argtypes = new KBEDATATYPE_BASE[msgargtypes.Count];
for(int i=0; i<msgargtypes.Count; i++)
{
if(!EntityDef.id2datatypes.TryGetValue(msgargtypes[i], out argtypes[i]))
{
Dbg.ERROR_MSG("Message::Message(): argtype(" + msgargtypes[i] + ") is not found!");
}
}
// Dbg.DEBUG_MSG(string.Format("Message::Message(): ({0}/{1}/{2})!",
// msgname, msgid, msglen));
}
/*
从二进制数据流中创建该消息的参数数据
*/
public object[] createFromStream(MemoryStream msgstream)
{
if(argtypes.Length <= 0)
return new object[]{msgstream};
object[] result = new object[argtypes.Length];
for(int i=0; i<argtypes.Length; i++)
{
result[i] = argtypes[i].createFromStream(msgstream);
}
return result;
}
/*
将一个消息包反序列化后交给消息相关联的函数处理
例如:KBEngineApp.Client_onRemoteMethodCall
*/
public void handleMessage(MemoryStream msgstream)
{
if(argtypes.Length <= 0)
{
if(argsType < 0)
handler.Invoke(KBEngineApp.app, new object[]{msgstream});
else
handler.Invoke(KBEngineApp.app, new object[]{});
}
else
{
handler.Invoke(KBEngineApp.app, createFromStream(msgstream));
}
}
}
}