___ _ _ ____ / | __________(_|_) __ \____ _ ____ __ / /| | / ___/ ___/ / / / / / __ \| |/_/ / / / / ___ |(__ ) /__/ / / /_/ / /_/ /> </ /_/ / /_/ |_/____/\___/_/_/_____/\____/_/|_|\__, / /____/
[ Home | What is AsciiDoxy? | Getting started | Reference documentation | Examples | Contributing | Changelog | GitHub ]
C++
This is an example demonstrating documentation for a C++ project. The used source code files come from the SFML project.
This is not meant to be complete documentation. Some links in the documentation may not work due to missing documentation. |
# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path
# into which the generated documentation will be written. If a relative path is
# entered, it will be relative to the location where doxygen was started. If
# left blank the current directory will be used.
OUTPUT_DIRECTORY = $(OUTPUT_DIR)
# If the GENERATE_HTML tag is set to YES, doxygen will generate HTML output
# The default value is: YES.
GENERATE_HTML = NO
# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output.
# The default value is: YES.
GENERATE_LATEX = NO
# If the GENERATE_XML tag is set to YES, doxygen will generate an XML file that
# captures the structure of the code including all documentation.
# The default value is: NO.
GENERATE_XML = YES
${insert("sf::UdpSocket")}
${insert("sf::Packet")}
${insert("sf::IpAddress")}
UdpSocket
#include <UdpSocket.hpp>
class sf::UdpSocket
Specialized socket using the UDP protocol.
A UDP socket is a connectionless socket. Instead of connecting once to a remote host, like TCP sockets, it can send to and receive from any host at any time.
It is a datagram protocol: bounded blocks of data (datagrams) are transfered over the network rather than a continuous stream of data (TCP). Therefore, one call to send will always match one call to receive (if the datagram is not lost), with the same data that was sent.
The UDP protocol is lightweight but unreliable. Unreliable means that datagrams may be duplicated, be lost or arrive reordered. However, if a datagram arrives, its data is guaranteed to be valid.
UDP is generally used for real-time communication (audio or video streaming, real-time games, etc.) where speed is crucial and lost data doesn’t matter much.
Sending and receiving data can use either the low-level or the high-level functions. The low-level functions process a raw sequence of bytes, whereas the high-level interface uses packets (see sf::Packet), which are easier to use and provide more safety regarding the data that is exchanged. You can look at the sf::Packet class to get more details about how they work.
It is important to note that UdpSocket is unable to send datagrams bigger than MaxDatagramSize. In this case, it returns an error and doesn’t send anything. This applies to both raw data and packets. Indeed, even packets are unable to split and recompose data, due to the unreliability of the protocol (dropped, mixed or duplicated datagrams may lead to a big mess when trying to recompose a packet).
If the socket is bound to a port, it is automatically unbound from it when the socket is destroyed. However, you can unbind the socket explicitly with the Unbind function if necessary, to stop receiving messages or make the port available for other sockets.
Usage example:
// ----- The client -----
// Create a socket and bind it to the port 55001
<<cpp-classsf_1_1UdpSocket,sf::UdpSocket>> socket;
socket.<<cpp-classsf_1_1UdpSocket_1ad764c3d06d90b4714dcc97a0d1647bcc,bind>>(55001);
// Send a message to 192.168.1.50 on port 55002
std::string message = "Hi, I am " + <<cpp-classsf_1_1IpAddress_1a4c31622ad87edca48adbb8e8ed00ee4a,sf::IpAddress::getLocalAddress>>().<<cpp-classsf_1_1IpAddress_1a88507954142d7fc2176cce7f36422340,toString>>();
socket.<<cpp-classsf_1_1UdpSocket_1a664ab8f26f37c21cc4de1b847c2efcca,send>>(message.c_str(), message.size() + 1, "192.168.1.50", 55002);
// Receive an answer (most likely from 192.168.1.50, but could be anyone else)
char buffer[1024];
std::size_t received = 0;
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> sender;
unsigned short port;
socket.<<cpp-classsf_1_1UdpSocket_1ade9ca0f7ed7919136917b0b997a9833a,receive>>(buffer, sizeof(buffer), received, sender, port);
std::cout << sender.ToString() << " said: " << buffer << std::endl;
// ----- The server -----
// Create a socket and bind it to the port 55002
<<cpp-classsf_1_1UdpSocket,sf::UdpSocket>> socket;
socket.<<cpp-classsf_1_1UdpSocket_1ad764c3d06d90b4714dcc97a0d1647bcc,bind>>(55002);
// Receive a message from anyone
char buffer[1024];
std::size_t received = 0;
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> sender;
unsigned short port;
socket.<<cpp-classsf_1_1UdpSocket_1ade9ca0f7ed7919136917b0b997a9833a,receive>>(buffer, sizeof(buffer), received, sender, port);
std::cout << sender.ToString() << " said: " << buffer << std::endl;
// Send an answer
std::string message = "Welcome " + sender.<<cpp-classsf_1_1IpAddress_1a88507954142d7fc2176cce7f36422340,toString>>();
socket.<<cpp-classsf_1_1UdpSocket_1a664ab8f26f37c21cc4de1b847c2efcca,send>>(message.c_str(), message.size() + 1, sender, port);
See
sf::Socket, sf::TcpSocket, sf::Packet |
Public Enclosed Types |
|
---|---|
Public Constructors |
|
Public Methods |
|
@0
#include <UdpSocket.hpp>
enum sf::UdpSocket::@0
The maximum number of bytes that can be sent in a single UDP datagram. |
Members
UdpSocket
UdpSocket()
Default constructor.
getLocalPort
unsigned short getLocalPort() const
Get the port to which the socket is bound locally.
If the socket is not bound to a port, this function returns 0.
See
|
Returns |
|
---|
bind
Status bind(unsigned short port,
const IpAddress & address)
Bind the socket to a specific port.
Binding the socket to a port is necessary for being able to receive data on that port.
When providing sf::Socket::AnyPort as port, the listener will request an available port from the system. The chosen port can be retrieved by calling getLocalPort().
Since the socket can only be bound to a single port at any given moment, if it is already bound when this function is called, it will be unbound from the previous port before being bound to the new one.
See
|
Parameters |
|
---|---|
Returns |
|
unbind
void unbind()
Unbind the socket from the local port to which it is bound.
The port that the socket was previously bound to is immediately made available to the operating system after this function is called. This means that a subsequent call to bind() will be able to re-bind the port if no other process has done so in the mean time. If the socket is not bound to a port, this function has no effect.
See
|
send
Status send(const void * data,
std::size_t size,
const IpAddress & remoteAddress,
unsigned short remotePort)
Send raw data to a remote peer.
Make sure that size is not greater than UdpSocket::MaxDatagramSize, otherwise this function will fail and no data will be sent.
See
|
Parameters |
|
---|---|
Returns |
|
receive
Status receive(void * data,
std::size_t size,
std::size_t & received,
IpAddress & remoteAddress,
unsigned short & remotePort)
Receive raw data from a remote peer.
In blocking mode, this function will wait until some bytes are actually received. Be careful to use a buffer which is large enough for the data that you intend to receive, if it is too small then an error will be returned and all the data will be lost.
See
|
Parameters |
|
---|---|
Returns |
|
send
Send a formatted packet of data to a remote peer.
Make sure that the packet size is not greater than UdpSocket::MaxDatagramSize, otherwise this function will fail and no data will be sent.
See
|
Parameters |
|
---|---|
Returns |
|
receive
Receive a formatted packet of data from a remote peer.
In blocking mode, this function will wait until the whole packet has been received.
See
|
Parameters |
|
---|---|
Returns |
|
Packet
#include <Packet.hpp>
class sf::Packet
Utility class to build blocks of data to transfer over the network.
Packets provide a safe and easy way to serialize data, in order to send it over the network using sockets (sf::TcpSocket, sf::UdpSocket).
Packets solve 2 fundamental problems that arise when transferring data over the network:
-
data is interpreted correctly according to the endianness
-
the bounds of the packet are preserved (one send == one receive)
The sf::Packet class provides both input and output modes. It is designed to follow the behavior of standard C++ streams, using operators >> and << to extract and insert data.
It is recommended to use only fixed-size types (like sf::Int32, etc.), to avoid possible differences between the sender and the receiver. Indeed, the native C++ types may have different sizes on two platforms and your data may be corrupted if that happens.
Usage example:
sf::Uint32 x = 24;
std::string s = "hello";
double d = 5.89;
// Group the variables to send into a packet
<<cpp-classsf_1_1Packet,sf::Packet>> packet;
packet << x << s << d;
// Send it over the network (socket is a valid sf::TcpSocket)
socket.send(packet);
-----------------------------------------------------------------
// Receive the packet at the other end
<<cpp-classsf_1_1Packet,sf::Packet>> packet;
socket.receive(packet);
// Extract the variables contained in the packet
sf::Uint32 x;
std::string s;
double d;
if (packet >> x >> s >> d)
{
// Data extracted successfully...
}
Packets have built-in operator >> and << overloads for standard types:
-
bool
-
fixed-size integer types (sf::Int8/16/32, sf::Uint8/16/32)
-
floating point numbers (float, double)
-
string types (char*, wchar_t*, std::string, std::wstring, sf::String)
Like standard streams, it is also possible to define your own overloads of operators >> and << in order to handle your custom types.
struct MyStruct
{
float number;
sf::Int8 integer;
std::string str;
};
<<cpp-classsf_1_1Packet,sf::Packet>>& <<cpp-classsf_1_1Packet_1ae02c874e0aac18a0497fca982a8f9083,operator <<>>(<<cpp-classsf_1_1Packet,sf::Packet>>& packet, const MyStruct& m)
{
return packet << m.number << m.integer << m.str;
}
<<cpp-classsf_1_1Packet,sf::Packet>>& <<cpp-classsf_1_1Packet_1a8b6403506fec6b69f033278de33c8145,operator >>>>(<<cpp-classsf_1_1Packet,sf::Packet>>& packet, MyStruct& m)
{
return packet >> m.number >> m.integer >> m.str;
}
Packets also provide an extra feature that allows to apply custom transformations to the data before it is sent, and after it is received. This is typically used to handle automatic compression or encryption of the data. This is achieved by inheriting from sf::Packet, and overriding the onSend and onReceive functions.
Here is an example:
class ZipPacket : public <<cpp-classsf_1_1Packet,sf::Packet>>
{
virtual const void* <<cpp-classsf_1_1Packet_1af0003506bcb290407dcf5fe7f13a887d,onSend>>(std::size_t& size)
{
const void* srcData = <<cpp-classsf_1_1Packet_1a998b70df024bee4792e2ecdc915ae46e,getData>>();
std::size_t srcSize = <<cpp-classsf_1_1Packet_1a0fae6eccf2ca704fc5099cd90a9f56f7,getDataSize>>();
return MySuperZipFunction(srcData, srcSize, &size);
}
virtual void <<cpp-classsf_1_1Packet_1ab71a31ef0f1d5d856de6f9fc75434128,onReceive>>(const void* data, std::size_t size)
{
std::size_t dstSize;
const void* dstData = MySuperUnzipFunction(data, size, &dstSize);
<<cpp-classsf_1_1Packet_1a7dd6e429b87520008326c4d71f1cf011,append>>(dstData, dstSize);
}
};
// Use like regular packets:
ZipPacket packet;
packet << x << s << d;
...
See
sf::TcpSocket, sf::UdpSocket |
Public Constructors |
|
---|---|
Public Destructors |
|
Public Operators |
|
Public Methods |
|
Protected Methods |
|
Members
Packet
Packet()
Default constructor.
Creates an empty packet.
~Packet
~Packet()
Virtual destructor.
operator>>
Packet & operator>>(bool & data)
Overload of operator >> to read data from the packet
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(Int8 & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(Uint8 & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(Int16 & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(Uint16 & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(Int32 & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(Uint32 & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(Int64 & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(Uint64 & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(float & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(double & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(char * data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(std::string & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(wchar_t * data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(std::wstring & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator>>
Packet & operator>>(String & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(bool data)
Overload of operator << to write data into the packet
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(Int8 data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(Uint8 data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(Int16 data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(Uint16 data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(Int32 data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(Uint32 data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(Int64 data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(Uint64 data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(float data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(double data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(const char * data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(const std::string & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(const wchar_t * data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(const std::wstring & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
operator<<
Packet & operator<<(const String & data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters |
|
---|---|
Returns |
|
append
void append(const void * data,
std::size_t sizeInBytes)
Append data to the end of the packet.
See
|
See
|
Parameters |
|
---|
getReadPosition
std::size_t getReadPosition() const
Get the current reading position in the packet.
The next read operation will read data from this position
See
|
Returns |
|
---|
getData
const void * getData() const
Get a pointer to the data contained in the packet.
Warning: the returned pointer may become invalid after you append data to the packet, therefore it should never be stored. The return pointer is NULL if the packet is empty.
See
|
getDataSize
std::size_t getDataSize() const
Get the size of the data contained in the packet.
This function returns the number of bytes pointed to by what getData returns.
See
|
Returns |
|
---|
endOfPacket
bool endOfPacket() const
Tell if the reading position has reached the end of the packet.
This function is useful to know if there is some data left to be read, without actually reading it.
See
operator bool |
Returns |
|
---|
onSend
const void * onSend(std::size_t & size)
Called before the packet is sent over the network.
This function can be defined by derived classes to transform the data before it is sent; this can be used for compression, encryption, etc. The function must return a pointer to the modified data, as well as the number of bytes pointed. The default implementation provides the packet’s data without transforming it.
See
|
Parameters |
|
---|
onReceive
void onReceive(const void * data,
std::size_t size)
Called after the packet is received over the network.
This function can be defined by derived classes to transform the data after it is received; this can be used for decompression, decryption, etc. The function receives a pointer to the received data, and must fill the packet with the transformed bytes. The default implementation fills the packet directly without transforming the data.
See
|
Parameters |
|
---|
IpAddress
#include <IpAddress.hpp>
class sf::IpAddress
Encapsulate an IPv4 network address.
sf::IpAddress is a utility class for manipulating network addresses. It provides a set a implicit constructors and conversion functions to easily build or transform an IP address from/to various representations.
Usage example:
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> a0; // an invalid address
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> a1 = <<cpp-classsf_1_1IpAddress_1a4619b4abbe3c8fef056e7299db967404,sf::IpAddress::None>>; // an invalid address (same as a0)
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> a2("127.0.0.1"); // the local host address
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> a3 = <<cpp-classsf_1_1IpAddress_1aa93d1d57b65d243f2baf804b6035465c,sf::IpAddress::Broadcast>>; // the broadcast address
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> a4(192, 168, 1, 56); // a local address
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> a5("my_computer"); // a local address created from a network name
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> a6("89.54.1.169"); // a distant address
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> a7("www.google.com"); // a distant address created from a network name
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> a8 = <<cpp-classsf_1_1IpAddress_1a4c31622ad87edca48adbb8e8ed00ee4a,sf::IpAddress::getLocalAddress>>(); // my address on the local network
<<cpp-classsf_1_1IpAddress,sf::IpAddress>> a9 = <<cpp-classsf_1_1IpAddress_1a5c5cbf67e4aacf23c24f2ad991df4c55,sf::IpAddress::getPublicAddress>>(); // my address on the internet
Note that sf::IpAddress currently doesn’t support IPv6 nor other types of network addresses.
Public Constructors |
|
---|---|
Public Variables |
|
Public Static Methods |
|
Public Methods |
|
Members
IpAddress
IpAddress()
Default constructor.
This constructor creates an empty (invalid) address
IpAddress
IpAddress(const std::string & address)
Construct the address from a string.
Here address can be either a decimal address (ex: "192.168.1.56") or a network name (ex: "localhost").
Parameters |
|
---|
IpAddress
IpAddress(const char * address)
Construct the address from a string.
Here address can be either a decimal address (ex: "192.168.1.56") or a network name (ex: "localhost"). This is equivalent to the constructor taking a std::string parameter, it is defined for convenience so that the implicit conversions from literal strings to IpAddress work.
Parameters |
|
---|
IpAddress
IpAddress(Uint8 byte0,
Uint8 byte1,
Uint8 byte2,
Uint8 byte3)
Construct the address from 4 bytes.
Calling IpAddress(a, b, c, d) is equivalent to calling IpAddress("a.b.c.d"), but safer as it doesn’t have to parse a string to get the address components.
Parameters |
|
---|
IpAddress
IpAddress(Uint32 address)
Construct the address from a 32-bits integer.
This constructor uses the internal representation of the address directly. It should be used for optimization purposes, and only if you got that representation from IpAddress::toInteger().
See
|
Parameters |
|
---|
const IpAddress None
Value representing an empty/invalid address.
const IpAddress Any
Value representing any address (0.0.0.0)
const IpAddress LocalHost
The "localhost" address (for connecting a computer to itself locally)
const IpAddress Broadcast
The "broadcast" address (for sending UDP messages to everyone on a local network)
getLocalAddress
static IpAddress getLocalAddress()
Get the computer’s local address.
The local address is the address of the computer from the LAN point of view, i.e. something like 192.168.1.56. It is meaningful only for communications over the local network. Unlike getPublicAddress, this function is fast and may be used safely anywhere.
See
|
Returns |
|
---|
getPublicAddress
static IpAddress getPublicAddress(Time timeout = Time::Zero)
Get the computer’s public address.
The public address is the address of the computer from the internet point of view, i.e. something like 89.54.1.169. It is necessary for communications over the world wide web. The only way to get a public address is to ask it to a distant website; as a consequence, this function depends on both your network connection and the server, and may be very slow. You should use it as few as possible. Because this function depends on the network connection and on a distant server, you may use a time limit if you don’t want your program to be possibly stuck waiting in case there is a problem; this limit is deactivated by default.
See
|
Parameters |
|
---|---|
Returns |
|
toString
std::string toString() const
Get a string representation of the address.
The returned string is the decimal representation of the IP address (like "192.168.1.56"), even if it was constructed from a host name.
See
|
Returns |
|
---|
toInteger
Uint32 toInteger() const
Get an integer representation of the address.
The returned number is the internal representation of the address, and should be used for optimization purposes only (like sending the address through a socket). The integer produced by this function can then be converted back to a sf::IpAddress with the proper constructor.
See
|
Returns |
|
---|