| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- 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
- {
- /// <summary>
- /// 对socket的统一调度
- /// </summary>
- public class SocketServerManager
- {
- static SocketServerManager()
- {
- dic_client = new ConcurrentDictionary<IntPtr, ws_client>();
- }
- /// <summary>
- /// 外部调用注册 当连接到达
- /// </summary>
- private static on_connect_delegate on_connect_method;
- /// <summary>
- /// 外部调用注册 当信息到达(byte)
- /// </summary>
- private static on_message_delegate1 on_message_byte;
- /// <summary>
- /// 外部调用注册 当信息到达(string)
- /// </summary>
- private static on_message_delegate2 on_message_string;
- /// <summary>
- /// 外部调用注册 当连接关闭
- /// </summary>
- private static on_close_delegate on_close_method;
- /// <summary>
- /// 所有连接列表
- /// </summary>
- private static ConcurrentDictionary<IntPtr, ws_client> dic_client { get; set; }
- /// <summary>
- /// 启动一个socket服务器
- /// </summary>
- /// <param name="url">监听地址</param>
- /// <param name="method1">消息到达(byte)处理方法</param>
- /// <param name="method2">消息到达(string)处理方法</param>
- /// <param name="method3">连接关闭处理方法</param>
- /// <param name="method4">连接到达处理方法</param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 连接到达
- /// </summary>
- /// <param name="connId"></param>
- public static void on_connect(IntPtr connId)
- {
- if (on_connect_method != null)
- {
- on_connect_method(connId);
- }
- }
- /// <summary>
- /// 消息到达
- /// </summary>
- /// <param name="connId"></param>
- /// <param name="data"></param>
- public static void on_message(IntPtr connId, string data)
- {
- if (on_message_string != null)
- {
- on_message_string(connId, data);
- }
- }
- /// <summary>
- /// 消息到达
- /// </summary>
- /// <param name="connId"></param>
- /// <param name="data"></param>
- public static void on_message(IntPtr connId, byte[] data)
- {
- if (on_message_byte != null)
- {
- on_message_byte(connId, data);
- }
- }
- /// <summary>
- /// 发送byte给同一个token的所有连接
- /// </summary>
- /// <param name="connId"></param>
- /// <param name="data"></param>
- 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);
- }
- }
- }
- /// <summary>
- /// 发送string给同一个token的所有连接
- /// </summary>
- /// <param name="connId"></param>
- /// <param name="data"></param>
- 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);
- }
- }
- }
- /// <summary>
- /// 发送给单个连接
- /// </summary>
- /// <param name="connId"></param>
- /// <param name="data"></param>
- public static void Send_Single(IntPtr connId, string data)
- {
- try
- {
- SocketServer.Send(connId, data);
- }
- catch (Exception ee)
- {
- LogManager.WriteLog(ee);
- }
- }
- /// <summary>
- /// 发送给所有连接
- /// </summary>
- /// <param name="data"></param>
- //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);
- // }
- //}
- /// <summary>
- /// 获取某连接的token所属的所有连接
- /// </summary>
- /// <param name="connId"></param>
- /// <returns></returns>
- private static List<ws_client> get_same_token_client_list(IntPtr connId)
- {
- List<ws_client> result = new List<ws_client>();
- 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;
- }
- /// <summary>
- /// 添加一个连接到连接列表
- /// </summary>
- /// <param name="_connId"></param>
- /// <param name="_token"></param>
- public static void add_client(IntPtr _connId, string _token)
- {
- dic_client[_connId] = new ws_client() { connId = _connId, token = _token };
- }
- /// <summary>
- /// 连接关闭
- /// </summary>
- /// <param name="connId"></param>
- /// <param name="connection_count"></param>
- public static void on_close(IntPtr connId, int connection_count)
- {
- if (on_close_method != null)
- {
- on_close_method(connId, connection_count);
- }
- }
- }
- }
|