亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統 1585 0

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

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

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

根據上述職責描述,可以定義IActiveGroupManager接口:

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

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

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

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

1 public enum ServiceType
2 {
3 Basic, // IBasicRequestDealer
4 Function,
5 P2PMessage, // 對于P2P消息,服務器僅僅轉發,P2PMessageDealer
6 FriendRelation, // 如好友列表、好友資料等,FriendRelationDealer
7 GroupMessage , // 如多人聯網游戲中的同步消息等
8 CustomServiceType // 自定義服務種類
9 }

接著,對于GroupMessage這個大類,我們需要進一步細分:

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

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

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

另外,我們需要一個幫手來實現這個轉換:

public interface IActiveGroupHelper
{
IActiveGroupMessageParseGroupMessage(NetMessagemsg);
}

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

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處理器添加到默認的處理器工廠EsbRequestDealerFactory中。(代碼略)
說明一下,ActiveGroup的命名空間是ESFramework.Network.ActiveGroup,表明其與協議是無關的,即它可以用于Tcp協議,也可以用于Udp協議。
感謝關注!

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

轉到: ESFramework 可復用的通信框架(序)

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产不卡福利 | 在线不卡日韩 | 国产91福利在线精品剧情尤物 | 色吧综合网| h视频网站在线观看 | 国内精品久久久久久影院网站小说 | 91这里只有精品 | 亚洲精品老司机综合影院 | 日韩高清欧美 | 国产一区在线mmai | 91精品久久久久久久久网影视 | 99精品欧美一区二区三区美图 | 美欧毛片 | 在线不卡日本 | 久久99精品国产麻豆不卡 | 久草成人 | 久久一本一区二区三区 | 久久久久久久久久免免费精品 | 色国产精品一区在线观看 | 在线成人a毛片免费播放 | 色一情一欲一爱一乱 | 精品国产免费观看久久久 | 中国国产高清一级毛片 | 狠狠狠狠狠狠 | 香蕉视频在线观看免费 | 久久91综合国产91久久精品 | 在线视频日韩精品 | 国产小视频在线观看 | 九九热只有精品 | 欧美一级久久久久久久大 | 黄色大全网站 | 久久久精品午夜免费不卡 | 四虎影视在线看免费观看 | 色网站在线播放 | 国产青草 | 成人夜视频 | 国产欧美精品一区二区三区-老狼 | 亚洲高清专区 | 久久精品99久久香蕉国产色戒 | 麻豆精品久久精品色综合 | 色综合a怡红院怡红院首页 色综合h |