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)
|
||||
{
|
||||
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;
|
||||
|
||||
// Listen to the given port for incoming connections
|
||||
if (listener.listen(port) != sf::Socket::Done)
|
||||
if (listener.listen(port) != sf::Socket::Status::Done)
|
||||
return;
|
||||
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
|
||||
|
||||
// Wait for a connection
|
||||
sf::TcpSocket socket;
|
||||
if (listener.accept(socket) != sf::Socket::Done)
|
||||
if (listener.accept(socket) != sf::Socket::Status::Done)
|
||||
return;
|
||||
std::cout << "Client connected: " << socket.getRemoteAddress().value() << std::endl;
|
||||
|
||||
// Send a message to the connected client
|
||||
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;
|
||||
std::cout << "Message sent to the client: " << std::quoted(out) << std::endl;
|
||||
|
||||
// Receive a message back from the client
|
||||
char in[128];
|
||||
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;
|
||||
std::cout << "Answer received from the client: " << std::quoted(in) << std::endl;
|
||||
}
|
||||
@ -63,20 +63,20 @@ void runTcpClient(unsigned short port)
|
||||
sf::TcpSocket socket;
|
||||
|
||||
// Connect to the server
|
||||
if (socket.connect(server.value(), port) != sf::Socket::Done)
|
||||
if (socket.connect(server.value(), port) != sf::Socket::Status::Done)
|
||||
return;
|
||||
std::cout << "Connected to server " << server.value() << std::endl;
|
||||
|
||||
// Receive a message from the server
|
||||
char in[128];
|
||||
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;
|
||||
std::cout << "Message received from the server: " << std::quoted(in) << std::endl;
|
||||
|
||||
// Send an answer to the server
|
||||
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;
|
||||
std::cout << "Message sent to the server: " << std::quoted(out) << std::endl;
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ void runUdpServer(unsigned short port)
|
||||
sf::UdpSocket socket;
|
||||
|
||||
// Listen to messages on the specified port
|
||||
if (socket.bind(port) != sf::Socket::Done)
|
||||
if (socket.bind(port) != sf::Socket::Status::Done)
|
||||
return;
|
||||
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::optional<sf::IpAddress> sender;
|
||||
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;
|
||||
std::cout << "Message received from client " << sender.value() << ": " << std::quoted(in) << std::endl;
|
||||
|
||||
// Send an answer to the client
|
||||
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;
|
||||
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
|
||||
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;
|
||||
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::optional<sf::IpAddress> sender;
|
||||
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;
|
||||
std::cout << "Message received from " << sender.value() << ": " << std::quoted(in) << std::endl;
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ private:
|
||||
////////////////////////////////////////////////////////////
|
||||
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;
|
||||
return true;
|
||||
@ -72,7 +72,7 @@ private:
|
||||
packet.append(samples, sampleCount * sizeof(std::int16_t));
|
||||
|
||||
// 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;
|
||||
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;
|
||||
}
|
||||
|
@ -41,12 +41,12 @@ public:
|
||||
if (!m_hasFinished)
|
||||
{
|
||||
// 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;
|
||||
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
|
||||
|
||||
// Wait for a connection
|
||||
if (m_listener.accept(m_client) != sf::Socket::Done)
|
||||
if (m_listener.accept(m_client) != sf::Socket::Status::Done)
|
||||
return;
|
||||
std::cout << "Client connected: " << m_client.getRemoteAddress().value() << std::endl;
|
||||
|
||||
@ -115,7 +115,7 @@ private:
|
||||
{
|
||||
// Get waiting audio data from the network
|
||||
sf::Packet packet;
|
||||
if (m_client.receive(packet) != sf::Socket::Done)
|
||||
if (m_client.receive(packet) != sf::Socket::Status::Done)
|
||||
break;
|
||||
|
||||
// Extract the message ID
|
||||
|
@ -53,7 +53,7 @@ public:
|
||||
/// \brief Enumeration of transfer modes
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
enum TransferMode
|
||||
enum class TransferMode
|
||||
{
|
||||
Binary, //!< Binary mode (file is transfered as a sequence of bytes)
|
||||
Ascii, //!< Text mode using ASCII encoding
|
||||
@ -71,7 +71,7 @@ public:
|
||||
/// \brief Status codes possibly returned by a FTP response
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
enum Status
|
||||
enum class Status
|
||||
{
|
||||
// 1xx: the requested action is being initiated,
|
||||
// expect another reply before proceeding with a new command
|
||||
@ -143,7 +143,7 @@ public:
|
||||
/// \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
|
||||
@ -479,7 +479,7 @@ public:
|
||||
////////////////////////////////////////////////////////////
|
||||
[[nodiscard]] Response download(const std::filesystem::path& remoteFile,
|
||||
const std::filesystem::path& localPath,
|
||||
TransferMode mode = Binary);
|
||||
TransferMode mode = TransferMode::Binary);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Upload a file to the server
|
||||
@ -504,7 +504,7 @@ public:
|
||||
////////////////////////////////////////////////////////////
|
||||
[[nodiscard]] Response upload(const std::string& localFile,
|
||||
const std::string& remotePath,
|
||||
TransferMode mode = Binary,
|
||||
TransferMode mode = TransferMode::Binary,
|
||||
bool append = false);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
@ -616,7 +616,7 @@ private:
|
||||
/// std::cout << "Created new directory" << std::endl;
|
||||
///
|
||||
/// // 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())
|
||||
/// std::cout << "File uploaded" << std::endl;
|
||||
///
|
||||
|
@ -59,7 +59,7 @@ public:
|
||||
/// \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
|
||||
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
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
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
|
||||
@ -101,7 +101,7 @@ public:
|
||||
///
|
||||
/// See the Method enumeration for a complete list of all
|
||||
/// 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
|
||||
///
|
||||
@ -196,7 +196,7 @@ public:
|
||||
/// \brief Enumerate all the valid status codes for a response
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
enum Status
|
||||
enum class Status
|
||||
{
|
||||
// 2xx: success
|
||||
Ok = 200, //!< Most common code returned when operation was successful
|
||||
@ -477,7 +477,7 @@ private:
|
||||
///
|
||||
/// // Check the status code and display the result
|
||||
/// 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;
|
||||
/// }
|
||||
|
@ -50,7 +50,7 @@ public:
|
||||
/// \brief Status codes that may be returned by socket functions
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
enum Status
|
||||
enum class Status
|
||||
{
|
||||
Done, //!< The socket has sent / received the data
|
||||
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
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
enum Type
|
||||
enum class Type
|
||||
{
|
||||
Tcp, //!< TCP protocol
|
||||
Udp //!< UDP protocol
|
||||
@ -214,7 +214,7 @@ private:
|
||||
/// In non-blocking mode, all the socket functions will
|
||||
/// return immediately. If the socket is not ready to complete
|
||||
/// 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
|
||||
/// generally used, in combination with threads or selectors.
|
||||
|
@ -223,7 +223,7 @@ private:
|
||||
/// {
|
||||
/// // The listener is ready: there is a pending connection
|
||||
/// 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
|
||||
/// // be notified when he sends something
|
||||
@ -247,7 +247,7 @@ private:
|
||||
/// {
|
||||
/// // The client has sent some data, we can receive it
|
||||
/// 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
|
||||
///
|
||||
/// 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
|
||||
/// anything else in order to guarantee the packet arrives at the remote
|
||||
/// peer uncorrupted.
|
||||
|
@ -270,7 +270,7 @@ private:
|
||||
/// std::size_t received = 0;
|
||||
/// std::optional<sf::IpAddress> sender;
|
||||
/// 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;
|
||||
///
|
||||
/// // ----- The server -----
|
||||
@ -284,7 +284,7 @@ private:
|
||||
/// std::size_t received = 0;
|
||||
/// std::optional<sf::IpAddress> sender;
|
||||
/// 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;
|
||||
///
|
||||
/// // Send an answer
|
||||
|
@ -86,7 +86,7 @@ Ftp::Response::Response(Status code, const std::string& message) : m_status(code
|
||||
////////////////////////////////////////////////////////////
|
||||
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)
|
||||
{
|
||||
// Connect to the server
|
||||
if (m_commandSocket.connect(server, port, timeout) != Socket::Done)
|
||||
return Response(Response::ConnectionFailed);
|
||||
if (m_commandSocket.connect(server, port, timeout) != Socket::Status::Done)
|
||||
return Response(Response::Status::ConnectionFailed);
|
||||
|
||||
// Get the response to the connection
|
||||
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
|
||||
std::ostringstream directoryData;
|
||||
DataChannel data(*this);
|
||||
Response response = data.open(Ascii);
|
||||
Response response = data.open(TransferMode::Ascii);
|
||||
if (response.isOk())
|
||||
{
|
||||
// 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();
|
||||
std::ofstream file(filepath, std::ios_base::binary | std::ios_base::trunc);
|
||||
if (!file)
|
||||
return Response(Response::InvalidFile);
|
||||
return Response(Response::Status::InvalidFile);
|
||||
|
||||
// Receive the file data
|
||||
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
|
||||
std::ifstream file(localFile.c_str(), std::ios_base::binary);
|
||||
if (!file)
|
||||
return Response(Response::InvalidFile);
|
||||
return Response(Response::Status::InvalidFile);
|
||||
|
||||
// Extract the filename from the file path
|
||||
std::string filename = localFile;
|
||||
@ -369,8 +369,8 @@ Ftp::Response Ftp::sendCommand(const std::string& command, const std::string& pa
|
||||
commandStr = command + "\r\n";
|
||||
|
||||
// Send it to the server
|
||||
if (m_commandSocket.send(commandStr.c_str(), commandStr.length()) != Socket::Done)
|
||||
return Response(Response::ConnectionClosed);
|
||||
if (m_commandSocket.send(commandStr.c_str(), commandStr.length()) != Socket::Status::Done)
|
||||
return Response(Response::Status::ConnectionClosed);
|
||||
|
||||
// Get the response
|
||||
return getResponse();
|
||||
@ -395,8 +395,8 @@ Ftp::Response Ftp::getResponse()
|
||||
|
||||
if (m_receiveBuffer.empty())
|
||||
{
|
||||
if (m_commandSocket.receive(buffer, sizeof(buffer), length) != Socket::Done)
|
||||
return Response(Response::ConnectionClosed);
|
||||
if (m_commandSocket.receive(buffer, sizeof(buffer), length) != Socket::Status::Done)
|
||||
return Response(Response::Status::ConnectionClosed);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -511,7 +511,7 @@ Ftp::Response Ftp::getResponse()
|
||||
else
|
||||
{
|
||||
// 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]);
|
||||
|
||||
// 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
|
||||
std::string modeStr;
|
||||
switch (mode)
|
||||
{
|
||||
case Ftp::Binary:
|
||||
case Ftp::TransferMode::Binary:
|
||||
modeStr = "I";
|
||||
break;
|
||||
case Ftp::Ascii:
|
||||
case Ftp::TransferMode::Ascii:
|
||||
modeStr = "A";
|
||||
break;
|
||||
case Ftp::Ebcdic:
|
||||
case Ftp::TransferMode::Ebcdic:
|
||||
modeStr = "E";
|
||||
break;
|
||||
}
|
||||
@ -582,7 +582,7 @@ Ftp::Response Ftp::DataChannel::open(Ftp::TransferMode mode)
|
||||
else
|
||||
{
|
||||
// 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
|
||||
char buffer[1024];
|
||||
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));
|
||||
|
||||
@ -636,7 +636,7 @@ void Ftp::DataChannel::send(std::istream& stream)
|
||||
if (count > 0)
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
else
|
||||
|
@ -96,19 +96,19 @@ std::string Http::Request::prepare() const
|
||||
std::string method;
|
||||
switch (m_method)
|
||||
{
|
||||
case Get:
|
||||
case Method::Get:
|
||||
method = "GET";
|
||||
break;
|
||||
case Post:
|
||||
case Method::Post:
|
||||
method = "POST";
|
||||
break;
|
||||
case Head:
|
||||
case Method::Head:
|
||||
method = "HEAD";
|
||||
break;
|
||||
case Put:
|
||||
case Method::Put:
|
||||
method = "PUT";
|
||||
break;
|
||||
case Delete:
|
||||
case Method::Delete:
|
||||
method = "DELETE";
|
||||
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
|
||||
{
|
||||
// Invalid HTTP version
|
||||
m_status = InvalidResponse;
|
||||
m_status = Status::InvalidResponse;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -219,7 +219,7 @@ void Http::Response::parse(const std::string& data)
|
||||
else
|
||||
{
|
||||
// Invalid status code
|
||||
m_status = InvalidResponse;
|
||||
m_status = Status::InvalidResponse;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -359,7 +359,7 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout)
|
||||
out << toSend.m_body.size();
|
||||
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");
|
||||
}
|
||||
@ -372,7 +372,7 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout)
|
||||
Response received;
|
||||
|
||||
// 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
|
||||
std::string requestStr = toSend.prepare();
|
||||
@ -380,13 +380,13 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout)
|
||||
if (!requestStr.empty())
|
||||
{
|
||||
// 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
|
||||
std::string receivedStr;
|
||||
std::size_t size = 0;
|
||||
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);
|
||||
}
|
||||
|
@ -161,9 +161,9 @@ std::optional<IpAddress> IpAddress::getPublicAddress(Time timeout)
|
||||
// (not very hard: the web page contains only our IP address).
|
||||
|
||||
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);
|
||||
if (page.getStatus() == Http::Response::Ok)
|
||||
if (page.getStatus() == Http::Response::Status::Ok)
|
||||
return IpAddress::resolve(page.getBody());
|
||||
|
||||
// Something failed: return an invalid address
|
||||
|
@ -79,7 +79,7 @@ void Socket::create()
|
||||
// Don't create the socket if it already exists
|
||||
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())
|
||||
{
|
||||
@ -104,7 +104,7 @@ void Socket::create(SocketHandle handle)
|
||||
// Set the current blocking state
|
||||
setBlocking(m_isBlocking);
|
||||
|
||||
if (m_type == Tcp)
|
||||
if (m_type == Type::Tcp)
|
||||
{
|
||||
// Disable the Nagle algorithm (i.e. removes buffering of TCP packets)
|
||||
int yes = 1;
|
||||
|
@ -36,7 +36,7 @@
|
||||
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
|
||||
if (address == IpAddress::Broadcast)
|
||||
return Error;
|
||||
return Status::Error;
|
||||
|
||||
// Bind the socket to the specified 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...
|
||||
err() << "Failed to bind listener socket to port " << port << std::endl;
|
||||
return Error;
|
||||
return Status::Error;
|
||||
}
|
||||
|
||||
// Listen to the bound port
|
||||
@ -87,10 +87,10 @@ Socket::Status TcpListener::listen(unsigned short port, const IpAddress& address
|
||||
{
|
||||
// Oops, socket is deaf
|
||||
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())
|
||||
{
|
||||
err() << "Failed to accept a new connection, the socket is not listening" << std::endl;
|
||||
return Error;
|
||||
return Status::Error;
|
||||
}
|
||||
|
||||
// Accept a new connection
|
||||
@ -125,7 +125,7 @@ Socket::Status TcpListener::accept(TcpSocket& socket)
|
||||
socket.close();
|
||||
socket.create(remote);
|
||||
|
||||
return Done;
|
||||
return Status::Done;
|
||||
}
|
||||
|
||||
} // namespace sf
|
||||
|
@ -53,7 +53,7 @@ const int flags = 0;
|
||||
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();
|
||||
|
||||
// Connection succeeded
|
||||
return Done;
|
||||
return Status::Done;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -154,7 +154,7 @@ Socket::Status TcpSocket::connect(const IpAddress& remoteAddress, unsigned short
|
||||
{
|
||||
// We got instantly connected! (it may no happen a lot...)
|
||||
setBlocking(blocking);
|
||||
return Done;
|
||||
return Status::Done;
|
||||
}
|
||||
|
||||
// Get the error status
|
||||
@ -165,7 +165,7 @@ Socket::Status TcpSocket::connect(const IpAddress& remoteAddress, unsigned short
|
||||
return status;
|
||||
|
||||
// Otherwise, wait until something happens to our socket (success, timeout or error)
|
||||
if (status == Socket::NotReady)
|
||||
if (status == Socket::Status::NotReady)
|
||||
{
|
||||
// Setup the selector
|
||||
fd_set selector;
|
||||
@ -185,7 +185,7 @@ Socket::Status TcpSocket::connect(const IpAddress& remoteAddress, unsigned short
|
||||
if (getRemoteAddress().has_value())
|
||||
{
|
||||
// Connection accepted
|
||||
status = Done;
|
||||
status = Status::Done;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -238,7 +238,7 @@ Socket::Status TcpSocket::send(const void* data, std::size_t size, std::size_t&
|
||||
if (!data || (size == 0))
|
||||
{
|
||||
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
|
||||
@ -257,14 +257,14 @@ Socket::Status TcpSocket::send(const void* data, std::size_t size, std::size_t&
|
||||
{
|
||||
Status status = priv::SocketImpl::getErrorStatus();
|
||||
|
||||
if ((status == NotReady) && sent)
|
||||
return Partial;
|
||||
if ((status == Status::NotReady) && sent)
|
||||
return Status::Partial;
|
||||
|
||||
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)
|
||||
{
|
||||
err() << "Cannot receive data from the network (the destination buffer is invalid)" << std::endl;
|
||||
return Error;
|
||||
return Status::Error;
|
||||
}
|
||||
|
||||
#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)
|
||||
{
|
||||
received = static_cast<std::size_t>(sizeReceived);
|
||||
return Done;
|
||||
return Status::Done;
|
||||
}
|
||||
else if (sizeReceived == 0)
|
||||
{
|
||||
return Socket::Disconnected;
|
||||
return Socket::Status::Disconnected;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -351,11 +351,11 @@ Socket::Status TcpSocket::send(Packet& packet)
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
// In the case of a partial send, record the location to resume from
|
||||
if (status == Partial)
|
||||
if (status == Status::Partial)
|
||||
{
|
||||
packet.m_sendPos += sent;
|
||||
}
|
||||
else if (status == Done)
|
||||
else if (status == Status::Done)
|
||||
{
|
||||
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);
|
||||
m_pendingPacket.SizeReceived += received;
|
||||
|
||||
if (status != Done)
|
||||
if (status != Status::Done)
|
||||
return status;
|
||||
}
|
||||
|
||||
@ -403,7 +403,7 @@ Socket::Status TcpSocket::receive(Packet& packet)
|
||||
// Receive a chunk of data
|
||||
std::size_t sizeToGet = std::min(packetSize - m_pendingPacket.Data.size(), sizeof(buffer));
|
||||
Status status = receive(buffer, sizeToGet, received);
|
||||
if (status != Done)
|
||||
if (status != Status::Done)
|
||||
return status;
|
||||
|
||||
// Append it into the packet
|
||||
@ -422,7 +422,7 @@ Socket::Status TcpSocket::receive(Packet& packet)
|
||||
// Clear the pending packet data
|
||||
m_pendingPacket = PendingPacket();
|
||||
|
||||
return Done;
|
||||
return Status::Done;
|
||||
}
|
||||
|
||||
|
||||
|
@ -38,7 +38,7 @@
|
||||
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
|
||||
if (address == IpAddress::Broadcast)
|
||||
return Error;
|
||||
return Status::Error;
|
||||
|
||||
// Bind the socket
|
||||
sockaddr_in addr = priv::SocketImpl::createAddress(address.toInteger(), port);
|
||||
if (::bind(getHandle(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1)
|
||||
{
|
||||
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 "
|
||||
<< "(the number of bytes to send is greater than sf::UdpSocket::MaxDatagramSize)" << std::endl;
|
||||
return Error;
|
||||
return Status::Error;
|
||||
}
|
||||
|
||||
// Build the target address
|
||||
@ -128,7 +128,7 @@ Socket::Status UdpSocket::send(const void* data, std::size_t size, const IpAddre
|
||||
if (sent < 0)
|
||||
return priv::SocketImpl::getErrorStatus();
|
||||
|
||||
return Done;
|
||||
return Status::Done;
|
||||
}
|
||||
|
||||
|
||||
@ -148,7 +148,7 @@ Socket::Status UdpSocket::receive(void* data,
|
||||
if (!data)
|
||||
{
|
||||
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
|
||||
@ -176,7 +176,7 @@ Socket::Status UdpSocket::receive(void* data,
|
||||
remoteAddress = IpAddress(ntohl(address.sin_addr.s_addr));
|
||||
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
|
||||
packet.clear();
|
||||
if ((status == Done) && (received > 0))
|
||||
if ((status == Status::Done) && (received > 0))
|
||||
packet.onReceive(m_buffer.data(), received);
|
||||
|
||||
return status;
|
||||
|
@ -93,19 +93,19 @@ Socket::Status SocketImpl::getErrorStatus()
|
||||
// so we have to make a special case for them in order
|
||||
// to avoid having double values in the switch case
|
||||
if ((errno == EAGAIN) || (errno == EINPROGRESS))
|
||||
return Socket::NotReady;
|
||||
return Socket::Status::NotReady;
|
||||
|
||||
// clang-format off
|
||||
switch (errno)
|
||||
{
|
||||
case EWOULDBLOCK: return Socket::NotReady;
|
||||
case ECONNABORTED: return Socket::Disconnected;
|
||||
case ECONNRESET: return Socket::Disconnected;
|
||||
case ETIMEDOUT: return Socket::Disconnected;
|
||||
case ENETRESET: return Socket::Disconnected;
|
||||
case ENOTCONN: return Socket::Disconnected;
|
||||
case EPIPE: return Socket::Disconnected;
|
||||
default: return Socket::Error;
|
||||
case EWOULDBLOCK: return Socket::Status::NotReady;
|
||||
case ECONNABORTED: return Socket::Status::Disconnected;
|
||||
case ECONNRESET: return Socket::Status::Disconnected;
|
||||
case ETIMEDOUT: return Socket::Status::Disconnected;
|
||||
case ENETRESET: return Socket::Status::Disconnected;
|
||||
case ENOTCONN: return Socket::Status::Disconnected;
|
||||
case EPIPE: return Socket::Status::Disconnected;
|
||||
default: return Socket::Status::Error;
|
||||
}
|
||||
// clang-format on
|
||||
}
|
||||
|
@ -75,15 +75,15 @@ Socket::Status SocketImpl::getErrorStatus()
|
||||
// clang-format off
|
||||
switch (WSAGetLastError())
|
||||
{
|
||||
case WSAEWOULDBLOCK: return Socket::NotReady;
|
||||
case WSAEALREADY: return Socket::NotReady;
|
||||
case WSAECONNABORTED: return Socket::Disconnected;
|
||||
case WSAECONNRESET: return Socket::Disconnected;
|
||||
case WSAETIMEDOUT: return Socket::Disconnected;
|
||||
case WSAENETRESET: return Socket::Disconnected;
|
||||
case WSAENOTCONN: return Socket::Disconnected;
|
||||
case WSAEISCONN: return Socket::Done; // when connecting a non-blocking socket
|
||||
default: return Socket::Error;
|
||||
case WSAEWOULDBLOCK: return Socket::Status::NotReady;
|
||||
case WSAEALREADY: return Socket::Status::NotReady;
|
||||
case WSAECONNABORTED: return Socket::Status::Disconnected;
|
||||
case WSAECONNRESET: return Socket::Status::Disconnected;
|
||||
case WSAETIMEDOUT: return Socket::Status::Disconnected;
|
||||
case WSAENETRESET: return Socket::Status::Disconnected;
|
||||
case WSAENOTCONN: return Socket::Status::Disconnected;
|
||||
case WSAEISCONN: return Socket::Status::Done; // when connecting a non-blocking socket
|
||||
default: return Socket::Status::Error;
|
||||
}
|
||||
// clang-format on
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user