Example #1
0
        public RClient(Uri uri, int timeOut = 3000)
        {
            if (string.IsNullOrEmpty(uri.Scheme) || string.Compare(uri.Scheme, "rpc", true) != 0)
            {
                ExceptionCollector.Add("Consumer.RClient.Init Error", new RPCSocketException("当前连接协议不正确,请使用格式rpc://ip:port"));
                return;
            }

            _timeOut = timeOut;

            _disorderSyncHelper = new DisorderSyncHelper(_timeOut);

            var ipPort = DNSHelper.GetIPPort(uri);

            _rUnpacker = new RUnpacker();

            SocketOptionBuilder builder = SocketOptionBuilder.Instance;

            var option = builder.SetSocket()
                         .UseIocp <RUnpacker>()
                         .SetIP(ipPort.Item1)
                         .SetPort(ipPort.Item2)
                         .SetReadBufferSize(10240)
                         .SetWriteBufferSize(10240)
                         .SetTimeOut(_timeOut)
                         .Build();

            _client = SocketFactory.CreateClientSocket(option);

            _client.OnReceive += OnReceived;

            _client.OnDisconnected += _client_OnDisConnected;
        }
Example #2
0
        public MqttTcpServerListener(
            AddressFamily addressFamily,
            MqttServerTcpEndpointBaseOptions options,
            X509Certificate2 tlsCertificate,
            CancellationToken cancellationToken,
            IMqttNetChildLogger logger)
        {
            _cancellationToken = cancellationToken;
            _logger            = logger;
            _addressFamily     = addressFamily;

            var sb = new SocketOptionBuilder().SetSocket(Sockets.Model.SocketType.Tcp).UseStream();

            if (options is MqttServerTlsTcpEndpointOptions tlsOptions)
            {
                sb = sb.WithSsl(tlsCertificate, tlsOptions.SslProtocol);
            }

            sb = sb.SetPort(options.Port);

            if (_addressFamily == AddressFamily.InterNetworkV6)
            {
                sb = sb.UseIPV6();
            }

            socketOption = sb.Build();

            serverSokcet = SocketFactory.CreateServerSocket(socketOption, cancellationToken);

            serverSokcet.OnAccepted += ServerSokcet_OnAccepted;
        }
Example #3
0
        public RClient(Uri uri)
        {
            if (string.IsNullOrEmpty(uri.Scheme) || string.Compare(uri.Scheme, "rpc", true) != 0)
            {
                ExceptionCollector.Add("Consumer.RClient.Init Error", new RPCSocketException("当前连接协议不正确,请使用格式rpc://ip:port"));
                return;
            }

            var ipPort = DNSHelper.GetIPPort(uri);

            _RContext = new RContext();

            SocketOptionBuilder builder = SocketOptionBuilder.Instance;

            var option = builder.SetSocket()
                         .UseIocp(_RContext)
                         .SetIP(ipPort.Item1)
                         .SetPort(ipPort.Item2)
                         .SetReadBufferSize()
                         .SetWriteBufferSize()
                         .Build();

            _client = SocketFactory.CreateClientSocket(option);

            _client.OnReceive += OnReceived;

            _client.OnDisconnected += _client_OnDisConnected;
        }
Example #4
0
        public HttpSocket(int port, int bufferSize = 1024 * 10, int count = 10000, int timeOut = 120 * 1000)
        {
            var optionBuilder = new SocketOptionBuilder()
                                .SetSocket(Sockets.Model.SocketType.Tcp)
                                .UseIocp(new HContext())
                                .SetPort(port)
                                .SetCount(count)
                                .SetBufferSize(bufferSize)
                                .SetTimeOut(timeOut);
            var option = optionBuilder.Build();

            _serverSokcet            = SocketFactory.CreateServerSocket(option);
            _serverSokcet.OnReceive += _serverSokcet_OnReceive;
        }
Example #5
0
        public HttpSocketDebug(int port, int bufferSize = 1024 * 10, int count = 10000, int timeOut = 120 * 1000)
        {
            var optionBuilder = new SocketOptionBuilder()
                                .SetSocket(Sockets.Model.SAEASocketType.Tcp)
                                .UseIocp <HContext>()
                                .SetPort(port)
                                .SetCount(count)
                                .SetReadBufferSize(bufferSize)
                                .SetTimeOut(timeOut)
                                .ReusePort(false);

            _option = optionBuilder.Build();

            _serverSokcet            = SocketFactory.CreateServerSocket(_option);
            _serverSokcet.OnReceive += _serverSokcet_OnReceive;
        }
Example #6
0
        public HttpSocket(int port, int bufferSize = 1024 * 10, int count = 10000, int timeOut = 120 * 1000, bool isDebug = false)
        {
            var optionBuilder = new SocketOptionBuilder()
                                .SetSocket(SAEASocketType.Tcp)
                                .UseIocp(new HContext())
                                .SetPort(port)
                                .SetCount(count)
                                .SetReadBufferSize(bufferSize)
                                .SetWriteBufferSize(bufferSize)
                                .SetTimeOut(timeOut);
            var option = optionBuilder.Build();

            _serverSokcet            = SocketFactory.CreateServerSocket(option);
            _serverSokcet.OnReceive += _serverSokcet_OnReceive;

            IsDebug = isDebug;
        }
Example #7
0
        public async Task ConnectAsync(CancellationToken cancellationToken)
        {
            IClientSocket client = null;

            try
            {
                SocketOptionBuilder sb = SocketOptionBuilder.Instance;
                sb = sb.SetReadBufferSize(_options.BufferSize);
                sb = sb.SetWriteBufferSize(_options.BufferSize);
                if (!_options.NoDelay)
                {
                    sb = sb.SetDelay();
                }

                IPAddress ip;

                string ipStr = _options.Server;

                if (ipStr.IndexOf("localhost") > -1)
                {
                    ip = IPAddress.Parse("127.0.0.1");
                }
                else
                {
                    ip = IPAddress.Parse(ipStr);
                }

                if (_options.AddressFamily == AddressFamily.InterNetworkV6 || ip.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    sb = sb.UseIPv6();
                }
                sb = sb.SetIPEndPoint(new IPEndPoint(ip, _options.GetPort()));

                client = SocketFactory.CreateClientSocket(sb.Build());

                _stream = await((StreamClientSocket)client).ConnectAsync(InternalUserCertificateValidationCallback, LoadCertificates, !_options.TlsOptions.IgnoreCertificateRevocationErrors);

                Endpoint = client.Endpoint;
            }
            catch (Exception)
            {
                client?.Dispose();
                throw;
            }
        }
Example #8
0
        public MqttTcpChannel(IMqttClientOptions clientOptions)
        {
            _clientOptions = clientOptions ?? throw new ArgumentNullException(nameof(clientOptions));
            _options       = (MqttClientTcpOptions)clientOptions.ChannelOptions;

            var builder = new SocketOptionBuilder()
                          .UseStream()
                          .SetIP(_options.Server)
                          .SetPort(_options.Port ?? 1883);

            if (_options.TlsOptions.UseTls)
            {
                builder = builder.WithSsl(_options.TlsOptions.SslProtocol);
            }

            var option = builder.Build();

            _clientSocket = SocketFactory.CreateClientSocket(option);
        }