Example #1
0
        public void RetrieveAvailableUsers()
        {
            // preparing request
            byte[] byteToSend = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(_me));

            while (_continueRequesting)
            {
                bool timedOut = false;

                // sending request
                _udpClient.Send(byteToSend, byteToSend.Length, _broadcastIp);

                // ip end point used to record address and port of sender
                IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

                while (!timedOut)
                {
                    byte[] recBytes = null;

                    try
                    {
                        // blocked until a message is received
                        recBytes = _udpClient.Receive(ref remoteIpEndPoint);
                    }
                    catch (SocketException e)
                    {
                        if (!e.SocketErrorCode.Equals(SocketError.TimedOut))
                        {
                            throw new Exception("Unexpected exception", e);
                        }

                        timedOut = true;

                        // call the functions registered to the delegate, in particular in userSelection
                        // clearing expired users
                        CleanUsersEvent();
                    }

                    if (!IpUtils.IsSelfMessage(remoteIpEndPoint) && !timedOut)
                    {
                        // reading response
                        string readData = Encoding.ASCII.GetString(recBytes);

                        // parsing available user
                        User availableUser = JsonConvert.DeserializeObject <User>(readData);
                        availableUser.IPAddress  = remoteIpEndPoint.Address.ToString();
                        availableUser.LastUpTime = DateTime.Now;

                        // call the functions registered to the delegate, in particular in userSelection
                        // adding new user
                        AddUserEvent(availableUser);
                    }
                }
            }
        }
Example #2
0
        public void Listen()
        {
            while (_statusAvailableEvent.WaitOne())
            {
                try
                {
                    // ip end point used to record address and port of sender
                    IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

                    // blocked until a message is received
                    byte[] recBytes = _udpServer.Receive(ref remoteIpEndPoint);

                    if (!IpUtils.IsSelfMessage(remoteIpEndPoint))
                    {
                        // reading requester user
                        //string readData = Encoding.ASCII.GetString(recBytes);

                        //User requesterUser = JsonConvert.DeserializeObject<User>(readData);
                        //requesterUser.IPAddress = remoteIpEndPoint.Address.ToString();

                        // updating machine identity
                        UpdateMe();

                        // preparing response
                        byte[] byteToSend = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(_me));

                        // sending response
                        _udpServer.Send(byteToSend, byteToSend.Length, remoteIpEndPoint);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("problems while udp responding");
                    Console.WriteLine(e);
                }
            }
        }