using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace QuickMsg { /// /// 消息服务端类 /// public class MsgMain { readonly CustomMsg MSG; /// /// 判断当前消息主服务器是否创建成功 /// public bool IsOk { get;private set; } /// /// 当收到消息时激发 /// public event CustomMsg.MsgEventHandler OnMsg; /// /// 当收到自定义消息时(非系统消息)激发 /// public event CustomMsg.MsgEventHandler OnCustomMsg; /// /// 有消息客户端发起连接请求时激发 /// public event CustomMsg.MsgEventHandler OnConnect; /// /// 有消息客户端连接成功时激发 /// public event CustomMsg.MsgEventHandler OnConnected; /// /// 有消息客户端断开连接时激发 /// public event CustomMsg.MsgEventHandler OnDisConnect; private readonly Form form=null; /// /// /// /// 服务端ID,需要唯一性,共客户端识别 /// 服务端 public MsgMain(string id,Form _form) { object mainHandle = RyMemoryShare.ReadFromMemory(1024, typeof(Int64), id); if (mainHandle == null) { RyMemoryShare.WriteToMemory(1024, _form.Handle.ToInt64(), id); form = _form; MSG = new CustomMsg(_form);//创建消息通道 MSG.OnMsg += MSG_OnMsg; IsOk = true; } else { IsOk = false; } } /// /// 释放资源 /// public void Free() { if(form!=null) { // RyMemoryShare.WriteToMemory(1024, form.Handle.ToInt64(), id); } } /// /// 判断是否包含指定客户端 /// /// /// public bool HaveClient(IntPtr handle) { bool isHave = false; for (int i = 0; i < Client_list.Count; i++) { if (Client_list[i].Handle == handle) { isHave = true; break; } } return isHave; } /// /// /// public List Client_list { get;private set; } = new List(); /// /// 设置客户端大小 /// public void Resize(int width,int height) { ryCommon.Storage Stor = new ryCommon.Storage(); Stor.SelectNodeBySet(); Stor.SetAttrValue("op", "resize"); Stor.SetAttrValue("width", width); Stor.SetAttrValue("height", height); SendAll(MyDb.RyWin32.Sys_chanel_id, "xml"+Stor.XMLText); } /// /// 根据客户端ID,获取客户端对象 /// /// /// public ClientItem GetClientItem(string ClientId) { for (int i = Client_list.Count - 1; i >= 0; i--) { if (Client_list[i].ClientId == ClientId) { return Client_list[i]; } } return null; } /// /// 根据客户端句柄,获取客户端对象 /// /// /// public ClientItem GetClientItem(IntPtr Handle) { for (int i = Client_list.Count - 1; i >= 0; i--) { if (Client_list[i].Handle == Handle) { return Client_list[i]; } } return null; } /// /// 关闭所有客户端 /// public void CloseClient() { SendAll(MyDb.RyWin32.Sys_chanel_id, "close"); Client_list.Clear(); } /// /// 关闭指定客户端 /// /// public void CloseClient(IntPtr handle) { MSG.Send(handle, MyDb.RyWin32.Sys_chanel_id, "close"); } /// /// 关闭指定客户端 /// /// public void CloseClient(ClientItem item) { MSG.Send(item.Handle, MyDb.RyWin32.Sys_chanel_id, "close"); for (int i = Client_list.Count-1; i >=0; i--) { if(Client_list[i].Handle== item.Handle) { Client_list.RemoveAt(i); } } } /// /// 向所有客户端发送消息,单线程 /// /// /// public void SendAll(int wParam, string msg) { for (int i = 0; i < Client_list.Count; i++) { MSG.Send2(Client_list[i].Handle, wParam, msg);//发送消息 } } /// /// 向指定客户端发送消息 /// /// /// /// public void Send(IntPtr handle, int wParam, string msg) { MSG.Send(handle, wParam, msg);//发送消息 } /// /// 关闭在指定时间前失去响应的子客户端 /// /// public void CloseLoseClient(DateTime dt) { for (int i = Client_list.Count - 1; i >= 0; i--) { if (Client_list[i].HeartTime<= dt) { Client_list.RemoveAt(i); } } } private void MSG_OnMsg(object sender, MsgArgs e) { OnMsg?.Invoke(this,e); if(e.WParam== MyDb.RyWin32.Sys_chanel_id) //如果是系统通道请求 { switch(e.Msg_text) { case "disconnect"://断开连接 #region 断开连接 for (int i = Client_list.Count - 1; i >= 0; i--) { if (Client_list[i].Handle == e.From_handle) { Client_list.RemoveAt(i); break; } } OnDisConnect?.Invoke(this, e); break; #endregion case "heart"://心跳包 #region 更新心跳时间 MSG.Send(e.From_handle, MyDb.RyWin32.Sys_chanel_id,"heart");//回应给客户端 for (int i = Client_list.Count - 1; i >= 0; i--) { if (Client_list[i].Handle == e.From_handle) { Client_list[i].HeartTime=DateTime.Now; break; } } break; #endregion default: #region 如果是xml消息 if (e.Msg_text.StartsWith("xml")) { ryCommon.Storage Stor = new ryCommon.Storage(e.Msg_text.Substring(3)); Stor.SelectNodeBySet(); switch (Stor.GetAttrValue("op")) { case "connect"://连接请求 #region 连接请求 OnConnect?.Invoke(this, e); if (!e.Cancel)//如果允许连接 { bool isHave = false; for (int i = 0; i < Client_list.Count; i++) { if (Client_list[i].Handle == e.From_handle) { Client_list[i].ConnectTime = DateTime.Now; isHave = true; break; } } if (!isHave) { Client_list.Add(new ClientItem() { Handle = e.From_handle, ClientId= Stor.GetAttrValue("clientid") });//加入到客户端列表里 } MSG.Send(e.From_handle, e.WParam, "connected");//发送连接成功的通知 OnConnected?.Invoke(this, e); } break; #endregion } } break; #endregion } } else { OnCustomMsg?.Invoke(this, e); } } } /// /// 客户端信息 /// public class ClientItem { /// /// 客户端句柄 /// public IntPtr Handle { get; set; } = IntPtr.Zero; /// /// 客户端ID /// public string ClientId { get; set; } =""; /// /// 客户端连接时间 /// public DateTime ConnectTime { get; set; } = DateTime.Now; /// /// 心跳时间 /// public DateTime HeartTime { get; set; } = DateTime.Now; } }