Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
guo450212436
et
提交
05b2758d
E
et
项目概览
guo450212436
/
et
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
et
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
05b2758d
编写于
10月 18, 2016
作者:
T
tanghai
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
将普通消息与Rpc消息区分开来,目前rpcid放在协议中,下一步可能会放到消息中,还没想清楚
上级
1868d274
变更
22
隐藏空白更改
内联
并排
Showing
22 changed file
with
345 addition
and
160 deletion
+345
-160
Server/App/Program.cs
Server/App/Program.cs
+1
-1
Server/Base/Server.Base.csproj
Server/Base/Server.Base.csproj
+2
-2
Server/Controller/C2S_LoginEvent.cs
Server/Controller/C2S_LoginEvent.cs
+3
-3
Server/Controller/C2S_SubscribeLogEvent.cs
Server/Controller/C2S_SubscribeLogEvent.cs
+4
-4
Server/Model/Server.Model.csproj
Server/Model/Server.Model.csproj
+2
-2
Unity/Assets/Plugins/Base/Message/AMEvent.cs
Unity/Assets/Plugins/Base/Message/AMEvent.cs
+13
-2
Unity/Assets/Plugins/Base/Message/IErrorMessage.cs
Unity/Assets/Plugins/Base/Message/IErrorMessage.cs
+14
-8
Unity/Assets/Plugins/Base/Message/IMRegister.cs
Unity/Assets/Plugins/Base/Message/IMRegister.cs
+4
-3
Unity/Assets/Plugins/Base/Message/MessageAttribute.cs
Unity/Assets/Plugins/Base/Message/MessageAttribute.cs
+3
-6
Unity/Assets/Plugins/Base/Message/MessageHandlerAttribute.cs
Unity/Assets/Plugins/Base/Message/MessageHandlerAttribute.cs
+17
-0
Unity/Assets/Plugins/Base/Message/MessageHandlerAttribute.cs.meta
...sets/Plugins/Base/Message/MessageHandlerAttribute.cs.meta
+2
-2
Unity/Assets/Plugins/Base/Message/OpcodeAttribute.cs
Unity/Assets/Plugins/Base/Message/OpcodeAttribute.cs
+0
-14
Unity/Assets/Res/Code.meta
Unity/Assets/Res/Code.meta
+2
-2
Unity/Assets/Scripts/Component/MessageComponent.cs
Unity/Assets/Scripts/Component/MessageComponent.cs
+49
-85
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs
+210
-0
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs.meta
...ssets/Scripts/Component/MessageDispatherComponent.cs.meta
+0
-0
Unity/Assets/Scripts/Message/Message.cs
Unity/Assets/Scripts/Message/Message.cs
+9
-12
Unity/Controller/Event/InitSceneStartEvent_InitGame.cs
Unity/Controller/Event/InitSceneStartEvent_InitGame.cs
+3
-3
Unity/Controller/Message/S2C_ServerLogEvent.cs
Unity/Controller/Message/S2C_ServerLogEvent.cs
+2
-1
Unity/Unity.CSharp.Editor.csproj
Unity/Unity.CSharp.Editor.csproj
+2
-5
Unity/Unity.CSharp.Plugins.csproj
Unity/Unity.CSharp.Plugins.csproj
+1
-2
Unity/Unity.CSharp.csproj
Unity/Unity.CSharp.csproj
+2
-3
未找到文件。
Server/App/Program.cs
浏览文件 @
05b2758d
...
...
@@ -27,7 +27,7 @@ namespace App
Game
.
Scene
.
AddComponent
<
EventComponent
>();
Game
.
Scene
.
AddComponent
<
TimerComponent
>();
Game
.
Scene
.
AddComponent
<
NetworkComponent
,
NetworkProtocol
,
string
,
int
>(
options
.
Protocol
,
options
.
Host
,
options
.
Port
);
Game
.
Scene
.
AddComponent
<
Message
Handl
erComponent
,
string
>(
options
.
AppType
);
Game
.
Scene
.
AddComponent
<
Message
Dispath
erComponent
,
string
>(
options
.
AppType
);
// 根据不同的AppType添加不同的组件
switch
(
options
.
AppType
)
...
...
Server/Base/Server.Base.csproj
浏览文件 @
05b2758d
...
...
@@ -134,8 +134,8 @@
<Compile
Include=
"..\..\Unity\Assets\Plugins\Base\Message\MessageAttribute.cs"
>
<Link>
Message\MessageAttribute.cs
</Link>
</Compile>
<Compile
Include=
"..\..\Unity\Assets\Plugins\Base\Message\
Opcode
Attribute.cs"
>
<Link>
Message\
Opcode
Attribute.cs
</Link>
<Compile
Include=
"..\..\Unity\Assets\Plugins\Base\Message\
MessageHandler
Attribute.cs"
>
<Link>
Message\
MessageHandler
Attribute.cs
</Link>
</Compile>
<Compile
Include=
"..\..\Unity\Assets\Plugins\Base\Message\RpcException.cs"
>
<Link>
Message\RpcException.cs
</Link>
...
...
Server/Controller/C2S_LoginEvent.cs
浏览文件 @
05b2758d
...
...
@@ -3,13 +3,13 @@ using Model;
namespace
Controller
{
[
Message
(
AppType
.
Realm
)]
public
class
C2S_LoginEvent
:
AMEvent
<
C2S_Login
>
[
Message
Handler
(
AppType
.
Realm
)]
public
class
C2S_LoginEvent
:
AM
Rpc
Event
<
C2S_Login
>
{
protected
override
void
Run
(
Entity
scene
,
C2S_Login
message
,
uint
rpcId
)
{
Log
.
Info
(
MongoHelper
.
ToJson
(
message
));
scene
.
GetComponent
<
MessageComponent
>().
Send
(
rpcId
,
new
S2C_Login
{
ErrorMessage
=
new
ErrorMessage
()
}
);
scene
.
GetComponent
<
MessageComponent
>().
Reply
(
rpcId
,
new
S2C_Login
()
);
}
}
}
\ No newline at end of file
Server/Controller/C2S_SubscribeLogEvent.cs
浏览文件 @
05b2758d
...
...
@@ -3,16 +3,16 @@ using Model;
namespace
Controller
{
[
Message
(
AppType
.
Realm
)]
public
class
C2S_SubscribeLogEvent
:
AMEvent
<
C2S_SubscribeLog
>
[
Message
Handler
(
AppType
.
Realm
)]
public
class
C2S_SubscribeLogEvent
:
AM
Rpc
Event
<
C2S_SubscribeLog
>
{
protected
override
void
Run
(
Entity
entity
,
C2S_SubscribeLog
message
,
uint
rpcId
)
{
Log
.
Info
(
MongoHelper
.
ToJson
(
message
));
//
entity.AddComponent<LogToClientComponent>();
entity
.
AddComponent
<
LogToClientComponent
>();
entity
.
GetComponent
<
MessageComponent
>().
Send
(
rpcId
,
new
S2C_SubscribeLog
{
ErrorMessage
=
new
ErrorMessage
()
}
);
entity
.
GetComponent
<
MessageComponent
>().
Reply
(
rpcId
,
new
S2C_SubscribeLog
()
);
}
}
}
\ No newline at end of file
Server/Model/Server.Model.csproj
浏览文件 @
05b2758d
...
...
@@ -50,8 +50,8 @@
<Compile
Include=
"..\..\Unity\Assets\Scripts\Component\MessageComponent.cs"
>
<Link>
Component\MessageComponent.cs
</Link>
</Compile>
<Compile
Include=
"..\..\Unity\Assets\Scripts\Component\Message
Handl
erComponent.cs"
>
<Link>
Component\Message
Handl
erComponent.cs
</Link>
<Compile
Include=
"..\..\Unity\Assets\Scripts\Component\Message
Dispath
erComponent.cs"
>
<Link>
Component\Message
Dispath
erComponent.cs
</Link>
</Compile>
<Compile
Include=
"..\..\Unity\Assets\Scripts\Component\NetworkComponent.cs"
>
<Link>
Component\NetworkComponent.cs
</Link>
...
...
Unity/Assets/Plugins/Base/Message/AMEvent.cs
浏览文件 @
05b2758d
...
...
@@ -2,12 +2,23 @@
{
public
abstract
class
AMEvent
<
T
>:
IMRegister
{
protected
abstract
void
Run
(
Entity
scene
,
T
message
,
uint
rpcId
);
protected
abstract
void
Run
(
Entity
scene
,
T
message
);
public
void
Register
(
IMessage
Handl
er
component
)
public
void
Register
(
IMessage
Dispath
er
component
)
{
ushort
opcode
=
component
.
GetOpcode
(
typeof
(
T
));
component
.
RegisterHandler
<
T
>(
opcode
,
Run
);
}
}
public
abstract
class
AMRpcEvent
<
T
>
:
IMRegister
{
protected
abstract
void
Run
(
Entity
scene
,
T
message
,
uint
rpcId
);
public
void
Register
(
IMessageDispather
component
)
{
ushort
opcode
=
component
.
GetOpcode
(
typeof
(
T
));
component
.
RegisterRpcHandler
<
T
>(
opcode
,
Run
);
}
}
}
\ No newline at end of file
Unity/Assets/Plugins/Base/Message/IErrorMessage.cs
浏览文件 @
05b2758d
namespace
Base
{
/// <summary>
/// 服务端回的RPC消息需要继承这个接口
/// </summary>
public
interface
IErrorMessage
public
abstract
class
AMessage
{
}
public
abstract
class
ARequest
:
AMessage
{
ErrorMessage
ErrorMessage
{
get
;
set
;
}
public
uint
RpcId
;
}
public
class
ErrorMessage
/// <summary>
/// 服务端回的RPC消息需要继承这个抽象类
/// </summary>
public
abstract
class
AResponse
:
AMessage
{
public
int
Errno
=
0
;
public
uint
RpcId
;
public
int
ErrorCode
=
0
;
public
string
Message
=
""
;
}
}
Unity/Assets/Plugins/Base/Message/IMRegister.cs
浏览文件 @
05b2758d
...
...
@@ -2,14 +2,15 @@
namespace
Base
{
public
interface
IMessage
Handl
er
public
interface
IMessage
Dispath
er
{
void
RegisterHandler
<
T
>(
ushort
opcode
,
Action
<
Entity
,
T
,
uint
>
action
);
ushort
GetOpcode
(
Type
type
);
void
RegisterHandler
<
T
>(
ushort
opcode
,
Action
<
Entity
,
T
>
action
);
void
RegisterRpcHandler
<
T
>(
ushort
opcode
,
Action
<
Entity
,
T
,
uint
>
action
);
}
public
interface
IMRegister
{
void
Register
(
IMessage
Handler
messageHandl
er
);
void
Register
(
IMessage
Dispather
messageDispath
er
);
}
}
Unity/Assets/Plugins/Base/Message/MessageAttribute.cs
浏览文件 @
05b2758d
...
...
@@ -2,16 +2,13 @@
namespace
Base
{
/// <summary>
/// 搭配MessageComponent用来分发消息
/// </summary>
public
class
MessageAttribute
:
Attribute
{
public
string
AppTyp
e
{
get
;
private
set
;
}
public
ushort
Opcod
e
{
get
;
private
set
;
}
public
MessageAttribute
(
string
appTyp
e
)
public
MessageAttribute
(
ushort
opcod
e
)
{
this
.
AppType
=
appTyp
e
;
this
.
Opcode
=
opcod
e
;
}
}
}
\ No newline at end of file
Unity/Assets/Plugins/Base/Message/MessageHandlerAttribute.cs
0 → 100644
浏览文件 @
05b2758d
using
System
;
namespace
Base
{
/// <summary>
/// 搭配MessageComponent用来分发消息
/// </summary>
public
class
MessageHandlerAttribute
:
Attribute
{
public
string
AppType
{
get
;
private
set
;
}
public
MessageHandlerAttribute
(
string
appType
)
{
this
.
AppType
=
appType
;
}
}
}
\ No newline at end of file
Unity/Assets/Plugins/Base/Message/
Opcode
Attribute.cs.meta
→
Unity/Assets/Plugins/Base/Message/
MessageHandler
Attribute.cs.meta
浏览文件 @
05b2758d
fileFormatVersion: 2
guid:
485eb61dbc2b9664d80d40e19aa80c49
timeCreated: 1476
612496
guid:
fcf293ee7dda23746a98f4308723280e
timeCreated: 1476
426533
licenseType: Pro
MonoImporter:
serializedVersion: 2
...
...
Unity/Assets/Plugins/Base/Message/OpcodeAttribute.cs
已删除
100644 → 0
浏览文件 @
1868d274
using
System
;
namespace
Base
{
public
class
OpcodeAttribute
:
Attribute
{
public
ushort
Opcode
{
get
;
private
set
;
}
public
OpcodeAttribute
(
ushort
opcode
)
{
this
.
Opcode
=
opcode
;
}
}
}
\ No newline at end of file
Unity/Assets/Res/Code.meta
浏览文件 @
05b2758d
fileFormatVersion: 2
guid:
de24d6c9c5a460f48a4af1bff83d11f3
guid:
4805d347ea85a2c4ab97f544d710a65d
folderAsset: yes
timeCreated: 1476
243665
timeCreated: 1476
778403
licenseType: Pro
DefaultImporter:
userData:
...
...
Unity/Assets/Scripts/Component/MessageComponent.cs
浏览文件 @
05b2758d
...
...
@@ -22,13 +22,12 @@ namespace Model
{
private
static
uint
RpcId
{
get
;
set
;
}
private
readonly
Dictionary
<
uint
,
Action
<
byte
[],
int
,
int
>>
requestCallback
=
new
Dictionary
<
uint
,
Action
<
byte
[],
int
,
int
>>();
private
readonly
Dictionary
<
ushort
,
Action
<
byte
[],
int
,
int
>>
waitCallback
=
new
Dictionary
<
ushort
,
Action
<
byte
[],
int
,
int
>>();
private
AChannel
channel
;
private
Message
HandlerComponent
messageHandl
er
;
private
Message
DispatherComponent
messageDispath
er
;
public
void
Awake
(
AChannel
aChannel
)
{
this
.
message
Handler
=
Game
.
Scene
.
GetComponent
<
MessageHandl
erComponent
>();
this
.
message
Dispather
=
Game
.
Scene
.
GetComponent
<
MessageDispath
erComponent
>();
this
.
channel
=
aChannel
;
this
.
StartRecv
();
}
...
...
@@ -81,8 +80,10 @@ namespace Model
private
void
Run
(
ushort
opcode
,
byte
[]
messageBytes
)
{
int
offset
=
0
;
uint
flagUInt
=
BitConverter
.
ToUInt32
(
messageBytes
,
2
);
bool
isCompressed
=
(
flagUInt
&
0x80000000
)
>
0
;
uint
flag
=
BitConverter
.
ToUInt32
(
messageBytes
,
2
);
uint
rpcFlag
=
flag
&
0x40000000
;
uint
rpcId
=
flag
&
0x3fffffff
;
bool
isCompressed
=
(
flag
&
0x80000000
)
>
0
;
if
(
isCompressed
)
// 最高位为1,表示有压缩,需要解压缩
{
messageBytes
=
ZipHelper
.
Decompress
(
messageBytes
,
6
,
messageBytes
.
Length
-
6
);
...
...
@@ -92,34 +93,41 @@ namespace Model
{
offset
=
6
;
}
uint
rpcId
=
flagUInt
&
0x7fffffff
;
this
.
RunDecompressedBytes
(
opcode
,
rpcId
,
messageBytes
,
offset
);
this
.
RunDecompressedBytes
(
opcode
,
rpcId
,
rpcFlag
,
messageBytes
,
offset
);
}
private
void
RunDecompressedBytes
(
ushort
opcode
,
uint
rpcId
,
byte
[]
messageBytes
,
int
offset
)
private
void
RunDecompressedBytes
(
ushort
opcode
,
uint
rpcId
,
uint
rpcFlag
,
byte
[]
messageBytes
,
int
offset
)
{
Action
<
byte
[],
int
,
int
>
action
;
if
(
this
.
requestCallback
.
TryGetValue
(
rpcId
,
out
action
))
if
(
rpcId
==
0
)
{
this
.
requestCallback
.
Remove
(
rpcId
);
action
(
messageBytes
,
offset
,
messageBytes
.
Length
-
offset
);
return
;
this
.
messageDispather
.
Handle
(
this
.
Owner
,
opcode
,
messageBytes
,
offset
);
}
if
(
this
.
waitCallback
.
TryGetValue
(
opcode
,
out
action
))
// rpcFlag>0 表示这是一个rpc响应消息
if
(
rpcFlag
>
0
)
{
this
.
waitCallback
.
Remove
(
opcode
);
action
(
messageBytes
,
offset
,
messageBytes
.
Length
-
offset
);
return
;
Action
<
byte
[],
int
,
int
>
action
;
if
(
this
.
requestCallback
.
TryGetValue
(
rpcId
,
out
action
))
{
this
.
requestCallback
.
Remove
(
rpcId
);
action
(
messageBytes
,
offset
,
messageBytes
.
Length
-
offset
);
}
}
else
// 这是一个rpc请求消息
{
this
.
messageDispather
.
HandleRpc
(
this
.
Owner
,
opcode
,
messageBytes
,
offset
,
rpcId
);
}
this
.
messageHandler
.
Handle
(
this
.
Owner
,
opcode
,
messageBytes
,
offset
);
}
public
Task
<
Response
>
CallAsync
<
Response
>(
object
request
,
CancellationToken
cancellationToken
)
where
Response
:
IErrorMessage
/// <summary>
/// Rpc调用
/// </summary>
public
Task
<
Response
>
Call
<
Request
,
Response
>(
Request
request
,
CancellationToken
cancellationToken
)
where
Request
:
ARequest
where
Response
:
AResponse
{
this
.
Send
(++
RpcId
,
request
);
this
.
Send
Message
(++
RpcId
,
request
);
var
tcs
=
new
TaskCompletionSource
<
Response
>();
...
...
@@ -128,9 +136,9 @@ namespace Model
try
{
Response
response
=
MongoHelper
.
FromBson
<
Response
>(
bytes
,
offset
,
count
);
if
(
response
.
Error
Message
.
Errno
!=
0
)
if
(
response
.
Error
Code
!=
0
)
{
tcs
.
SetException
(
new
RpcException
(
response
.
Error
Message
.
Errno
,
response
.
ErrorMessag
e
.
Message
));
tcs
.
SetException
(
new
RpcException
(
response
.
Error
Code
,
respons
e
.
Message
));
return
;
}
tcs
.
SetResult
(
response
);
...
...
@@ -149,21 +157,21 @@ namespace Model
/// <summary>
/// Rpc调用,发送一个消息,等待返回一个消息
/// </summary>
/// <typeparam name="Response"></typeparam>
/// <param name="request"></param>
/// <returns></returns>
public
Task
<
Response
>
CallAsync
<
Response
>(
object
request
)
where
Response
:
IErrorMessage
public
Task
<
Response
>
Call
<
Request
,
Response
>(
Request
request
)
where
Request
:
ARequest
where
Response
:
AResponse
{
this
.
Send
(++
RpcId
,
request
);
request
.
RpcId
=
++
RpcId
;
this
.
SendMessage
(++
RpcId
,
request
);
var
tcs
=
new
TaskCompletionSource
<
Response
>();
this
.
requestCallback
[
RpcId
]
=
(
bytes
,
offset
,
count
)
=>
{
try
{
Response
response
=
MongoHelper
.
FromBson
<
Response
>(
bytes
,
offset
,
count
);
if
(
response
.
Error
Message
.
Errno
!=
0
)
if
(
response
.
Error
Code
!=
0
)
{
tcs
.
SetException
(
new
RpcException
(
response
.
Error
Message
.
Errno
,
response
.
ErrorMessag
e
.
Message
));
tcs
.
SetException
(
new
RpcException
(
response
.
Error
Code
,
respons
e
.
Message
));
return
;
}
tcs
.
SetResult
(
response
);
...
...
@@ -177,68 +185,24 @@ namespace Model
return
tcs
.
Task
;
}
/// <summary>
/// 不发送消息,直接等待返回一个消息
/// </summary>
/// <typeparam name="Response"></typeparam>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public
Task
<
Response
>
WaitAsync
<
Response
>(
CancellationToken
cancellationToken
)
where
Response
:
class
{
var
tcs
=
new
TaskCompletionSource
<
Response
>();
ushort
opcode
=
this
.
messageHandler
.
messageOpcode
[
typeof
(
Response
)];
this
.
waitCallback
[
opcode
]
=
(
bytes
,
offset
,
count
)
=>
{
try
{
Response
response
=
MongoHelper
.
FromBson
<
Response
>(
bytes
,
offset
,
count
);
tcs
.
SetResult
(
response
);
}
catch
(
Exception
e
)
{
tcs
.
SetException
(
new
Exception
(
$"Wait Error:
{
typeof
(
Response
).
FullName
}
"
,
e
));
}
};
cancellationToken
.
Register
(()
=>
{
this
.
waitCallback
.
Remove
(
opcode
);
});
return
tcs
.
Task
;
}
/// <summary>
/// 不发送消息,直接等待返回一个消息
/// </summary>
/// <typeparam name="Response"></typeparam>
/// <returns></returns>
public
Task
<
Response
>
WaitAsync
<
Response
>()
where
Response
:
class
public
void
Send
(
object
message
)
{
var
tcs
=
new
TaskCompletionSource
<
Response
>();
ushort
opcode
=
this
.
messageHandler
.
messageOpcode
[
typeof
(
Response
)];
this
.
waitCallback
[
opcode
]
=
(
bytes
,
offset
,
count
)
=>
{
try
{
Response
response
=
MongoHelper
.
FromBson
<
Response
>(
bytes
,
offset
,
count
);
tcs
.
SetResult
(
response
);
}
catch
(
Exception
e
)
{
tcs
.
SetException
(
new
Exception
(
$"Wait Error:
{
typeof
(
Response
).
FullName
}
"
,
e
));
}
};
return
tcs
.
Task
;
this
.
SendMessage
(
0
,
message
);
}
public
void
Send
(
object
message
)
public
void
Reply
<
T
>(
uint
rpcId
,
T
message
)
where
T
:
AResponse
{
this
.
Send
(
0
,
messag
e
);
this
.
Send
Message
(
rpcId
,
message
,
fals
e
);
}
p
ublic
void
Send
(
uint
rpcId
,
object
messag
e
)
p
rivate
void
SendMessage
(
uint
rpcId
,
object
message
,
bool
isCall
=
tru
e
)
{
ushort
opcode
=
this
.
message
Handl
er
.
GetOpcode
(
message
.
GetType
());
ushort
opcode
=
this
.
message
Dispath
er
.
GetOpcode
(
message
.
GetType
());
byte
[]
opcodeBytes
=
BitConverter
.
GetBytes
(
opcode
);
if
(
rpcId
>
0
&&
!
isCall
)
{
rpcId
=
rpcId
|
0x4fffffff
;
}
byte
[]
seqBytes
=
BitConverter
.
GetBytes
(
rpcId
);
byte
[]
messageBytes
=
MongoHelper
.
ToBson
(
message
);
...
...
Unity/Assets/Scripts/Component/Message
Handl
erComponent.cs
→
Unity/Assets/Scripts/Component/Message
Dispath
erComponent.cs
浏览文件 @
05b2758d
...
...
@@ -7,7 +7,7 @@ using Object = Base.Object;
namespace
Model
{
[
ObjectEvent
]
public
class
MessageHandlerComponentEvent
:
ObjectEvent
<
Message
Handl
erComponent
>,
ILoader
,
IAwake
<
string
>
public
class
MessageHandlerComponentEvent
:
ObjectEvent
<
Message
Dispath
erComponent
>,
ILoader
,
IAwake
<
string
>
{
public
void
Load
()
{
...
...
@@ -19,15 +19,25 @@ namespace Model
this
.
GetValue
().
Awake
(
appType
);
}
}
/// <summary>
/// 消息分发组件
/// </summary>
public
class
Message
HandlerComponent
:
Component
,
IMessageHandl
er
public
class
Message
DispatherComponent
:
Component
,
IMessageDispath
er
{
private
class
MessageInfo
{
public
byte
[]
MessageBytes
;
public
int
Offset
;
public
int
Count
;
public
uint
RpcId
;
}
private
string
AppType
;
private
Dictionary
<
ushort
,
List
<
Action
<
Entity
,
byte
[],
int
,
int
>>>
events
;
public
Dictionary
<
Type
,
ushort
>
messageOpcode
{
get
;
private
set
;
}
=
new
Dictionary
<
Type
,
ushort
>();
private
Dictionary
<
ushort
,
List
<
Action
<
Entity
,
MessageInfo
>>>
events
;
private
Dictionary
<
ushort
,
List
<
Action
<
Entity
,
MessageInfo
>>>
rpcHandlers
;
public
Dictionary
<
Type
,
MessageAttribute
>
messageOpcode
{
get
;
private
set
;
}
=
new
Dictionary
<
Type
,
MessageAttribute
>();
public
void
Awake
(
string
appType
)
{
...
...
@@ -37,8 +47,9 @@ namespace Model
public
void
Load
()
{
this
.
events
=
new
Dictionary
<
ushort
,
List
<
Action
<
Entity
,
byte
[],
int
,
int
>>>();
this
.
messageOpcode
=
new
Dictionary
<
Type
,
ushort
>();
this
.
events
=
new
Dictionary
<
ushort
,
List
<
Action
<
Entity
,
MessageInfo
>>>();
this
.
rpcHandlers
=
new
Dictionary
<
ushort
,
List
<
Action
<
Entity
,
MessageInfo
>>>();
this
.
messageOpcode
=
new
Dictionary
<
Type
,
MessageAttribute
>();
Assembly
[]
assemblies
=
Object
.
ObjectManager
.
GetAssemblies
();
...
...
@@ -47,14 +58,14 @@ namespace Model
Type
[]
types
=
assembly
.
GetTypes
();
foreach
(
Type
type
in
types
)
{
object
[]
attrs
=
type
.
GetCustomAttributes
(
typeof
(
Opcod
eAttribute
),
false
);
object
[]
attrs
=
type
.
GetCustomAttributes
(
typeof
(
Messag
eAttribute
),
false
);
if
(
attrs
.
Length
==
0
)
{
continue
;
}
OpcodeAttribute
opcodeAttribute
=
(
Opcod
eAttribute
)
attrs
[
0
];
this
.
messageOpcode
[
type
]
=
opcodeAttribute
.
Opcod
e
;
MessageAttribute
messageAttribute
=
(
Messag
eAttribute
)
attrs
[
0
];
this
.
messageOpcode
[
type
]
=
messageAttribut
e
;
}
}
...
...
@@ -63,14 +74,14 @@ namespace Model
Type
[]
types
=
assembly
.
GetTypes
();
foreach
(
Type
type
in
types
)
{
object
[]
attrs
=
type
.
GetCustomAttributes
(
typeof
(
MessageAttribute
),
false
);
object
[]
attrs
=
type
.
GetCustomAttributes
(
typeof
(
Message
Handler
Attribute
),
false
);
if
(
attrs
.
Length
==
0
)
{
continue
;
}
Message
Attribute
messageAttribute
=
(
Message
Attribute
)
attrs
[
0
];
if
(
messageAttribute
.
AppType
!=
this
.
AppType
)
Message
HandlerAttribute
messageHandlerAttribute
=
(
MessageHandler
Attribute
)
attrs
[
0
];
if
(
message
Handler
Attribute
.
AppType
!=
this
.
AppType
)
{
continue
;
}
...
...
@@ -89,39 +100,61 @@ namespace Model
public
ushort
GetOpcode
(
Type
type
)
{
return
this
.
messageOpcode
[
type
];
return
this
.
messageOpcode
[
type
]
.
Opcode
;
}
public
void
RegisterHandler
<
T
>(
ushort
opcode
,
Action
<
Entity
,
T
,
uint
>
action
)
public
void
RegisterHandler
<
T
>(
ushort
opcode
,
Action
<
Entity
,
T
>
action
)
{
if
(!
this
.
events
.
ContainsKey
(
opcode
))
{
this
.
events
.
Add
(
opcode
,
new
List
<
Action
<
Entity
,
byte
[],
int
,
int
>>());
this
.
events
.
Add
(
opcode
,
new
List
<
Action
<
Entity
,
MessageInfo
>>());
}
List
<
Action
<
Entity
,
byte
[],
int
,
int
>>
actions
=
this
.
events
[
opcode
];
List
<
Action
<
Entity
,
MessageInfo
>>
actions
=
this
.
events
[
opcode
];
actions
.
Add
((
entity
,
message
Bytes
,
offset
,
count
)
=>
actions
.
Add
((
entity
,
message
Info
)
=>
{
T
t
;
uint
rpcId
;
try
{
rpcId
=
BitConverter
.
ToUInt32
(
messageBytes
,
2
)
&
0x7fffffff
;
t
=
MongoHelper
.
FromBson
<
T
>(
messageBytes
,
offset
,
count
);
t
=
MongoHelper
.
FromBson
<
T
>(
messageInfo
.
MessageBytes
,
messageInfo
.
Offset
,
messageInfo
.
Count
);
}
catch
(
Exception
ex
)
{
throw
new
Exception
(
"解释消息失败:"
+
opcode
,
ex
);
}
action
(
entity
,
t
,
rpcId
);
action
(
entity
,
t
);
});
}
public
void
RegisterRpcHandler
<
T
>(
ushort
opcode
,
Action
<
Entity
,
T
,
uint
>
action
)
{
if
(!
this
.
rpcHandlers
.
ContainsKey
(
opcode
))
{
this
.
rpcHandlers
.
Add
(
opcode
,
new
List
<
Action
<
Entity
,
MessageInfo
>>());
}
List
<
Action
<
Entity
,
MessageInfo
>>
actions
=
this
.
rpcHandlers
[
opcode
];
actions
.
Add
((
entity
,
messageInfo
)
=>
{
T
t
;
try
{
t
=
MongoHelper
.
FromBson
<
T
>(
messageInfo
.
MessageBytes
,
messageInfo
.
Offset
,
messageInfo
.
Count
);
}
catch
(
Exception
ex
)
{
throw
new
Exception
(
"解释消息失败:"
+
opcode
,
ex
);
}
action
(
entity
,
t
,
messageInfo
.
RpcId
);
});
}
public
void
Handle
(
Entity
entity
,
ushort
opcode
,
byte
[]
messageBytes
,
int
offset
)
{
List
<
Action
<
Entity
,
byte
[],
int
,
int
>>
actions
;
List
<
Action
<
Entity
,
MessageInfo
>>
actions
;
if
(!
this
.
events
.
TryGetValue
(
opcode
,
out
actions
))
{
Log
.
Error
(
$"消息
{
opcode
}
没有处理"
);
...
...
@@ -132,7 +165,7 @@ namespace Model
{
try
{
ev
(
entity
,
messageBytes
,
offset
,
messageBytes
.
Length
-
offset
);
ev
(
entity
,
new
MessageInfo
{
MessageBytes
=
messageBytes
,
Offset
=
offset
,
Count
=
messageBytes
.
Length
-
offset
}
);
}
catch
(
Exception
e
)
{
...
...
@@ -140,7 +173,29 @@ namespace Model
}
}
}
public
void
HandleRpc
(
Entity
entity
,
ushort
opcode
,
byte
[]
messageBytes
,
int
offset
,
uint
rpcId
)
{
List
<
Action
<
Entity
,
MessageInfo
>>
actions
;
if
(!
this
.
rpcHandlers
.
TryGetValue
(
opcode
,
out
actions
))
{
Log
.
Error
(
$"Rpc消息
{
opcode
}
没有处理"
);
return
;
}
foreach
(
var
ev
in
actions
)
{
try
{
ev
(
entity
,
new
MessageInfo
{
MessageBytes
=
messageBytes
,
Offset
=
offset
,
Count
=
messageBytes
.
Length
-
offset
,
RpcId
=
rpcId
});
}
catch
(
Exception
e
)
{
Log
.
Error
(
e
.
ToString
());
}
}
}
public
override
void
Dispose
()
{
...
...
Unity/Assets/Scripts/Component/Message
Handl
erComponent.cs.meta
→
Unity/Assets/Scripts/Component/Message
Dispath
erComponent.cs.meta
浏览文件 @
05b2758d
文件已移动
Unity/Assets/Scripts/Message/Message.cs
浏览文件 @
05b2758d
...
...
@@ -3,25 +3,23 @@ using MongoDB.Bson.Serialization.Attributes;
namespace
Model
{
[
Opcod
e
(
1
)]
[
Messag
e
(
1
)]
[
BsonIgnoreExtraElements
]
public
class
C2S_Login
public
class
C2S_Login
:
ARequest
{
public
string
Account
;
public
string
Password
;
}
[
Opcod
e
(
2
)]
[
Messag
e
(
2
)]
[
BsonIgnoreExtraElements
]
public
class
S2C_Login
:
IErrorMessag
e
public
class
S2C_Login
:
ARespons
e
{
public
ErrorMessage
ErrorMessage
{
get
;
set
;
}
public
string
Host
;
public
int
Port
;
}
[
Opcod
e
(
3
)]
[
Messag
e
(
3
)]
[
BsonIgnoreExtraElements
]
public
class
S2C_ServerLog
{
...
...
@@ -29,16 +27,15 @@ namespace Model
public
string
Log
;
}
[
Opcod
e
(
4
)]
[
Messag
e
(
4
)]
[
BsonIgnoreExtraElements
]
public
class
C2S_SubscribeLog
public
class
C2S_SubscribeLog
:
ARequest
{
}
[
Opcod
e
(
5
)]
[
Messag
e
(
5
)]
[
BsonIgnoreExtraElements
]
public
class
S2C_SubscribeLog
:
IErrorMessag
e
public
class
S2C_SubscribeLog
:
ARespons
e
{
public
ErrorMessage
ErrorMessage
{
get
;
set
;
}
}
}
Unity/Controller/Event/InitSceneStartEvent_InitGame.cs
浏览文件 @
05b2758d
...
...
@@ -12,15 +12,15 @@ namespace Controller
{
public
async
void
Run
()
{
Game
.
Scene
.
AddComponent
<
Message
Handl
erComponent
,
string
>(
"Client"
);
Game
.
Scene
.
AddComponent
<
Message
Dispath
erComponent
,
string
>(
"Client"
);
NetworkComponent
networkComponent
=
Game
.
Scene
.
AddComponent
<
NetworkComponent
,
NetworkProtocol
>(
NetworkProtocol
.
TCP
);
Entity
session
=
networkComponent
.
Get
(
"127.0.0.1:8888"
);
try
{
// 订阅服务端日志, 服务端收到这个消息会将之后的日志转发给客户端
await
session
.
GetComponent
<
MessageComponent
>().
Call
Async
<
S2C_SubscribeLog
>(
new
C2S_SubscribeLog
());
S2C_Login
s2CLogin
=
await
session
.
GetComponent
<
MessageComponent
>().
Call
Async
<
S2C_Login
>(
new
C2S_Login
{
Account
=
"tanghai"
,
Password
=
"1111111"
});
await
session
.
GetComponent
<
MessageComponent
>().
Call
<
C2S_SubscribeLog
,
S2C_SubscribeLog
>(
new
C2S_SubscribeLog
());
S2C_Login
s2CLogin
=
await
session
.
GetComponent
<
MessageComponent
>().
Call
<
C2S_Login
,
S2C_Login
>(
new
C2S_Login
{
Account
=
"tanghai"
,
Password
=
"1111111"
});
Log
.
Info
(
MongoHelper
.
ToJson
(
s2CLogin
));
}
catch
(
RpcException
e
)
...
...
Unity/Controller/Message/S2C_ServerLogEvent.cs
浏览文件 @
05b2758d
...
...
@@ -3,9 +3,10 @@ using Model;
namespace
Controller
{
[
MessageHandler
(
AppType
.
Client
)]
public
class
S2C_ServerLogEvent
:
AMEvent
<
S2C_ServerLog
>
{
protected
override
void
Run
(
Entity
scene
,
S2C_ServerLog
message
,
uint
rpcId
)
protected
override
void
Run
(
Entity
scene
,
S2C_ServerLog
message
)
{
Log
.
Debug
(
message
.
Log
);
}
...
...
Unity/Unity.CSharp.Editor.csproj
浏览文件 @
05b2758d
...
...
@@ -13,13 +13,11 @@
<TargetFrameworkIdentifier>
.NETFramework
</TargetFrameworkIdentifier>
<TargetFrameworkVersion>
v3.5
</TargetFrameworkVersion>
<TargetFrameworkProfile>
Unity Full v3.5
</TargetFrameworkProfile>
<CompilerResponseFile>
</CompilerResponseFile>
<CompilerResponseFile></CompilerResponseFile>
<UnityProjectType>
Editor:5
</UnityProjectType>
<UnityBuildTarget>
StandaloneWindows:5
</UnityBuildTarget>
<UnityVersion>
5.4.2f1
</UnityVersion>
<RootNamespace>
</RootNamespace>
<RootNamespace></RootNamespace>
</PropertyGroup>
<PropertyGroup
Condition=
" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "
>
<DebugType>
pdbonly
</DebugType>
...
...
@@ -121,6 +119,5 @@
<None
Include=
"Assets\CSharp 6.0 Support\AsyncTools\Plugins\AsyncBridge.Net35.xml"
/>
<None
Include=
"Assets\CSharp 6.0 Support\AsyncTools\Plugins\System.Threading.xml"
/>
</ItemGroup>
<ItemGroup
/>
<Import
Project=
"$(MSBuildExtensionsPath)\SyntaxTree\UnityVS\2015\UnityVS.CSharp.targets"
/>
</Project>
Unity/Unity.CSharp.Plugins.csproj
浏览文件 @
05b2758d
...
...
@@ -102,7 +102,7 @@
<Compile
Include=
"Assets\Plugins\Base\Message\AMEvent.cs"
/>
<Compile
Include=
"Assets\Plugins\Base\Message\IErrorMessage.cs"
/>
<Compile
Include=
"Assets\Plugins\Base\Message\IMRegister.cs"
/>
<Compile
Include=
"Assets\Plugins\Base\Message\
Opcode
Attribute.cs"
/>
<Compile
Include=
"Assets\Plugins\Base\Message\
MessageHandler
Attribute.cs"
/>
<Compile
Include=
"Assets\Plugins\Base\Message\MessageAttribute.cs"
/>
<Compile
Include=
"Assets\Plugins\Base\Message\RpcException.cs"
/>
<Compile
Include=
"Assets\Plugins\Base\MultiMap.cs"
/>
...
...
@@ -510,6 +510,5 @@
<None
Include=
"Assets\CSharp 6.0 Support\AsyncTools\Plugins\AsyncBridge.Net35.xml"
/>
<None
Include=
"Assets\CSharp 6.0 Support\AsyncTools\Plugins\System.Threading.xml"
/>
</ItemGroup>
<ItemGroup
/>
<Import
Project=
"$(MSBuildExtensionsPath)\SyntaxTree\UnityVS\2015\UnityVS.CSharp.targets"
/>
</Project>
Unity/Unity.CSharp.csproj
浏览文件 @
05b2758d
...
...
@@ -80,12 +80,12 @@
</ItemGroup>
<ItemGroup>
<Compile
Include=
"Assets\Scripts\Component\ChildrenComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\GameObjectComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\EventComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\GameObjectComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\GlobalConfigComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\KVComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\MessageComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\Message
Handl
erComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\Message
Dispath
erComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\NetworkComponent.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\Scene.cs"
/>
<Compile
Include=
"Assets\Scripts\Component\TimeComponent.cs"
/>
...
...
@@ -111,6 +111,5 @@
<None
Include=
"Assets\CSharp 6.0 Support\AsyncTools\Plugins\AsyncBridge.Net35.xml"
/>
<None
Include=
"Assets\CSharp 6.0 Support\AsyncTools\Plugins\System.Threading.xml"
/>
</ItemGroup>
<ItemGroup
/>
<Import
Project=
"$(MSBuildExtensionsPath)\SyntaxTree\UnityVS\2015\UnityVS.CSharp.targets"
/>
</Project>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录