Class FTP#
Defined in File FTP.h
Nested Relationships#
Nested Types#
Class Documentation#
-
class FTP#
A FTP client.
Note
Only supports IPv4.
Public Types
Public Functions
-
FTP() noexcept = default#
Constructor.
-
~FTP()#
Automatically closes the connection with the server if it is still opened.
-
Response Connect(const IPv4Address &server, u16 port = 21, Utils::TimeStep timeout = Utils::TimeStep(0.0f))#
Connect to the specified FTP server.
The port has a default value of 21, which is the standard port used by the FTP protocol. You shouldn’t use a different value, unless you really know what you do. This function tries to connect to the server so it may take a while to complete, especially if the server is not reachable. To avoid blocking your application for too long, you can use a timeout. The default value, Time::Zero, means that the system timeout will be used (which is usually pretty long).
- Parameters:
server – Name or address of the FTP server to connect to.
port – Port used for the connection.
timeout – Maximum time to wait.
- Returns:
Server response to the request.
-
Response Disconnect()#
Close the connection with the server.
- Returns:
Server response to the request.
-
Response Login()#
Log in using an anonymous account.
Logging in is mandatory after connecting to the server. Users that are not logged in cannot perform any operation.
- Returns:
Server response to the request.
-
Response Login(const std::string &name, const std::string &password)#
Log in using a username and a password.
Logging in is mandatory after connecting to the server. Users that are not logged in cannot perform any operation.
- Parameters:
name – User name.
password – Password.
- Returns:
Server response to the request.
-
Response KeepAlive()#
Send a null command to keep the connection alive.
This command is useful because the server may close the connection automatically if no command is sent.
- Returns:
Server response to the request.
-
DirectoryResponse GetWorkingDirectory()#
Get the current working directory.
The working directory is the root path for subsequent operations involving directories and/or filenames.
- Returns:
Server response to the request.
-
ListingResponse GetDirectoryListing(const std::filesystem::path &directory = "")#
Get the contents of the given directory.
This function retrieves the sub-directories and files contained in the given directory. It is not recursive. The directory parameter is relative to the current working directory.
- Parameters:
directory – Directory to list.
- Returns:
Server response to the request.
-
Response ChangeDirectory(const std::filesystem::path &directory)#
Change the current working directory.
The new directory must be relative to the current one.
- Parameters:
directory – New working directory.
- Returns:
Server response to the request.
-
Response ParentDirectory()#
Go to the parent directory of the current one.
- Returns:
Server response to the request.
-
Response CreateDirectory(const std::filesystem::path &name)#
Create a new directory.
The new directory is created as a child of the current working directory.
- Parameters:
name – Name of the directory to create.
- Returns:
Server response to the request.
-
Response DeleteDirectory(const std::filesystem::path &name)#
Remove an existing directory.
The directory to remove must be relative to the current working directory. Use this function with caution, the directory will be removed permanently!
- Parameters:
name – Name of the directory to remove.
- Returns:
Server response to the request.
-
Response RenameFile(const std::filesystem::path &file, const std::filesystem::path &newName)#
Rename an existing file.
The filenames must be relative to the current working directory.
- Parameters:
file – File to rename.
newName – New name of the file.
- Returns:
Server response to the request.
-
Response DeleteFile(const std::filesystem::path &name)#
Remove an existing file.
The file name must be relative to the current working directory. Use this function with caution, the file will be removed permanently!
- Parameters:
name – File to remove.
- Returns:
Server response to the request.
-
Response Download(const std::filesystem::path &remoteFile, const std::filesystem::path &path, TransferMode mode = TransferMode::Binary)#
Download a file from the server.
The filename of the distant file is relative to the current working directory of the server, and the local destination path is relative to the current directory of you application. If a file with the same filename as the distant file already exists in the local destination path, it will be overwritten.
- Parameters:
remoteFile – Filename of the distant file to download.
path – The directory in which to put the file on the local computer.
mode – Transfer mode.
- Returns:
Server response to the request.
-
Response Upload(const std::filesystem::path &localFile, const std::filesystem::path &remotePath, TransferMode mode = TransferMode::Binary, bool append = false)#
Upload a file to the server.
The name of the local file is relative to the current working directory of your application, and the remote path is relative to the current directory of the FTP server.
The append parameter controls whether the remote file is appended to or overwritten if it already exists.
- Parameters:
localFile – Path of the local file to upload.
remotePath – The directory in which to put the file on the server.
mode – Transfer mode.
append – Pass true to append to or false to overwrite the remote file if it already exists.
- Returns:
Server response to the request.
-
Response SendCommand(const std::string &command, const std::string ¶meter = "")#
Send a command to the FTP server.
While the most often used commands are provided as member functions in the TRAP::Network::FTP class, this method can be used to send any FTP command to the server. If the command requires one or more parameters, they can be specified in parameter. If the server returns information, you can extract it from the response using TRAP::Network::FTP::Response::GetMessage().
- Parameters:
command – Command to send.
parameter – Command parameter.
- Returns:
Server response to the request.
-
class DirectoryResponse : public TRAP::Network::FTP::Response#
Specialization of FTP response returning a directory.
Public Functions
-
explicit DirectoryResponse(const Response &response)#
Constructor.
- Parameters:
response – Source response.
-
DirectoryResponse(const DirectoryResponse&) = default#
Copy constructor.
-
DirectoryResponse &operator=(const DirectoryResponse&) = default#
Copy assignment operator.
-
DirectoryResponse(DirectoryResponse&&) noexcept = default#
Move constructor.
-
DirectoryResponse &operator=(DirectoryResponse&&) noexcept = default#
Move assignment operator.
-
~DirectoryResponse() = default#
Destructor.
-
const std::filesystem::path &GetDirectory() const noexcept#
Get the directory returned in the response.
- Returns:
Directory name.
-
explicit DirectoryResponse(const Response &response)#
-
class ListingResponse : public TRAP::Network::FTP::Response#
Specialization of FTP response returning a filename listing.
Public Functions
-
constexpr ListingResponse(const Response &response, std::string_view data)#
Constructor.
- Parameters:
response – Source response.
data – Data containing the raw listing.
-
ListingResponse(const ListingResponse&) = default#
Copy constructor.
-
ListingResponse &operator=(const ListingResponse&) = default#
Copy assignment operator.
-
ListingResponse(ListingResponse&&) noexcept = default#
Move constructor.
-
ListingResponse &operator=(ListingResponse&&) noexcept = default#
Move assignment operator.
-
~ListingResponse() = default#
Destructor.
-
constexpr const std::vector<std::filesystem::path> &GetListing() const noexcept#
Return the array of directory/file names.
- Returns:
Array containing the requested listing.
-
constexpr ListingResponse(const Response &response, std::string_view data)#
-
class Response#
Subclassed by TRAP::Network::FTP::DirectoryResponse, TRAP::Network::FTP::ListingResponse
Public Types
-
enum class Status#
Status codes possibly returned by a FTP response.
Values:
-
enumerator RestartMarkerReply#
-
enumerator ServiceReadySoon#
-
enumerator DataConnectionAlreadyOpened#
-
enumerator OpeningDataConnection#
-
enumerator OK#
-
enumerator PointlessCommand#
-
enumerator SystemStatus#
-
enumerator DirectoryStatus#
-
enumerator FileStatus#
-
enumerator HelpMessage#
-
enumerator SystemType#
-
enumerator ServiceReady#
-
enumerator ClosingConnection#
-
enumerator DataConnectionOpened#
-
enumerator ClosingDataConnection#
-
enumerator EnteringPassiveMode#
-
enumerator LoggedIn#
-
enumerator FileActionOK#
-
enumerator DirectoryOK#
-
enumerator NeedPassword#
-
enumerator NeedAccountToLogIn#
-
enumerator NeedInformation#
-
enumerator TransferAborted#
-
enumerator FileActionAborted#
-
enumerator LocalError#
-
enumerator InsufficientStorageSpace#
-
enumerator CommandUnknown#
-
enumerator ParametersUnknown#
-
enumerator CommandNotImplemented#
-
enumerator BadCommandSequence#
-
enumerator ParameterNotImplemented#
-
enumerator NotLoggedIn#
-
enumerator NeedAccountToStore#
-
enumerator PageTypeUnknown#
-
enumerator NotEnoughMemory#
-
enumerator FilenameNotAllowed#
-
enumerator InvalidResponse#
-
enumerator ConnectionFailed#
-
enumerator ConnectionClosed#
-
enumerator InvalidFile#
-
enumerator RestartMarkerReply#
Public Functions
-
explicit constexpr Response(Status code = Status::InvalidResponse, std::string message = "") noexcept#
This constructor is used by the FTP client to build the response.
-
constexpr ~Response() = default#
Destructor.
-
constexpr bool IsOK() const noexcept#
Check if the status code means a success.
This function is defined for convenience, it is equivalent to testing if the status code is < 400.
- Returns:
True if the status is a success, false if it is a failure.
-
constexpr Status GetStatus() const noexcept#
Get the status code of the response.
- Returns:
Status code.
-
constexpr std::string GetMessage() const noexcept#
Get the full message contained in the response.
- Returns:
The response message.
-
enum class Status#
-
FTP() noexcept = default#