using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections.Concurrent; using System.Net; using HPSocket; using HPSocket.Tcp; using HPSocket.Ssl; using HPSocket.WebSocket; using HXX.Scanner.Common; namespace HXX.Scanner.Socket { /// /// 对socket的统一调度 /// public class SocketServerManager { static SocketServerManager() { dic_client = new ConcurrentDictionary(); } /// /// 外部调用注册 当连接到达 /// private static on_connect_delegate on_connect_method; /// /// 外部调用注册 当信息到达(byte) /// private static on_message_delegate1 on_message_byte; /// /// 外部调用注册 当信息到达(string) /// private static on_message_delegate2 on_message_string; /// /// 外部调用注册 当连接关闭 /// private static on_close_delegate on_close_method; /// /// 所有连接列表 /// private static ConcurrentDictionary dic_client { get; set; } /// /// 启动一个socket服务器 /// /// 监听地址 /// 消息到达(byte)处理方法 /// 消息到达(string)处理方法 /// 连接关闭处理方法 /// 连接到达处理方法 /// public static bool Start(string url, on_message_delegate1 method1, on_message_delegate2 method2, on_close_delegate method3, on_connect_delegate method4) { on_message_byte = method1; on_message_string = method2; on_close_method = method3; on_connect_method = method4; return SocketServer.Start(url); } /// /// 连接到达 /// /// public static void on_connect(IntPtr connId) { if (on_connect_method != null) { on_connect_method(connId); } } /// /// 消息到达 /// /// /// public static void on_message(IntPtr connId, string data) { if (on_message_string != null) { on_message_string(connId, data); } } /// /// 消息到达 /// /// /// public static void on_message(IntPtr connId, byte[] data) { if (on_message_byte != null) { on_message_byte(connId, data); } } /// /// 发送byte给同一个token的所有连接 /// /// /// public static void Send_Family(IntPtr connId, byte[] data) { foreach (var f in get_same_token_client_list(connId)) { try { SocketServer.Send(f.connId, data); } catch (Exception ee) { LogManager.WriteLog(ee); } } } /// /// 发送string给同一个token的所有连接 /// /// /// public static void Send_Family(IntPtr connId, string data) { foreach (var f in get_same_token_client_list(connId)) { try { SocketServer.Send(f.connId, data); } catch (Exception ee) { LogManager.WriteLog(ee); } } } /// /// 发送给单个连接 /// /// /// public static void Send_Single(IntPtr connId, string data) { try { SocketServer.Send(connId, data); } catch (Exception ee) { LogManager.WriteLog(ee); } } /// /// 发送给所有连接 /// /// //public static void Send_All(string data) //{ // try // { // if (SocketServer.server != null) // { // foreach (var connId in SocketServer.server.GetAllConnectionIds()) // { // SocketServer.Send(connId, data); // } // } // } // catch (Exception ee) // { // LogManager.WriteLog(ee); // } //} /// /// 获取某连接的token所属的所有连接 /// /// /// private static List get_same_token_client_list(IntPtr connId) { List result = new List(); var t = dic_client.FirstOrDefault(x => x.Key == connId); if (t.Key != null && t.Value != null) { var family = dic_client.Where(x => x.Value.token == t.Value.token).Select(x => x.Value); var current = SocketServer.server.GetAllConnectionIds(); foreach (var f in family) { if (current.Any(x => x == f.connId)) { result.Add(f); } else { dic_client.TryRemove(f.connId, out ws_client temp); } } if (t.Value != null && string.IsNullOrEmpty(t.Value.token)) { result = result.Where(x => x.connId == connId).ToList(); } } else { dic_client.TryRemove(connId, out ws_client temp); } return result; } /// /// 添加一个连接到连接列表 /// /// /// public static void add_client(IntPtr _connId, string _token) { dic_client[_connId] = new ws_client() { connId = _connId, token = _token }; } /// /// 连接关闭 /// /// /// public static void on_close(IntPtr connId, int connection_count) { if (on_close_method != null) { on_close_method(connId, connection_count); } } } }