28#include <SFML/Network/Ftp.hpp>
29#include <SFML/Network/IPAddress.hpp>
64 void Send(
const std::vector<char>&
Data);
98 return myStatus < 400;
129 std::string::size_type
Begin =
Resp.GetMessage().find(
'"', 0);
130 std::string::size_type End =
Resp.GetMessage().find(
'"',
Begin + 1);
131 myDirectory =
Resp.GetMessage().substr(
Begin + 1, End -
Begin - 1);
155 std::string::size_type
LastPos = 0;
170 return myFilenames.size();
179 return myFilenames[
Index];
202 return GetResponse();
211 return Login(
"anonymous",
"user@sfml-dev.org");
236 myCommandSocket.
Close();
247 return SendCommand(
"NOOP");
280 Resp = GetResponse();
302 return SendCommand(
"CDUP");
311 return SendCommand(
"MKD",
Name);
320 return SendCommand(
"RMD",
Name);
342 return SendCommand(
"DELE",
Name);
365 Resp = GetResponse();
370 std::string::size_type
Pos =
Filename.find_last_of(
"/\\");
371 if (
Pos != std::string::npos)
399 std::ifstream
File(
LocalFile.c_str(), std::ios_base::binary);
402 File.seekg(0, std::ios::end);
404 File.seekg(0, std::ios::beg);
411 std::string::size_type
Pos =
Filename.find_last_of(
"/\\");
412 if (
Pos != std::string::npos)
433 Resp = GetResponse();
458 return GetResponse();
466Ftp::Response Ftp::GetResponse()
471 unsigned int LastCode = 0;
472 bool IsInsideMultiline =
false;
480 if (myCommandSocket.
Receive(Buffer,
sizeof(Buffer), Length) != sf::Socket::Done)
484 std::istringstream In(std::string(Buffer, Length), std::ios_base::binary);
496 if ((Sep ==
'-') && !IsInsideMultiline)
499 IsInsideMultiline =
true;
506 std::getline(In, Message);
509 Message.erase(Message.length() - 1);
510 Message = Sep + Message +
"\n";
516 if ((Sep !=
'-') && ((Code == LastCode) || (LastCode == 0)))
519 IsInsideMultiline =
false;
523 std::getline(In, Line);
526 Line.erase(Line.length() - 1);
529 if (Code == LastCode)
531 std::ostringstream Out;
532 Out <<
Code << Sep << Line;
533 Message += Out.str();
537 Message = Sep + Line;
550 std::getline(In, Line);
555 Line.erase(Line.length() - 1);
558 std::ostringstream Out;
559 Out <<
Code << Sep << Line <<
"\n";
560 Message += Out.str();
565 else if (LastCode != 0)
574 std::getline(In, Line);
579 Line.erase(Line.length() - 1);
582 Message += Line +
"\n";
613 myDataSocket.Close();
627 std::string::size_type
begin =
Resp.GetMessage().find_first_of(
"0123456789");
628 if (
begin != std::string::npos)
632 std::size_t
Index = 0;
633 for (
int i = 0;
i < 6; ++
i)
654 if (myDataSocket.Connect(
Port,
Address) == Socket::Done)
695 myDataSocket.Close();
706 myDataSocket.Send(&
Data[0],
Data.size());
709 myDataSocket.Close();
Ftp::Response Open(Ftp::TransferMode Mode)
Open the data channel using the specified mode and port.
~DataChannel()
Destructor.
void Send(const std::vector< char > &Data)
Send data on the data channel.
void Receive(std::vector< char > &Data)
Receive data on the data channel until it is closed.
DataChannel(Ftp &Owner)
Constructor.
Specialization of FTP response returning a directory.
DirectoryResponse(Response Resp)
Default constructor.
const std::string & GetDirectory() const
Get the directory returned in the response.
Specialization of FTP response returning a filename lisiting.
std::size_t GetCount() const
Get the number of filenames in the listing.
const std::string & GetFilename(std::size_t Index) const
Get the Index-th filename in the directory.
ListingResponse(Response Resp, const std::vector< char > &Data)
Default constructor.
This class wraps a FTP response, which is basically :
Response(Status Code=InvalidResponse, const std::string &Message="")
Default constructor.
const std::string & GetMessage() const
Get the full message contained in the response.
Status GetStatus() const
Get the response status code.
bool IsOk() const
Convenience function to check if the response status code means a success.
Status
Enumerate all the valid status codes returned in a FTP response.
@ ConnectionFailed
Connection with server failed.
@ InvalidResponse
Response is not a valid FTP one.
@ ConnectionClosed
Connection with server closed.
@ InvalidFile
Invalid file to upload / download.
This class provides methods for manipulating the FTP protocol (described in RFC 959).
Response KeepAlive()
Send a null command just to prevent from being disconnected.
Response Download(const std::string &DistantFile, const std::string &DestPath, TransferMode Mode=Binary)
Download a file from the server.
TransferMode
Enumeration of transfer modes.
@ Binary
Binary mode (file is transfered as a sequence of bytes)
@ Ebcdic
Text mode using EBCDIC encoding.
@ Ascii
Text mode using ASCII encoding.
ListingResponse GetDirectoryListing(const std::string &Directory="")
Get the contents of the given directory (subdirectories and files)
~Ftp()
Destructor – close the connection with the server.
Response MakeDirectory(const std::string &Name)
Create a new directory.
Response Upload(const std::string &LocalFile, const std::string &DestPath, TransferMode Mode=Binary)
Upload a file to the server.
Response ParentDirectory()
Go to the parent directory of the current one.
Response Disconnect()
Close the connection with FTP server.
Response ChangeDirectory(const std::string &Directory)
Change the current working directory.
Response RenameFile(const std::string &File, const std::string &NewName)
Rename a file.
DirectoryResponse GetWorkingDirectory()
Get the current working directory.
Response DeleteDirectory(const std::string &Name)
Remove an existing directory.
Response Login()
Log in using anonymous account.
Response DeleteFile(const std::string &Name)
Remove an existing file.
Response Connect(const IPAddress &Server, unsigned short Port=21, float Timeout=0.f)
Connect to the specified FTP server.
IPAddress provides easy manipulation of IP v4 addresses.
SocketTCP wraps a socket using TCP protocol to send data safely (but a bit slower)
Socket::Status Receive(char *Data, std::size_t MaxSize, std::size_t &SizeReceived)
Receive an array of bytes from the host (must be connected first).
Socket::Status Send(const char *Data, std::size_t Size)
Send an array of bytes to the host (must be connected first)
bool Close()
Close the socket.
Socket::Status Connect(unsigned short Port, const IPAddress &HostAddress, float Timeout=0.f)
Connect to another computer on a specified port.
Vector2 is an utility class for manipulating 2 dimensional vectors.
Utility base class to easily declare non-copyable classes.