mirror of
https://github.com/SFML/CSFML.git
synced 2026-10-27 13:36:12 +01:00
Update Network module code
This commit is contained in:
1 parent
2adc2db065
commit
58cd82b532
9 files changed
+98
-2
No files matched your search
@@ -132,7 +132,7 @@ CSFML_NETWORK_API void sfIpAddress_toString(sfIpAddress address, char* string);
|
|||||||
/// address, and should be used for optimization purposes only
|
/// address, and should be used for optimization purposes only
|
||||||
/// (like sending the address through a socket).
|
/// (like sending the address through a socket).
|
||||||
/// The integer produced by this function can then be converted
|
/// The integer produced by this function can then be converted
|
||||||
/// back to a sfIpAddress with sfIpAddress_FromInteger.
|
/// back to a sfIpAddress with sfIpAddress_fromInteger.
|
||||||
///
|
///
|
||||||
/// \param address Address object
|
/// \param address Address object
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -75,6 +75,8 @@ CSFML_NETWORK_API void sfPacket_append(sfPacket* packet, const void* data, size_
|
|||||||
///
|
///
|
||||||
/// The next read operation will read data from this position
|
/// The next read operation will read data from this position
|
||||||
///
|
///
|
||||||
|
/// \param packet Packet object
|
||||||
|
///
|
||||||
/// \return The byte offset of the current read position
|
/// \return The byte offset of the current read position
|
||||||
///
|
///
|
||||||
/// \see append
|
/// \see append
|
||||||
@@ -163,6 +165,8 @@ CSFML_NETWORK_API int16_t sfPacket_readInt16(sfPacket* packet);
|
|||||||
CSFML_NETWORK_API uint16_t sfPacket_readUint16(sfPacket* packet);
|
CSFML_NETWORK_API uint16_t sfPacket_readUint16(sfPacket* packet);
|
||||||
CSFML_NETWORK_API int32_t sfPacket_readInt32(sfPacket* packet);
|
CSFML_NETWORK_API int32_t sfPacket_readInt32(sfPacket* packet);
|
||||||
CSFML_NETWORK_API uint32_t sfPacket_readUint32(sfPacket* packet);
|
CSFML_NETWORK_API uint32_t sfPacket_readUint32(sfPacket* packet);
|
||||||
|
CSFML_NETWORK_API int64_t sfPacket_readInt64(sfPacket* packet);
|
||||||
|
CSFML_NETWORK_API uint64_t sfPacket_readUint64(sfPacket* packet);
|
||||||
CSFML_NETWORK_API float sfPacket_readFloat(sfPacket* packet);
|
CSFML_NETWORK_API float sfPacket_readFloat(sfPacket* packet);
|
||||||
CSFML_NETWORK_API double sfPacket_readDouble(sfPacket* packet);
|
CSFML_NETWORK_API double sfPacket_readDouble(sfPacket* packet);
|
||||||
CSFML_NETWORK_API void sfPacket_readString(sfPacket* packet, char* string);
|
CSFML_NETWORK_API void sfPacket_readString(sfPacket* packet, char* string);
|
||||||
@@ -181,6 +185,8 @@ CSFML_NETWORK_API void sfPacket_writeInt16(sfPacket* packet, int16_t);
|
|||||||
CSFML_NETWORK_API void sfPacket_writeUint16(sfPacket* packet, uint16_t);
|
CSFML_NETWORK_API void sfPacket_writeUint16(sfPacket* packet, uint16_t);
|
||||||
CSFML_NETWORK_API void sfPacket_writeInt32(sfPacket* packet, int32_t);
|
CSFML_NETWORK_API void sfPacket_writeInt32(sfPacket* packet, int32_t);
|
||||||
CSFML_NETWORK_API void sfPacket_writeUint32(sfPacket* packet, uint32_t);
|
CSFML_NETWORK_API void sfPacket_writeUint32(sfPacket* packet, uint32_t);
|
||||||
|
CSFML_NETWORK_API void sfPacket_writeInt64(sfPacket* packet, int64_t);
|
||||||
|
CSFML_NETWORK_API void sfPacket_writeUint64(sfPacket* packet, uint64_t);
|
||||||
CSFML_NETWORK_API void sfPacket_writeFloat(sfPacket* packet, float);
|
CSFML_NETWORK_API void sfPacket_writeFloat(sfPacket* packet, float);
|
||||||
CSFML_NETWORK_API void sfPacket_writeDouble(sfPacket* packet, double);
|
CSFML_NETWORK_API void sfPacket_writeDouble(sfPacket* packet, double);
|
||||||
CSFML_NETWORK_API void sfPacket_writeString(sfPacket* packet, const char* string);
|
CSFML_NETWORK_API void sfPacket_writeString(sfPacket* packet, const char* string);
|
||||||
|
|||||||
@@ -99,6 +99,10 @@ CSFML_NETWORK_API unsigned short sfTcpListener_getLocalPort(const sfTcpListener*
|
|||||||
/// If the socket was previously listening to another port,
|
/// If the socket was previously listening to another port,
|
||||||
/// it will be stopped first and bound to the new port.
|
/// it will be stopped first and bound to the new port.
|
||||||
///
|
///
|
||||||
|
/// When providing `sfTcpListener_anyPort()` as port, the listener
|
||||||
|
/// will request an available port from the system.
|
||||||
|
/// The chosen port can be retrieved by calling `sfTcpListener_getLocalPort()`.
|
||||||
|
///
|
||||||
/// If there is no specific address to listen to, pass sfIpAddress_Any
|
/// If there is no specific address to listen to, pass sfIpAddress_Any
|
||||||
///
|
///
|
||||||
/// \param listener TCP listener object
|
/// \param listener TCP listener object
|
||||||
@@ -110,6 +114,17 @@ CSFML_NETWORK_API unsigned short sfTcpListener_getLocalPort(const sfTcpListener*
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
CSFML_NETWORK_API sfSocketStatus sfTcpListener_listen(sfTcpListener* listener, unsigned short port, sfIpAddress address);
|
CSFML_NETWORK_API sfSocketStatus sfTcpListener_listen(sfTcpListener* listener, unsigned short port, sfIpAddress address);
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
/// \brief Stop listening and close the socket
|
||||||
|
///
|
||||||
|
/// This function gracefully stops the listener. If the
|
||||||
|
/// socket is not listening, this function has no effect.
|
||||||
|
///
|
||||||
|
/// \param listener TCP listener object
|
||||||
|
///
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
CSFML_NETWORK_API void sfTcpListener_close(sfTcpListener* listener);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief Accept a new connection
|
/// \brief Accept a new connection
|
||||||
///
|
///
|
||||||
@@ -127,3 +142,12 @@ CSFML_NETWORK_API sfSocketStatus sfTcpListener_listen(sfTcpListener* listener, u
|
|||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
CSFML_NETWORK_API sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** connected);
|
CSFML_NETWORK_API sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** connected);
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
/// \brief Return the special value that tells the system
|
||||||
|
/// to pick any available port
|
||||||
|
///
|
||||||
|
/// \return The value to use for any port
|
||||||
|
///
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
CSFML_NETWORK_API unsigned short sfTcpListener_anyPort(void);
|
||||||
@@ -102,6 +102,10 @@ CSFML_NETWORK_API unsigned short sfUdpSocket_getLocalPort(const sfUdpSocket* soc
|
|||||||
/// system to automatically pick an available port, and then
|
/// system to automatically pick an available port, and then
|
||||||
/// call sfUdpSocket_getLocalPort to retrieve the chosen port.
|
/// call sfUdpSocket_getLocalPort to retrieve the chosen port.
|
||||||
///
|
///
|
||||||
|
/// When providing `sfUdpSocket_anyPort()` as port, the listener
|
||||||
|
/// will request an available port from the system.
|
||||||
|
/// The chosen port can be retrieved by calling `sfUdpSocket_getLocalPort()`.
|
||||||
|
///
|
||||||
/// If there is no specific address to listen to, pass sfIpAddress_Any
|
/// If there is no specific address to listen to, pass sfIpAddress_Any
|
||||||
///
|
///
|
||||||
/// \param socket UDP socket object
|
/// \param socket UDP socket object
|
||||||
@@ -215,3 +219,12 @@ CSFML_NETWORK_API sfSocketStatus
|
|||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
CSFML_NETWORK_API unsigned int sfUdpSocket_maxDatagramSize(void);
|
CSFML_NETWORK_API unsigned int sfUdpSocket_maxDatagramSize(void);
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
/// \brief Return the special value that tells the system
|
||||||
|
/// to pick any available port
|
||||||
|
///
|
||||||
|
/// \return The value to use for any port
|
||||||
|
///
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
CSFML_NETWORK_API unsigned short sfUdpSocket_anyPort(void);
|
||||||
@@ -48,7 +48,7 @@ namespace
|
|||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
const sfIpAddress sfIpAddress_None = sfIpAddress_fromBytes(0, 0, 0, 0);
|
const sfIpAddress sfIpAddress_None = {{0}};
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@@ -156,6 +156,20 @@ uint32_t sfPacket_readUint32(sfPacket* packet)
|
|||||||
*packet >> value;
|
*packet >> value;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
int64_t sfPacket_readInt64(sfPacket* packet)
|
||||||
|
{
|
||||||
|
assert(packet);
|
||||||
|
int64_t value = 0;
|
||||||
|
*packet >> value;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
uint64_t sfPacket_readUint64(sfPacket* packet)
|
||||||
|
{
|
||||||
|
assert(packet);
|
||||||
|
uint64_t value = 0;
|
||||||
|
*packet >> value;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
float sfPacket_readFloat(sfPacket* packet)
|
float sfPacket_readFloat(sfPacket* packet)
|
||||||
{
|
{
|
||||||
assert(packet);
|
assert(packet);
|
||||||
@@ -219,6 +233,16 @@ void sfPacket_writeUint32(sfPacket* packet, uint32_t value)
|
|||||||
assert(packet);
|
assert(packet);
|
||||||
*packet << value;
|
*packet << value;
|
||||||
}
|
}
|
||||||
|
void sfPacket_writeInt64(sfPacket* packet, int64_t value)
|
||||||
|
{
|
||||||
|
assert(packet);
|
||||||
|
*packet << value;
|
||||||
|
}
|
||||||
|
void sfPacket_writeUint64(sfPacket* packet, uint64_t value)
|
||||||
|
{
|
||||||
|
assert(packet);
|
||||||
|
*packet << value;
|
||||||
|
}
|
||||||
void sfPacket_writeFloat(sfPacket* packet, float value)
|
void sfPacket_writeFloat(sfPacket* packet, float value)
|
||||||
{
|
{
|
||||||
assert(packet);
|
assert(packet);
|
||||||
|
|||||||
@@ -86,6 +86,14 @@ sfSocketStatus sfTcpListener_listen(sfTcpListener* listener, unsigned short port
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
void sfTcpListener_close(sfTcpListener* listener)
|
||||||
|
{
|
||||||
|
assert(listener);
|
||||||
|
listener->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** connected)
|
sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** connected)
|
||||||
{
|
{
|
||||||
@@ -102,3 +110,10 @@ sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** conne
|
|||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
unsigned short sfTcpListener_anyPort()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -203,3 +203,10 @@ unsigned int sfUdpSocket_maxDatagramSize()
|
|||||||
{
|
{
|
||||||
return sf::UdpSocket::MaxDatagramSize;
|
return sf::UdpSocket::MaxDatagramSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
unsigned short sfUdpSocket_anyPort()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <catch2/catch_test_macros.hpp>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
TEST_CASE("[Network] sfIpAddress")
|
TEST_CASE("[Network] sfIpAddress")
|
||||||
{
|
{
|
||||||
SECTION("Constants")
|
SECTION("Constants")
|
||||||
@@ -10,6 +12,11 @@ TEST_CASE("[Network] sfIpAddress")
|
|||||||
CHECK(sfIpAddress_toInteger(sfIpAddress_Any) == 0);
|
CHECK(sfIpAddress_toInteger(sfIpAddress_Any) == 0);
|
||||||
CHECK(sfIpAddress_toInteger(sfIpAddress_LocalHost) == 0x7F000001);
|
CHECK(sfIpAddress_toInteger(sfIpAddress_LocalHost) == 0x7F000001);
|
||||||
CHECK(sfIpAddress_toInteger(sfIpAddress_Broadcast) == 0xFFFFFFFF);
|
CHECK(sfIpAddress_toInteger(sfIpAddress_Broadcast) == 0xFFFFFFFF);
|
||||||
|
|
||||||
|
CHECK(std::strcmp(sfIpAddress_None.address, "") == 0);
|
||||||
|
CHECK(std::strcmp(sfIpAddress_Any.address, "0.0.0.0") == 0);
|
||||||
|
CHECK(std::strcmp(sfIpAddress_LocalHost.address, "127.0.0.1") == 0);
|
||||||
|
CHECK(std::strcmp(sfIpAddress_Broadcast.address, "255.255.255.255") == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("sfIpAddress_fromString")
|
SECTION("sfIpAddress_fromString")
|
||||||
|
|||||||
Reference in new issue
Block a user