Example #1
0
 /// <summary>
 ///
 /// </summary>
 public void CloseClient()
 {
     if (CommClient != null)
     {
         CommClient.Close();
     }
 }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        private void SetUpReceivingTask()
        {
            TcpReceivingTask = new Task(() =>
            {
                var bytes = new byte[4];

                try
                {
                    while (true)
                    {
                        var count = CommClient.Client.Receive(bytes, 4, SocketFlags.None);

                        if (count == 0)
                        {
                            throw new Exception("Remote Endpoint could be closed");
                        }

                        buffer = buffer.Concat(bytes.Take(count)).ToArray();
                        HandleBuffer();
                    }
                }
                catch (Exception ex)
                {
                    throw new CommunicationTaskException(ex);
                }
                finally
                {
                    CommClient.Close();
                    CommClient = null;
                }
            });
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        public async Task StartServer(CancellationToken token)
        {
            bool accepted       = false;
            bool listenerReturn = false;

            try
            {
                var commEndPoint = new IPEndPoint(IPAddress.Any, 10080);
                Listener        = new TcpListener(commEndPoint);
                BroadcastSender = new UdpClient();

                Listener.Start();

                var taskAccept = Task.Run(() =>
                {
                    try
                    {
                        CommClient = Listener.AcceptTcpClient();
                        accepted   = true;
                    }
                    finally
                    {
                        listenerReturn = true;
                    }
                });

                var taskBroadcast = Task.Run(async() =>
                {
                    while (true)
                    {
                        if (listenerReturn)
                        {
                            break;
                        }

                        try
                        {
                            var remoteEndPoint = new IPEndPoint(IPAddress.Broadcast, 10080);
                            BroadcastSender.Send(new byte[] { 0xcc, 0xdd }, 2, remoteEndPoint);
                        }
                        catch { }

                        await Task.Delay(2000);
                    }
                });

                var cancellationCheck = Task.Run(async() =>
                {
                    try
                    {
                        while (true)
                        {
                            await Task.Delay(500);

                            if (token.IsCancellationRequested)
                            {
                                if (!accepted)
                                {
                                    Listener.Stop();
                                }

                                break;
                            }

                            if (listenerReturn)
                            {
                                break;
                            }
                        }
                    }
                    catch { }
                });

                await taskAccept;
            }
            catch
            {
                throw;
            }
            finally
            {
                Listener.Stop();
                BroadcastSender.Close();

                if ((!accepted) && (CommClient != null))
                {
                    CommClient.Close();
                    CommClient = null;
                }

                if (accepted)
                {
                    SetUpReceivingTask();
                    CommunicationEstablished?.Invoke(true);
                }
            }
        }
Example #4
0
 /// <summary>
 ///
 /// </summary>
 public void CloseClient()
 {
     CommClient?.Close();
 }