/// <summary> /// Asynchronously connects to Rserve identified by an IP address. /// </summary> /// <param name="addr"> /// Address of the Rserve server, or nothing for localhost /// </param> /// <param name="port"> /// Port on which the server listens /// </param> /// <param name="credentials"> /// Credentials for authentication or <c>null</c> for anonymous /// </param> /// <param name="timeout"> /// Optional timeout, defaults to one second /// </param> public static async Task <RConnection> ConnectAsync( IPAddress addr = null, int port = 6311, NetworkCredential credentials = null, TimeSpan?timeout = null) { var ipe = new IPEndPoint(addr ?? new IPAddress(new byte[] { 127, 0, 0, 1 }), port); Socket socket = null; try { socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Connect with timeout // From http://stackoverflow.com/questions/1062035/how-to-config-socket-connect-timeout-in-c-sharp var connectTask = socket.ConnectAsync(ipe); var timeoutTask = Task.Delay(timeout ?? TimeSpan.FromSeconds(1)); var completedTask = await Task.WhenAny(connectTask, timeoutTask).ContinueContextFree(); if (completedTask == timeoutTask) { connectTask.IgnoreFault(); await timeoutTask.ContinueContextFree(); throw new SocketException((int)SocketError.TimedOut); } await connectTask.ContinueContextFree(); if (!socket.Connected) { return(null); } var connection = new RConnection(ref socket); await connection.InitAsync(credentials?.UserName, credentials?.Password).ContinueContextFree(); return(connection); } finally { socket?.Dispose(); } }
/// <summary> /// Asynchronously connects to Rserve identified by an IP address. /// </summary> /// <param name="addr"> /// Address of the Rserve server, or nothing for localhost /// </param> /// <param name="port"> /// Port on which the server listens /// </param> /// <param name="credentials"> /// Credentials for authentication or <c>null</c> for anonymous /// </param> public static async Task<RConnection> ConnectAsync(IPAddress addr = null, int port = 6311, NetworkCredential credentials = null) { var ipe = new IPEndPoint(addr ?? new IPAddress(new byte[] { 127, 0, 0, 1 }), port); Socket socket = null; try { socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Connect with a one-second timeout // From http://stackoverflow.com/questions/1062035/how-to-config-socket-connect-timeout-in-c-sharp var connectTask = socket.ConnectAsync(ipe); var timeoutTask = Task.Delay(TimeSpan.FromSeconds(1)); var completedTask = await Task.WhenAny(connectTask, timeoutTask) .ContinueContextFree(); if (completedTask == timeoutTask) { connectTask.IgnoreFault(); await timeoutTask.ContinueContextFree(); throw new SocketException((int)SocketError.TimedOut); } await connectTask.ContinueContextFree(); if (!socket.Connected) return null; var connection = new RConnection(ref socket); await connection.InitAsync(credentials?.UserName, credentials?.Password).ContinueContextFree(); return connection; } finally { socket?.Dispose(); } }
RConnection(RConnection connection) : this(ref connection._socket) { _protocol = connection._protocol; _connectionParameters = connection._connectionParameters; }