Exemple #1
0
        private void HandleIncomingRequests(object ipAddressAsObject)
        {
            try
            {
                var ipAddress = (ipAddressAsObject != null) ? IPAddress.Parse((string)ipAddressAsObject) : IPAddress.Any;

                Log.Debug("Creating listener for ip '{0}'", ipAddress);

                using (var udpClient = new UdpClient())
                {
                    udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                    udpClient.ExclusiveAddressUse = false;
                    udpClient.EnableBroadcast     = true;

                    udpClient.Client.Bind(new IPEndPoint(ipAddress, Port));

                    var licenseSignature = string.Empty;

                    while (true)
                    {
                        try
                        {
                            if (string.IsNullOrEmpty(licenseSignature))
                            {
                                var currentLicense = _licenseService.CurrentLicense;
                                if (currentLicense != null)
                                {
                                    licenseSignature = currentLicense.Signature;
                                }
                            }

                            if (string.IsNullOrWhiteSpace(licenseSignature))
                            {
                                // No reason to wait for something, wait and continue
                                Thread.Sleep(5000);
                                continue;
                            }

                            var ipEndPoint = new IPEndPoint(IPAddress.Any, Port);
                            var data       = udpClient.Receive(ref ipEndPoint);

                            var message = Encoding.ASCII.GetString(data);
                            if (string.Equals(message, licenseSignature))
                            {
                                Log.Debug("Received request from '{0}' on '{1}' to get currently used license", ipEndPoint.Address, udpClient.Client.LocalEndPoint);

                                var licenseUsage = new NetworkLicenseUsage(_machineId, ipAddress.ToString(), _userName, licenseSignature, _startDateTime);

                                var responseMessage = licenseUsage.ToNetworkMessage();
                                var responseBytes   = ASCIIEncoding.ASCII.GetBytes(responseMessage);

                                udpClient.Send(responseBytes, responseBytes.Length, ipEndPoint);
                            }
                        }
                        catch (SocketException)
                        {
                            // Ignore, it's probably the timeout
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to handle incoming requests, probably a process is already running on the same port");
            }
        }
Exemple #2
0
        private List <NetworkLicenseUsage> BroadcastMessage(string ipAddress, string message, int maxTimeout = 1000)
        {
            var licenseUsages = new Dictionary <string, NetworkLicenseUsage>();

            Log.Debug("Broadcasting via ip '{0}' to see how much users are currently using the license", ipAddress);

            try
            {
                using (var udpClient = new UdpClient())
                {
                    udpClient.Client.ReceiveTimeout = maxTimeout;
                    udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                    udpClient.EnableBroadcast = true;

                    udpClient.Client.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), 0));

                    var sendBuffer = Encoding.ASCII.GetBytes(message);

                    var remoteEndPoint = new IPEndPoint(IPAddress.Broadcast, Port);
                    udpClient.Send(sendBuffer, sendBuffer.Length, remoteEndPoint);

                    var endDateTime = DateTime.Now.AddMilliseconds(maxTimeout);

                    while (endDateTime >= DateTime.Now)
                    {
                        try
                        {
                            var ipEndPoint    = new IPEndPoint(IPAddress.Any, 0);
                            var receiveBuffer = udpClient.Receive(ref ipEndPoint);
                            if (receiveBuffer != null && receiveBuffer.Length > 0)
                            {
                                var receivedMessage = Encoding.ASCII.GetString(receiveBuffer);

                                Log.Debug("Received message '{0}' from '{1}'", receivedMessage, ipEndPoint.Address);

                                var licenseUsage = NetworkLicenseUsage.Parse(receivedMessage);
                                if (licenseUsage == null)
                                {
                                    continue;
                                }

                                if (string.Equals(licenseUsage.LicenseSignature, message))
                                {
                                    Log.Debug("Received message from '{0}' that license is being used", ipEndPoint.Address);

                                    var computerId = licenseUsage.ComputerId;

                                    var add = true;

                                    // Only update if this is an older timestamp
                                    if (licenseUsages.ContainsKey(computerId) && (licenseUsages[computerId].StartDateTime < licenseUsage.StartDateTime))
                                    {
                                        add = false;
                                    }

                                    if (add)
                                    {
                                        licenseUsages[computerId] = licenseUsage;
                                    }
                                }
                            }
                        }
                        catch (SocketException)
                        {
                            // ignore
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to broadcast message, defaulting to 0 currently active licences");
            }

            return(licenseUsages.Values.ToList());
        }