示例#1
0
        /// <summary>
        /// 处理请求接收连接后的方法
        /// </summary>
        /// <param name="obj">Accpt对象</param>
        protected override void ThreadPoolLogin(object obj)
        {
            if (obj is Socket socket)
            {
                // 接收一条信息,指定当前请求的数据订阅信息的关键字
                OperateResult <int, string> receive = ReceiveStringContentFromSocket(socket);
                if (!receive.IsSuccess)
                {
                    return;
                }

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

                SendStringAndCheckReceive(socket, 0, "");

                // 允许发布订阅信息
                AppSession session = new AppSession( );
                session.KeyGroup   = receive.Content2;
                session.WorkSocket = socket;
                try
                {
                    session.IpEndPoint = (System.Net.IPEndPoint)socket.RemoteEndPoint;
                    session.IpAddress  = session.IpEndPoint.Address.ToString( );
                }
                catch (Exception ex)
                {
                    LogNet?.WriteException(ToString( ), "Ip信息获取失败", ex);
                }

                try
                {
                    socket.BeginReceive(session.BytesHead, 0, session.BytesHead.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), session);
                }
                catch (Exception ex)
                {
                    LogNet?.WriteException(ToString( ), "开启信息接收失败", ex);
                    return;
                }

                LogNet?.WriteDebug(ToString( ), $"客户端 [ {session.IpEndPoint} ] 上线");
                PushGroupClient push = GetPushGroupClient(receive.Content2);
                if (push != null)
                {
                    System.Threading.Interlocked.Increment(ref onlineCount);
                    push.AddPushClient(session);
                }
            }
        }
示例#2
0
        private PushGroupClient GetPushGroupClient(string key)
        {
            PushGroupClient result = null;

            dicHybirdLock.Enter( );

            if (dictPushClients.ContainsKey(key))
            {
                result = dictPushClients[key];
            }

            dicHybirdLock.Leave( );

            return(result);
        }
        /// <summary>
        /// 处理请求接收连接后的方法
        /// </summary>
        /// <param name="obj">Accpt对象</param>
        protected override void ThreadPoolLogin(object obj)
        {
            if (obj is Socket socket)
            {
                // 接收一条信息,指定当前请求的数据订阅信息的关键字
                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;
                }

                SendStringAndCheckReceive(socket, 0, "");

                // 允许发布订阅信息
                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( );
                }
            }
        }