/// <summary> /// 对单一分组发送消息 /// </summary> /// <param name="msg">要发送的消息</param> /// <returns>是否成功</returns> public static bool send(Msg msg) { List<Peer> peers = Singleton.BaseGet(msg.target) as List<Peer>; if (!msg.valid || peers == null) return false; if (peers.Count == 0) return false; for (int i = 0; i < peers.Count; i++) peers[i].send(msg, false); return true; }
/// <summary> /// 通知同步返回 /// </summary> /// <param name="sender">发送消息的主机</param> /// <param name="msg">消息</param> public static void doCallBack(Peer sender, Msg msg) { if (ht[msg.type] != null) { List<CallBack> list = ht[msg.type] as List<CallBack>; lock (list) { for (int i = 0; i < list.Count; i++) list[i].data = msg.data; ht[msg.type] = null; } } }
/// <summary> /// 处理其他消息 /// </summary> /// <param name="peer">消息来源主机</param> /// <param name="msg">消息</param> private static void OtherMessage(Peer peer, Msg msg) { switch (msg.type) { case 21: peer.close(); IPAddress add = msg.data as IPAddress; Socket s = NetUtils.TestConnect(new IPEndPoint(add, Configuration.Port)); if (s == null) HoldingServer.Singleton.TestServer(); else HoldingServer.Singleton.ClientStart(new Peer(s, Message)); break; } }
/// <summary> /// 处理管理主机消息 /// </summary> /// <param name="peer">消息来源主机</param> /// <param name="msg">消息</param> private static void ServerMessage(Peer peer, Msg msg) { switch (msg.type) { case 11: HostInfo info = msg.data as HostInfo; peer.info.id = info.id; ResHost curHost = HostManager.Singleton[info.id.ToString()]; if (curHost == null) return; curHost.rGroup(info.groups); curHost.searchService = info.SearchService; HoldingServer.Singleton.UpdateServices(HostGroup.getServices(HoldingServer.Self)); Group.send(new Msg(13)); break; case 12: peer.send(new Msg(13, HostGroup.getServices(msg.data as string[]))); break; case 22: peer.send(new Msg(21, HoldingServer.Client.remote)); break; } }
/// <summary> /// 处理部分消息的FinalDeal事件 /// </summary> /// <param name="peer">消息要发送到的主机</param> /// <param name="msg">消息</param> public static void onFinalDeal(Peer peer, Msg msg) { switch (msg.type) { case 13: if (peer.info.id == null) { msg.valid = false; return; } msg.valid = true; ResHost rh = HostManager.Singleton[peer.info.id.ToString()]; msg.data = HostGroup.getServices(rh); break; } }
/// <summary> /// 消息路由函数 /// </summary> /// <param name="peer">消息来源主机</param> /// <param name="msg">消息</param> public static void onMessage(Peer peer, Msg msg) { if (HoldingServer.State == 1) ServerMessage(peer, msg); else if (HoldingServer.State == 2) ClientMessage(peer, msg); else OtherMessage(peer, msg); CallBack.doCallBack(peer, msg); }
/// <summary> /// 对该主机发送一条消息 /// </summary> /// <param name="msg">要发送的消息</param> /// <param name="ifAsync">是否异步</param> public void send(Msg msg, bool ifAsync) { if (ifAsync) { Thread t = new Thread(new ParameterizedThreadStart(SendProc)); t.IsBackground = true; t.Start(msg); } else SendProc(msg); }
/// <summary> /// 对该主机使用异步方法发送一条消息 /// </summary> /// <param name="msg">要发送的消息</param> public void send(Msg msg) { Thread t = new Thread(new ParameterizedThreadStart(SendProc)); t.IsBackground = true; t.Start(msg); }