private void Worker()
        {
            Port = DEFAULT_PORT;

            // Register URL
            using (RegistryKey key = OutlookRegistryUtils.OpenOutlookKey(REG_KEY, Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                if (key != null)
                {
                    // Set only if empty or already our URL
                    string oldURL = key.GetValueString(REG_VALUE);
                    if (string.IsNullOrWhiteSpace(oldURL) || oldURL.Contains("/" + URL_IDENTIFIER + "/"))
                    {
                        key.SetValue(REG_VALUE, string.Format(URL, Port));
                    }
                }
            }

            FreeBusyServer server = new FreeBusyServer(this);

            // Run
            TcpListener listener = new TcpListener(IPAddress.Loopback, Port);

            listener.Start();
            for (;;)
            {
                Interlocked.Increment(ref _iterationCount);
                try
                {
                    for (;;)
                    {
                        // Wait for a connection
                        TcpClient client = listener.AcceptTcpClient();
                        Interlocked.Increment(ref _requestCount);
                        // And handle it in the UI thread to allow GAB access
                        Tasks.Task(null, this, "FreeBusyHandler", () => server.HandleRequest(client));
                    }
                }
                catch (Exception e)
                {
                    Logger.Instance.Error(this, "Error in FreeBusy server: {0}", e);
                }
            }
        }
Exemple #2
0
        private void Worker()
        {
            Port = DEFAULT_PORT;

            // Check the URL to reuse the port number if possible
            try
            {
                using (RegistryKey key = OutlookRegistryUtils.OpenOutlookKey(REG_KEY))
                {
                    if (key != null)
                    {
                        string oldURL = key.GetValueString(REG_VALUE);
                        if (oldURL.StartsWith(URL_BASE))
                        {
                            string rest = oldURL.Substring(URL_BASE.Length);
                            int    sep  = rest.IndexOf('/');
                            if (sep >= 0)
                            {
                                rest = rest.Substring(0, sep);
                            }
                            int port = int.Parse(rest);
                            if (port > 0 && port < 65536)
                            {
                                Port = port;
                            }
                        }
                    }
                }
            }
            catch (Exception) { Port = DEFAULT_PORT; }

            TcpListener listener;

            listener = new TcpListener(IPAddress.Loopback, Port);
            try
            {
                listener.Start();
            }
            catch (SocketException)
            {
                // Error opening port, try with a default one
                listener = new TcpListener(IPAddress.Loopback, 0);
                listener.Start();
                Port = ((IPEndPoint)listener.LocalEndpoint).Port;
            }

            // Register URL
            using (RegistryKey key = OutlookRegistryUtils.OpenOutlookKey(REG_KEY, Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                if (key != null)
                {
                    // Set only if empty or already our URL
                    string oldURL = key.GetValueString(REG_VALUE);
                    if (string.IsNullOrWhiteSpace(oldURL) || oldURL.Contains("/" + URL_IDENTIFIER + "/"))
                    {
                        key.SetValue(REG_VALUE, string.Format(URL, Port));
                    }
                }
            }

            FreeBusyServer server = new FreeBusyServer(this);

            // Run
            for (;;)
            {
                Interlocked.Increment(ref _iterationCount);
                try
                {
                    for (;;)
                    {
                        // Wait for a connection
                        TcpClient client = listener.AcceptTcpClient();
                        Interlocked.Increment(ref _requestCount);
                        // And handle it in the UI thread to allow GAB access
                        Tasks.Task(null, this, "FreeBusyHandler", () => server.HandleRequest(client));
                    }
                }
                catch (Exception e)
                {
                    Logger.Instance.Error(this, "Error in FreeBusy server: {0}", e);
                }
            }
        }