Пример #1
0
        /// <summary>
        /// 客户端连接时,会发送端口信息,此时服务端根据此信息
        /// 开始侦听新的端口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConnectionManager_AppAdded(object sender, AppChangedEventArgs e)
        {
            Server.Logger.Debug("AppTcpClientMapConfigConnected");
            int      port = 0;
            Protocol protocol;

            //string host = "";
            //TODO 如果有host 则分配到相同的group中
            foreach (var kv in ServerContext.PortAppMap)
            {
                if (kv.Value.ActivateApp.AppId == e.App.AppId &&
                    kv.Value.ActivateApp.ClientId == e.App.ClientId)
                {
                    port     = kv.Value.ActivateApp.ConsumePort;
                    protocol = kv.Value.ActivateApp.AppProtocol;
                    break;
                }
            }
            if (port == 0)
            {
                throw new Exception("app未注册");
            }
            //var ct = new CancellationToken();

            _ = ListenConsumeAsync(port);
        }
Пример #2
0
        /// <summary>
        /// 客户端连接时,会发送端口信息,此时服务端根据此信息
        /// 开始侦听新的端口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConnectionManager_AppAdded(object sender, AppChangedEventArgs e)
        {
            Server.Logger.Debug("AppTcpClientMapConfigConnected");
            int port = 0;
            Dictionary <int, NSPAppGroup> appMap = null;

            if (e.App.AppProtocol == Protocol.UDP)
            {
                appMap = ServerContext.UDPPortAppMap;
            }
            else
            {
                appMap = ServerContext.PortAppMap;
            }
            //多个app共用一个端口时,只需要对这个端口开启一次侦听循环
            foreach (var kv in appMap)
            {
                if (kv.Value.ActivateApp.AppId == e.App.AppId &&
                    kv.Value.ActivateApp.ClientId == e.App.ClientId)
                {
                    port = kv.Value.ActivateApp.ConsumePort;
                    break;
                }
            }
            if (port == 0)
            {
                throw new Exception("app未注册");
            }

            _ = ListenConsumeAsync(port, appMap);
        }
Пример #3
0
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="incomeClient"></param>
        /// <returns></returns>
        private async Task ProcessInComeRequest(TcpClient incomeClient)
        {
            try
            {
                //立即侦听一次并且分配连接
                byte[] bytes = new byte[4];
                await incomeClient.GetStream().ReadAsync(bytes);

                var clientIdAppId = GetAppFromBytes(bytes);
                Server.Logger.Debug("已获取到消息ClientID:" + clientIdAppId.ClientID.ToString()
                                    + "AppID:" + clientIdAppId.AppID.ToString()
                                    );
                //根据不同的服务端appid安排不同的连接池
                lock (_lockObject)
                {
                    AppTcpClientMap.GetOrAdd(clientIdAppId, new List <TcpClient>()).Add(incomeClient);
                }
                var arg = new AppChangedEventArgs();
                arg.App = clientIdAppId;
                AppAdded(this, arg);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Пример #4
0
        /// <summary>
        /// 有连接连上则开始侦听新的端口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConnectionManager_AppAdded(object sender, AppChangedEventArgs e)
        {
            Server.Logger.Debug("added事件已触发");
            int port = 0;

            foreach (var kv in ConnectionManager.PortAppMap)
            {
                if (kv.Value.AppID == e.App.AppID &&
                    kv.Value.ClientID == e.App.ClientID)
                {
                    port = kv.Key;
                }
            }
            if (port == 0)
            {
                throw new Exception("app未注册");
            }
            var  ct  = new CancellationToken();
            Task tsk = AcceptConsumeAsync(port, ct);
        }
Пример #5
0
        /// <summary>
        /// 有连接连上则开始侦听新的端口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConnectionManager_AppAdded(object sender, AppChangedEventArgs e)
        {
            Server.Logger.Debug("AppTcpClientMapReverseConnected事件已触发");
            int port = 0;

            foreach (var kv in ServerContext.PortAppMap)
            {
                if (kv.Value.AppId == e.App.AppId &&
                    kv.Value.ClientId == e.App.ClientId)
                {
                    port = kv.Key;
                }
            }
            if (port == 0)
            {
                throw new Exception("app未注册");
            }
            var ct = new CancellationToken();

            ListenConsumeAsync(port);
        }