Beispiel #1
0
        /// <summary>
        /// Main Listener function that wait for any client that wants to send file or profile images
        /// </summary>
        /// <param name="tokenEndListener">Cancellation token from the MainWindows</param>
        /// <returns>Return a task, it is used to allow the Task asynchronous programming model (TAP)</returns>
        public async Task Listener(CancellationToken tokenEndListener)
        {
            while (!tokenEndListener.IsCancellationRequested)
            {
                source = new CancellationTokenSource();
                CancellationToken tokenListener = source.Token;

                lock (_referenceData.cvListener) {
                    while (_referenceData.GetLocalIPAddress().Equals(""))
                    {
                        Monitor.Wait(_referenceData.cvListener);
                    }
                }
                Console.WriteLine($"{DateTime.Now.ToString()}\t - Change Listener local IP  {_referenceData.GetLocalIPAddress()}");

                IPAddress localAddr = IPAddress.Parse(_referenceData.GetLocalIPAddress());

                try {
                    // Create a TCPLIstenr object
                    server = new TcpListener(localAddr, SharedInfo.TCPPort);
                    server.Start();

                    // Listener loop
                    while (!tokenListener.IsCancellationRequested)
                    {
                        Console.WriteLine($"{DateTime.Now.ToString()}\t - TCPListener Waiting for connection...");
                        await server.AcceptTcpClientAsync().WithWaitCancellation(tokenListener).ContinueWith(async(antecedent) => {
                            try {
                                TcpClient client = antecedent.Result;
                                await ServeClient((TcpClient)antecedent.Result);
                            }
                            catch (Exception e) {
                                Console.WriteLine($"{DateTime.Now.ToString()}\t - TCPListener Exception {e.Message}");
                            }
                        }, TaskContinuationOptions.OnlyOnRanToCompletion);
                    }
                }
                catch (SocketException e) {
                    Console.WriteLine($"{DateTime.Now.ToString()}\t - TCPListener SocketException - {e.Message}");
                }
                catch (OperationCanceledException e) {
                    Console.WriteLine($"{DateTime.Now.ToString()}\t - TCPListener OperationCanceledException - {e.Message}");
                    Console.WriteLine($"{DateTime.Now.ToString()}\t - End execution because of destruction of the cancellation token");
                }
                catch (Exception e) {
                    Console.WriteLine($"{DateTime.Now.ToString()}\t - TCPListener Exception - {e.GetType()} {e.Message}");
                }
                finally {
                    if (server != null)
                    {
                        server.Stop();
                    }
                }
            }
        }