/// <summary> /// 连接 /// </summary> /// <param name="endPoint"></param> /// <returns></returns> public async Task <bool> ConnectAsync(EndPoint endPoint) { bool r = await channel.ConnectAsync(endPoint); if (r) { StartRecvice(); } return(r); }
public async Task ConnectAsync(Uri uri) { if (uri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase)) { _channel = new SecureSocketChannel(); await _channel.ConnectAsync(uri.Host, uri.Port == 0? 443 : uri.Port); } else { _channel = new SocketChannel(); await _channel.ConnectAsync(uri.Host, uri.Port == 0? 443 : uri.Port); } _encodingContext = new EncodingContext(_channel); _encoder = new FrameEncoder(_encodingContext); Buffer.BlockCopy(ClientPreface, 0, _encodingContext.Buffer, _encodingContext.Offset, ClientPreface.Length); _encodingContext.Offset += ClientPreface.Length; var settingsFrame = new SettingsFrame(); await _encoder.EncodeAsync(settingsFrame); if (_encodingContext.ContainsData) { await _encodingContext.SendAsync(); } var ackOnOurFrame = await _decoder.DecodeAsync(_decodingContext) as SettingsFrame; if (ackOnOurFrame == null) { //TODO: Protocol error } var serverSettings = await _decoder.DecodeAsync(_decodingContext) as SettingsFrame; if (serverSettings == null) { //TODO: protocol error } }
public async Task ConnectAsync(Uri uri) { int port = uri.Port; if (uri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase)) { _socket = new SecureSocketChannel(); if (port == 0) { port = uri.Port == 0 ? 443 : uri.Port; } } else { _socket = new SocketChannel(); if (port == 0) { port = uri.Port == 0 ? 80 : uri.Port; } } await _socket.ConnectAsync(uri.Host, port); _context = new EncodingContext(_socket); _encoder = new FrameEncoder(_context); var http2Settings = new SettingsFrame(); http2Settings.Encode(1, new EncodingContext()); var handshake = string.Format(@"GET / HTTP/1.1 Host: {0} Connection: Upgrade, HTTP2-Settings Upgrade: h2c HTTP2-Settings: {1} ", uri.Host, http2Settings); }