mirror of
https://github.com/SFML/SFML.git
synced 2024-11-25 04:41:05 +08:00
Use scoped enumerations in Network module
This commit is contained in:
parent
e01e4760e9
commit
34ee40c835
@ -15,7 +15,7 @@
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
std::ostream& operator<<(std::ostream& stream, const sf::Ftp::Response& response)
|
std::ostream& operator<<(std::ostream& stream, const sf::Ftp::Response& response)
|
||||||
{
|
{
|
||||||
return stream << response.getStatus() << response.getMessage();
|
return stream << static_cast<int>(response.getStatus()) << response.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,26 +19,26 @@ void runTcpServer(unsigned short port)
|
|||||||
sf::TcpListener listener;
|
sf::TcpListener listener;
|
||||||
|
|
||||||
// Listen to the given port for incoming connections
|
// Listen to the given port for incoming connections
|
||||||
if (listener.listen(port) != sf::Socket::Done)
|
if (listener.listen(port) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
|
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
|
||||||
|
|
||||||
// Wait for a connection
|
// Wait for a connection
|
||||||
sf::TcpSocket socket;
|
sf::TcpSocket socket;
|
||||||
if (listener.accept(socket) != sf::Socket::Done)
|
if (listener.accept(socket) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Client connected: " << socket.getRemoteAddress().value() << std::endl;
|
std::cout << "Client connected: " << socket.getRemoteAddress().value() << std::endl;
|
||||||
|
|
||||||
// Send a message to the connected client
|
// Send a message to the connected client
|
||||||
const char out[] = "Hi, I'm the server";
|
const char out[] = "Hi, I'm the server";
|
||||||
if (socket.send(out, sizeof(out)) != sf::Socket::Done)
|
if (socket.send(out, sizeof(out)) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Message sent to the client: " << std::quoted(out) << std::endl;
|
std::cout << "Message sent to the client: " << std::quoted(out) << std::endl;
|
||||||
|
|
||||||
// Receive a message back from the client
|
// Receive a message back from the client
|
||||||
char in[128];
|
char in[128];
|
||||||
std::size_t received;
|
std::size_t received;
|
||||||
if (socket.receive(in, sizeof(in), received) != sf::Socket::Done)
|
if (socket.receive(in, sizeof(in), received) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Answer received from the client: " << std::quoted(in) << std::endl;
|
std::cout << "Answer received from the client: " << std::quoted(in) << std::endl;
|
||||||
}
|
}
|
||||||
@ -63,20 +63,20 @@ void runTcpClient(unsigned short port)
|
|||||||
sf::TcpSocket socket;
|
sf::TcpSocket socket;
|
||||||
|
|
||||||
// Connect to the server
|
// Connect to the server
|
||||||
if (socket.connect(server.value(), port) != sf::Socket::Done)
|
if (socket.connect(server.value(), port) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Connected to server " << server.value() << std::endl;
|
std::cout << "Connected to server " << server.value() << std::endl;
|
||||||
|
|
||||||
// Receive a message from the server
|
// Receive a message from the server
|
||||||
char in[128];
|
char in[128];
|
||||||
std::size_t received;
|
std::size_t received;
|
||||||
if (socket.receive(in, sizeof(in), received) != sf::Socket::Done)
|
if (socket.receive(in, sizeof(in), received) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Message received from the server: " << std::quoted(in) << std::endl;
|
std::cout << "Message received from the server: " << std::quoted(in) << std::endl;
|
||||||
|
|
||||||
// Send an answer to the server
|
// Send an answer to the server
|
||||||
const char out[] = "Hi, I'm a client";
|
const char out[] = "Hi, I'm a client";
|
||||||
if (socket.send(out, sizeof(out)) != sf::Socket::Done)
|
if (socket.send(out, sizeof(out)) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Message sent to the server: " << std::quoted(out) << std::endl;
|
std::cout << "Message sent to the server: " << std::quoted(out) << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ void runUdpServer(unsigned short port)
|
|||||||
sf::UdpSocket socket;
|
sf::UdpSocket socket;
|
||||||
|
|
||||||
// Listen to messages on the specified port
|
// Listen to messages on the specified port
|
||||||
if (socket.bind(port) != sf::Socket::Done)
|
if (socket.bind(port) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Server is listening to port " << port << ", waiting for a message... " << std::endl;
|
std::cout << "Server is listening to port " << port << ", waiting for a message... " << std::endl;
|
||||||
|
|
||||||
@ -28,13 +28,13 @@ void runUdpServer(unsigned short port)
|
|||||||
std::size_t received;
|
std::size_t received;
|
||||||
std::optional<sf::IpAddress> sender;
|
std::optional<sf::IpAddress> sender;
|
||||||
unsigned short senderPort;
|
unsigned short senderPort;
|
||||||
if (socket.receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Done)
|
if (socket.receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Message received from client " << sender.value() << ": " << std::quoted(in) << std::endl;
|
std::cout << "Message received from client " << sender.value() << ": " << std::quoted(in) << std::endl;
|
||||||
|
|
||||||
// Send an answer to the client
|
// Send an answer to the client
|
||||||
const char out[] = "Hi, I'm the server";
|
const char out[] = "Hi, I'm the server";
|
||||||
if (socket.send(out, sizeof(out), sender.value(), senderPort) != sf::Socket::Done)
|
if (socket.send(out, sizeof(out), sender.value(), senderPort) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Message sent to the client: " << std::quoted(out) << std::endl;
|
std::cout << "Message sent to the client: " << std::quoted(out) << std::endl;
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ void runUdpClient(unsigned short port)
|
|||||||
|
|
||||||
// Send a message to the server
|
// Send a message to the server
|
||||||
const char out[] = "Hi, I'm a client";
|
const char out[] = "Hi, I'm a client";
|
||||||
if (socket.send(out, sizeof(out), server.value(), port) != sf::Socket::Done)
|
if (socket.send(out, sizeof(out), server.value(), port) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Message sent to the server: " << std::quoted(out) << std::endl;
|
std::cout << "Message sent to the server: " << std::quoted(out) << std::endl;
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ void runUdpClient(unsigned short port)
|
|||||||
std::size_t received;
|
std::size_t received;
|
||||||
std::optional<sf::IpAddress> sender;
|
std::optional<sf::IpAddress> sender;
|
||||||
unsigned short senderPort;
|
unsigned short senderPort;
|
||||||
if (socket.receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Done)
|
if (socket.receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Message received from " << sender.value() << ": " << std::quoted(in) << std::endl;
|
std::cout << "Message received from " << sender.value() << ": " << std::quoted(in) << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ private:
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
bool onStart() override
|
bool onStart() override
|
||||||
{
|
{
|
||||||
if (m_socket.connect(m_host, m_port) == sf::Socket::Done)
|
if (m_socket.connect(m_host, m_port) == sf::Socket::Status::Done)
|
||||||
{
|
{
|
||||||
std::cout << "Connected to server " << m_host << std::endl;
|
std::cout << "Connected to server " << m_host << std::endl;
|
||||||
return true;
|
return true;
|
||||||
@ -72,7 +72,7 @@ private:
|
|||||||
packet.append(samples, sampleCount * sizeof(std::int16_t));
|
packet.append(samples, sampleCount * sizeof(std::int16_t));
|
||||||
|
|
||||||
// Send the audio packet to the server
|
// Send the audio packet to the server
|
||||||
return m_socket.send(packet) == sf::Socket::Done;
|
return m_socket.send(packet) == sf::Socket::Status::Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
@ -85,7 +85,7 @@ private:
|
|||||||
sf::Packet packet;
|
sf::Packet packet;
|
||||||
packet << clientEndOfStream;
|
packet << clientEndOfStream;
|
||||||
|
|
||||||
if (m_socket.send(packet) != sf::Socket::Done)
|
if (m_socket.send(packet) != sf::Socket::Status::Done)
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to send end-of-stream packet" << std::endl;
|
std::cerr << "Failed to send end-of-stream packet" << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -41,12 +41,12 @@ public:
|
|||||||
if (!m_hasFinished)
|
if (!m_hasFinished)
|
||||||
{
|
{
|
||||||
// Listen to the given port for incoming connections
|
// Listen to the given port for incoming connections
|
||||||
if (m_listener.listen(port) != sf::Socket::Done)
|
if (m_listener.listen(port) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
|
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
|
||||||
|
|
||||||
// Wait for a connection
|
// Wait for a connection
|
||||||
if (m_listener.accept(m_client) != sf::Socket::Done)
|
if (m_listener.accept(m_client) != sf::Socket::Status::Done)
|
||||||
return;
|
return;
|
||||||
std::cout << "Client connected: " << m_client.getRemoteAddress().value() << std::endl;
|
std::cout << "Client connected: " << m_client.getRemoteAddress().value() << std::endl;
|
||||||
|
|
||||||
@ -115,7 +115,7 @@ private:
|
|||||||
{
|
{
|
||||||
// Get waiting audio data from the network
|
// Get waiting audio data from the network
|
||||||
sf::Packet packet;
|
sf::Packet packet;
|
||||||
if (m_client.receive(packet) != sf::Socket::Done)
|
if (m_client.receive(packet) != sf::Socket::Status::Done)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Extract the message ID
|
// Extract the message ID
|
||||||
|
@ -53,7 +53,7 @@ public:
|
|||||||
/// \brief Enumeration of transfer modes
|
/// \brief Enumeration of transfer modes
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
enum TransferMode
|
enum class TransferMode
|
||||||
{
|
{
|
||||||
Binary, //!< Binary mode (file is transfered as a sequence of bytes)
|
Binary, //!< Binary mode (file is transfered as a sequence of bytes)
|
||||||
Ascii, //!< Text mode using ASCII encoding
|
Ascii, //!< Text mode using ASCII encoding
|
||||||
@ -71,7 +71,7 @@ public:
|
|||||||
/// \brief Status codes possibly returned by a FTP response
|
/// \brief Status codes possibly returned by a FTP response
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
enum Status
|
enum class Status
|
||||||
{
|
{
|
||||||
// 1xx: the requested action is being initiated,
|
// 1xx: the requested action is being initiated,
|
||||||
// expect another reply before proceeding with a new command
|
// expect another reply before proceeding with a new command
|
||||||
@ -143,7 +143,7 @@ public:
|
|||||||
/// \param message Response message
|
/// \param message Response message
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
explicit Response(Status code = InvalidResponse, const std::string& message = "");
|
explicit Response(Status code = Status::InvalidResponse, const std::string& message = "");
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief Check if the status code means a success
|
/// \brief Check if the status code means a success
|
||||||
@ -479,7 +479,7 @@ public:
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
[[nodiscard]] Response download(const std::filesystem::path& remoteFile,
|
[[nodiscard]] Response download(const std::filesystem::path& remoteFile,
|
||||||
const std::filesystem::path& localPath,
|
const std::filesystem::path& localPath,
|
||||||
TransferMode mode = Binary);
|
TransferMode mode = TransferMode::Binary);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief Upload a file to the server
|
/// \brief Upload a file to the server
|
||||||
@ -504,7 +504,7 @@ public:
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
[[nodiscard]] Response upload(const std::string& localFile,
|
[[nodiscard]] Response upload(const std::string& localFile,
|
||||||
const std::string& remotePath,
|
const std::string& remotePath,
|
||||||
TransferMode mode = Binary,
|
TransferMode mode = TransferMode::Binary,
|
||||||
bool append = false);
|
bool append = false);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
@ -616,7 +616,7 @@ private:
|
|||||||
/// std::cout << "Created new directory" << std::endl;
|
/// std::cout << "Created new directory" << std::endl;
|
||||||
///
|
///
|
||||||
/// // Upload a file to this new directory
|
/// // Upload a file to this new directory
|
||||||
/// response = ftp.upload("local-path/file.txt", "files", sf::Ftp::Ascii);
|
/// response = ftp.upload("local-path/file.txt", "files", sf::Ftp::TransferMode::Ascii);
|
||||||
/// if (response.isOk())
|
/// if (response.isOk())
|
||||||
/// std::cout << "File uploaded" << std::endl;
|
/// std::cout << "File uploaded" << std::endl;
|
||||||
///
|
///
|
||||||
|
@ -59,7 +59,7 @@ public:
|
|||||||
/// \brief Enumerate the available HTTP methods for a request
|
/// \brief Enumerate the available HTTP methods for a request
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
enum Method
|
enum class Method
|
||||||
{
|
{
|
||||||
Get, //!< Request in get mode, standard method to retrieve a page
|
Get, //!< Request in get mode, standard method to retrieve a page
|
||||||
Post, //!< Request in post mode, usually to send data to a page
|
Post, //!< Request in post mode, usually to send data to a page
|
||||||
@ -79,7 +79,7 @@ public:
|
|||||||
/// \param body Content of the request's body
|
/// \param body Content of the request's body
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
Request(const std::string& uri = "/", Method method = Get, const std::string& body = "");
|
Request(const std::string& uri = "/", Method method = Method::Get, const std::string& body = "");
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief Set the value of a field
|
/// \brief Set the value of a field
|
||||||
@ -101,7 +101,7 @@ public:
|
|||||||
///
|
///
|
||||||
/// See the Method enumeration for a complete list of all
|
/// See the Method enumeration for a complete list of all
|
||||||
/// the availale methods.
|
/// the availale methods.
|
||||||
/// The method is Http::Request::Get by default.
|
/// The method is Http::Request::Method::Get by default.
|
||||||
///
|
///
|
||||||
/// \param method Method to use for the request
|
/// \param method Method to use for the request
|
||||||
///
|
///
|
||||||
@ -196,7 +196,7 @@ public:
|
|||||||
/// \brief Enumerate all the valid status codes for a response
|
/// \brief Enumerate all the valid status codes for a response
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
enum Status
|
enum class Status
|
||||||
{
|
{
|
||||||
// 2xx: success
|
// 2xx: success
|
||||||
Ok = 200, //!< Most common code returned when operation was successful
|
Ok = 200, //!< Most common code returned when operation was successful
|
||||||
@ -477,7 +477,7 @@ private:
|
|||||||
///
|
///
|
||||||
/// // Check the status code and display the result
|
/// // Check the status code and display the result
|
||||||
/// sf::Http::Response::Status status = response.getStatus();
|
/// sf::Http::Response::Status status = response.getStatus();
|
||||||
/// if (status == sf::Http::Response::Ok)
|
/// if (status == sf::Http::Response::Status::Ok)
|
||||||
/// {
|
/// {
|
||||||
/// std::cout << response.getBody() << std::endl;
|
/// std::cout << response.getBody() << std::endl;
|
||||||
/// }
|
/// }
|
||||||
|
@ -50,7 +50,7 @@ public:
|
|||||||
/// \brief Status codes that may be returned by socket functions
|
/// \brief Status codes that may be returned by socket functions
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
enum Status
|
enum class Status
|
||||||
{
|
{
|
||||||
Done, //!< The socket has sent / received the data
|
Done, //!< The socket has sent / received the data
|
||||||
NotReady, //!< The socket is not ready to send / receive data yet
|
NotReady, //!< The socket is not ready to send / receive data yet
|
||||||
@ -121,7 +121,7 @@ protected:
|
|||||||
/// \brief Types of protocols that the socket can use
|
/// \brief Types of protocols that the socket can use
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
enum Type
|
enum class Type
|
||||||
{
|
{
|
||||||
Tcp, //!< TCP protocol
|
Tcp, //!< TCP protocol
|
||||||
Udp //!< UDP protocol
|
Udp //!< UDP protocol
|
||||||
@ -214,7 +214,7 @@ private:
|
|||||||
/// In non-blocking mode, all the socket functions will
|
/// In non-blocking mode, all the socket functions will
|
||||||
/// return immediately. If the socket is not ready to complete
|
/// return immediately. If the socket is not ready to complete
|
||||||
/// the requested operation, the function simply returns
|
/// the requested operation, the function simply returns
|
||||||
/// the proper status code (Socket::NotReady).
|
/// the proper status code (Socket::Status::NotReady).
|
||||||
///
|
///
|
||||||
/// The default mode, which is blocking, is the one that is
|
/// The default mode, which is blocking, is the one that is
|
||||||
/// generally used, in combination with threads or selectors.
|
/// generally used, in combination with threads or selectors.
|
||||||
|
@ -223,7 +223,7 @@ private:
|
|||||||
/// {
|
/// {
|
||||||
/// // The listener is ready: there is a pending connection
|
/// // The listener is ready: there is a pending connection
|
||||||
/// auto client = std::make_unique<sf::TcpSocket>();
|
/// auto client = std::make_unique<sf::TcpSocket>();
|
||||||
/// if (listener.accept(*client) == sf::Socket::Done)
|
/// if (listener.accept(*client) == sf::Socket::Status::Done)
|
||||||
/// {
|
/// {
|
||||||
/// // Add the new client to the selector so that we will
|
/// // Add the new client to the selector so that we will
|
||||||
/// // be notified when he sends something
|
/// // be notified when he sends something
|
||||||
@ -247,7 +247,7 @@ private:
|
|||||||
/// {
|
/// {
|
||||||
/// // The client has sent some data, we can receive it
|
/// // The client has sent some data, we can receive it
|
||||||
/// sf::Packet packet;
|
/// sf::Packet packet;
|
||||||
/// if (client.receive(packet) == sf::Socket::Done)
|
/// if (client.receive(packet) == sf::Socket::Status::Done)
|
||||||
/// {
|
/// {
|
||||||
/// ...
|
/// ...
|
||||||
/// }
|
/// }
|
||||||
|
@ -179,7 +179,7 @@ public:
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief Send a formatted packet of data to the remote peer
|
/// \brief Send a formatted packet of data to the remote peer
|
||||||
///
|
///
|
||||||
/// In non-blocking mode, if this function returns sf::Socket::Partial,
|
/// In non-blocking mode, if this function returns sf::Socket::Status::Partial,
|
||||||
/// you \em must retry sending the same unmodified packet before sending
|
/// you \em must retry sending the same unmodified packet before sending
|
||||||
/// anything else in order to guarantee the packet arrives at the remote
|
/// anything else in order to guarantee the packet arrives at the remote
|
||||||
/// peer uncorrupted.
|
/// peer uncorrupted.
|
||||||
|
@ -270,7 +270,7 @@ private:
|
|||||||
/// std::size_t received = 0;
|
/// std::size_t received = 0;
|
||||||
/// std::optional<sf::IpAddress> sender;
|
/// std::optional<sf::IpAddress> sender;
|
||||||
/// unsigned short port;
|
/// unsigned short port;
|
||||||
/// if (socket.receive(buffer, sizeof(buffer), received, sender, port) == sf::Socket::Done)
|
/// if (socket.receive(buffer, sizeof(buffer), received, sender, port) == sf::Socket::Status::Done)
|
||||||
/// std::cout << sender->toString() << " said: " << buffer << std::endl;
|
/// std::cout << sender->toString() << " said: " << buffer << std::endl;
|
||||||
///
|
///
|
||||||
/// // ----- The server -----
|
/// // ----- The server -----
|
||||||
@ -284,7 +284,7 @@ private:
|
|||||||
/// std::size_t received = 0;
|
/// std::size_t received = 0;
|
||||||
/// std::optional<sf::IpAddress> sender;
|
/// std::optional<sf::IpAddress> sender;
|
||||||
/// unsigned short port;
|
/// unsigned short port;
|
||||||
/// if (socket.receive(buffer, sizeof(buffer), received, sender, port) == sf::Socket::Done)
|
/// if (socket.receive(buffer, sizeof(buffer), received, sender, port) == sf::Socket::Status::Done)
|
||||||
/// std::cout << sender->toString() << " said: " << buffer << std::endl;
|
/// std::cout << sender->toString() << " said: " << buffer << std::endl;
|
||||||
///
|
///
|
||||||
/// // Send an answer
|
/// // Send an answer
|
||||||
|
@ -86,7 +86,7 @@ Ftp::Response::Response(Status code, const std::string& message) : m_status(code
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
bool Ftp::Response::isOk() const
|
bool Ftp::Response::isOk() const
|
||||||
{
|
{
|
||||||
return m_status < 400;
|
return static_cast<int>(m_status) < 400;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -158,8 +158,8 @@ Ftp::~Ftp()
|
|||||||
Ftp::Response Ftp::connect(const IpAddress& server, unsigned short port, Time timeout)
|
Ftp::Response Ftp::connect(const IpAddress& server, unsigned short port, Time timeout)
|
||||||
{
|
{
|
||||||
// Connect to the server
|
// Connect to the server
|
||||||
if (m_commandSocket.connect(server, port, timeout) != Socket::Done)
|
if (m_commandSocket.connect(server, port, timeout) != Socket::Status::Done)
|
||||||
return Response(Response::ConnectionFailed);
|
return Response(Response::Status::ConnectionFailed);
|
||||||
|
|
||||||
// Get the response to the connection
|
// Get the response to the connection
|
||||||
return getResponse();
|
return getResponse();
|
||||||
@ -216,7 +216,7 @@ Ftp::ListingResponse Ftp::getDirectoryListing(const std::string& directory)
|
|||||||
// Open a data channel on default port (20) using ASCII transfer mode
|
// Open a data channel on default port (20) using ASCII transfer mode
|
||||||
std::ostringstream directoryData;
|
std::ostringstream directoryData;
|
||||||
DataChannel data(*this);
|
DataChannel data(*this);
|
||||||
Response response = data.open(Ascii);
|
Response response = data.open(TransferMode::Ascii);
|
||||||
if (response.isOk())
|
if (response.isOk())
|
||||||
{
|
{
|
||||||
// Tell the server to send us the listing
|
// Tell the server to send us the listing
|
||||||
@ -297,7 +297,7 @@ Ftp::Response Ftp::download(const std::filesystem::path& remoteFile, const std::
|
|||||||
const std::filesystem::path filepath = localPath / remoteFile.filename();
|
const std::filesystem::path filepath = localPath / remoteFile.filename();
|
||||||
std::ofstream file(filepath, std::ios_base::binary | std::ios_base::trunc);
|
std::ofstream file(filepath, std::ios_base::binary | std::ios_base::trunc);
|
||||||
if (!file)
|
if (!file)
|
||||||
return Response(Response::InvalidFile);
|
return Response(Response::Status::InvalidFile);
|
||||||
|
|
||||||
// Receive the file data
|
// Receive the file data
|
||||||
data.receive(file);
|
data.receive(file);
|
||||||
@ -324,7 +324,7 @@ Ftp::Response Ftp::upload(const std::string& localFile, const std::string& remot
|
|||||||
// Get the contents of the file to send
|
// Get the contents of the file to send
|
||||||
std::ifstream file(localFile.c_str(), std::ios_base::binary);
|
std::ifstream file(localFile.c_str(), std::ios_base::binary);
|
||||||
if (!file)
|
if (!file)
|
||||||
return Response(Response::InvalidFile);
|
return Response(Response::Status::InvalidFile);
|
||||||
|
|
||||||
// Extract the filename from the file path
|
// Extract the filename from the file path
|
||||||
std::string filename = localFile;
|
std::string filename = localFile;
|
||||||
@ -369,8 +369,8 @@ Ftp::Response Ftp::sendCommand(const std::string& command, const std::string& pa
|
|||||||
commandStr = command + "\r\n";
|
commandStr = command + "\r\n";
|
||||||
|
|
||||||
// Send it to the server
|
// Send it to the server
|
||||||
if (m_commandSocket.send(commandStr.c_str(), commandStr.length()) != Socket::Done)
|
if (m_commandSocket.send(commandStr.c_str(), commandStr.length()) != Socket::Status::Done)
|
||||||
return Response(Response::ConnectionClosed);
|
return Response(Response::Status::ConnectionClosed);
|
||||||
|
|
||||||
// Get the response
|
// Get the response
|
||||||
return getResponse();
|
return getResponse();
|
||||||
@ -395,8 +395,8 @@ Ftp::Response Ftp::getResponse()
|
|||||||
|
|
||||||
if (m_receiveBuffer.empty())
|
if (m_receiveBuffer.empty())
|
||||||
{
|
{
|
||||||
if (m_commandSocket.receive(buffer, sizeof(buffer), length) != Socket::Done)
|
if (m_commandSocket.receive(buffer, sizeof(buffer), length) != Socket::Status::Done)
|
||||||
return Response(Response::ConnectionClosed);
|
return Response(Response::Status::ConnectionClosed);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -511,7 +511,7 @@ Ftp::Response Ftp::getResponse()
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Error: cannot extract the code, and we are not in a multiline response
|
// Error: cannot extract the code, and we are not in a multiline response
|
||||||
return Response(Response::InvalidResponse);
|
return Response(Response::Status::InvalidResponse);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -559,19 +559,19 @@ Ftp::Response Ftp::DataChannel::open(Ftp::TransferMode mode)
|
|||||||
IpAddress address(data[0], data[1], data[2], data[3]);
|
IpAddress address(data[0], data[1], data[2], data[3]);
|
||||||
|
|
||||||
// Connect the data channel to the server
|
// Connect the data channel to the server
|
||||||
if (m_dataSocket.connect(address, port) == Socket::Done)
|
if (m_dataSocket.connect(address, port) == Socket::Status::Done)
|
||||||
{
|
{
|
||||||
// Translate the transfer mode to the corresponding FTP parameter
|
// Translate the transfer mode to the corresponding FTP parameter
|
||||||
std::string modeStr;
|
std::string modeStr;
|
||||||
switch (mode)
|
switch (mode)
|
||||||
{
|
{
|
||||||
case Ftp::Binary:
|
case Ftp::TransferMode::Binary:
|
||||||
modeStr = "I";
|
modeStr = "I";
|
||||||
break;
|
break;
|
||||||
case Ftp::Ascii:
|
case Ftp::TransferMode::Ascii:
|
||||||
modeStr = "A";
|
modeStr = "A";
|
||||||
break;
|
break;
|
||||||
case Ftp::Ebcdic:
|
case Ftp::TransferMode::Ebcdic:
|
||||||
modeStr = "E";
|
modeStr = "E";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -582,7 +582,7 @@ Ftp::Response Ftp::DataChannel::open(Ftp::TransferMode mode)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Failed to connect to the server
|
// Failed to connect to the server
|
||||||
response = Ftp::Response(Ftp::Response::ConnectionFailed);
|
response = Ftp::Response(Ftp::Response::Status::ConnectionFailed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -597,7 +597,7 @@ void Ftp::DataChannel::receive(std::ostream& stream)
|
|||||||
// Receive data
|
// Receive data
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
std::size_t received;
|
std::size_t received;
|
||||||
while (m_dataSocket.receive(buffer, sizeof(buffer), received) == Socket::Done)
|
while (m_dataSocket.receive(buffer, sizeof(buffer), received) == Socket::Status::Done)
|
||||||
{
|
{
|
||||||
stream.write(buffer, static_cast<std::streamsize>(received));
|
stream.write(buffer, static_cast<std::streamsize>(received));
|
||||||
|
|
||||||
@ -636,7 +636,7 @@ void Ftp::DataChannel::send(std::istream& stream)
|
|||||||
if (count > 0)
|
if (count > 0)
|
||||||
{
|
{
|
||||||
// we could read more data from the stream: send them
|
// we could read more data from the stream: send them
|
||||||
if (m_dataSocket.send(buffer, count) != Socket::Done)
|
if (m_dataSocket.send(buffer, count) != Socket::Status::Done)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -96,19 +96,19 @@ std::string Http::Request::prepare() const
|
|||||||
std::string method;
|
std::string method;
|
||||||
switch (m_method)
|
switch (m_method)
|
||||||
{
|
{
|
||||||
case Get:
|
case Method::Get:
|
||||||
method = "GET";
|
method = "GET";
|
||||||
break;
|
break;
|
||||||
case Post:
|
case Method::Post:
|
||||||
method = "POST";
|
method = "POST";
|
||||||
break;
|
break;
|
||||||
case Head:
|
case Method::Head:
|
||||||
method = "HEAD";
|
method = "HEAD";
|
||||||
break;
|
break;
|
||||||
case Put:
|
case Method::Put:
|
||||||
method = "PUT";
|
method = "PUT";
|
||||||
break;
|
break;
|
||||||
case Delete:
|
case Method::Delete:
|
||||||
method = "DELETE";
|
method = "DELETE";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -141,7 +141,7 @@ bool Http::Request::hasField(const std::string& field) const
|
|||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
Http::Response::Response() : m_status(ConnectionFailed), m_majorVersion(0), m_minorVersion(0)
|
Http::Response::Response() : m_status(Status::ConnectionFailed), m_majorVersion(0), m_minorVersion(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,7 +205,7 @@ void Http::Response::parse(const std::string& data)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Invalid HTTP version
|
// Invalid HTTP version
|
||||||
m_status = InvalidResponse;
|
m_status = Status::InvalidResponse;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,7 +219,7 @@ void Http::Response::parse(const std::string& data)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Invalid status code
|
// Invalid status code
|
||||||
m_status = InvalidResponse;
|
m_status = Status::InvalidResponse;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -359,7 +359,7 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout)
|
|||||||
out << toSend.m_body.size();
|
out << toSend.m_body.size();
|
||||||
toSend.setField("Content-Length", out.str());
|
toSend.setField("Content-Length", out.str());
|
||||||
}
|
}
|
||||||
if ((toSend.m_method == Request::Post) && !toSend.hasField("Content-Type"))
|
if ((toSend.m_method == Request::Method::Post) && !toSend.hasField("Content-Type"))
|
||||||
{
|
{
|
||||||
toSend.setField("Content-Type", "application/x-www-form-urlencoded");
|
toSend.setField("Content-Type", "application/x-www-form-urlencoded");
|
||||||
}
|
}
|
||||||
@ -372,7 +372,7 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout)
|
|||||||
Response received;
|
Response received;
|
||||||
|
|
||||||
// Connect the socket to the host
|
// Connect the socket to the host
|
||||||
if (m_connection.connect(m_host.value(), m_port, timeout) == Socket::Done)
|
if (m_connection.connect(m_host.value(), m_port, timeout) == Socket::Status::Done)
|
||||||
{
|
{
|
||||||
// Convert the request to string and send it through the connected socket
|
// Convert the request to string and send it through the connected socket
|
||||||
std::string requestStr = toSend.prepare();
|
std::string requestStr = toSend.prepare();
|
||||||
@ -380,13 +380,13 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout)
|
|||||||
if (!requestStr.empty())
|
if (!requestStr.empty())
|
||||||
{
|
{
|
||||||
// Send it through the socket
|
// Send it through the socket
|
||||||
if (m_connection.send(requestStr.c_str(), requestStr.size()) == Socket::Done)
|
if (m_connection.send(requestStr.c_str(), requestStr.size()) == Socket::Status::Done)
|
||||||
{
|
{
|
||||||
// Wait for the server's response
|
// Wait for the server's response
|
||||||
std::string receivedStr;
|
std::string receivedStr;
|
||||||
std::size_t size = 0;
|
std::size_t size = 0;
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
while (m_connection.receive(buffer, sizeof(buffer), size) == Socket::Done)
|
while (m_connection.receive(buffer, sizeof(buffer), size) == Socket::Status::Done)
|
||||||
{
|
{
|
||||||
receivedStr.append(buffer, buffer + size);
|
receivedStr.append(buffer, buffer + size);
|
||||||
}
|
}
|
||||||
|
@ -161,9 +161,9 @@ std::optional<IpAddress> IpAddress::getPublicAddress(Time timeout)
|
|||||||
// (not very hard: the web page contains only our IP address).
|
// (not very hard: the web page contains only our IP address).
|
||||||
|
|
||||||
Http server("www.sfml-dev.org");
|
Http server("www.sfml-dev.org");
|
||||||
Http::Request request("/ip-provider.php", Http::Request::Get);
|
Http::Request request("/ip-provider.php", Http::Request::Method::Get);
|
||||||
Http::Response page = server.sendRequest(request, timeout);
|
Http::Response page = server.sendRequest(request, timeout);
|
||||||
if (page.getStatus() == Http::Response::Ok)
|
if (page.getStatus() == Http::Response::Status::Ok)
|
||||||
return IpAddress::resolve(page.getBody());
|
return IpAddress::resolve(page.getBody());
|
||||||
|
|
||||||
// Something failed: return an invalid address
|
// Something failed: return an invalid address
|
||||||
|
@ -79,7 +79,7 @@ void Socket::create()
|
|||||||
// Don't create the socket if it already exists
|
// Don't create the socket if it already exists
|
||||||
if (m_socket == priv::SocketImpl::invalidSocket())
|
if (m_socket == priv::SocketImpl::invalidSocket())
|
||||||
{
|
{
|
||||||
SocketHandle handle = socket(PF_INET, m_type == Tcp ? SOCK_STREAM : SOCK_DGRAM, 0);
|
SocketHandle handle = socket(PF_INET, m_type == Type::Tcp ? SOCK_STREAM : SOCK_DGRAM, 0);
|
||||||
|
|
||||||
if (handle == priv::SocketImpl::invalidSocket())
|
if (handle == priv::SocketImpl::invalidSocket())
|
||||||
{
|
{
|
||||||
@ -104,7 +104,7 @@ void Socket::create(SocketHandle handle)
|
|||||||
// Set the current blocking state
|
// Set the current blocking state
|
||||||
setBlocking(m_isBlocking);
|
setBlocking(m_isBlocking);
|
||||||
|
|
||||||
if (m_type == Tcp)
|
if (m_type == Type::Tcp)
|
||||||
{
|
{
|
||||||
// Disable the Nagle algorithm (i.e. removes buffering of TCP packets)
|
// Disable the Nagle algorithm (i.e. removes buffering of TCP packets)
|
||||||
int yes = 1;
|
int yes = 1;
|
||||||
|
@ -36,7 +36,7 @@
|
|||||||
namespace sf
|
namespace sf
|
||||||
{
|
{
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
TcpListener::TcpListener() : Socket(Tcp)
|
TcpListener::TcpListener() : Socket(Type::Tcp)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ Socket::Status TcpListener::listen(unsigned short port, const IpAddress& address
|
|||||||
|
|
||||||
// Check if the address is valid
|
// Check if the address is valid
|
||||||
if (address == IpAddress::Broadcast)
|
if (address == IpAddress::Broadcast)
|
||||||
return Error;
|
return Status::Error;
|
||||||
|
|
||||||
// Bind the socket to the specified port
|
// Bind the socket to the specified port
|
||||||
sockaddr_in addr = priv::SocketImpl::createAddress(address.toInteger(), port);
|
sockaddr_in addr = priv::SocketImpl::createAddress(address.toInteger(), port);
|
||||||
@ -79,7 +79,7 @@ Socket::Status TcpListener::listen(unsigned short port, const IpAddress& address
|
|||||||
{
|
{
|
||||||
// Not likely to happen, but...
|
// Not likely to happen, but...
|
||||||
err() << "Failed to bind listener socket to port " << port << std::endl;
|
err() << "Failed to bind listener socket to port " << port << std::endl;
|
||||||
return Error;
|
return Status::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen to the bound port
|
// Listen to the bound port
|
||||||
@ -87,10 +87,10 @@ Socket::Status TcpListener::listen(unsigned short port, const IpAddress& address
|
|||||||
{
|
{
|
||||||
// Oops, socket is deaf
|
// Oops, socket is deaf
|
||||||
err() << "Failed to listen to port " << port << std::endl;
|
err() << "Failed to listen to port " << port << std::endl;
|
||||||
return Error;
|
return Status::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Done;
|
return Status::Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ Socket::Status TcpListener::accept(TcpSocket& socket)
|
|||||||
if (getHandle() == priv::SocketImpl::invalidSocket())
|
if (getHandle() == priv::SocketImpl::invalidSocket())
|
||||||
{
|
{
|
||||||
err() << "Failed to accept a new connection, the socket is not listening" << std::endl;
|
err() << "Failed to accept a new connection, the socket is not listening" << std::endl;
|
||||||
return Error;
|
return Status::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accept a new connection
|
// Accept a new connection
|
||||||
@ -125,7 +125,7 @@ Socket::Status TcpListener::accept(TcpSocket& socket)
|
|||||||
socket.close();
|
socket.close();
|
||||||
socket.create(remote);
|
socket.create(remote);
|
||||||
|
|
||||||
return Done;
|
return Status::Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sf
|
} // namespace sf
|
||||||
|
@ -53,7 +53,7 @@ const int flags = 0;
|
|||||||
namespace sf
|
namespace sf
|
||||||
{
|
{
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
TcpSocket::TcpSocket() : Socket(Tcp)
|
TcpSocket::TcpSocket() : Socket(Type::Tcp)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,7 +136,7 @@ Socket::Status TcpSocket::connect(const IpAddress& remoteAddress, unsigned short
|
|||||||
return priv::SocketImpl::getErrorStatus();
|
return priv::SocketImpl::getErrorStatus();
|
||||||
|
|
||||||
// Connection succeeded
|
// Connection succeeded
|
||||||
return Done;
|
return Status::Done;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -154,7 +154,7 @@ Socket::Status TcpSocket::connect(const IpAddress& remoteAddress, unsigned short
|
|||||||
{
|
{
|
||||||
// We got instantly connected! (it may no happen a lot...)
|
// We got instantly connected! (it may no happen a lot...)
|
||||||
setBlocking(blocking);
|
setBlocking(blocking);
|
||||||
return Done;
|
return Status::Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the error status
|
// Get the error status
|
||||||
@ -165,7 +165,7 @@ Socket::Status TcpSocket::connect(const IpAddress& remoteAddress, unsigned short
|
|||||||
return status;
|
return status;
|
||||||
|
|
||||||
// Otherwise, wait until something happens to our socket (success, timeout or error)
|
// Otherwise, wait until something happens to our socket (success, timeout or error)
|
||||||
if (status == Socket::NotReady)
|
if (status == Socket::Status::NotReady)
|
||||||
{
|
{
|
||||||
// Setup the selector
|
// Setup the selector
|
||||||
fd_set selector;
|
fd_set selector;
|
||||||
@ -185,7 +185,7 @@ Socket::Status TcpSocket::connect(const IpAddress& remoteAddress, unsigned short
|
|||||||
if (getRemoteAddress().has_value())
|
if (getRemoteAddress().has_value())
|
||||||
{
|
{
|
||||||
// Connection accepted
|
// Connection accepted
|
||||||
status = Done;
|
status = Status::Done;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -238,7 +238,7 @@ Socket::Status TcpSocket::send(const void* data, std::size_t size, std::size_t&
|
|||||||
if (!data || (size == 0))
|
if (!data || (size == 0))
|
||||||
{
|
{
|
||||||
err() << "Cannot send data over the network (no data to send)" << std::endl;
|
err() << "Cannot send data over the network (no data to send)" << std::endl;
|
||||||
return Error;
|
return Status::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop until every byte has been sent
|
// Loop until every byte has been sent
|
||||||
@ -257,14 +257,14 @@ Socket::Status TcpSocket::send(const void* data, std::size_t size, std::size_t&
|
|||||||
{
|
{
|
||||||
Status status = priv::SocketImpl::getErrorStatus();
|
Status status = priv::SocketImpl::getErrorStatus();
|
||||||
|
|
||||||
if ((status == NotReady) && sent)
|
if ((status == Status::NotReady) && sent)
|
||||||
return Partial;
|
return Status::Partial;
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Done;
|
return Status::Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -278,7 +278,7 @@ Socket::Status TcpSocket::receive(void* data, std::size_t size, std::size_t& rec
|
|||||||
if (!data)
|
if (!data)
|
||||||
{
|
{
|
||||||
err() << "Cannot receive data from the network (the destination buffer is invalid)" << std::endl;
|
err() << "Cannot receive data from the network (the destination buffer is invalid)" << std::endl;
|
||||||
return Error;
|
return Status::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma GCC diagnostic push
|
#pragma GCC diagnostic push
|
||||||
@ -292,11 +292,11 @@ Socket::Status TcpSocket::receive(void* data, std::size_t size, std::size_t& rec
|
|||||||
if (sizeReceived > 0)
|
if (sizeReceived > 0)
|
||||||
{
|
{
|
||||||
received = static_cast<std::size_t>(sizeReceived);
|
received = static_cast<std::size_t>(sizeReceived);
|
||||||
return Done;
|
return Status::Done;
|
||||||
}
|
}
|
||||||
else if (sizeReceived == 0)
|
else if (sizeReceived == 0)
|
||||||
{
|
{
|
||||||
return Socket::Disconnected;
|
return Socket::Status::Disconnected;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -351,11 +351,11 @@ Socket::Status TcpSocket::send(Packet& packet)
|
|||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
|
|
||||||
// In the case of a partial send, record the location to resume from
|
// In the case of a partial send, record the location to resume from
|
||||||
if (status == Partial)
|
if (status == Status::Partial)
|
||||||
{
|
{
|
||||||
packet.m_sendPos += sent;
|
packet.m_sendPos += sent;
|
||||||
}
|
}
|
||||||
else if (status == Done)
|
else if (status == Status::Done)
|
||||||
{
|
{
|
||||||
packet.m_sendPos = 0;
|
packet.m_sendPos = 0;
|
||||||
}
|
}
|
||||||
@ -383,7 +383,7 @@ Socket::Status TcpSocket::receive(Packet& packet)
|
|||||||
Status status = receive(data, sizeof(m_pendingPacket.Size) - m_pendingPacket.SizeReceived, received);
|
Status status = receive(data, sizeof(m_pendingPacket.Size) - m_pendingPacket.SizeReceived, received);
|
||||||
m_pendingPacket.SizeReceived += received;
|
m_pendingPacket.SizeReceived += received;
|
||||||
|
|
||||||
if (status != Done)
|
if (status != Status::Done)
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -403,7 +403,7 @@ Socket::Status TcpSocket::receive(Packet& packet)
|
|||||||
// Receive a chunk of data
|
// Receive a chunk of data
|
||||||
std::size_t sizeToGet = std::min(packetSize - m_pendingPacket.Data.size(), sizeof(buffer));
|
std::size_t sizeToGet = std::min(packetSize - m_pendingPacket.Data.size(), sizeof(buffer));
|
||||||
Status status = receive(buffer, sizeToGet, received);
|
Status status = receive(buffer, sizeToGet, received);
|
||||||
if (status != Done)
|
if (status != Status::Done)
|
||||||
return status;
|
return status;
|
||||||
|
|
||||||
// Append it into the packet
|
// Append it into the packet
|
||||||
@ -422,7 +422,7 @@ Socket::Status TcpSocket::receive(Packet& packet)
|
|||||||
// Clear the pending packet data
|
// Clear the pending packet data
|
||||||
m_pendingPacket = PendingPacket();
|
m_pendingPacket = PendingPacket();
|
||||||
|
|
||||||
return Done;
|
return Status::Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@
|
|||||||
namespace sf
|
namespace sf
|
||||||
{
|
{
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
UdpSocket::UdpSocket() : Socket(Udp), m_buffer(MaxDatagramSize)
|
UdpSocket::UdpSocket() : Socket(Type::Udp), m_buffer(MaxDatagramSize)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,17 +73,17 @@ Socket::Status UdpSocket::bind(unsigned short port, const IpAddress& address)
|
|||||||
|
|
||||||
// Check if the address is valid
|
// Check if the address is valid
|
||||||
if (address == IpAddress::Broadcast)
|
if (address == IpAddress::Broadcast)
|
||||||
return Error;
|
return Status::Error;
|
||||||
|
|
||||||
// Bind the socket
|
// Bind the socket
|
||||||
sockaddr_in addr = priv::SocketImpl::createAddress(address.toInteger(), port);
|
sockaddr_in addr = priv::SocketImpl::createAddress(address.toInteger(), port);
|
||||||
if (::bind(getHandle(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1)
|
if (::bind(getHandle(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1)
|
||||||
{
|
{
|
||||||
err() << "Failed to bind socket to port " << port << std::endl;
|
err() << "Failed to bind socket to port " << port << std::endl;
|
||||||
return Error;
|
return Status::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Done;
|
return Status::Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ Socket::Status UdpSocket::send(const void* data, std::size_t size, const IpAddre
|
|||||||
{
|
{
|
||||||
err() << "Cannot send data over the network "
|
err() << "Cannot send data over the network "
|
||||||
<< "(the number of bytes to send is greater than sf::UdpSocket::MaxDatagramSize)" << std::endl;
|
<< "(the number of bytes to send is greater than sf::UdpSocket::MaxDatagramSize)" << std::endl;
|
||||||
return Error;
|
return Status::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the target address
|
// Build the target address
|
||||||
@ -128,7 +128,7 @@ Socket::Status UdpSocket::send(const void* data, std::size_t size, const IpAddre
|
|||||||
if (sent < 0)
|
if (sent < 0)
|
||||||
return priv::SocketImpl::getErrorStatus();
|
return priv::SocketImpl::getErrorStatus();
|
||||||
|
|
||||||
return Done;
|
return Status::Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -148,7 +148,7 @@ Socket::Status UdpSocket::receive(void* data,
|
|||||||
if (!data)
|
if (!data)
|
||||||
{
|
{
|
||||||
err() << "Cannot receive data from the network (the destination buffer is invalid)" << std::endl;
|
err() << "Cannot receive data from the network (the destination buffer is invalid)" << std::endl;
|
||||||
return Error;
|
return Status::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data that will be filled with the other computer's address
|
// Data that will be filled with the other computer's address
|
||||||
@ -176,7 +176,7 @@ Socket::Status UdpSocket::receive(void* data,
|
|||||||
remoteAddress = IpAddress(ntohl(address.sin_addr.s_addr));
|
remoteAddress = IpAddress(ntohl(address.sin_addr.s_addr));
|
||||||
remotePort = ntohs(address.sin_port);
|
remotePort = ntohs(address.sin_port);
|
||||||
|
|
||||||
return Done;
|
return Status::Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -211,7 +211,7 @@ Socket::Status UdpSocket::receive(Packet& packet, std::optional<IpAddress>& remo
|
|||||||
|
|
||||||
// If we received valid data, we can copy it to the user packet
|
// If we received valid data, we can copy it to the user packet
|
||||||
packet.clear();
|
packet.clear();
|
||||||
if ((status == Done) && (received > 0))
|
if ((status == Status::Done) && (received > 0))
|
||||||
packet.onReceive(m_buffer.data(), received);
|
packet.onReceive(m_buffer.data(), received);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
|
@ -93,19 +93,19 @@ Socket::Status SocketImpl::getErrorStatus()
|
|||||||
// so we have to make a special case for them in order
|
// so we have to make a special case for them in order
|
||||||
// to avoid having double values in the switch case
|
// to avoid having double values in the switch case
|
||||||
if ((errno == EAGAIN) || (errno == EINPROGRESS))
|
if ((errno == EAGAIN) || (errno == EINPROGRESS))
|
||||||
return Socket::NotReady;
|
return Socket::Status::NotReady;
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
switch (errno)
|
switch (errno)
|
||||||
{
|
{
|
||||||
case EWOULDBLOCK: return Socket::NotReady;
|
case EWOULDBLOCK: return Socket::Status::NotReady;
|
||||||
case ECONNABORTED: return Socket::Disconnected;
|
case ECONNABORTED: return Socket::Status::Disconnected;
|
||||||
case ECONNRESET: return Socket::Disconnected;
|
case ECONNRESET: return Socket::Status::Disconnected;
|
||||||
case ETIMEDOUT: return Socket::Disconnected;
|
case ETIMEDOUT: return Socket::Status::Disconnected;
|
||||||
case ENETRESET: return Socket::Disconnected;
|
case ENETRESET: return Socket::Status::Disconnected;
|
||||||
case ENOTCONN: return Socket::Disconnected;
|
case ENOTCONN: return Socket::Status::Disconnected;
|
||||||
case EPIPE: return Socket::Disconnected;
|
case EPIPE: return Socket::Status::Disconnected;
|
||||||
default: return Socket::Error;
|
default: return Socket::Status::Error;
|
||||||
}
|
}
|
||||||
// clang-format on
|
// clang-format on
|
||||||
}
|
}
|
||||||
|
@ -75,15 +75,15 @@ Socket::Status SocketImpl::getErrorStatus()
|
|||||||
// clang-format off
|
// clang-format off
|
||||||
switch (WSAGetLastError())
|
switch (WSAGetLastError())
|
||||||
{
|
{
|
||||||
case WSAEWOULDBLOCK: return Socket::NotReady;
|
case WSAEWOULDBLOCK: return Socket::Status::NotReady;
|
||||||
case WSAEALREADY: return Socket::NotReady;
|
case WSAEALREADY: return Socket::Status::NotReady;
|
||||||
case WSAECONNABORTED: return Socket::Disconnected;
|
case WSAECONNABORTED: return Socket::Status::Disconnected;
|
||||||
case WSAECONNRESET: return Socket::Disconnected;
|
case WSAECONNRESET: return Socket::Status::Disconnected;
|
||||||
case WSAETIMEDOUT: return Socket::Disconnected;
|
case WSAETIMEDOUT: return Socket::Status::Disconnected;
|
||||||
case WSAENETRESET: return Socket::Disconnected;
|
case WSAENETRESET: return Socket::Status::Disconnected;
|
||||||
case WSAENOTCONN: return Socket::Disconnected;
|
case WSAENOTCONN: return Socket::Status::Disconnected;
|
||||||
case WSAEISCONN: return Socket::Done; // when connecting a non-blocking socket
|
case WSAEISCONN: return Socket::Status::Done; // when connecting a non-blocking socket
|
||||||
default: return Socket::Error;
|
default: return Socket::Status::Error;
|
||||||
}
|
}
|
||||||
// clang-format on
|
// clang-format on
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user