Exemplo n.º 1
0
 private void EstablishConnection()
 {
     while (true)
     {
         try
         {
             if (Wtcp == null)
             {
                 if (Debug)
                 {
                     Console.WriteLine("Attempting connection to " + ServerIp + ":" + ServerPort);
                 }
                 Wtcp = new WatsonTcpSslClient(ServerIp, ServerPort, CertFile, CertPass, AcceptInvalidCerts, MutuallyAuthenticate, ServerConnected, ServerDisconnected, MsgReceived, Debug);
             }
             else if (!Wtcp.IsConnected())
             {
                 if (Debug)
                 {
                     Console.WriteLine("Attempting reconnect to " + ServerIp + ":" + ServerPort);
                 }
                 Wtcp.Dispose();
                 Wtcp = new WatsonTcpSslClient(ServerIp, ServerPort, CertFile, CertPass, AcceptInvalidCerts, MutuallyAuthenticate, ServerConnected, ServerDisconnected, MsgReceived, Debug);
             }
             Thread.Sleep(1000);
         }
         catch (Exception e)
         {
             if (Debug)
             {
                 Console.WriteLine("Exception: " + e.Message);
             }
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Start the cluster client.
        /// </summary>
        /// <param name="serverIp">The IP address of the peer server.</param>
        /// <param name="serverPort">The TCP port of the peer server.</param>
        /// <param name="certFile">The PFX file containing the certificate.</param>
        /// <param name="certPass">The password to the certificate.</param>
        /// <param name="acceptInvalidCerts">True to accept invalid SSL certificates.</param>
        /// <param name="debug">Enable or disable debug logging to the console.</param>
        /// <param name="clusterHealthy">Function to be called when the cluster becomes healthy.</param>
        /// <param name="clusterUnhealthy">Function to be called when the cluster becomes unhealthy.</param>
        /// <param name="messageReceived">Function to be called when a message is received from the peer.</param>
        public ClusterClientSsl(
            string serverIp,
            int serverPort,
            string certFile,
            string certPass,
            bool acceptInvalidCerts,
            bool debug,
            Func <bool> clusterHealthy,
            Func <bool> clusterUnhealthy,
            Func <byte[], bool> messageReceived)
        {
            if (String.IsNullOrEmpty(serverIp))
            {
                throw new ArgumentNullException(nameof(serverIp));
            }
            if (serverPort < IPEndPoint.MinPort || serverPort > IPEndPoint.MaxPort)
            {
                throw new ArgumentOutOfRangeException(nameof(serverPort));
            }
            if (String.IsNullOrEmpty(certFile))
            {
                throw new ArgumentNullException(nameof(certFile));
            }
            if (clusterHealthy == null)
            {
                throw new ArgumentNullException(nameof(clusterHealthy));
            }
            if (clusterUnhealthy == null)
            {
                throw new ArgumentNullException(nameof(clusterUnhealthy));
            }
            if (messageReceived == null)
            {
                throw new ArgumentNullException(nameof(messageReceived));
            }

            ServerIp           = serverIp;
            ServerPort         = serverPort;
            CertFile           = certFile;
            CertPass           = certPass;
            AcceptInvalidCerts = acceptInvalidCerts;
            Debug            = debug;
            Wtcp             = null;
            ClusterHealthy   = clusterHealthy;
            ClusterUnhealthy = clusterUnhealthy;
            MessageReceived  = messageReceived;
            Task.Run(() => EstablishConnection());
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.Write("Server IP        : ");
            serverIp = Console.ReadLine();

            Console.Write("Server Port      : ");
            serverPort = Convert.ToInt32(Console.ReadLine());

            Console.Write("Certificate File : ");
            certFile = Console.ReadLine();

            Console.Write("Certificate Pass : "******"Command [? for help]: ");
                string userInput = Console.ReadLine();
                if (String.IsNullOrEmpty(userInput))
                {
                    continue;
                }

                switch (userInput)
                {
                case "?":
                    Console.WriteLine("Available commands:");
                    Console.WriteLine("  ?          help (this menu)");
                    Console.WriteLine("  q          quit");
                    Console.WriteLine("  cls        clear screen");
                    Console.WriteLine("  send       send message to server");
                    Console.WriteLine("  sendasync  send message to server asynchronously");
                    Console.WriteLine("  status     show if client connected");
                    Console.WriteLine("  dispose    dispose of the connection");
                    Console.WriteLine("  connect    connect to the server if not connected");
                    Console.WriteLine("  reconnect  disconnect if connected, then reconnect");
                    break;

                case "q":
                    runForever = false;
                    break;

                case "cls":
                    Console.Clear();
                    break;

                case "send":
                    Console.Write("Data: ");
                    userInput = Console.ReadLine();
                    if (String.IsNullOrEmpty(userInput))
                    {
                        break;
                    }

                    client.Send(Encoding.UTF8.GetBytes(userInput));
                    break;

                case "sendasync":
                    Console.Write("Data: ");
                    userInput = Console.ReadLine();
                    if (String.IsNullOrEmpty(userInput))
                    {
                        break;
                    }

                    client.SendAsync(Encoding.UTF8.GetBytes(userInput));
                    break;

                case "status":
                    if (client == null)
                    {
                        Console.WriteLine("Connected: False (null)");
                    }
                    else
                    {
                        Console.WriteLine("Connected: " + client.IsConnected());
                    }

                    break;

                case "dispose":
                    client.Dispose();
                    break;

                case "connect":
                    if (client != null && client.IsConnected())
                    {
                        Console.WriteLine("Already connected");
                    }
                    else
                    {
                        client = new WatsonTcpSslClient(serverIp, serverPort, certFile, certPass, true, false, ServerConnected, ServerDisconnected, MessageReceived, true);
                    }
                    break;

                case "reconnect":
                    if (client != null)
                    {
                        client.Dispose();
                    }

                    client = new WatsonTcpSslClient(serverIp, serverPort, certFile, certPass, true, false, ServerConnected, ServerDisconnected, MessageReceived, true);
                    break;

                default:
                    break;
                }
            }
        }