Esempio n. 1
0
        /// <summary>
        /// Initiates a connection to the router.
        /// </summary>
        /// <returns></returns>
        private static Task <SimpleSocket> InitiateConnectionToRouter()
        {
            var socketContextTCS = new TaskCompletionSource <SimpleSocket>();
            var socketContext    = new SimpleSocket();

            socketContext.Connected = async context =>
            {
                socketContextTCS.TrySetResult(context);
            };

            Task.Run(async() =>
            {
                try
                {
                    // If connecting as a client, try once, otherwise try to listen multiple time (in case port is shared)
                    switch (ConnectionMode)
                    {
                    case RouterConnectionMode.Connect:
                        await socketContext.StartClient("127.0.0.1", DefaultPort);
                        break;

                    case RouterConnectionMode.Listen:
                        await socketContext.StartServer(DefaultListenPort, true, 10);
                        break;

                    case RouterConnectionMode.ConnectThenListen:
                        bool clientException = false;
                        try
                        {
                            await socketContext.StartClient("127.0.0.1", DefaultPort);
                        }
                        catch (Exception)     // Ideally we should filter SocketException, but not available on some platforms (maybe it should be wrapped in a type available on all paltforms?)
                        {
                            clientException = true;
                        }
                        if (clientException)
                        {
                            await socketContext.StartServer(DefaultListenPort, true, 10);
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                catch (Exception e)
                {
                    Log.Error("Could not connect to connection router using mode {0}: {1}", ConnectionMode, e.Message);
                    throw;
                }
            });

            // Wait for server to connect to us (as a Task)
            return(socketContextTCS.Task);
        }
Esempio n. 2
0
        /// <summary>
        /// Initiates a connection to the router.
        /// </summary>
        /// <returns></returns>
        private static Task<SimpleSocket> InitiateConnectionToRouter()
        {
            var socketContextTCS = new TaskCompletionSource<SimpleSocket>();
            var socketContext = new SimpleSocket();
            socketContext.Connected = async context =>
            {
                socketContextTCS.TrySetResult(context);
            };

            Task.Run(async () =>
            {
                // If connecting as a client, try once, otherwise try to listen multiple time (in case port is shared)
                switch (ConnectionMode)
                {
                    case RouterConnectionMode.Connect:
                        await socketContext.StartClient("127.0.0.1", DefaultPort);
                        break;
                    case RouterConnectionMode.Listen:
                        await socketContext.StartServer(DefaultListenPort, true, 10);
                        break;
                    case RouterConnectionMode.ConnectThenListen:
                        bool clientException = false;
                        try
                        {
                            await socketContext.StartClient("127.0.0.1", DefaultPort);
                        }
                        catch (Exception) // Ideally we should filter SocketException, but not available on some platforms (maybe it should be wrapped in a type available on all paltforms?)
                        {
                            clientException = true;
                        }
                        if (clientException)
                        {
                            await socketContext.StartServer(DefaultListenPort, true, 10);
                        }
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            });

            // Wait for server to connect to us (as a Task)
            return socketContextTCS.Task;
        }
Esempio n. 3
0
        /// <summary>
        /// Initiates a connection to the router.
        /// </summary>
        /// <returns></returns>
        private static Task <SimpleSocket> InitiateConnectionToRouter()
        {
            var socketContextTCS = new TaskCompletionSource <SimpleSocket>();
            var socketContext    = new SimpleSocket();

            socketContext.Connected = async context =>
            {
                socketContextTCS.TrySetResult(context);
            };

            Task.Run(async() =>
            {
                // Keep trying to establish connections until no errors
                bool hasErrors;
                do
                {
                    try
                    {
                        hasErrors = false;
                        if (PlatformIsPortForward)
                        {
                            await socketContext.StartServer(DefaultListenPort, true);
                        }
                        else
                        {
                            await socketContext.StartClient("127.0.0.1", DefaultPort);
                        }
                    }
                    catch (Exception)
                    {
                        hasErrors = true;
                    }
                    if (hasErrors)
                    {
                        // Wait a little bit before next try
                        await Task.Delay(100);
                    }
                } while (hasErrors);
            });

            // Wait for server to connect to us (as a Task)
            return(socketContextTCS.Task);
        }
Esempio n. 4
0
        /// <summary>
        /// Initiates a connection to the router.
        /// </summary>
        /// <returns></returns>
        private static Task<SimpleSocket> InitiateConnectionToRouter()
        {
            var socketContextTCS = new TaskCompletionSource<SimpleSocket>();
            var socketContext = new SimpleSocket();
            socketContext.Connected = async context =>
            {
                socketContextTCS.TrySetResult(context);
            };

            Task.Run(async () =>
            {
                // Keep trying to establish connections until no errors
                bool hasErrors;
                do
                {
                    try
                    {
                        hasErrors = false;
                        if (PlatformIsPortForward)
                            await socketContext.StartServer(DefaultListenPort, true);
                        else
                            await socketContext.StartClient("127.0.0.1", DefaultPort);
                    }
                    catch (Exception)
                    {
                        hasErrors = true;
                    }
                    if (hasErrors)
                    {
                        // Wait a little bit before next try
                        await Task.Delay(100);
                    }
                } while (hasErrors);
            });

            // Wait for server to connect to us (as a Task)
            return socketContextTCS.Task;
        }