Exemplo n.º 1
0
        public void StartServer(Server serverConfig, Context context, Action <Connection> onNewClient)
        {
            ValidateProtocolName(serverConfig);

            ServerConfig config = new ServerConfig();

            config.Port             = ProtocolUtils.retrieveProtocolSetting(serverConfig, "port", 34837);
            config.MaxRequestLength = 100000;

            string host = ProtocolUtils.retrieveProtocolSetting(serverConfig, "host", "Any");

            if (host != "Any")
            {
                IPAddress[] ipAddresses = Dns.GetHostAddresses(host);
                if (ipAddresses.Length == 0)
                {
                    throw new Error(ErrorCode.CONNECTION_ERROR, "Cannot identify IP address by hostname.");
                }
                config.Ip = ipAddresses[0].ToString();  // we take first entry as it does not matter which one is used
            }
            else
            {
                config.Ip = "Any";
            }

            IWSJServer server = wsjServerFactory.Construct(onNewClient);

            server.Setup(config);
            server.Start();
        }
Exemplo n.º 2
0
        public void StartServer(Server serverConfig, Context context, Action <Connection> onNewClient)
        {
            ValidateProtocolName(serverConfig);

            int    port = ProtocolUtils.retrieveProtocolSetting(serverConfig, "port", 34838);
            string host = ProtocolUtils.retrieveProtocolSetting(serverConfig, "host", "Any");

            IPAddress localaddr;

            if (host != "Any")
            {
                IPAddress[] ipAddresses = Dns.GetHostAddresses(host);
                if (ipAddresses.Length == 0)
                {
                    throw new Error(ErrorCode.CONNECTION_ERROR, "Cannot identify IP address by hostname.");
                }
                localaddr = ipAddresses[0];  // we take first entry as it does not matter which one is used
            }
            else
            {
                localaddr = IPAddress.Any;
            }

            // Create connection listener.
            TcpListener listener = new TcpListener(localaddr, port);

            listener.Start();

            // Wait for the first connection asynchronously.
            var serverState = new AsyncServerState {
                Listener = listener, Callback = onNewClient
            };

            listener.BeginAcceptTcpClient(CreateBPConnection, serverState);
        }
Exemplo n.º 3
0
        private void ValidateProtocolName(Server serverConfig)
        {
            string protocol = ProtocolUtils.retrieveProtocolSetting <string>(serverConfig, "name", null);

            if (protocol != "websocket-json")
            {
                throw new Error(ErrorCode.CONNECTION_ERROR, "Given сonfig is not for websocket-json protocol.");
            }
        }
Exemplo n.º 4
0
        public void OpenConnection(Server serverConfig, Context context, Action <Connection> onConnected)
        {
            ValidateProtocolName(serverConfig);

            int    port = ProtocolUtils.retrieveProtocolSetting(serverConfig, "port", -1);
            string host = ProtocolUtils.retrieveProtocolSetting(serverConfig, "host", (string)null);

            if (port == -1 || host == null)
            {
                throw new Error(ErrorCode.CONNECTION_ERROR, "No port and/or IP address is present in configuration.");
            }

            var socketAdapter = new BPSocketAdapter(host, port);

            onConnected(new WSJConnection(socketAdapter));
            socketAdapter.Open();
        }
Exemplo n.º 5
0
        public void OpenConnection(Server serverConfig, Context context, Action <Connection> onConnected)
        {
            ValidateProtocolName(serverConfig);

            int    port = ProtocolUtils.retrieveProtocolSetting(serverConfig, "port", -1);
            string host = ProtocolUtils.retrieveProtocolSetting(serverConfig, "host", (string)null);

            if (port == -1 || host == null)
            {
                throw new Error(ErrorCode.CONNECTION_ERROR, "No port and/or IP address is present in configuration.");
            }

            ISocket socket = webSocketFactory.Construct("ws://" + host + ":" + port + "/");

            socket.Opened += (sender, e) => onConnected(new WSJConnection(socket));
            socket.Error  += (sender, e) => {
                logger.WarnException("Error in connection to " + host + ":" + port, e.Exception);
            };
            socket.Open();
        }