/// <summary> /// Initializes a new instance of the <see cref="NetworkSession"/> class. /// </summary> /// <param name="socket">The socket to wrap.</param> protected NetworkSession(Socket socket) : this() { if (SocketType.Stream == socket.SocketType) { _socket = socket; } else { _sslSocket = new SSLSocketWrapper(socket); } }
/// <summary> /// Connects the session to the given host:port using the given socket properties. /// </summary> /// <param name="host">The host to connect to (hostname or IP address).</param> /// <param name="port">The port to connect to.</param> /// <param name="socketType">Type of the socket.</param> /// <param name="protocolType">Type of the socket protocol.</param> /// <param name="useIPv6">if set to <c>true</c> use IPv6, otherwise use IPv4.</param> public async Task ConnectAsync(string host, int port, SocketType socketType, ProtocolType protocolType, bool useIPv6) { // TODO: disconnect first? Logger.Info($"Session {Id} connecting to {host}:{port}..."); try { _socketConnecting = true; Socket socket = await NetUtil.ConnectAsync(host, port, socketType, protocolType, useIPv6).ConfigureAwait(false); if (null == socket) { await ErrorAsync($"Could not connect to {host}:{port}!").ConfigureAwait(false); return; } if (SocketType.Stream == socketType) { _sslSocket = new SSLSocketWrapper(socket); } else { _socket = socket; } } finally { _socketConnecting = false; } if (IsConnected) { ConnectedEvent?.Invoke(this, new ConnectedEventArgs()); } else { // TODO: error? } }