Class FtpSocketStream
- Namespace
- FluentFTP
- Assembly
- FluentFTP.dll
Stream class used for talking. Used by FtpClient, extended by FtpDataStream
public class FtpSocketStream : Stream, IDisposable, IAsyncDisposable
- Inheritance
-
FtpSocketStream
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
FtpSocketStream(BaseFtpClient)
This stream connection to be associated with this client
public FtpSocketStream(BaseFtpClient conn)
Parameters
conn
BaseFtpClient
Fields
Client
The client this stream is associated with
public readonly BaseFtpClient Client
Field Value
SslSessionLength
Keep a count of command/response transactions on the control connection
public int SslSessionLength
Field Value
Properties
BaseStream
Gets the underlying stream, could be a NetworkStream, SslStream or CustomStream
protected Stream BaseStream { get; }
Property Value
CanRead
Gets a value indicating if this stream can be read
public override bool CanRead { get; }
Property Value
CanSeek
Gets a value indicating if this stream if seekable
public override bool CanSeek { get; }
Property Value
CanWrite
Gets a value indicating if this stream can be written to
public override bool CanWrite { get; }
Property Value
ConnectTimeout
Gets or sets the length of time milliseconds to wait for a connection succeed before giving up. The default is 0 = disable, use system default timeout.
public int ConnectTimeout { get; set; }
Property Value
IsConnected
Gets a value indicating if this socket stream is connected
public bool IsConnected { get; }
Property Value
IsControlConnection
Is this stream the control connection?
public bool IsControlConnection { get; set; }
Property Value
IsDisposed
Gets a value indicating if this socket stream is disposed
public bool IsDisposed { get; set; }
Property Value
IsEncrypted
Gets a value indicating if encryption is being used
public bool IsEncrypted { get; }
Property Value
Length
Gets the length of the stream
public override long Length { get; }
Property Value
LocalEndPoint
Gets the local end point of the socket
public IPEndPoint LocalEndPoint { get; }
Property Value
Position
Gets the current position of the stream. Trying to set this property throws an InvalidOperationException()
public override long Position { get; set; }
Property Value
ReadTimeout
Gets or sets the amount of time to wait for a read operation to complete. Default value is Timeout.Infinite.
public override int ReadTimeout { get; set; }
Property Value
RemoteEndPoint
Gets the remote end point of the socket
public IPEndPoint RemoteEndPoint { get; }
Property Value
Socket
The socket used for talking
protected Socket Socket { get; }
Property Value
SocketPollInterval
Gets or sets the length of time in milliseconds that must pass since the last socket activity before calling Poll() on the socket to test for connectivity. Setting this interval too low will have a negative impact on performance. Setting this interval to 0 disables Poll()'ing all together. The default value is 15 seconds.
public int SocketPollInterval { get; set; }
Property Value
SslProtocolActive
The negotiated SSL/TLS protocol version. Will have a valid value after connection is complete.
public SslProtocols SslProtocolActive { get; }
Property Value
Methods
Accept()
Accepts a connection from a listening socket
public void Accept()
AcceptAsync(CancellationToken)
Accepts a connection from a listening socket
public Task AcceptAsync(CancellationToken token = default)
Parameters
token
CancellationToken
Returns
ActivateEncryption(string, X509CertificateCollection, SslProtocols)
Activates SSL on this stream using the specified protocols. Fires the ValidateCertificate event. If this event is not handled and there are SslPolicyErrors present, the certificate will not be accepted.
public void ActivateEncryption(string targetHost, X509CertificateCollection clientCerts, SslProtocols sslProtocols)
Parameters
targetHost
stringThe host to authenticate the certificate against
clientCerts
X509CertificateCollectionA collection of client certificates to use when authenticating the SSL stream
sslProtocols
SslProtocolsA bitwise parameter for supported encryption protocols.
Exceptions
- AuthenticationException
Thrown when authentication fails
ActivateEncryptionAsync(string, X509CertificateCollection, SslProtocols, CancellationToken)
Activates SSL on this stream using the specified protocols. Fires the ValidateCertificate event. If this event is not handled and there are SslPolicyErrors present, the certificate will not be accepted.
public Task ActivateEncryptionAsync(string targetHost, X509CertificateCollection clientCerts, SslProtocols sslProtocols, CancellationToken token = default)
Parameters
targetHost
stringThe host to authenticate the certificate against
clientCerts
X509CertificateCollectionA collection of client certificates to use when authenticating the SSL stream
sslProtocols
SslProtocolsA bitwise parameter for supported encryption protocols.
token
CancellationTokenThe token that can be used to cancel the entire process
Returns
Exceptions
- AuthenticationException
Thrown when authentication fails
Close()
Disconnects from server
public override void Close()
CloseAsync(CancellationToken)
CloseAsync, disconnects from server
public virtual ValueTask CloseAsync(CancellationToken token = default)
Parameters
token
CancellationToken
Returns
Connect(string, int, FtpIpVersion)
Connect to the specified host
public void Connect(string host, int port, FtpIpVersion ipVersions)
Parameters
host
stringThe host to connect to
port
intThe port to connect to
ipVersions
FtpIpVersionInternet Protocol versions to support during the connection phase
ConnectAsync(string, int, FtpIpVersion, CancellationToken)
Connect to the specified host
public Task ConnectAsync(string host, int port, FtpIpVersion ipVersions, CancellationToken token)
Parameters
host
stringThe host to connect to
port
intThe port to connect to
ipVersions
FtpIpVersionInternet Protocol versions to support during the connection phase
token
CancellationTokenThe token that can be used to cancel the entire process
Returns
Dispose()
Disconnects from server
protected void Dispose()
DisposeAsync()
DisposeAsync, disconnects from server
public override ValueTask DisposeAsync()
Returns
DisposeAsyncCore()
Disconnects from server, actually
protected ValueTask DisposeAsyncCore()
Returns
Flush()
Flushes the stream
public override void Flush()
FlushAsync(CancellationToken)
Flushes the stream asynchronously
public override Task FlushAsync(CancellationToken token)
Parameters
token
CancellationTokenThe CancellationToken for this task
Returns
Listen(IPAddress, int)
Instructs this stream to listen for connections on the specified address and port
public void Listen(IPAddress address, int port)
Parameters
OnValidateCertificate(X509Certificate, X509Chain, SslPolicyErrors)
Fires the SSL certificate validation event
protected bool OnValidateCertificate(X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
Parameters
certificate
X509CertificateCertificate being validated
chain
X509ChainCertificate chain
errors
SslPolicyErrorsPolicy errors if any
Returns
- bool
True if it was accepted, false otherwise
OnValidateCertificate(X509Certificate, X509Chain, string)
Fires the SSL certificate validation event for custom encrypted streams
protected bool OnValidateCertificate(X509Certificate certificate, X509Chain chain, string errorMessage)
Parameters
certificate
X509CertificateCertificate being validated
chain
X509ChainCertificate chain
errorMessage
stringPolicy errors if any
Returns
- bool
True if it was accepted, false otherwise
Read(byte[], int, int)
Reads data from the stream
public override int Read(byte[] buffer, int offset, int count)
Parameters
buffer
byte[]Buffer to read into
offset
intWhere in the buffer to start
count
intNumber of bytes to be read
Returns
- int
The amount of bytes read from the stream
Read(Span<byte>)
Reads data from the stream
public override int Read(Span<byte> buffer)
Parameters
Returns
- int
The amount of bytes read from the stream
ReadAllLines(Encoding, int)
Reads all lines from the socket
public IEnumerable<string> ReadAllLines(Encoding encoding, int bufferSize)
Parameters
encoding
EncodingThe type of encoding used to convert from byte[] to string
bufferSize
intThe size of the buffer
Returns
- IEnumerable<string>
A list of lines from the stream
ReadAllLinesAsync(Encoding, int, CancellationToken)
Reads all lines from the socket
public Task<IEnumerable<string>> ReadAllLinesAsync(Encoding encoding, int bufferSize, CancellationToken token)
Parameters
encoding
EncodingThe type of encoding used to convert from byte[] to string
bufferSize
intThe size of the buffer
token
CancellationTokenThe token that can be used to cancel the entire process
Returns
- Task<IEnumerable<string>>
A list of lines from the stream
ReadAsync(byte[], int, int, CancellationToken)
Reads data from the stream
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken token)
Parameters
buffer
byte[]Buffer to read into
offset
intWhere in the buffer to start
count
intNumber of bytes to be read
token
CancellationTokenThe CancellationToken for this task
Returns
ReadAsync(Memory<byte>, CancellationToken)
Reads data from the stream
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken token)
Parameters
buffer
Memory<byte>Buffer to read into
token
CancellationTokenThe CancellationToken for this task
Returns
ReadLine(Encoding)
Reads a line from the socket
public string ReadLine(Encoding encoding)
Parameters
encoding
EncodingThe type of encoding used to convert from byte[] to string
Returns
- string
A line from the stream, null if there is nothing to read
ReadLineAsync(Encoding, CancellationToken)
Reads a line from the socket asynchronously
public Task<string> ReadLineAsync(Encoding encoding, CancellationToken token)
Parameters
encoding
EncodingThe type of encoding used to convert from byte[] to string
token
CancellationTokenThe CancellationToken for this task
Returns
Seek(long, SeekOrigin)
Throws an InvalidOperationException
public override long Seek(long offset, SeekOrigin origin)
Parameters
offset
longIgnored
origin
SeekOriginIgnored
Returns
SetLength(long)
Throws an InvalidOperationException
public override void SetLength(long value)
Parameters
value
longIgnored
SetSocketOption(SocketOptionLevel, SocketOptionName, bool)
Sets socket options on the underlying socket
public void SetSocketOption(SocketOptionLevel level, SocketOptionName name, bool value)
Parameters
level
SocketOptionLevelSocketOptionLevel
name
SocketOptionNameSocketOptionName
value
boolSocketOptionValue
Write(byte[], int, int)
Writes data to the stream
public override void Write(byte[] buffer, int offset, int count)
Parameters
buffer
byte[]Buffer to write to stream
offset
intWhere in the buffer to start
count
intNumber of bytes to be written
Write(ReadOnlySpan<byte>)
Writes data to the stream
public override void Write(ReadOnlySpan<byte> buffer)
Parameters
buffer
ReadOnlySpan<byte>Buffer to write to stream
WriteAsync(byte[], int, int, CancellationToken)
Writes data to the stream asynchronously
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken token)
Parameters
buffer
byte[]Buffer to write to stream
offset
intWhere in the buffer to start
count
intNumber of bytes to be written
token
CancellationTokenThe CancellationToken for this task
Returns
WriteAsync(ReadOnlyMemory<byte>, CancellationToken)
Writes data to the stream asynchronously
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken token)
Parameters
buffer
ReadOnlyMemory<byte>Buffer to write to stream
token
CancellationTokenThe CancellationToken for this task
Returns
WriteLine(Encoding, string)
Writes a line to the stream using the specified encoding
public void WriteLine(Encoding encoding, string buf)
Parameters
WriteLineAsync(Encoding, string, CancellationToken)
Writes a line to the stream using the specified encoding asynchronously
public Task WriteLineAsync(Encoding encoding, string buf, CancellationToken token)
Parameters
encoding
EncodingEncoding used for writing the line
buf
stringThe data to write
token
CancellationTokenThe CancellationToken for this task
Returns
Events
ValidateCertificate
Event is fired when a SSL certificate needs to be validated
public event FtpSocketStreamSslValidation ValidateCertificate