NetPushServer.cs 15.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
using HslCommunication.Core;
using HslCommunication.Core.Net;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;



/**********************************************************************************
 * 
 *    发布订阅类的服务器类
 *    
 *    实现从客户端进行数据的订阅操作
 * 
 *********************************************************************************/




namespace HslCommunication.Enthernet
{
    /// <summary>
    /// 发布订阅服务器的类,支持按照关键字进行数据信息的订阅
    /// </summary>
    /// <remarks>
    /// 详细的使用说明,请参照博客<a href="http://www.cnblogs.com/dathlin/p/8992315.html">http://www.cnblogs.com/dathlin/p/8992315.html</a>
    /// </remarks>
    /// <example>
    /// 此处贴上了Demo项目的服务器配置的示例代码
    /// <code lang="cs" source="TestProject\PushNetServer\FormServer.cs" region="NetPushServer" title="NetPushServer示例" />
    /// </example>
    public class NetPushServer : NetworkServerBase
    {
        #region Constructor

        /// <summary>
        /// 实例化一个对象
        /// </summary>
        public NetPushServer()
        {
            dictPushClients = new Dictionary<string, PushGroupClient>();
            dictSendHistory = new Dictionary<string, string>();
            dicHybirdLock = new SimpleHybirdLock();
            dicSendCacheLock = new SimpleHybirdLock();
            sendAction = new Action<AppSession, string>(SendString);

            hybirdLock = new SimpleHybirdLock();
            pushClients = new List<NetPushClient>();
        }


        #endregion

        #region Server Override

        /// <summary>
        /// 当接收到了新的请求的时候执行的操作
        /// </summary>
        /// <param name="socket">异步对象</param>
        /// <param name="endPoint">终结点</param>
        protected override void ThreadPoolLogin(Socket socket, IPEndPoint endPoint)
        {
            // 接收一条信息,指定当前请求的数据订阅信息的关键字
            OperateResult<int, string> receive = ReceiveStringContentFromSocket(socket);
            if (!receive.IsSuccess) return;

            // 判断当前的关键字在服务器是否有消息发布
            //if(!IsPushGroupOnline(receive.Content2))
            //{
            //    SendStringAndCheckReceive( socket, 1, StringResources.Language.KeyIsNotExist );
            //    LogNet?.WriteWarn( ToString( ), StringResources.Language.KeyIsNotExist );
            //    socket?.Close( );
            //    return;
            //}

            // 确认订阅的信息
            OperateResult check = SendStringAndCheckReceive(socket, 0, "");
            if (!check.IsSuccess)
            {
                socket?.Close();
                return;
            }

            // 允许发布订阅信息
            AppSession session = new AppSession
            {
                KeyGroup = receive.Content2,
                WorkSocket = socket
            };

            try
            {
                session.IpEndPoint = (System.Net.IPEndPoint)socket.RemoteEndPoint;
                session.IpAddress = session.IpEndPoint.Address.ToString();
            }
            catch (Exception ex)
            {
                LogNet?.WriteException(ToString(), StringResources.Language.GetClientIpaddressFailed, ex);
            }

            try
            {
                socket.BeginReceive(session.BytesHead, 0, session.BytesHead.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), session);
            }
            catch (Exception ex)
            {
                LogNet?.WriteException(ToString(), StringResources.Language.SocketReceiveException, ex);
                return;
            }

            LogNet?.WriteDebug(ToString(), string.Format(StringResources.Language.ClientOnlineInfo, session.IpEndPoint));
            PushGroupClient push = GetPushGroupClient(receive.Content2);
            if (push != null)
            {
                System.Threading.Interlocked.Increment(ref onlineCount);
                push.AddPushClient(session);

                dicSendCacheLock.Enter();
                if (dictSendHistory.ContainsKey(receive.Content2))
                {
                    if (isPushCacheAfterConnect) SendString(session, dictSendHistory[receive.Content2]);
                }
                dicSendCacheLock.Leave();
            }
        }

        /// <summary>
        /// 关闭服务器的引擎
        /// </summary>
        public override void ServerClose()
        {
            base.ServerClose();
        }

        #endregion

        #region Public Method

        /// <summary>
        /// 主动推送数据内容
        /// </summary>
        /// <param name="key">关键字</param>
        /// <param name="content">数据内容</param>
        public void PushString(string key, string content)
        {
            dicSendCacheLock.Enter();
            if (dictSendHistory.ContainsKey(key))
            {
                dictSendHistory[key] = content;
            }
            else
            {
                dictSendHistory.Add(key, content);
            }
            dicSendCacheLock.Leave();


            AddPushKey(key);
            GetPushGroupClient(key)?.PushString(content, sendAction);
        }

        /// <summary>
        /// 移除关键字信息,通常应用于一些特殊临时用途的关键字
        /// </summary>
        /// <param name="key">关键字</param>
        public void RemoveKey(string key)
        {
            dicHybirdLock.Enter();

            if (dictPushClients.ContainsKey(key))
            {
                int count = dictPushClients[key].RemoveAllClient();
                for (int i = 0; i < count; i++)
                {
                    System.Threading.Interlocked.Decrement(ref onlineCount);
                }
            }

            dicHybirdLock.Leave();
        }


        /// <summary>
        /// 创建一个远程服务器的数据推送操作,以便推送给子客户端
        /// </summary>
        /// <param name="ipAddress">远程的IP地址</param>
        /// <param name="port">远程的端口号</param>
        /// <param name="key">订阅的关键字</param>
        public OperateResult CreatePushRemote(string ipAddress, int port, string key)
        {
            OperateResult result;

            hybirdLock.Enter();


            if (pushClients.Find(m => m.KeyWord == key) == null)
            {
                NetPushClient pushClient = new NetPushClient(ipAddress, port, key);
                result = pushClient.CreatePush(GetPushFromServer);
                pushClients.Add(pushClient);
            }
            else
            {
                result = new OperateResult(StringResources.Language.KeyIsExistAlready);
            }

            hybirdLock.Leave();

            return result;
        }


        #endregion

        #region Public Properties

        /// <summary>
        /// 在线客户端的数量
        /// </summary>
        public int OnlineCount
        {
            get => onlineCount;
        }

        /// <summary>
        /// 在客户端上线之后,是否推送缓存的数据,默认设置为true
        /// </summary>
        public bool PushCacheAfterConnect
        {
            get { return isPushCacheAfterConnect; }
            set { isPushCacheAfterConnect = value; }
        }

        #endregion

        #region Private Method


        private void ReceiveCallback(IAsyncResult ar)
        {
            if (ar.AsyncState is AppSession session)
            {
                try
                {
                    Socket client = session.WorkSocket;
                    int bytesRead = client.EndReceive(ar);

                    // 正常下线退出
                    LogNet?.WriteDebug(ToString(), string.Format(StringResources.Language.ClientOfflineInfo, session.IpEndPoint));
                    RemoveGroupOnlien(session.KeyGroup, session.ClientUniqueID);
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains(StringResources.Language.SocketRemoteCloseException))
                    {
                        // 正常下线
                        LogNet?.WriteDebug(ToString(), string.Format(StringResources.Language.ClientOfflineInfo, session.IpEndPoint));
                        RemoveGroupOnlien(session.KeyGroup, session.ClientUniqueID);
                    }
                    else
                    {
                        LogNet?.WriteException(ToString(), string.Format(StringResources.Language.ClientOfflineInfo, session.IpEndPoint), ex);
                        RemoveGroupOnlien(session.KeyGroup, session.ClientUniqueID);
                    }
                }
            }
        }



        /// <summary>
        /// 判断当前的关键字订阅是否在服务器的词典里面
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private bool IsPushGroupOnline(string key)
        {
            bool result = false;

            dicHybirdLock.Enter();

            if (dictPushClients.ContainsKey(key)) result = true;

            dicHybirdLock.Leave();

            return result;
        }

        private void AddPushKey(string key)
        {
            dicHybirdLock.Enter();

            if (!dictPushClients.ContainsKey(key))
            {
                dictPushClients.Add(key, new PushGroupClient());
            }

            dicHybirdLock.Leave();
        }

        private PushGroupClient GetPushGroupClient(string key)
        {
            PushGroupClient result = null;
            dicHybirdLock.Enter();

            if (dictPushClients.ContainsKey(key))
            {
                result = dictPushClients[key];
            }
            else
            {
                result = new PushGroupClient();
                dictPushClients.Add(key, result);
            }

            dicHybirdLock.Leave();

            return result;
        }

        /// <summary>
        /// 移除客户端的数据信息
        /// </summary>
        /// <param name="key">指定的客户端</param>
        /// <param name="clientID">指定的客户端唯一的id信息</param>
        private void RemoveGroupOnlien(string key, string clientID)
        {
            PushGroupClient push = GetPushGroupClient(key);
            if (push != null)
            {
                if (push.RemovePushClient(clientID))
                {
                    // 移除成功
                    System.Threading.Interlocked.Decrement(ref onlineCount);
                }
            }
        }


        private void SendString(AppSession appSession, string content)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(m =>
            {
                PushSendAsync(appSession, HslProtocol.CommandBytes(0, Token, content));
            }), null);
        }


        #region Send Bytes Async

        /// <summary>
        /// 发送数据的方法
        /// </summary>
        /// <param name="session">通信用的核心对象</param>
        /// <param name="content">完整的字节信息</param>
        internal void PushSendAsync(AppSession session, byte[] content)
        {
            try
            {
                // 进入发送数据的锁,然后开启异步的数据发送
                session.HybirdLockSend.Enter();

                // 启用另外一个网络封装对象进行发送数据
                AsyncStateSend state = new AsyncStateSend()
                {
                    WorkSocket = session.WorkSocket,
                    Content = content,
                    AlreadySendLength = 0,
                    HybirdLockSend = session.HybirdLockSend,
                    Key = session.KeyGroup,
                    ClientId = session.ClientUniqueID,
                };

                state.WorkSocket.BeginSend(
                    state.Content,
                    state.AlreadySendLength,
                    state.Content.Length - state.AlreadySendLength,
                    SocketFlags.None,
                    new AsyncCallback(PushSendCallBack),
                    state);
            }
            catch (ObjectDisposedException)
            {
                // 不操作
                session.HybirdLockSend.Leave();
                RemoveGroupOnlien(session.KeyGroup, session.ClientUniqueID);
            }
            catch (Exception ex)
            {
                session.HybirdLockSend.Leave();
                if (!ex.Message.Contains(StringResources.Language.SocketRemoteCloseException))
                {
                    LogNet?.WriteException(ToString(), StringResources.Language.SocketSendException, ex);
                }
                RemoveGroupOnlien(session.KeyGroup, session.ClientUniqueID);
            }
        }

        /// <summary>
        /// 发送回发方法
        /// </summary>
        /// <param name="ar">异步数据</param>
        internal void PushSendCallBack(IAsyncResult ar)
        {
            if (ar.AsyncState is AsyncStateSend stateone)
            {
                try
                {
                    stateone.AlreadySendLength += stateone.WorkSocket.EndSend(ar);
                    if (stateone.AlreadySendLength < stateone.Content.Length)
                    {
                        // 继续发送
                        stateone.WorkSocket.BeginSend(stateone.Content,
                        stateone.AlreadySendLength,
                        stateone.Content.Length - stateone.AlreadySendLength,
                        SocketFlags.None,
                        new AsyncCallback(PushSendCallBack),
                        stateone);
                    }
                    else
                    {
                        stateone.HybirdLockSend.Leave();
                        // 发送完成
                        stateone = null;
                    }
                }
                catch (ObjectDisposedException)
                {
                    stateone.HybirdLockSend.Leave();
                    RemoveGroupOnlien(stateone.Key, stateone.ClientId);
                }
                catch (Exception ex)
                {
                    LogNet?.WriteException(ToString(), StringResources.Language.SocketEndSendException, ex);
                    stateone.HybirdLockSend.Leave();
                    RemoveGroupOnlien(stateone.Key, stateone.ClientId);
                }
            }
        }

        #endregion


        private void GetPushFromServer(NetPushClient pushClient, string data)
        {
            // 推送给其他的客户端,当然也有可能是工作站
            PushString(pushClient.KeyWord, data);
        }


        #endregion

        #region Private Member

        private Dictionary<string, string> dictSendHistory;                  // 词典缓存的数据发送对象
        private Dictionary<string, PushGroupClient> dictPushClients;         // 系统的数据词典
        private SimpleHybirdLock dicHybirdLock;                              // 词典锁
        private SimpleHybirdLock dicSendCacheLock;                           // 缓存数据的锁
        private Action<AppSession, string> sendAction;                       // 发送数据的委托
        private int onlineCount = 0;                                         // 在线客户端的数量,用于监视显示
        private List<NetPushClient> pushClients;                             // 客户端列表
        private SimpleHybirdLock hybirdLock;                                 // 客户端列表的锁
        private bool isPushCacheAfterConnect = true;                         // 在客户端上线之后,是否推送缓存的数据

        #endregion

        #region Object Override

        /// <summary>
        /// 返回表示当前对象的字符串
        /// </summary>
        /// <returns>字符串</returns>
        public override string ToString()
        {
            return "NetPushServer";
        }


        #endregion
    }

}