Exemplo n.º 1
0
        /// <summary>
        /// Start the cluster server.
        /// </summary>
        /// <param name="peerIp">The IP address of the peer client.</param>
        /// <param name="port">The TCP port on which the cluster server should listen.</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="clientConnected">Function to be called when the peer connects.</param>
        /// <param name="clientDisconnected">Function to be called when the peer disconnects.</param>
        /// <param name="messageReceived">Function to be called when a message is received from the peer.</param>
        public ClusterServerSsl(string peerIp, int port, string certFile, string certPass, bool acceptInvalidCerts, bool debug, Func <string, bool> clientConnected, Func <string, bool> clientDisconnected, Func <string, byte[], bool> messageReceived)
        {
            if (String.IsNullOrEmpty(peerIp))
            {
                throw new ArgumentNullException(nameof(peerIp));
            }
            if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }
            if (String.IsNullOrEmpty(certFile))
            {
                throw new ArgumentNullException(nameof(certFile));
            }
            if (clientConnected == null)
            {
                throw new ArgumentNullException(nameof(clientConnected));
            }
            if (clientDisconnected == null)
            {
                throw new ArgumentNullException(nameof(clientDisconnected));
            }
            if (messageReceived == null)
            {
                throw new ArgumentNullException(nameof(messageReceived));
            }

            Port               = port;
            CertFile           = certFile;
            CertPass           = certPass;
            AcceptInvalidCerts = acceptInvalidCerts;
            Debug              = debug;
            ClientConnected    = clientConnected;
            ClientDisconnected = clientDisconnected;
            MessageReceived    = messageReceived;

            List <string> permittedIps = new List <string>();

            permittedIps.Add(peerIp);

            Wtcp = new WatsonTcpSslServer(null, Port, CertFile, CertPass, AcceptInvalidCerts, permittedIps, ClientConnect, ClientDisconnect, MsgReceived, Debug);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Start the cluster server.
        /// </summary>
        /// <param name="permittedIps">The list of IP addresses allowed to connect.</param>
        /// <param name="port">The TCP port on which the cluster server should listen.</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="clientConnected">Function to be called when the peer connects.</param>
        /// <param name="clientDisconnected">Function to be called when the peer disconnects.</param>
        /// <param name="messageReceived">Function to be called when a message is received from the peer.</param>
        public ClusterServerSsl(IEnumerable <string> permittedIps, int port, string certFile, string certPass, bool acceptInvalidCerts, bool debug, Func <string, bool> clientConnected, Func <string, bool> clientDisconnected, Func <string, byte[], bool> messageReceived)
        {
            if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }
            if (String.IsNullOrEmpty(certFile))
            {
                throw new ArgumentNullException(nameof(certFile));
            }
            if (clientConnected == null)
            {
                throw new ArgumentNullException(nameof(clientConnected));
            }
            if (clientDisconnected == null)
            {
                throw new ArgumentNullException(nameof(clientDisconnected));
            }
            if (messageReceived == null)
            {
                throw new ArgumentNullException(nameof(messageReceived));
            }

            Port               = port;
            CertFile           = certFile;
            CertPass           = certPass;
            AcceptInvalidCerts = acceptInvalidCerts;
            Debug              = debug;
            ClientConnected    = clientConnected;
            ClientDisconnected = clientDisconnected;
            MessageReceived    = messageReceived;

            List <string> PermittedIps = null;

            if (permittedIps != null && permittedIps.Count() > 0)
            {
                PermittedIps = new List <string>(permittedIps);
            }

            Wtcp = new WatsonTcpSslServer(null, Port, CertFile, CertPass, AcceptInvalidCerts, MutuallyAuthenticate, ClientConnect, ClientDisconnect, MsgReceived, Debug);
        }
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();

                    List <string> clients;
                    string        ipPort;

                    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("  list     list clients");
                        Console.WriteLine("  send     send message to client");
                        Console.WriteLine("  remove   disconnect client");
                        break;

                    case "q":
                        runForever = false;
                        break;

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

                    case "list":
                        clients = server.ListClients();
                        if (clients != null && clients.Count > 0)
                        {
                            Console.WriteLine("Clients");
                            foreach (string curr in clients)
                            {
                                Console.WriteLine("  " + curr);
                            }
                        }
                        else
                        {
                            Console.WriteLine("None");
                        }
                        break;

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

                        server.Send(ipPort, Encoding.UTF8.GetBytes(userInput));
                        break;

                    case "remove":
                        Console.Write("IP:Port: ");
                        ipPort = Console.ReadLine();
                        server.DisconnectClient(ipPort);
                        break;

                    default:
                        break;
                    }
                }
            }
        }