gftp/stream
The stream module provides an interface for working with a network stream, which can either be a TCP stream or a TLS stream. It abstracts away the differences between the two types of streams and provides a unified interface for reading and writing data.
Types
Data Stream used for communications. It can be both of type Tcp in case of plain communication or Ssl in case of FTPS
pub type DataStream {
Ssl(ssl: kafein.SslSocket, tcp: mug.Socket)
Tcp(socket: mug.Socket)
}
Constructors
-
Ssl(ssl: kafein.SslSocket, tcp: mug.Socket)Contains the SSL socket and the underlying TCP socket. The TCP socket is necessary in case we want to switch back to a plain TCP stream.
-
Tcp(socket: mug.Socket)
Values
pub fn downgrade_to_tcp(stream: DataStream) -> DataStream
Downgrades an SSL stream to a TCP stream. If the stream is already a TCP stream, it returns it as is.
pub fn local_address(
stream: DataStream,
) -> Result(#(String, Int), mug.Error)
Retrieves the local address (IP and port) of the underlying TCP socket.
pub fn peer_address(
stream: DataStream,
) -> Result(#(String, Int), mug.Error)
Retrieves the peer address (IP and port) of the underlying TCP socket.
pub fn receive(
stream: DataStream,
timeout: Int,
) -> Result(BitArray, mug.Error)
Receive a message from the peer.
Errors if the socket is closed, if the timeout is reached, or if any other error occurs during the receive operation.
The error is returned as a [mug.Error]. In case of success, it returns the received data as a BitArray.
pub fn receive_exact(
stream: DataStream,
size: Int,
timeout: Int,
) -> Result(BitArray, mug.Error)
Receive an exact number of bytes from the peer.
Errors if the socket is closed, if the timeout is reached, or if any other error occurs during the receive operation.
The error is returned as a [mug.Error]. In case of success, it returns the received data as a BitArray.
pub fn send(
stream: DataStream,
data: BitArray,
) -> Result(Nil, mug.Error)
Sends data over the provided stream. It handles both SSL and TCP streams, abstracting away the differences between them.
Returns a Result indicating success (Nil) or an error ([mug.Error]) if the send operation fails.
pub fn set_line_mode(stream: DataStream) -> Nil
Set the socket to line-delimited packet mode ({packet, line}). In this mode, recv returns one complete line per call.
pub fn set_raw_mode(stream: DataStream) -> Nil
Set the socket back to raw packet mode ({packet, raw}). In this mode, recv returns whatever data is available.
pub fn shutdown(stream: DataStream) -> Result(Nil, mug.Error)
Shuts down the provided stream, whether it’s an SSL or TCP stream. It abstracts away the differences between the two types of streams and provides a unified interface for shutting them down.
pub fn upgrade_to_ssl(
stream: DataStream,
options: kafein.WrapOptions,
) -> Result(DataStream, kafein.Error)
Upgrades a TCP stream to an SSL stream using the provided options. If the stream is already an SSL stream, it returns it as is.