コード例 #1
0
        protected override void OnStart(string[] args)
        {
            var serverConfig = new TcpSocketSaeaServerConfiguration();

            serverConfig.AppKeepAlive             = true;
            serverConfig.PendingConnectionBacklog = 0;
            var trunkService = TcpSocketsFactory.CreateServerAgent(TcpSocketSaeaSessionType.Packet, serverConfig,
                                                                   (notity, session) =>
            {
                switch (notity)
                {
                case TcpSessionNotify.OnConnected:
                    LogHelper.DebugWriteLog("OnConnected");
                    break;

                case TcpSessionNotify.OnSend:
                    break;

                case TcpSessionNotify.OnDataReceiveing:
                    break;

                case TcpSessionNotify.OnDataReceived:
                    _handlerBinder.InvokePacketHandler(session, session.CompletedBuffer.GetMessageHead <TrunkMessageHead>(), this);
                    break;

                case TcpSessionNotify.OnClosed:
                    this.SessionClosedHandler(session);
                    break;

                default:
                    break;
                }
            });

            bool completed = false;

            for (int trycount = 0; trycount < 100; trycount++)
            {
                try
                {
                    _port = 10000 + trycount;
                    var ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), _port);
                    trunkService.Listen(ipEndPoint);
                    completed = true;
                    break;
                }
                catch (Exception ex)
                {
                    LogHelper.WriteErrorByCurrentMethod("trunkService open listen exception:" + ex.Message);
                }
                completed = false;
                Thread.Sleep(1000);
            }
            if (!completed)
            {
                LogHelper.WriteErrorByCurrentMethod("listen all tcp port not completed,please check!");
                Environment.Exit(0);//监听所有端口失败
            }


            // Obtain session ID for active session.
            uint dwSessionId = Kernel32.WTSGetActiveConsoleSessionId();

            // Check for RDP session.  If active, use that session ID instead.
            var rdpSessionID = Win32Interop.GetRDPSession();

            if (rdpSessionID > 0)
            {
                dwSessionId = rdpSessionID;
            }
            this.CreateProcessAsUser(dwSessionId, true);
            ThreadHelper.CreateThread(() =>
            {
                while (_isRun)
                {
                    lock (_lock)
                        for (int i = 0; i < _userProcessSessionIdList.Count; i++)
                        {
                            var token = _userProcessSessionIdList[i];
                            if (token.Actived)
                            {
                                continue;
                            }
                            LogHelper.DebugWriteLog("!Actived:" + token.SessionId);
                            if ((int)(DateTime.Now - token.LastActiveTime).TotalSeconds > 5)      //如果用户进程5秒内未重新激活
                            {
                                bool completed = this.CreateProcessAsUser((uint)token.SessionId); //可能用户进程已结束,重新启动用户进程
                                LogHelper.DebugWriteLog("Restart ProcessAsUser:"******"Restart ProcessAsUser activeSessionId:" + activeSessionId + " status:" + isOk);
                                    }
                                    continue;
                                }
                                token.LastActiveTime = DateTime.Now;//延迟最后时间,给用户进程足够时间激活
                            }
                        }
                    Thread.Sleep(1000);
                }
            }, true);
        }