示例#1
0
        //Disable the socket server
        public async Task SocketServerDisable()
        {
            try
            {
                Debug.WriteLine("Disabling the socket server (S)");

                //Stop the socket servers
                TcpServerStop(vTcpServer);
                UdpServerStop(vUdpServer);

                //Disconnect all the clients
                TcpClientDisconnectAll();
                UdpClientDisconnectAll();

                //Stop the server loops
                await AVActions.TaskStopLoop(vTask_TcpReceiveLoop);

                await AVActions.TaskStopLoop(vTask_UdpReceiveLoop);

                //Stop the clean loops
                await AVActions.TaskStopLoop(vTask_TcpCleanLoop);

                await AVActions.TaskStopLoop(vTask_UdpCleanLoop);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to disable the socket server (S): " + ex.Message);
            }
        }
示例#2
0
        //Start the tcp server
        private async Task TcpServerStart()
        {
            try
            {
                //Set the server endpoint
                IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, vSocketServerPort);

                //Start tcp server
                vTcpServer = new TcpListener(serverEndPoint);
                vTcpServer.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                vTcpServer.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                vTcpServer.Server.LingerState = new LingerOption(true, 0);
                vTcpServer.Start();

                Debug.WriteLine("Tcp server is running on (S): " + serverEndPoint.Address + ":" + serverEndPoint.Port);

                //Start receive loop
                AVActions.TaskStartLoop(TcpReceiveLoop, vTask_TcpReceiveLoop);

                //Start clean loop
                AVActions.TaskStartLoop(TcpCleanLoop, vTask_TcpCleanLoop);
            }
            catch (Exception ex)
            {
                await SocketServerException(ex);
            }
        }
示例#3
0
        //Tcp receive loop
        async Task TcpReceiveLoop()
        {
            try
            {
                while (!vTask_TcpReceiveLoop.TaskStopRequest)
                {
                    try
                    {
                        TcpClient tcpClient = await vTcpServer.AcceptTcpClientAsync();

                        if (tcpClient != null && tcpClient.Connected)
                        {
                            Debug.WriteLine("New tcp client connected from (S): " + tcpClient.Client.RemoteEndPoint);
                            async void TaskAction()
                            {
                                try
                                {
                                    await TcpClientHandler(tcpClient);
                                }
                                catch { }
                            }
                            await AVActions.TaskStart(TaskAction);
                        }
                    }
                    catch { }
                }
            }
            catch { }
        }
        //Launch an uwp application manually
        public static async Task <Process> ProcessLauncherUwpAndWin32StoreAsync(string appUserModelId, string runArgument)
        {
            try
            {
                //Prepare the process launch
                Process TaskAction()
                {
                    try
                    {
                        //Show launching message
                        Debug.WriteLine("Launching UWP or Win32Store: " + appUserModelId + " / " + runArgument);

                        //Get detailed application information
                        Package     appPackage  = UwpGetAppPackageByAppUserModelId(appUserModelId);
                        AppxDetails appxDetails = UwpGetAppxDetailsFromAppPackage(appPackage);
                        appUserModelId = appxDetails.FamilyNameId;

                        //Start the process
                        UWPActivationManager UWPActivationManager = new UWPActivationManager();
                        UWPActivationManager.ActivateApplication(appUserModelId, runArgument, UWPActivationManagerOptions.None, out int processId);

                        //Return process
                        Process returnProcess = GetProcessById(processId);
                        Debug.WriteLine("Launched UWP or Win32Store process identifier: " + returnProcess.Id);
                        return(returnProcess);
                    }
                    catch { }
                    Debug.WriteLine("Failed launching UWP or Win32Store: " + appUserModelId + " / " + runArgument);
                    return(null);
                };

                //Launch the process
                return(await AVActions.TaskStartReturn(TaskAction));
            }
            catch { }
            Debug.WriteLine("Failed launching UWP or Win32Store: " + appUserModelId + " / " + runArgument);
            return(null);
        }
示例#5
0
        //Start the udp server
        private async Task UdpServerStart()
        {
            try
            {
                //Set the server endpoint
                IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, vSocketServerPort);

                //Start udp server
                vUdpServer = new UdpClient(serverEndPoint);

                Debug.WriteLine("Udp server is running on (S): " + serverEndPoint.Address + ":" + serverEndPoint.Port);

                //Start receive loop
                AVActions.TaskStartLoop(UdpReceiveLoop, vTask_UdpReceiveLoop);

                //Start clean loop
                AVActions.TaskStartLoop(UdpCleanLoop, vTask_UdpCleanLoop);
            }
            catch (Exception ex)
            {
                await SocketServerException(ex);
            }
        }
示例#6
0
        //Focus on a process window
        public static async Task <bool> FocusProcessWindow(string processTitle, int processId, IntPtr processWindowHandle, WindowShowCommand windowShowCommand, bool setWindowState, bool setTempTopMost)
        {
            try
            {
                //Prepare the process focus
                async Task <bool> TaskAction()
                {
                    try
                    {
                        //Close open Windows prompts
                        await CloseOpenWindowsPrompts();

                        //Get the current focused application
                        ProcessMulti foregroundProcess = GetProcessMultiFromWindowHandle(GetForegroundWindow());

                        //Close open start menu, cortana or search
                        await CloseOpenWindowsStartMenu(foregroundProcess);

                        //Close open Windows system menu
                        await CloseOpenWindowsSystemMenu(foregroundProcess);

                        //Detect the previous window state
                        if (windowShowCommand == WindowShowCommand.None && setWindowState)
                        {
                            WindowPlacement processWindowState = new WindowPlacement();
                            GetWindowPlacement(processWindowHandle, ref processWindowState);
                            Debug.WriteLine("Detected the previous window state: " + processWindowState.windowFlags);
                            if (processWindowState.windowFlags == WindowFlags.RestoreToMaximized)
                            {
                                windowShowCommand = WindowShowCommand.ShowMaximized;
                            }
                            else
                            {
                                windowShowCommand = WindowShowCommand.Restore;
                            }
                        }

                        //Change the window state command
                        if (setWindowState)
                        {
                            ShowWindowAsync(processWindowHandle, windowShowCommand);
                            await Task.Delay(10);

                            ShowWindow(processWindowHandle, windowShowCommand);
                            await Task.Delay(10);
                        }

                        //Set the window as top most
                        if (setTempTopMost)
                        {
                            SetWindowPos(processWindowHandle, (IntPtr)WindowPosition.TopMost, 0, 0, 0, 0, (int)WindowSWP.NOMOVE | (int)WindowSWP.NOSIZE);
                            await Task.Delay(10);
                        }

                        //Retry to show the window
                        for (int i = 0; i < 2; i++)
                        {
                            try
                            {
                                //Allow changing window
                                AllowSetForegroundWindow(processId);
                                await Task.Delay(10);

                                //Bring window to top
                                BringWindowToTop(processWindowHandle);
                                await Task.Delay(10);

                                //Switch to the window
                                SwitchToThisWindow(processWindowHandle, true);
                                await Task.Delay(10);

                                //Focus on the window
                                UiaFocusWindowHandle(processWindowHandle);
                                await Task.Delay(10);
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine("Process focus error: " + ex.Message);
                            }
                        }

                        //Disable the window as top most
                        if (setTempTopMost)
                        {
                            SetWindowPos(processWindowHandle, (IntPtr)WindowPosition.NoTopMost, 0, 0, 0, 0, (int)WindowSWP.NOMOVE | (int)WindowSWP.NOSIZE);
                            await Task.Delay(10);
                        }

                        //Return bool
                        Debug.WriteLine("Focused process window: " + processTitle + " WindowHandle: " + processWindowHandle + " ShowCmd: " + windowShowCommand);
                        return(true);
                    }
                    catch { }
                    Debug.WriteLine("Failed focusing process: " + processTitle);
                    return(false);
                };

                //Focus the process
                return(await AVActions.TaskStartReturn(TaskAction).Result);
            }
            catch { }
            Debug.WriteLine("Failed focusing process: " + processTitle);
            return(false);
        }
        //Launch a win32 application manually async
        public static async Task <Process> ProcessLauncherWin32Async(string pathExe, string pathLaunch, string runArgument, bool runAsAdmin, bool createNoWindow)
        {
            try
            {
                //Prepare the process launch
                Process TaskAction()
                {
                    try
                    {
                        //Check if the exe file exists
                        if (!File.Exists(pathExe))
                        {
                            Debug.WriteLine("Launch executable not found.");
                            return(null);
                        }

                        //Show launching message
                        Debug.WriteLine("Launching Win32: " + Path.GetFileNameWithoutExtension(pathExe));

                        //Check the working path
                        if (string.IsNullOrWhiteSpace(pathLaunch))
                        {
                            pathLaunch = Path.GetDirectoryName(pathExe);
                        }

                        //Create process to start
                        Process launchProcess = new Process();
                        launchProcess.StartInfo.FileName = pathExe;
                        if (!string.IsNullOrWhiteSpace(pathLaunch))
                        {
                            launchProcess.StartInfo.WorkingDirectory = pathLaunch;
                        }
                        if (!string.IsNullOrWhiteSpace(runArgument))
                        {
                            launchProcess.StartInfo.Arguments = runArgument;
                        }
                        if (createNoWindow)
                        {
                            launchProcess.StartInfo.UseShellExecute = false;
                            launchProcess.StartInfo.CreateNoWindow  = true;
                        }
                        if (runAsAdmin)
                        {
                            launchProcess.StartInfo.Verb = "runas";
                        }

                        //Start the process
                        launchProcess.Start();

                        //Return process
                        Debug.WriteLine("Launched Win32 process identifier: " + launchProcess.Id);
                        return(launchProcess);
                    }
                    catch { }
                    Debug.WriteLine("Failed launching Win32: " + Path.GetFileNameWithoutExtension(pathExe));
                    return(null);
                };

                //Launch the process
                return(await AVActions.TaskStartReturn(TaskAction));
            }
            catch { }
            Debug.WriteLine("Failed launching Win32: " + Path.GetFileNameWithoutExtension(pathExe));
            return(null);
        }