public object Close(Message msg) { if (m_socket == null) return null; m_socket.AcceptedSocket -= m_socket_AcceptedSocket; m_socket.Disconnected -= m_socket_Disconnected; m_socket.Disconnect(); return null; }
/// <summary> /// 创建消息 /// </summary> /// <param name="msg">消息标识</param> /// <param name="sender">发送者</param> /// <param name="receivers">接收者列表</param> /// <param name="wParam">消息附加参数</param> /// <param name="lParam">消息附加参数</param> /// <returns></returns> public static Message CreateMsg(uint msg, string sender, string[] receivers = null, object wParam = null, object lParam = null) { Message m = new Message { Sender = sender, Receivers = receivers, Msg = msg, WParam = wParam, LParam = lParam, tick = Environment.TickCount }; return m; }
public object Listen(Message msg) { if (m_socket == null) { throw new Exception("请先通过NetMsg.SetSocketType消息创建Socket"); } m_port = (int)msg.WParam; m_socket.AcceptedSocket += new AcceptSocketDelegate(m_socket_AcceptedSocket); m_socket.Disconnected += new DisconnectSocketDelegate(m_socket_Disconnected); m_socket.ListenLocal(m_port); return null; }
public object SetSocketType(Message msg) { Type type = msg.WParam as Type; if (type == null) { throw new ArgumentException("msg.WParam 参数必需为类的Type对象"); } m_socket = type.Assembly.CreateInstance(type.FullName) as JSocket; if (m_socket == null) { throw new ArgumentException("msg.WParam 参数必需为继续JSocket的类的Type对象"); } return null; }
public object Connect(Message msg) { if (m_socket == null) { throw new Exception("请先通过 NetMsg.SetSocketType 消息创建Socket"); } string ip = msg.WParam as string; int port = (int)msg.LParam; m_socket.ConnectedSuccess += new ConnectSocketDelegate(m_socket_ConnectedSuccess); m_socket.ConnectedFaild += new ConnectSocketDelegate(m_socket_ConnectedFaild); m_socket.Disconnected += new DisconnectSocketDelegate(m_socket_Disconnected); m_socket.Received += new ReceivePacketDelegate(m_socket_Received); m_socket.Connect(ip, port); return null; }
//[MsgHandler(MsgType.Dispose, false, "释放消息")] protected virtual object Dispose(Message msg) { MsgMonitor.RemoveReceiver(ID); return null; }
public object ReceiveSync(Message msg) { if (msg != null) { if(MsgHandlers.ContainsKey(msg.Msg)) return MsgHandlers[msg.Msg](msg); if (MsgMethodInfos.ContainsKey(msg.Msg)) return MsgMethodInfos[msg.Msg].Invoke(new object[] { msg }); } return null; }
public void ReceiveAsync(Message msg) { if (msg == null) return; lock (MsgList.SyncRoot) { MsgList.Enqueue(msg); if (Handling) { return; } Handling = true; } ThreadPool.QueueUserWorkItem(new WaitCallback(MsgHandle), this); }
/// <summary> /// 异步发送消息 /// </summary> /// <param name="msg"></param> public static void PostMsg(Message msg) { if (msgReceiverList.ContainsKey(msg.Msg)) { foreach (IMsgReceiver receiver in msgReceiverList[msg.Msg]) { receiver.ReceiveAsync(msg); } } if (msg.Receivers != null && msg.Receivers.Length > 0) { foreach (string id in msg.Receivers) { if (receiverList.ContainsKey(id)) { receiverList[id].ReceiveAsync(msg); } } } }
/// <summary> /// 同步发送消息 /// </summary> /// <param name="msg"></param> /// <returns>所有接收者对应的返回值列表</returns> public static Dictionary<string, object> SendMsg(Message msg) { Dictionary<string, object> rlt = new Dictionary<string, object>(); if (msgReceiverList.ContainsKey(msg.Msg)) { foreach (IMsgReceiver receiver in msgReceiverList[msg.Msg]) { object obj = receiver.ReceiveSync(msg); if (obj != null) rlt.Add(receiver.ID, obj); } } if (msg.Receivers != null && msg.Receivers.Length > 0) { foreach (string id in msg.Receivers) { if (receiverList.ContainsKey(id)) { object obj = receiverList[id].ReceiveSync(msg); if (obj != null) rlt.Add(id, obj); } } } return rlt; }