SFML - Simple and Fast Multimedia Library

SFML 3.1.0
Loading...
Searching...
No Matches
Ftp.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2026 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
30#include <SFML/Network/Export.hpp>
31
32#include <SFML/Network/TcpSocket.hpp>
33
34#include <SFML/System/Time.hpp>
35
36#include <filesystem>
37#include <string>
38#include <vector>
39
40
41namespace sf
42{
43class IpAddress;
44
51 class SFML_NETWORK_API [[deprecated("Use sf::Sftp if possible")]] Ftp
52{
53public:
58 enum class TransferMode
59 {
60 Binary,
61 Ascii,
62 Ebcdic
63 };
64
69 class SFML_NETWORK_API Response
70 {
71 public:
76 enum class Status
77 {
78 // 1xx: the requested action is being initiated,
79 // expect another reply before proceeding with a new command
80 RestartMarkerReply = 110,
81 ServiceReadySoon = 120,
82 DataConnectionAlreadyOpened = 125,
83 OpeningDataConnection = 150,
84
85 // 2xx: the requested action has been successfully completed
86 Ok = 200,
87 PointlessCommand = 202,
88 SystemStatus = 211,
89 DirectoryStatus = 212,
90 FileStatus = 213,
91 HelpMessage = 214,
92 SystemType = 215,
93 ServiceReady = 220,
94 ClosingConnection = 221,
95 DataConnectionOpened = 225,
96 ClosingDataConnection = 226,
97 EnteringPassiveMode = 227,
98 LoggedIn = 230,
99 FileActionOk = 250,
100 DirectoryOk = 257,
101
102 // 3xx: the command has been accepted, but the requested action
103 // is dormant, pending receipt of further information
104 NeedPassword = 331,
105 NeedAccountToLogIn = 332,
106 NeedInformation = 350,
107
108 // 4xx: the command was not accepted and the requested action did not take place,
109 // but the error condition is temporary and the action may be requested again
110 ServiceUnavailable = 421,
111 DataConnectionUnavailable = 425,
112 TransferAborted = 426,
113 FileActionAborted = 450,
114 LocalError = 451,
115 InsufficientStorageSpace = 452,
116
117 // 5xx: the command was not accepted and
118 // the requested action did not take place
119 CommandUnknown = 500,
120 ParametersUnknown = 501,
121 CommandNotImplemented = 502,
122 BadCommandSequence = 503,
123 ParameterNotImplemented = 504,
124 NotLoggedIn = 530,
125 NeedAccountToStore = 532,
126 FileUnavailable = 550,
127 PageTypeUnknown = 551,
128 NotEnoughMemory = 552,
129 FilenameNotAllowed = 553,
130
131 // 10xx: SFML custom codes
132 InvalidResponse = 1000,
133 ConnectionFailed = 1001,
134 ConnectionClosed = 1002,
135 InvalidFile = 1003
136 };
137
148 explicit Response(Status code = Status::InvalidResponse, std::string message = "");
149
159 [[nodiscard]] bool isOk() const;
160
167 [[nodiscard]] Status getStatus() const;
168
175 [[nodiscard]] const std::string& getMessage() const;
176
177 private:
179 // Member data
181 Status m_status;
182 std::string m_message;
183 };
184
189 class SFML_NETWORK_API DirectoryResponse : public Response
190 {
191 public:
198 DirectoryResponse(const Response& response);
199
206 [[nodiscard]] const std::filesystem::path& getDirectory() const;
207
208 private:
210 // Member data
212 std::filesystem::path m_directory;
213 };
214
215
220 class SFML_NETWORK_API ListingResponse : public Response
221 {
222 public:
230 ListingResponse(const Response& response, const std::string& data);
231
238 [[nodiscard]] const std::vector<std::string>& getListing() const;
239
240 private:
242 // Member data
244 std::vector<std::string> m_listing;
245 };
246
251 Ftp() = default;
252
260 ~Ftp();
261
266 Ftp(const Ftp&) = delete;
267
272 Ftp& operator=(const Ftp&) = delete;
273
295 [[nodiscard]] Response connect(IpAddress server, unsigned short port = 21, Time timeout = Time::Zero);
296
305 [[nodiscard]] Response disconnect();
306
316 [[nodiscard]] Response login();
317
330 [[nodiscard]] Response login(const std::string& name, const std::string& password);
331
341 [[nodiscard]] Response keepAlive();
342
354 [[nodiscard]] DirectoryResponse getWorkingDirectory();
355
371 [[nodiscard]] ListingResponse getDirectoryListing(const std::string& directory = "");
372
385 [[nodiscard]] Response changeDirectory(const std::string& directory);
386
395 [[nodiscard]] Response parentDirectory();
396
410 [[nodiscard]] Response createDirectory(const std::string& name);
411
427 [[nodiscard]] Response deleteDirectory(const std::string& name);
428
443 [[nodiscard]] Response renameFile(const std::filesystem::path& file, const std::filesystem::path& newName);
444
460 [[nodiscard]] Response deleteFile(const std::filesystem::path& name);
461
482 [[nodiscard]] Response download(const std::filesystem::path& remoteFile,
483 const std::filesystem::path& localPath,
484 TransferMode mode = TransferMode::Binary);
485
507 [[nodiscard]] Response upload(const std::filesystem::path& localFile,
508 const std::filesystem::path& remotePath,
509 TransferMode mode = TransferMode::Binary,
510 bool append = false);
511
528 [[nodiscard]] Response sendCommand(const std::string& command, const std::string& parameter = "");
529
530private:
540 Response getResponse();
541
547 class DataChannel;
548
549 friend class DataChannel;
550
552 // Member data
554 TcpSocket m_commandSocket;
555 std::string m_receiveBuffer;
556};
557
558} // namespace sf
559
560
Export.hpp
SFML_NETWORK_API
#define SFML_NETWORK_API
Definition Network/Export.hpp:42
TcpSocket.hpp
Time.hpp
sf::Ftp::DirectoryResponse
Specialization of FTP response returning a directory.
Definition Ftp.hpp:190
sf::Ftp::DirectoryResponse::DirectoryResponse
DirectoryResponse(const Response &response)
Default constructor.
sf::Ftp::DirectoryResponse::getDirectory
const std::filesystem::path & getDirectory() const
Get the directory returned in the response.
sf::Ftp::ListingResponse
Specialization of FTP response returning a file name listing.
Definition Ftp.hpp:221
sf::Ftp::ListingResponse::getListing
const std::vector< std::string > & getListing() const
Return the array of directory/file names.
sf::Ftp::ListingResponse::ListingResponse
ListingResponse(const Response &response, const std::string &data)
Default constructor.
sf::Ftp::Response
FTP response.
Definition Ftp.hpp:70
sf::Ftp::Response::isOk
bool isOk() const
Check if the status code means a success.
sf::Ftp::Response::getStatus
Status getStatus() const
Get the status code of the response.
sf::Ftp::Response::Response
Response(Status code=Status::InvalidResponse, std::string message="")
Default constructor.
sf::Ftp::Response::getMessage
const std::string & getMessage() const
Get the full message contained in the response.
sf::Ftp::Response::Status
Status
Status codes possibly returned by a FTP response.
Definition Ftp.hpp:77
sf::Ftp::Response::Status::InvalidResponse
@ InvalidResponse
Not part of the FTP standard, generated by SFML when a received response cannot be parsed.
Definition Ftp.hpp:132
sf::Ftp::TransferMode
TransferMode
Enumeration of transfer modes.
Definition Ftp.hpp:59
sf::Ftp::TransferMode::Binary
@ Binary
Binary mode (file is transferred as a sequence of bytes).
Definition Ftp.hpp:60
sf::Ftp::deleteFile
Response deleteFile(const std::filesystem::path &name)
Remove an existing file.
sf::Ftp::createDirectory
Response createDirectory(const std::string &name)
Create a new directory.
sf::Ftp::deleteDirectory
Response deleteDirectory(const std::string &name)
Remove an existing directory.
sf::Ftp::~Ftp
~Ftp()
Destructor.
sf::Ftp::sendCommand
Response sendCommand(const std::string &command, const std::string &parameter="")
Send a command to the FTP server.
sf::Ftp::connect
Response connect(IpAddress server, unsigned short port=21, Time timeout=Time::Zero)
Connect to the specified FTP server.
sf::Ftp::login
Response login()
Log in using an anonymous account.
sf::Ftp::getWorkingDirectory
DirectoryResponse getWorkingDirectory()
Get the current working directory.
sf::Ftp::changeDirectory
Response changeDirectory(const std::string &directory)
Change the current working directory.
sf::Ftp::DataChannel
friend class DataChannel
Definition Ftp.hpp:549
sf::Ftp::getDirectoryListing
ListingResponse getDirectoryListing(const std::string &directory="")
Get the contents of the given directory.
sf::Ftp::download
Response download(const std::filesystem::path &remoteFile, const std::filesystem::path &localPath, TransferMode mode=TransferMode::Binary)
Download a file from the server.
sf::Ftp::renameFile
Response renameFile(const std::filesystem::path &file, const std::filesystem::path &newName)
Rename an existing file.
sf::Ftp::login
Response login(const std::string &name, const std::string &password)
Log in using a username and a password.
sf::Ftp::keepAlive
Response keepAlive()
Send a null command to keep the connection alive.
sf::Ftp::Ftp
Ftp(const Ftp &)=delete
Deleted copy constructor.
sf::Ftp::Ftp
Ftp()=default
Default constructor.
sf::Ftp::disconnect
Response disconnect()
Close the connection with the server.
sf::Ftp::parentDirectory
Response parentDirectory()
Go to the parent directory of the current one.
sf::Ftp::upload
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.
sf::Ftp::operator=
Ftp & operator=(const Ftp &)=delete
Deleted copy assignment.
sf::IpAddress
Encapsulate an IPv4 network address.
Definition IpAddress.hpp:51
sf::TcpSocket
Specialized socket using the TCP protocol.
Definition TcpSocket.hpp:58
sf::Time
Represents a time value.
Definition Time.hpp:42
sf::Time::Zero
static const Time Zero
Predefined "zero" time value.
Definition Time.hpp:110
sf
Definition AudioResource.hpp:36