public ClientConnectionManager(HazelcastClient client)
        {
            _client = client;

            var config = client.GetClientConfig();

            _networkConfig = config.GetNetworkConfig();

            config.GetNetworkConfig().IsRedoOperation();
            _credentials = config.GetCredentials();

            //init socketInterceptor
            var sic = config.GetNetworkConfig().GetSocketInterceptorConfig();
            if (sic != null && sic.IsEnabled())
            {
                //TODO Socket interceptor
                throw new NotImplementedException("Socket Interceptor not yet implemented.");
            }
            _socketInterceptor = null;

            var timeout = EnvironmentUtil.ReadEnvironmentVar("hazelcast.client.invocation.timeout.seconds");
            if (timeout > 0)
            {
                ThreadUtil.TaskOperationTimeOutMilliseconds = timeout.Value*1000;
            }

            _heartbeatTimeout = EnvironmentUtil.ReadEnvironmentVar("hazelcast.client.heartbeat.timeout") ??
                                _heartbeatTimeout;
            _heartbeatInterval = EnvironmentUtil.ReadEnvironmentVar("hazelcast.client.heartbeat.interval") ??
                                 _heartbeatInterval;
        }
        private void HandleSocketOptions(XmlNode node, ClientNetworkConfig clientNetworkConfig)
        {
            var socketOptions = clientNetworkConfig.GetSocketOptions();

            foreach (XmlNode child in node.ChildNodes)
            {
                var nodeName = CleanNodeName(child);
                switch (nodeName)
                {
                case "tcp-no-delay":
                    socketOptions.SetTcpNoDelay(bool.Parse(GetTextContent(child)));
                    break;

                case "keep-alive":
                    socketOptions.SetKeepAlive(bool.Parse(GetTextContent(child)));
                    break;

                case "reuse-address":
                    socketOptions.SetReuseAddress(bool.Parse(GetTextContent(child)));
                    break;

                case "linger-seconds":
                    socketOptions.SetLingerSeconds(Convert.ToInt32(GetTextContent(child)));
                    break;

                case "timeout":
                    socketOptions.SetTimeout(Convert.ToInt32(GetTextContent(child)));
                    break;

                case "buffer-size":
                    socketOptions.SetBufferSize(Convert.ToInt32(GetTextContent(child)));
                    break;
                }
            }
        }
        //[Test]
        public virtual void TestConfig()
        {
            var config = new ClientConfig();
            var networkConfig = new ClientNetworkConfig();
            networkConfig.SetAddresses(new[] { "127.0.0.1:5701" });
            config.SetNetworkConfig(networkConfig);
            config.SetGroupConfig(new GroupConfig("mike-local", "password"));
            var _client = HazelcastClient.NewHazelcastClient(config);

            Assert.NotNull(_client);

            _client.Shutdown();
        }
Esempio n. 4
0
        private void HandleCloudConfig(XmlNode node, ClientNetworkConfig clientNetworkConfig)
        {
            var cloudConfig = clientNetworkConfig.GetCloudConfig();

            var enabledNode = node.Attributes.GetNamedItem("enabled");
            var enabled     = enabledNode != null && CheckTrue(GetTextContent(enabledNode).Trim());

            cloudConfig.SetEnabled(enabled);
            foreach (XmlNode child in node.ChildNodes)
            {
                var nodeName = CleanNodeName(child);
                if ("discovery-token".Equals(nodeName))
                {
                    cloudConfig.SetDiscoveryToken(GetTextContent(child));
                }
            }
        }
        private void HandleSSLConfig(XmlNode node, ClientNetworkConfig clientNetworkConfig)
        {
            var sslConfig = ParseSSLConfig(node);

            clientNetworkConfig.SetSSLConfig(sslConfig);
        }
        private void HandleSocketInterceptorConfig(XmlNode node, ClientNetworkConfig clientNetworkConfig)
        {
            var socketInterceptorConfig = ParseSocketInterceptorConfig(node);

            clientNetworkConfig.SetSocketInterceptorConfig(socketInterceptorConfig);
        }
Esempio n. 7
0
 public virtual void SetNetworkConfig(ClientNetworkConfig networkConfig)
 {
     _networkConfig = networkConfig;
 }
        /// <exception cref="System.IO.IOException"></exception>
        public ClientConnection(ClientConnectionManager clientConnectionManager,
            ClientInvocationService invocationService,
            int id,
            Address address,
            ClientNetworkConfig clientNetworkConfig)
        {
            _clientConnectionManager = clientConnectionManager;
            _id = id;

            var isa = address.GetInetSocketAddress();
            var socketOptions = clientNetworkConfig.GetSocketOptions();
            var socketFactory = socketOptions.GetSocketFactory() ?? new DefaultSocketFactory();
            _clientSocket = socketFactory.CreateSocket();

            try
            {
                _clientSocket = new Socket(isa.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                _clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

                var lingerOption = new LingerOption(true, 5);
                if (socketOptions.GetLingerSeconds() > 0)
                {
                    lingerOption.LingerTime = socketOptions.GetLingerSeconds();
                }
                _clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
                _clientSocket.NoDelay = socketOptions.IsTcpNoDelay();

                _clientSocket.ReceiveTimeout = socketOptions.GetTimeout() > 0 ? socketOptions.GetTimeout() : -1;

                var bufferSize = socketOptions.GetBufferSize() * 1024;
                if (bufferSize < 0)
                {
                    bufferSize = BufferSize;
                }

                _clientSocket.SendBufferSize = bufferSize;
                _clientSocket.ReceiveBufferSize = bufferSize;

                var connectionTimeout = clientNetworkConfig.GetConnectionTimeout() > -1
                    ? clientNetworkConfig.GetConnectionTimeout()
                    : ConnectionTimeout;
                var socketResult = _clientSocket.BeginConnect(address.GetHost(), address.GetPort(), null, null);

                if (!socketResult.AsyncWaitHandle.WaitOne(connectionTimeout, true) || !_clientSocket.Connected)
                {
                    // NOTE, MUST CLOSE THE SOCKET
                    _clientSocket.Close();
                    throw new IOException("Failed to connect to " + address);
                }
                _sendBuffer = ByteBuffer.Allocate(BufferSize);
                _receiveBuffer = ByteBuffer.Allocate(BufferSize);

                _builder = new ClientMessageBuilder(invocationService.HandleClientMessage);

                var networkStream = new NetworkStream(_clientSocket, false);
                if (clientNetworkConfig.GetSSLConfig().IsEnabled())
                {
                    var sslStream = new SslStream(networkStream, false,
                        (sender, certificate, chain, sslPolicyErrors) =>
                            RemoteCertificateValidationCallback(sender, certificate, chain, sslPolicyErrors,
                                clientNetworkConfig), null);
                    var certificateName = clientNetworkConfig.GetSSLConfig().GetCertificateName() ?? "";
                    sslStream.AuthenticateAsClient(certificateName);
                    _stream = sslStream;
                }
                else
                {
                    _stream = networkStream;
                }
                _live = new AtomicBoolean(true);
            }
            catch (Exception e)
            {
                _clientSocket.Close();
                if (_stream != null)
                {
                    _stream.Close();
                }
                throw new IOException("Cannot connect! Socket error:" + e.Message);
            }
        }
        private bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors, ClientNetworkConfig clientNetworkConfig)
        {
            if (sslPolicyErrors == SslPolicyErrors.None)
            {
                return true;
            }

            var validation = true;

            if (sslPolicyErrors.HasFlag(SslPolicyErrors.RemoteCertificateChainErrors))
            {
                var isValidateChain = clientNetworkConfig.GetSSLConfig().IsValidateCertificateChain();
                if (isValidateChain)
                {
                    Logger.Warning("Certificate error:" + sslPolicyErrors);
                    validation = false;
                }
                else
                {
                    Logger.Info("SSL Configured to ignore Certificate chain validation. Ignoring:");
                }
                foreach (var status in chain.ChainStatus)
                {
                    Logger.Info("Certificate chain status:" + status.StatusInformation);
                }
            }
            if (sslPolicyErrors.HasFlag(SslPolicyErrors.RemoteCertificateNameMismatch))
            {
                var isValidateName = clientNetworkConfig.GetSSLConfig().IsValidateCertificateName();
                if (isValidateName)
                {
                    Logger.Warning("Certificate error:" + sslPolicyErrors);
                    validation = false;
                }
                else
                {
                    Logger.Info("Certificate name mismatched but client is configured to ignore Certificate name validation.");
                }
            }
            if (sslPolicyErrors.HasFlag(SslPolicyErrors.RemoteCertificateNotAvailable))
            {
                Logger.Warning("Certificate error:" + sslPolicyErrors);
                validation = false;
            }
            return validation;
        }
Esempio n. 10
0
 /// <summary>
 /// Sets <see cref="ClientNetworkConfig"/>.
 /// </summary>
 /// <param name="networkConfig"><see cref="ClientNetworkConfig"/></param>
 /// <returns><see cref="ClientConfig"/> for chaining</returns>
 public virtual ClientConfig SetNetworkConfig(ClientNetworkConfig networkConfig)
 {
     _networkConfig = networkConfig;
     return(this);
 }
 private void HandleSSLConfig(XmlNode node, ClientNetworkConfig clientNetworkConfig)
 {
     var sslConfig = ParseSSLConfig(node);
     clientNetworkConfig.SetSSLConfig(sslConfig);
 }
 private void HandleSocketOptions(XmlNode node, ClientNetworkConfig clientNetworkConfig)
 {
     var socketOptions = clientNetworkConfig.GetSocketOptions();
     foreach (XmlNode child in node.ChildNodes)
     {
         var nodeName = CleanNodeName(child);
         switch (nodeName)
         {
             case "tcp-no-delay":
                 socketOptions.SetTcpNoDelay(bool.Parse(GetTextContent(child)));
                 break;
             case "keep-alive":
                 socketOptions.SetKeepAlive(bool.Parse(GetTextContent(child)));
                 break;
             case "reuse-address":
                 socketOptions.SetReuseAddress(bool.Parse(GetTextContent(child)));
                 break;
             case "linger-seconds":
                 socketOptions.SetLingerSeconds(Convert.ToInt32(GetTextContent(child)));
                 break;
             case "timeout":
                 socketOptions.SetTimeout(Convert.ToInt32(GetTextContent(child)));
                 break;
             case "buffer-size":
                 socketOptions.SetBufferSize(Convert.ToInt32(GetTextContent(child)));
                 break;
         }
     }
 }
 private void HandleSocketInterceptorConfig(XmlNode node, ClientNetworkConfig clientNetworkConfig)
 {
     var socketInterceptorConfig = ParseSocketInterceptorConfig(node);
     clientNetworkConfig.SetSocketInterceptorConfig(socketInterceptorConfig);
 }
        /// <exception cref="System.IO.IOException"></exception>
        public ClientConnection(ClientConnectionManager clientConnectionManager,
            ClientInvocationService invocationService,
            int id,
            Address address,
            ClientNetworkConfig clientNetworkConfig)
        {
            _clientConnectionManager = clientConnectionManager;
            _id = id;

            var isa = address.GetInetSocketAddress();
            var socketOptions = clientNetworkConfig.GetSocketOptions();
            var socketFactory = socketOptions.GetSocketFactory();

            if (socketFactory == null)
            {
                socketFactory = new DefaultSocketFactory();
            }
            _clientSocket = socketFactory.CreateSocket();
            try
            {
                _clientSocket = new Socket(isa.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                _clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

                var lingerOption = new LingerOption(true, 5);
                if (socketOptions.GetLingerSeconds() > 0)
                {
                    lingerOption.LingerTime = socketOptions.GetLingerSeconds();
                }
                _clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
                _clientSocket.NoDelay = socketOptions.IsTcpNoDelay();

                _clientSocket.ReceiveTimeout = socketOptions.GetTimeout() > 0 ? socketOptions.GetTimeout() : -1;

                var bufferSize = socketOptions.GetBufferSize()*1024;
                if (bufferSize < 0)
                {
                    bufferSize = BufferSize;
                }

                _clientSocket.SendBufferSize = bufferSize;
                _clientSocket.ReceiveBufferSize = bufferSize;

                _clientSocket.UseOnlyOverlappedIO = true;

                var connectionTimeout = clientNetworkConfig.GetConnectionTimeout() > -1
                    ? clientNetworkConfig.GetConnectionTimeout()
                    : ConnectionTimeout;
                var socketResult = _clientSocket.BeginConnect(address.GetHost(), address.GetPort(), null, null);

                if (!socketResult.AsyncWaitHandle.WaitOne(connectionTimeout, true) || !_clientSocket.Connected)
                {
                    // NOTE, MUST CLOSE THE SOCKET
                    _clientSocket.Close();
                    throw new IOException("Failed to connect to " + address);
                }
                _sendBuffer = ByteBuffer.Allocate(BufferSize);
                _receiveBuffer = ByteBuffer.Allocate(BufferSize);

                _builder = new ClientMessageBuilder(invocationService.HandleClientMessage);

                _live = true;
            }
            catch (Exception e)
            {
                _clientSocket.Close();
                throw new IOException("Cannot connect! Socket error:" + e.Message);
            }
        }