Exemplo n.º 1
0
        public BroadcastTask()
        {
            task = new Thread(() =>
            {
                UdpClient client          = new UdpClient();
                IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Broadcast, 45000);

                while (true)
                {
                    Thread.Sleep(3 * 1000);

                    string state;
                    if (SelectWindow.GetAppState().isBusy)
                    {
                        state = Definitions.BUSY;
                    }
                    else
                    {
                        state = Definitions.NAME;
                    }

                    byte[] data = Encoding.ASCII.GetBytes(state + SelectWindow.GetAppState().currentUsername);

                    client.SendAsync(data, data.Length, remoteEndPoint);
                }
            });
            task.IsBackground = true;
        }
Exemplo n.º 2
0
        public TcpListenerTask()
        {
            task = new Thread(() =>
            {
                try
                {
                    UdpClient client          = new UdpClient(45001);
                    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);

                    while (true)
                    {
                        if (!SelectWindow.GetAppState().isBusy)
                        {
                            Byte[] rec = client.Receive(ref remoteEndPoint);

                            string recstr = Encoding.ASCII.GetString(rec);
                            string header = recstr.Substring(0, Definitions.WANT_TO_CONNECT.Length);
                            if (header.Equals(Definitions.WANT_TO_CONNECT))
                            {
                                string username = "";
                                foreach (User u in SelectWindow.GetGuestList().ToList())
                                {
                                    if (u.Address.Equals(remoteEndPoint.Address))
                                    {
                                        username = u.Nickname;
                                    }
                                }
                                ConnectionPrompt window = new ConnectionPrompt(username);
                                if (window.ShowDialog() == DialogResult.Yes)
                                {
                                    byte[] data = Toolbox.StringToByte(Definitions.OK_TALK + SelectWindow.GetAppState().currentUsername);
                                    client.Send(data, data.Length, new IPEndPoint(remoteEndPoint.Address, 45001));

                                    receivedIP = remoteEndPoint.Address;
                                    ReceivedConnectionEvent?.Invoke();
                                    client.Close();
                                    break;
                                }
                            }
                            else if (header.Equals(Definitions.OK_TALK))
                            {
                                receivedIP = remoteEndPoint.Address;
                                ReceivedConnectionEvent?.Invoke();
                                client.Close();

                                break;
                            }
                        }
                    }
                } catch (ThreadAbortException taex)
                {
                    return;
                }
            });
            task.IsBackground = true;
        }
Exemplo n.º 3
0
        public SendRequestTask()
        {
            task = new Thread(() =>
            {
                UdpClient client          = new UdpClient();
                IPEndPoint remoteEndPoint = new IPEndPoint(IP, 45001);

                byte[] data = Toolbox.StringToByte(Definitions.WANT_TO_CONNECT + SelectWindow.GetAppState().currentUsername);

                client.SendAsync(data, data.Length, remoteEndPoint);
            });
            task.IsBackground = true;
        }
Exemplo n.º 4
0
        public CleanerTask()
        {
            int waitTime = SelectWindow.GetAppState().RefreshInterval;

            task = new Thread(() =>
            {
                while (true)
                {
                    Thread.Sleep(waitTime);
                    RemovingIdlers?.Invoke();
                }
            });
            task.IsBackground = true;
        }
Exemplo n.º 5
0
        public ObservableCollection <User> CleanIdleUsers(ObservableCollection <User> list)
        {
            var current   = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            var threshold = SelectWindow.GetAppState().RefreshInterval;

            foreach (User u in list.ToList())
            {
                long calc = current - u.ConnTime;
                if (calc >= threshold)
                {
                    list.Remove(u);
                }
            }
            return(list);
        }
Exemplo n.º 6
0
        public ListenerTask()
        {
            task = new Thread(() =>
            {
                List <User> guestsList = new List <User>();

                UdpClient client          = new UdpClient(45000);
                IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                string currentUser        = SelectWindow.GetAppState().currentUsername;
                while (true)
                {
                    //Invoke(new MethodInvoker(() => { _stop = stop; }));

                    if (_stop)
                    {
                        break;
                    }

                    Byte[] receivedBytes = client.Receive(ref remoteEndPoint);
                    string data          = Encoding.ASCII.GetString(receivedBytes);
                    string sub           = data.Substring(0, Definitions.NAME.Length);
                    if ((sub.Equals(Definitions.NAME) || sub.Equals(Definitions.BUSY)))
                    {
                        bool busy = false;
                        if (sub.Equals(Definitions.BUSY))
                        {
                            busy = true;
                        }
                        User incomingUser = new User(data.Substring(data.IndexOf("|") + 1), remoteEndPoint.Address, busy);
                        bool add          = true;

                        // Invoke(new MethodInvoker(() => { guestsList = SelectWindow.GetCurrentGuests().ToList(); }));

                        Parallel.Invoke(new Action(() => { guestsList = SelectWindow.GetCurrentGuests().ToList(); }));

                        foreach (User u in guestsList)
                        {
                            if (u.Address.Equals(incomingUser.Address))
                            {
                                if (u.Nickname.Equals(incomingUser.Nickname))
                                {
                                    if (!(u.Nickname.Equals(currentUser)))
                                    {
                                        add = false;
                                        u.UpdateConnTime();
                                        //TODO: odświeżanie listy, jeśli busy
                                        break;
                                    }
                                }
                            }
                        }
                        if (add)
                        {
                            //if(InvokeRequired)
                            //Invoke(new MethodInvoker(() => { SelectWindow.UpdateCurrentGuests(incomingUser); }));
                            Parallel.Invoke(new Action(() => { SelectWindow.UpdateCurrentGuests(incomingUser); }));
                        }
                    }
                }
            });
            task.IsBackground = true;
        }