日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

ESFramework介紹之(19)―― 對動態(tài)組ActiveGr

系統(tǒng) 1814 0

什么是動態(tài)組?舉個例子就清楚了。QQ支持多人在一個組中聊天的模式是群,這是一種靜態(tài)組;而MSN中的動態(tài)的將多個人拉到一個組中來群聊,就是動態(tài)組。關(guān)于動態(tài)組,還有一個很好的例子,就是多人聯(lián)網(wǎng)游戲,比如聯(lián)網(wǎng)的星際。首先由發(fā)起者創(chuàng)建一個組(動態(tài)組),然后其它玩家加入進(jìn)來開始游戲,游戲結(jié)束了,該動態(tài)組就銷毀了。所以說,動態(tài)組是個“臨時性的”,生命比較短暫;而靜態(tài)組,通常被存儲于永久介質(zhì)(如數(shù)據(jù)庫)中,即使這場游戲結(jié)束,這個組依然存在,如。

ESFramework對靜態(tài)組的支持是“好友管理器”――IFriendManager(存在于ESFramework V0.1+),而對動態(tài)組的支持主要是ESFramework.Network.ActiveGroup(存在于ESFramework V0.2+)命名空間。

在服務(wù)器運行的過程中,可能需要創(chuàng)建成百上千的動態(tài)組,所有這些動態(tài)組需要被管理起來,這個管理者就是IActiveGroupManager。動態(tài)組管理器主要有以下職責(zé):
(1)根據(jù)請求創(chuàng)建/銷毀動態(tài)組。
(2)將某用戶加入到某動態(tài)組。
(3)將某用戶從動態(tài)組中移除。
(4)將組消息在指定的組內(nèi)廣播。
(5)查詢所有動態(tài)組的消息。

根據(jù)上述職責(zé)描述,可以定義IActiveGroupManager接口:

1 public interface IActiveGroupManager
2 {
3 bool CreateGroup( string creator, string groupName); // 如果有同名的group存在、則返回false
4 bool DestroyGroup( string userID, string groupName); // 如果無權(quán)刪除、則返回false
5
6 JoinGroupResultJoinGroup( string userID, string groupName);
7 void DropoutFromGroup( string userID, string groupName);
8 void BroadcastMessage( string sourceUserID, string destGroupName,NetMessagemsg); // 在目標(biāo)組內(nèi)廣播消息
9
10 ICollectionGroups{ get ;}
11 bool CreatorOwner{ set ;} // 如果為true,表示只有創(chuàng)建者才有權(quán)DestroyGroup
12 IToClientSenderToClientSender{ set ;}
13 }
14
15 public enum JoinGroupResult
16 {
17 Succeed,GroupIsNotExist,MaxSized
18 }

CreatorOwner屬性表明是否只有組的創(chuàng)建者才有權(quán)銷毀組。ESFramework中IActiveGroupManager接口的參考實現(xiàn)是ActiveGroupManager。IActiveGroupManager管理的對象是動態(tài)組,那么動態(tài)組的定義了、它有哪些職責(zé):
(1)管理組內(nèi)所有用戶,支持用戶加入組、從組中撤出。
(2)維護(hù)組的最大容量。如果當(dāng)前Size已經(jīng)達(dá)到最大容量,則無法再加入用戶
(3)當(dāng)組為空時,觸發(fā)GroupEmptied事件。應(yīng)用可以處理該事件,比如從管理器中刪除對應(yīng)的組。
動態(tài)組接口IActiveGroup定義如下:

1 public interface IActiveGroup
2 {
3 string Creator{ get ;} // 創(chuàng)建者
4 string GroupName{ get ;}
5 int Count{ get ;}
6 int MaxSize{ get ; set ;}
7
8 IListUserList{ get ;}
9
10 bool AddUser( string userID); // 當(dāng)達(dá)到MaxSize時,返回false
11 void RemoveUser( string userID);
12
13 event CbSimpleGroupEmptied;
14 }

ESFramework中IActiveGroup接口的參考實現(xiàn)是EsbActiveGroup。
為了能處理來自客戶端的動態(tài)組請求/消息,我們先要能區(qū)分它,我們消息類型枚舉中增加定義:

1 public enum ServiceType
2 {
3 Basic, // IBasicRequestDealer
4 Function,
5 P2PMessage, // 對于P2P消息,服務(wù)器僅僅轉(zhuǎn)發(fā),P2PMessageDealer
6 FriendRelation, // 如好友列表、好友資料等,F(xiàn)riendRelationDealer
7 GroupMessage , // 如多人聯(lián)網(wǎng)游戲中的同步消息等
8 CustomServiceType // 自定義服務(wù)種類
9 }

接著,對于GroupMessage這個大類,我們需要進(jìn)一步細(xì)分:

public enum ActiveGroupMessageType
{
Create,
// 需要回復(fù)-創(chuàng)建是否成功
Destroy, // 需要回復(fù)-銷毀是否成功
Join, // 需要回復(fù)-加入是否成功
DropoutFromGroup, // 不需要回復(fù)
Broadcast // 如游戲同步消息,不需要回復(fù)
}

為了使ESFramework能一致的處理所有的GroupMessage,我們設(shè)定了IActiveGroupMessage接口,所有的GroupMessage都可以轉(zhuǎn)換到這個接口:

/// <summary>
/// IGroupMessageActiveGroup消息的基礎(chǔ)接口,所有的組消息協(xié)議都要實現(xiàn)此接口。
/// zhuweisky2006.04.06
/// </summary>
public interface IActiveGroupMessage
{
string GroupName{ get ;}
ActiveGroupMessageTypeActiveGroupMessageType{
get ;}
}

另外,我們需要一個幫手來實現(xiàn)這個轉(zhuǎn)換:

public interface IActiveGroupHelper
{
IActiveGroupMessageParseGroupMessage(NetMessagemsg);
}

好了,一切就緒了,我們只差一個對應(yīng)的消息處理器來處理所有的GroupMessage,這個消息處理器就是ActiveGroupMessageDealer,它借助于IActiveGroupManager實現(xiàn)了對所有GroupMessage的處理,下面給出它的實現(xiàn)源碼:

ActiveGroupMessageDealer
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1 public class ActiveGroupMessageDealer:IDataDealer
2 {
3 public ActiveGroupMessageDealer()
4 {
5 }
6
7 #region Property
8 #region ActiveGroupHelper
9 private IActiveGroupHelperactiveGroupHelper = null ;
10 public IActiveGroupHelperActiveGroupHelper
11 {
12 set
13 {
14 this .activeGroupHelper = value;
15 }
16 }
17 #endregion
18
19 #region ActiveGroupManager
20 private IActiveGroupManageractiveGroupManager = null ;
21 public IActiveGroupManagerActiveGroupManager
22 {
23 set
24 {
25 this .activeGroupManager = value;
26 }
27 }
28 #endregion
29 #endregion
30
31 #region IDataDealer成員
32
33 public NetMessageDealRequestMessage(NetMessagereqMsg)
34 {
35 IActiveGroupMessagegroupMsg = this .activeGroupHelper.ParseGroupMessage(reqMsg);
36 if (groupMsg == null )
37 {
38 return null ;
39 }
40
41 IMessageHeaderresHeader = (IMessageHeader)reqMsg.Header.Clone();
42
43 switch (groupMsg.ActiveGroupMessageType)
44 {
45 case ActiveGroupMessageType.Create:
46 {
47 bool suc = this .activeGroupManager.CreateGroup(reqMsg.Header.UserID,groupMsg.GroupName);
48 resHeader.MessageBodyLength = 0 ;
49 resHeader.Result = suc ? 1 :ServiceResultType.GroupIsExist;
50 return new NetMessage(resHeader, null );
51 }
52 case ActiveGroupMessageType.Destroy:
53 {
54 bool suc = this .activeGroupManager.DestroyGroup(reqMsg.Header.UserID,groupMsg.GroupName);
55 resHeader.MessageBodyLength = 0 ;
56 resHeader.Result = suc ? 1 :ServiceResultType.NoPermissionToDestroyGroup;
57 return new NetMessage(resHeader, null );
58 }
59 case ActiveGroupMessageType.Join:
60 {
61 JoinGroupResultresult = this .activeGroupManager.JoinGroup(reqMsg.Header.UserID,groupMsg.GroupName);
62 resHeader.MessageBodyLength = 0 ;
63 resHeader.Result = ActiveGroupMessageDealer.ConvertServiceResultType(result);
64 return new NetMessage(resHeader, null );
65 }
66 case ActiveGroupMessageType.DropoutFromGroup:
67 {
68 this .activeGroupManager.DropoutFromGroup(reqMsg.Header.UserID,groupMsg.GroupName);
69 return null ;
70 }
71 case ActiveGroupMessageType.Broadcast:
72 {
73 this .activeGroupManager.BroadcastMessage(reqMsg.Header.UserID,groupMsg.GroupName,reqMsg);
74 return null ;
75 }
76 default :
77 {
78 return null ;
79 }
80 }
81 }
82
83 #endregion
84
85 #region ConvertServiceResultType
86 public static int ConvertServiceResultType(JoinGroupResultresult)
87 {
88 switch (result)
89 {
90 case JoinGroupResult.GroupIsNotExist:
91 {
92 return ServiceResultType.GroupIsNotExist;
93 }
94 case JoinGroupResult.MaxSized:
95 {
96 return ServiceResultType.GroupSizeLimited;
97 }
98 case JoinGroupResult.Succeed:
99 {
100 return ServiceResultType.ServiceSucceed;
101 }
102 default :
103 {
104 return ServiceResultType.ServiceSucceed;
105 }
106 }
107 }
108 #endregion
109 }

最后,我們將ActiveGroupMessageDealer處理器添加到默認(rèn)的處理器工廠EsbRequestDealerFactory中。(代碼略)
說明一下,ActiveGroup的命名空間是ESFramework.Network.ActiveGroup,表明其與協(xié)議是無關(guān)的,即它可以用于Tcp協(xié)議,也可以用于Udp協(xié)議。
感謝關(guān)注!

上一篇文章: ESFramework介紹之(18)―― Tcp用戶管理器組件

轉(zhuǎn)到: ESFramework 可復(fù)用的通信框架(序)

ESFramework介紹之(19)―― 對動態(tài)組ActiveGroup的支持


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 新乐市| 黄陵县| 武平县| 潍坊市| 钟祥市| 泽普县| 成安县| 双鸭山市| 合山市| 武穴市| 长岭县| 三河市| 克拉玛依市| 肃宁县| 名山县| 雷山县| 定南县| 通辽市| 台江县| 阿尔山市| 庄浪县| 罗定市| 宣武区| 抚远县| 广水市| 常德市| 新丰县| 金昌市| 和林格尔县| 惠水县| 西平县| 红原县| 永兴县| 临武县| 汤原县| 上林县| 建平县| 安塞县| 青岛市| 新营市| 甘洛县|