/// <summary> /// WSClient /// </summary> /// <param name="ip"></param> /// <param name="port"></param> /// <param name="subProtocol"></param> /// <param name="origin"></param> /// <param name="bufferSize"></param> public WSClient(string ip = "127.0.0.1", int port = 16666, string subProtocol = SubProtocolType.Default, string origin = "", int bufferSize = 10 * 1024) { _serverIP = ip; _serverPort = port; if (string.IsNullOrEmpty(_url)) { _url = $"ws://{ip}:{port}/"; } _origin = origin; _wsContext = new WSContext(); var option = SocketOptionBuilder.Instance .UseIocp(_wsContext) .SetIP(ip) .SetPort(port) .SetReadBufferSize(bufferSize) .SetReadBufferSize(bufferSize) .Build(); _subProtocol = subProtocol; _client = SocketFactory.CreateClientSocket(option); _client.OnReceive += _client_OnReceive; _client.OnDisconnected += WSClient_OnDisconnected; _client.OnError += WSClient_OnError; }
public MessageClient(int bufferSize = 102400, string ip = "127.0.0.1", int port = 39654) { _messageContext = new MessageContext(); var option = SocketOptionBuilder.Instance .SetSocket() .UseIocp(_messageContext) .SetIP(ip) .SetPort(port) .SetReadBufferSize(bufferSize) .SetWriteBufferSize(bufferSize) .Build(); _client = SocketFactory.CreateClientSocket(option); _client.OnReceive += _client_OnReceive; HeartSpan = 10 * 1000; HeartAsync(); _batcher = new Batcher(10000, 10); _batcher.OnBatched += _batcher_OnBatched; }
/// <summary> /// 编码处理 /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <IResponse> Code(IRequest request, CancellationToken cancellationToken = default(CancellationToken)) { using (var udpClient = SocketFactory.CreateClientSocket(SocketOptionBuilder.Instance.SetSocket(SAEASocketType.Udp) .SetIPEndPoint(_ipEndpoint) .UseIocp <BaseUnpacker>() .SetReadBufferSize(SocketOption.UDPMaxLength) .SetWriteBufferSize(SocketOption.UDPMaxLength) .ReusePort() .Build())) { udpClient.OnReceive += UdpClient_OnReceive; udpClient.Connect(); var buffer = _orderSyncHelper.Wait(() => { udpClient.SendAsync(request.ToArray()); }); DnsResponseMessage response = DnsResponseMessage.FromArray(buffer); if (response.Truncated) { return(await _fallback.Code(request, cancellationToken)); } return(new Model.DnsClientResponse(request, response, buffer)); } }
public QClient(string name, int bufferSize = 100 * 1024, string ip = "127.0.0.1", int port = 39654) { _name = name; HeartSpan = 1000 * 1000; HeartAsync(); _batcher = new Batcher <byte[]>(3000, 10); //此参数用于控制产生或消费速读,过大会导致溢出异常 _batcher.OnBatched += _batcher_OnBatched; _qUnpacker = new QUnpacker(); _queueCoder = new QueueCoder(); ISocketOption socketOption = SocketOptionBuilder.Instance.SetSocket(Sockets.Model.SAEASocketType.Tcp) .UseIocp <QUnpacker>() .SetIP(ip) .SetPort(port) .SetWriteBufferSize(bufferSize) .SetReadBufferSize(bufferSize) .ReusePort(false) .Build(); _clientSocket = SocketFactory.CreateClientSocket(socketOption); _clientSocket.OnReceive += _clientSocket_OnReceive; _clientSocket.OnError += _clientSocket_OnError; _clientSocket.OnDisconnected += _clientSocket_OnDisconnected; }
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; }
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; }
public MyClient() { _myContext = new MyContext(); var option = SocketOptionBuilder.Instance.UseIocp(_myContext) .SetIP("127.0.0.1") .SetPort(39654) .Build(); _client = SocketFactory.CreateClientSocket(option); _client.OnReceive += _client_OnReceive; }
/// <summary> /// TCPClient /// </summary> public TCPClient(IPEndPoint endPoint) { _clientSokcet = SocketFactory.CreateClientSocket(SocketOptionBuilder.Instance .SetSocket(Model.SAEASocketType.Tcp) .SetIPEndPoint(endPoint) .UseIocp <Coder>() .Build()); _clientSokcet.OnReceive += ClientSokcet_OnReceive; _clientSokcet.OnDisconnected += ClientSokcet_OnDisconnected; _clientSokcet.OnError += ClientSokcet_OnError; SocketStream = new SocketStream(_clientSokcet); }
public JClient() { _jUnpacker = new JUnpacker(); _clientSokcet = SocketFactory.CreateClientSocket(SocketOptionBuilder.Instance .SetSocket(Model.SAEASocketType.Tcp) .SetIPEndPoint(new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 39808)) .UseIocp <JContext>() .Build()); _clientSokcet.OnReceive += ClientSokcet_OnReceive; _clientSokcet.OnDisconnected += ClientSokcet_OnDisconnected; _clientSokcet.OnError += ClientSokcet_OnError; }
public ClientSocket(ClientConfig config) { var option = SocketOptionBuilder.Instance .SetSocket() .UseIocp() .SetIP(config.IP) .SetPort(config.Port) .Build(); _clientSocket = SocketFactory.CreateClientSocket(option); _clientSocket.OnError += _clientSocket_OnError; _clientSocket.OnReceive += _clientSocket_OnReceive; _clientSocket.OnDisconnected += _clientSocket_OnDisconnected; }
public TransferClient(IPEndPoint endPoint) { var bContext = new BaseContext(); _udpClient = SocketFactory.CreateClientSocket(SocketOptionBuilder.Instance.SetSocket(SAEASocketType.Udp) .SetIPEndPoint(endPoint) .UseIocp(bContext) .SetReadBufferSize(SocketOption.UDPMaxLength) .SetWriteBufferSize(SocketOption.UDPMaxLength) .Build()); _baseUnpacker = (BaseUnpacker)bContext.Unpacker; _udpClient.OnReceive += _udpClient_OnReceive; }
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; } }
/// <summary> /// 编码处理 /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <IResponse> Code(IRequest request, CancellationToken cancellationToken = default(CancellationToken)) { var socketOption = SocketOptionBuilder.Instance .UseStream() .SetIP(dns.Address.ToString()) .SetPort(dns.Port) .Build(); using (var socket = SocketFactory.CreateClientSocket(socketOption, cancellationToken)) { socket.Connect(); Stream stream = socket.GetStream(); byte[] buffer = request.ToArray(); byte[] length = BitConverter.GetBytes((ushort)buffer.Length); if (BitConverter.IsLittleEndian) { Array.Reverse(length); } await stream.WriteAsync(length, 0, length.Length, cancellationToken); await stream.WriteAsync(buffer, 0, buffer.Length, cancellationToken); buffer = new byte[2]; await Read(stream, buffer, cancellationToken); if (BitConverter.IsLittleEndian) { Array.Reverse(buffer); } buffer = new byte[BitConverter.ToUInt16(buffer, 0)]; await Read(stream, buffer, cancellationToken); IResponse response = Protocol.DnsResponseMessage.FromArray(buffer); return(new Model.DnsClientResponse(request, response, buffer)); } }
public EventSocketClient(string ipAddress, int port) { _context = new Context(); ISocketOption option = SocketOptionBuilder.Instance.UseIocp(_context) .SetIP(ipAddress) .SetPort(port) .Build(); _client = SocketFactory.CreateClientSocket(option); // _client = new Core.Tcp.IocpClientSocket(option); _client.OnReceive += _client_OnReceive; _client.OnError += _client_OnError; _client.OnDisconnected += _client_OnDisconnected; sendAliveTimer = new System.Timers.Timer(30000); sendAliveTimer.Elapsed += SendAliveTimer_Elapsed; sendAliveTimer.AutoReset = true; }
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); }
public UDPClient() { //udpclient var bContext = new BaseContext(); _udpClient = SocketFactory.CreateClientSocket(SocketOptionBuilder.Instance.SetSocket(SAEASocketType.Udp) .SetIP("127.0.0.1") .SetPort(39656) .UseIocp(bContext) .SetReadBufferSize(SocketOption.UDPMaxLength) .SetWriteBufferSize(SocketOption.UDPMaxLength) .Build()); _udpClient.OnDisconnected += UdpClient_OnDisconnected; _udpClient.OnReceive += UdpClient_OnReceive; _udpClient.OnError += UdpClient_OnError; _baseUnpacker = (BaseUnpacker)bContext.Unpacker; }
public WSClient(string ip = "127.0.0.1", int port = 39654, int bufferSize = 10 * 1024) { _serverIP = ip; _serverPort = port; _bufferSize = bufferSize; _buffer = new byte[_bufferSize]; _wsContext = new WSContext(); var option = SocketOptionBuilder.Instance .UseIocp(_wsContext) .SetIP(ip) .SetCount(port) .SetReadBufferSize(bufferSize) .SetReadBufferSize(bufferSize) .Build(); _client = SocketFactory.CreateClientSocket(option); _client.OnReceive += _client_OnReceive; _client.OnDisconnected += WSClient_OnDisconnected; _client.OnError += WSClient_OnError; }
/// <summary> /// Http2Client /// </summary> /// <param name="host"></param> /// <param name="ip"></param> /// <param name="scheme"></param> /// <param name="path"></param> /// <param name="authority"></param> /// <param name="useHttp1Upgrade"></param> /// <param name="size"></param> /// <param name="timeOut"></param> public Http2Client(string host, int ip, string scheme, string path, string authority, bool useHttp1Upgrade = false, int size = 1024, int timeOut = 180 * 1000) { _path = path; _authority = authority; var options = SocketOptionBuilder.Instance.SetSocket(Sockets.Model.SAEASocketType.Tcp) .UseStream() .SetIP(host) .SetPort(ip) .SetReadBufferSize(size) .SetWriteBufferSize(size) .SetTimeOut(timeOut) .Build(); _clientSocket = SocketFactory.CreateClientSocket(options); _clientSocket.ConnectAsync().GetAwaiter(); var config = new ConnectionConfigurationBuilder(false) .UseSettings(Settings.Default) .UseHuffmanStrategy(HuffmanStrategy.IfSmaller) .Build(); var wrappedStreams = _clientSocket.Socket.CreateStreams(); if (useHttp1Upgrade) { var upgrade = new ClientUpgradeRequestBuilder() .SetHttp2Settings(config.Settings) .Build(); var upgradeReadStream = new UpgradeReadStream(wrappedStreams.ReadableStream); var needExplicitStreamClose = true; try { var upgradeHeader = "OPTIONS / HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: Upgrade, HTTP2-Settings\r\n" + "Upgrade: h2c\r\n" + "HTTP2-Settings: " + upgrade.Base64EncodedSettings + "\r\n\r\n"; var encodedHeader = Encoding.ASCII.GetBytes(upgradeHeader); wrappedStreams.WriteableStream.WriteAsync(new ArraySegment <byte>(encodedHeader)).GetAwaiter(); upgradeReadStream.WaitForHttpHeader().GetAwaiter(); var headerBytes = upgradeReadStream.HeaderBytes; upgradeReadStream.ConsumeHttpHeader(); var response = Http1Response.ParseFrom(Encoding.ASCII.GetString(headerBytes.Array, headerBytes.Offset, headerBytes.Count - 4)); if (response.StatusCode != "101") { throw new Exception("升级失败"); } if (!response.Headers.Any(hf => hf.Key == "connection" && hf.Value == "Upgrade") || !response.Headers.Any(hf => hf.Key == "upgrade" && hf.Value == "h2c")) { throw new Exception("升级失败"); } needExplicitStreamClose = false; var conn = new Connection(config, upgradeReadStream, wrappedStreams.WriteableStream, options: new Connection.Options { ClientUpgradeRequest = upgrade, }); var upgradeStream = upgrade.UpgradeRequestStream.GetAwaiter().GetResult(); upgradeStream.Cancel(); _conn = conn; } finally { if (needExplicitStreamClose) { wrappedStreams.WriteableStream.CloseAsync(); } } } else { _conn = new Connection(config, wrappedStreams.ReadableStream, wrappedStreams.WriteableStream, options: new Connection.Options()); } }