summaryrefslogtreecommitdiff
path: root/include/SFML/Network
diff options
context:
space:
mode:
authorChristoph Egger <Christoph.Egger@gmx.de>2008-05-20 13:58:13 +0200
committerChristoph Egger <Christoph.Egger@gmx.de>2008-05-20 13:58:13 +0200
commitff2dd2d0218cea258446af2c781298192404aa68 (patch)
treed36a5a7e71af593c65882d46619716cb0d82789d /include/SFML/Network
Imported Upstream version 1.2
Diffstat (limited to 'include/SFML/Network')
-rwxr-xr-xinclude/SFML/Network/IPAddress.hpp213
-rwxr-xr-xinclude/SFML/Network/Packet.hpp166
-rwxr-xr-xinclude/SFML/Network/Selector.hpp106
-rwxr-xr-xinclude/SFML/Network/Selector.inl108
-rwxr-xr-xinclude/SFML/Network/SocketHelper.hpp64
-rwxr-xr-xinclude/SFML/Network/SocketTCP.hpp220
-rwxr-xr-xinclude/SFML/Network/SocketUDP.hpp219
-rwxr-xr-xinclude/SFML/Network/Sockets.hpp45
-rwxr-xr-xinclude/SFML/Network/Unix/SocketHelper.hpp95
-rwxr-xr-xinclude/SFML/Network/Win32/SocketHelper.hpp90
10 files changed, 1326 insertions, 0 deletions
diff --git a/include/SFML/Network/IPAddress.hpp b/include/SFML/Network/IPAddress.hpp
new file mode 100755
index 0000000..c28e600
--- /dev/null
+++ b/include/SFML/Network/IPAddress.hpp
@@ -0,0 +1,213 @@
+////////////////////////////////////////////////////////////
+//
+// SFML - Simple and Fast Multimedia Library
+// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
+//
+// This software is provided 'as-is', without any express or implied warranty.
+// In no event will the authors be held liable for any damages arising from the use of this software.
+//
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it freely,
+// subject to the following restrictions:
+//
+// 1. The origin of this software must not be misrepresented;
+// you must not claim that you wrote the original software.
+// If you use this software in a product, an acknowledgment
+// in the product documentation would be appreciated but is not required.
+//
+// 2. Altered source versions must be plainly marked as such,
+// and must not be misrepresented as being the original software.
+//
+// 3. This notice may not be removed or altered from any source distribution.
+//
+////////////////////////////////////////////////////////////
+
+#ifndef SFML_IPADDRESS_HPP
+#define SFML_IPADDRESS_HPP
+
+////////////////////////////////////////////////////////////
+// Headers
+////////////////////////////////////////////////////////////
+#include <SFML/Config.hpp>
+#include <istream>
+#include <ostream>
+#include <string>
+
+
+namespace sf
+{
+////////////////////////////////////////////////////////////
+/// IPAddress provides easy manipulation of IP v4 addresses
+////////////////////////////////////////////////////////////
+class SFML_API IPAddress
+{
+public :
+
+ ////////////////////////////////////////////////////////////
+ /// Default constructor -- constructs an invalid address
+ ///
+ ////////////////////////////////////////////////////////////
+ IPAddress();
+
+ ////////////////////////////////////////////////////////////
+ /// Construct the address from a string
+ ///
+ /// \param Address : IP address ("xxx.xxx.xxx.xxx") or network name
+ ///
+ ////////////////////////////////////////////////////////////
+ IPAddress(const std::string& Address);
+
+ ////////////////////////////////////////////////////////////
+ /// Construct the address from a C-style string ;
+ /// Needed for implicit conversions from literal strings to IPAddress to work
+ ///
+ /// \param Address : IP address ("xxx.xxx.xxx.xxx") or network name
+ ///
+ ////////////////////////////////////////////////////////////
+ IPAddress(const char* Address);
+
+ ////////////////////////////////////////////////////////////
+ /// Construct the address from 4 bytes
+ ///
+ /// \param Byte0 : First byte of the address
+ /// \param Byte1 : Second byte of the address
+ /// \param Byte2 : Third byte of the address
+ /// \param Byte3 : Fourth byte of the address
+ ///
+ ////////////////////////////////////////////////////////////
+ IPAddress(Uint8 Byte0, Uint8 Byte1, Uint8 Byte2, Uint8 Byte3);
+
+ ////////////////////////////////////////////////////////////
+ /// Tell if the address is a valid one
+ ///
+ /// \return True if address has a valid syntax
+ ///
+ ////////////////////////////////////////////////////////////
+ bool IsValid() const;
+
+ ////////////////////////////////////////////////////////////
+ /// Get a string representation of the address
+ ///
+ /// \return String representation of the IP address ("xxx.xxx.xxx.xxx")
+ ///
+ ////////////////////////////////////////////////////////////
+ std::string ToString() const;
+
+ ////////////////////////////////////////////////////////////
+ /// Get the computer's local IP address (from the LAN point of view)
+ ///
+ /// \return Local IP address
+ ///
+ ////////////////////////////////////////////////////////////
+ static IPAddress GetLocalAddress();
+
+ ////////////////////////////////////////////////////////////
+ /// Get the computer's public IP address (from the web point of view).
+ /// The only way to get a public address is to ask it to a
+ /// distant website ; as a consequence, this function may be
+ /// very slow -- use it as few as possible !
+ ///
+ /// \return Public IP address
+ ///
+ ////////////////////////////////////////////////////////////
+ static IPAddress GetPublicAddress();
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator ==
+ ///
+ /// \param Other : Address to compare
+ ///
+ /// \return True if *this == Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator ==(const IPAddress& Other) const;
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator !=
+ ///
+ /// \param Other : Address to compare
+ ///
+ /// \return True if *this != Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator !=(const IPAddress& Other) const;
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator <
+ ///
+ /// \param Other : Address to compare
+ ///
+ /// \return True if *this < Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator <(const IPAddress& Other) const;
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator >
+ ///
+ /// \param Other : Address to compare
+ ///
+ /// \return True if *this > Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator >(const IPAddress& Other) const;
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator <=
+ ///
+ /// \param Other : Address to compare
+ ///
+ /// \return True if *this <= Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator <=(const IPAddress& Other) const;
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator >=
+ ///
+ /// \param Other : Address to compare
+ ///
+ /// \return True if *this >= Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator >=(const IPAddress& Other) const;
+
+ ////////////////////////////////////////////////////////////
+ // Static member data
+ ////////////////////////////////////////////////////////////
+ static const IPAddress LocalHost; ///< Local host address (to connect to the same computer)
+
+private :
+
+ ////////////////////////////////////////////////////////////
+ // Member data
+ ////////////////////////////////////////////////////////////
+ Uint32 myAddress; ///< Address stored as an unsigned 32 bits integer
+};
+
+////////////////////////////////////////////////////////////
+/// Operator >> overload to extract an address from an input stream
+///
+/// \param Stream : Input stream
+/// \param Address : Address to extract
+///
+/// \return Reference to the input stream
+///
+////////////////////////////////////////////////////////////
+SFML_API std::istream& operator >>(std::istream& Stream, IPAddress& Address);
+
+////////////////////////////////////////////////////////////
+/// Operator << overload to print an address to an output stream
+///
+/// \param Stream : Output stream
+/// \param Address : Address to print
+///
+/// \return Reference to the output stream
+///
+////////////////////////////////////////////////////////////
+SFML_API std::ostream& operator <<(std::ostream& Stream, const IPAddress& Address);
+
+} // namespace sf
+
+
+#endif // SFML_IPADDRESS_HPP
diff --git a/include/SFML/Network/Packet.hpp b/include/SFML/Network/Packet.hpp
new file mode 100755
index 0000000..4d9631a
--- /dev/null
+++ b/include/SFML/Network/Packet.hpp
@@ -0,0 +1,166 @@
+////////////////////////////////////////////////////////////
+//
+// SFML - Simple and Fast Multimedia Library
+// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
+//
+// This software is provided 'as-is', without any express or implied warranty.
+// In no event will the authors be held liable for any damages arising from the use of this software.
+//
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it freely,
+// subject to the following restrictions:
+//
+// 1. The origin of this software must not be misrepresented;
+// you must not claim that you wrote the original software.
+// If you use this software in a product, an acknowledgment
+// in the product documentation would be appreciated but is not required.
+//
+// 2. Altered source versions must be plainly marked as such,
+// and must not be misrepresented as being the original software.
+//
+// 3. This notice may not be removed or altered from any source distribution.
+//
+////////////////////////////////////////////////////////////
+
+#ifndef SFML_PACKET_HPP
+#define SFML_PACKET_HPP
+
+////////////////////////////////////////////////////////////
+// Headers
+////////////////////////////////////////////////////////////
+#include <SFML/Config.hpp>
+#include <string>
+#include <vector>
+
+
+namespace sf
+{
+////////////////////////////////////////////////////////////
+/// Packet wraps data to send / to receive through the network
+////////////////////////////////////////////////////////////
+class SFML_API Packet
+{
+public :
+
+ ////////////////////////////////////////////////////////////
+ /// Default constructor
+ ///
+ ////////////////////////////////////////////////////////////
+ Packet();
+
+ ////////////////////////////////////////////////////////////
+ /// Virtual destructor
+ ///
+ ////////////////////////////////////////////////////////////
+ virtual ~Packet();
+
+ ////////////////////////////////////////////////////////////
+ /// Append data to the end of the packet
+ ///
+ /// \param Data : Pointer to the bytes to append
+ /// \param SizeInBytes : Number of bytes to append
+ ///
+ ////////////////////////////////////////////////////////////
+ void Append(const void* Data, std::size_t SizeInBytes);
+
+ ////////////////////////////////////////////////////////////
+ /// Clear the packet data
+ ///
+ ////////////////////////////////////////////////////////////
+ void Clear();
+
+ ////////////////////////////////////////////////////////////
+ /// Get a pointer to the data contained in the packet
+ /// Warning : the returned pointer may be invalid after you
+ /// append data to the packet
+ ///
+ /// \return Pointer to the data
+ ///
+ ////////////////////////////////////////////////////////////
+ const char* GetData() const;
+
+ ////////////////////////////////////////////////////////////
+ /// Get the size of the data contained in the packet
+ ///
+ /// \return Data size, in bytes
+ ///
+ ////////////////////////////////////////////////////////////
+ Uint32 GetDataSize() const;
+
+ ////////////////////////////////////////////////////////////
+ /// Tell if the packet is valid for reading
+ ///
+ /// \return True if data can be extracted from the packet
+ ///
+ ////////////////////////////////////////////////////////////
+ operator bool() const;
+
+ ////////////////////////////////////////////////////////////
+ /// Operator >> overloads to extract data from the packet
+ ///
+ ////////////////////////////////////////////////////////////
+ Packet& operator >>(Int8& Data);
+ Packet& operator >>(Uint8& Data);
+ Packet& operator >>(Int16& Data);
+ Packet& operator >>(Uint16& Data);
+ Packet& operator >>(Int32& Data);
+ Packet& operator >>(Uint32& Data);
+ Packet& operator >>(float& Data);
+ Packet& operator >>(double& Data);
+ Packet& operator >>(char* Data);
+ Packet& operator >>(std::string& Data);
+
+ ////////////////////////////////////////////////////////////
+ /// Operator << overloads to put data into the packet
+ ///
+ ////////////////////////////////////////////////////////////
+ Packet& operator <<(Int8 Data);
+ Packet& operator <<(Uint8 Data);
+ Packet& operator <<(Int16 Data);
+ Packet& operator <<(Uint16 Data);
+ Packet& operator <<(Int32 Data);
+ Packet& operator <<(Uint32 Data);
+ Packet& operator <<(float Data);
+ Packet& operator <<(double Data);
+ Packet& operator <<(const char* Data);
+ Packet& operator <<(const std::string& Data);
+
+private :
+
+ friend class SocketTCP;
+ friend class SocketUDP;
+
+ ////////////////////////////////////////////////////////////
+ /// Check if the packet can extract a given size of bytes
+ ///
+ /// \param Size : Size to check
+ ///
+ /// \return True if Size bytes can be read from the packet's data
+ ///
+ ////////////////////////////////////////////////////////////
+ bool CheckSize(std::size_t Size);
+
+ ////////////////////////////////////////////////////////////
+ /// Called before the packet is sent to the network
+ ///
+ ////////////////////////////////////////////////////////////
+ virtual void OnSend();
+
+ ////////////////////////////////////////////////////////////
+ /// Called after the packet has been received from the network
+ ///
+ ////////////////////////////////////////////////////////////
+ virtual void OnReceive();
+
+ ////////////////////////////////////////////////////////////
+ // Member data
+ ////////////////////////////////////////////////////////////
+ std::vector<char> myData; ///< Data stored in the packet
+ std::size_t myReadPos; ///< Current reading position in the packet
+ bool myIsValid; ///< Reading state of the packet
+};
+
+} // namespace sf
+
+
+#endif // SFML_PACKET_HPP
diff --git a/include/SFML/Network/Selector.hpp b/include/SFML/Network/Selector.hpp
new file mode 100755
index 0000000..a993937
--- /dev/null
+++ b/include/SFML/Network/Selector.hpp
@@ -0,0 +1,106 @@
+////////////////////////////////////////////////////////////
+//
+// SFML - Simple and Fast Multimedia Library
+// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
+//
+// This software is provided 'as-is', without any express or implied warranty.
+// In no event will the authors be held liable for any damages arising from the use of this software.
+//
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it freely,
+// subject to the following restrictions:
+//
+// 1. The origin of this software must not be misrepresented;
+// you must not claim that you wrote the original software.
+// If you use this software in a product, an acknowledgment
+// in the product documentation would be appreciated but is not required.
+//
+// 2. Altered source versions must be plainly marked as such,
+// and must not be misrepresented as being the original software.
+//
+// 3. This notice may not be removed or altered from any source distribution.
+//
+////////////////////////////////////////////////////////////
+
+#ifndef SFML_SELECTOR_HPP
+#define SFML_SELECTOR_HPP
+
+////////////////////////////////////////////////////////////
+// Headers
+////////////////////////////////////////////////////////////
+#include <SFML/Network/SocketUDP.hpp>
+#include <SFML/Network/SocketTCP.hpp>
+#include <vector>
+
+
+namespace sf
+{
+////////////////////////////////////////////////////////////
+/// Selector allow reading from multiple sockets
+/// without blocking. It's a kind of multiplexer
+////////////////////////////////////////////////////////////
+template <typename Type>
+class Selector
+{
+public :
+
+ ////////////////////////////////////////////////////////////
+ /// Default constructor
+ ///
+ ////////////////////////////////////////////////////////////
+ Selector();
+
+ ////////////////////////////////////////////////////////////
+ /// Add a socket to watch
+ ///
+ /// \param Socket : Socket to add
+ ///
+ ////////////////////////////////////////////////////////////
+ void Add(Type Socket);
+
+ ////////////////////////////////////////////////////////////
+ /// Remove a socket
+ ///
+ /// \param Socket : Socket to remove
+ ///
+ ////////////////////////////////////////////////////////////
+ void Remove(Type Socket);
+
+ ////////////////////////////////////////////////////////////
+ /// Remove all sockets
+ ///
+ ////////////////////////////////////////////////////////////
+ void Clear();
+
+ ////////////////////////////////////////////////////////////
+ /// Retrieve all the sockets of the selector which are
+ /// ready for reading or writing. This functions will return
+ /// either when at least one socket is ready, or when given time is out
+ ///
+ /// \param Sockets : Array to fill with sockets that are ready for reading
+ /// \param Timeout : Timeout, in seconds (0 by default : no timeout)
+ ///
+ /// \return True if a socket is ready, false if time was out
+ ///
+ ////////////////////////////////////////////////////////////
+ bool GetSocketsReady(std::vector<Type>& Sockets, float Timeout = 0.f);
+
+private :
+
+ ////////////////////////////////////////////////////////////
+ // Member data
+ ////////////////////////////////////////////////////////////
+ fd_set mySet; ///< Set of socket to watch
+ int myMaxSocket; ///< Maximum socket index
+};
+
+#include <SFML/Network/Selector.inl>
+
+// Let's define the two only valid types of Selector
+typedef Selector<SocketUDP> SelectorUDP;
+typedef Selector<SocketTCP> SelectorTCP;
+
+} // namespace sf
+
+
+#endif // SFML_SELECTOR_HPP
diff --git a/include/SFML/Network/Selector.inl b/include/SFML/Network/Selector.inl
new file mode 100755
index 0000000..10f4b21
--- /dev/null
+++ b/include/SFML/Network/Selector.inl
@@ -0,0 +1,108 @@
+////////////////////////////////////////////////////////////
+//
+// SFML - Simple and Fast Multimedia Library
+// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
+//
+// This software is provided 'as-is', without any express or implied warranty.
+// In no event will the authors be held liable for any damages arising from the use of this software.
+//
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it freely,
+// subject to the following restrictions:
+//
+// 1. The origin of this software must not be misrepresented;
+// you must not claim that you wrote the original software.
+// If you use this software in a product, an acknowledgment
+// in the product documentation would be appreciated but is not required.
+//
+// 2. Altered source versions must be plainly marked as such,
+// and must not be misrepresented as being the original software.
+//
+// 3. This notice may not be removed or altered from any source distribution.
+//
+////////////////////////////////////////////////////////////
+
+
+////////////////////////////////////////////////////////////
+/// Default constructor
+////////////////////////////////////////////////////////////
+template <typename Type>
+Selector<Type>::Selector() :
+myMaxSocket(0)
+{
+ Clear();
+}
+
+
+////////////////////////////////////////////////////////////
+/// Add a socket to watch
+////////////////////////////////////////////////////////////
+template <typename Type>
+void Selector<Type>::Add(Type Socket)
+{
+ FD_SET(Socket.mySocket, &mySet);
+
+ if (static_cast<int>(Socket.mySocket) > myMaxSocket)
+ myMaxSocket = static_cast<int>(Socket.mySocket);
+}
+
+
+////////////////////////////////////////////////////////////
+/// Remove a socket
+////////////////////////////////////////////////////////////
+template <typename Type>
+void Selector<Type>::Remove(Type Socket)
+{
+ FD_CLR(Socket.mySocket, &mySet);
+}
+
+
+////////////////////////////////////////////////////////////
+/// Remove all sockets
+////////////////////////////////////////////////////////////
+template <typename Type>
+void Selector<Type>::Clear()
+{
+ FD_ZERO(&mySet);
+
+ myMaxSocket = 0;
+}
+
+
+////////////////////////////////////////////////////////////
+/// Retrieve all the sockets of the selector which are
+/// ready for reading or writing. This functions will return
+/// either when at least one socket is ready, or when given time is out
+////////////////////////////////////////////////////////////
+template <typename Type>
+bool Selector<Type>::GetSocketsReady(std::vector<Type>& Sockets, float Timeout)
+{
+ // First of all, clear the array to fill...
+ Sockets.clear();
+
+ // Setup the timeout structure
+ timeval Time;
+ Time.tv_sec = static_cast<long>(Timeout);
+ Time.tv_usec = (static_cast<long>(Timeout * 1000) % 1000) * 1000;
+
+ // Use a copy of the set, as it will be modified by select()
+ fd_set Set = mySet;
+
+ // Wait until one of the sockets is ready for reading, or timeout is reached
+ if (select(myMaxSocket + 1, &Set, NULL, NULL, Timeout > 0 ? &Time : NULL) != 0)
+ {
+ // One or more sockets are ready : put them into the array
+ for (int i = 0; i < myMaxSocket + 1; ++i)
+ {
+ if (FD_ISSET(i, &Set))
+ Sockets.push_back(Type(static_cast<SocketHelper::SocketType>(i)));
+ }
+
+ return true;
+ }
+ else
+ {
+ // Timeout reached...
+ return false;
+ }
+}
diff --git a/include/SFML/Network/SocketHelper.hpp b/include/SFML/Network/SocketHelper.hpp
new file mode 100755
index 0000000..4f7a748
--- /dev/null
+++ b/include/SFML/Network/SocketHelper.hpp
@@ -0,0 +1,64 @@
+////////////////////////////////////////////////////////////
+//
+// SFML - Simple and Fast Multimedia Library
+// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
+//
+// This software is provided 'as-is', without any express or implied warranty.
+// In no event will the authors be held liable for any damages arising from the use of this software.
+//
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it freely,
+// subject to the following restrictions:
+//
+// 1. The origin of this software must not be misrepresented;
+// you must not claim that you wrote the original software.
+// If you use this software in a product, an acknowledgment
+// in the product documentation would be appreciated but is not required.
+//
+// 2. Altered source versions must be plainly marked as such,
+// and must not be misrepresented as being the original software.
+//
+// 3. This notice may not be removed or altered from any source distribution.
+//
+////////////////////////////////////////////////////////////
+
+#ifndef SFML_SOCKETHELPER_HPP
+#define SFML_SOCKETHELPER_HPP
+
+////////////////////////////////////////////////////////////
+// Headers
+////////////////////////////////////////////////////////////
+#include <SFML/Config.hpp>
+
+
+namespace sf
+{
+namespace Socket
+{
+ ////////////////////////////////////////////////////////////
+ /// Enumeration of status returned by socket functions
+ ////////////////////////////////////////////////////////////
+ enum Status
+ {
+ Done, ///< The socket has sent / received the data
+ NotReady, ///< The socket is not ready to send / receive data yet
+ Disconnected, ///< The TCP socket has been disconnected
+ Error ///< An unexpected error happened
+ };
+}
+
+} // namespace sf
+
+
+#ifdef SFML_SYSTEM_WINDOWS
+
+ #include <SFML/Network/Win32/SocketHelper.hpp>
+
+#else
+
+ #include <SFML/Network/Unix/SocketHelper.hpp>
+
+#endif
+
+
+#endif // SFML_SOCKETHELPER_HPP
diff --git a/include/SFML/Network/SocketTCP.hpp b/include/SFML/Network/SocketTCP.hpp
new file mode 100755
index 0000000..bc6254e
--- /dev/null
+++ b/include/SFML/Network/SocketTCP.hpp
@@ -0,0 +1,220 @@
+////////////////////////////////////////////////////////////
+//
+// SFML - Simple and Fast Multimedia Library
+// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
+//
+// This software is provided 'as-is', without any express or implied warranty.
+// In no event will the authors be held liable for any damages arising from the use of this software.
+//
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it freely,
+// subject to the following restrictions:
+//
+// 1. The origin of this software must not be misrepresented;
+// you must not claim that you wrote the original software.
+// If you use this software in a product, an acknowledgment
+// in the product documentation would be appreciated but is not required.
+//
+// 2. Altered source versions must be plainly marked as such,
+// and must not be misrepresented as being the original software.
+//
+// 3. This notice may not be removed or altered from any source distribution.
+//
+////////////////////////////////////////////////////////////
+
+#ifndef SFML_SOCKETTCP_HPP
+#define SFML_SOCKETTCP_HPP
+
+////////////////////////////////////////////////////////////
+// Headers
+////////////////////////////////////////////////////////////
+#include <SFML/Network/SocketHelper.hpp>
+#include <cstddef>
+
+
+namespace sf
+{
+class Packet;
+class IPAddress;
+template <typename> class Selector;
+
+
+////////////////////////////////////////////////////////////
+/// SocketTCP wraps a socket using TCP protocol to
+/// send data safely (but a bit slower)
+////////////////////////////////////////////////////////////
+class SFML_API SocketTCP
+{
+public :
+
+ ////////////////////////////////////////////////////////////
+ /// Default constructor
+ ///
+ ////////////////////////////////////////////////////////////
+ SocketTCP();
+
+ ////////////////////////////////////////////////////////////
+ /// Change the blocking state of the socket.
+ /// The default behaviour of a socket is blocking
+ ///
+ /// \param Blocking : Pass true to set the socket as blocking, or false for non-blocking
+ ///
+ ////////////////////////////////////////////////////////////
+ void SetBlocking(bool Blocking);
+
+ ////////////////////////////////////////////////////////////
+ /// Connect to another computer on a specified port
+ ///
+ /// \param Port : Port to use for transfers (warning : ports < 1024 are reserved)
+ /// \param HostAddress : IP Address of the host to connect to
+ ///
+ /// \return True if operation has been successful
+ ///
+ ////////////////////////////////////////////////////////////
+ bool Connect(unsigned short Port, const IPAddress& HostAddress);
+
+ ////////////////////////////////////////////////////////////
+ /// Listen to a specified port for incoming data or connections
+ ///
+ /// \param Port : Port to listen to
+ ///
+ /// \return True if operation has been successful
+ ///
+ ////////////////////////////////////////////////////////////
+ bool Listen(unsigned short Port);
+
+ ////////////////////////////////////////////////////////////
+ /// Wait for a connection (must be listening to a port).
+ /// This function will block if the socket is blocking
+ ///
+ /// \param Connected : Socket containing the connection with the connected client
+ /// \param Address : Pointer to an address to fill with client infos (NULL by default)
+ ///
+ /// \return Status code
+ ///
+ ////////////////////////////////////////////////////////////
+ Socket::Status Accept(SocketTCP& Connected, IPAddress* Address = NULL);
+
+ ////////////////////////////////////////////////////////////
+ /// Send an array of bytes to the host (must be connected first)
+ ///
+ /// \param Data : Pointer to the bytes to send
+ /// \param Size : Number of bytes to send
+ ///
+ /// \return Status code
+ ///
+ ////////////////////////////////////////////////////////////
+ Socket::Status Send(const char* Data, std::size_t Size);
+
+ ////////////////////////////////////////////////////////////
+ /// Receive an array of bytes from the host (must be connected first).
+ /// This function will block if the socket is blocking
+ ///
+ /// \param Data : Pointer to a byte array to fill (make sure it is big enough)
+ /// \param MaxSize : Maximum number of bytes to read
+ /// \param SizeReceived : Number of bytes received
+ ///
+ /// \return Status code
+ ///
+ ////////////////////////////////////////////////////////////
+ Socket::Status Receive(char* Data, std::size_t MaxSize, std::size_t& SizeReceived);
+
+ ////////////////////////////////////////////////////////////
+ /// Send a packet of data to the host (must be connected first)
+ ///
+ /// \param PacketToSend : Packet to send
+ ///
+ /// \return Status code
+ ///
+ ////////////////////////////////////////////////////////////
+ Socket::Status Send(Packet& PacketToSend);
+
+ ////////////////////////////////////////////////////////////
+ /// Receive a packet from the host (must be connected first).
+ /// This function will block if the socket is blocking
+ ///
+ /// \param PacketToReceive : Packet to fill with received data
+ ///
+ /// \return Status code
+ ///
+ ////////////////////////////////////////////////////////////
+ Socket::Status Receive(Packet& PacketToReceive);
+
+ ////////////////////////////////////////////////////////////
+ /// Close the socket
+ ///
+ /// \return True if operation has been successful
+ ///
+ ////////////////////////////////////////////////////////////
+ bool Close();
+
+ ////////////////////////////////////////////////////////////
+ /// Check if the socket is in a valid state ; this function
+ /// can be called any time to check if the socket is OK
+ ///
+ /// \return True if the socket is valid
+ ///
+ ////////////////////////////////////////////////////////////
+ bool IsValid() const;
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator ==
+ ///
+ /// \param Other : Socket to compare
+ ///
+ /// \return True if *this == Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator ==(const SocketTCP& Other) const;
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator !=
+ ///
+ /// \param Other : Socket to compare
+ ///
+ /// \return True if *this != Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator !=(const SocketTCP& Other) const;
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator <.
+ /// Provided for compatibility with standard containers, as
+ /// comparing two sockets doesn't make much sense...
+ ///
+ /// \param Other : Socket to compare
+ ///
+ /// \return True if *this < Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator <(const SocketTCP& Other) const;
+
+private :
+
+ friend class Selector<SocketTCP>;
+
+ ////////////////////////////////////////////////////////////
+ /// Construct the socket from a socket descriptor
+ /// (for internal use only)
+ ///
+ /// \param Descriptor : Socket descriptor
+ ///
+ ////////////////////////////////////////////////////////////
+ SocketTCP(SocketHelper::SocketType Descriptor);
+
+ ////////////////////////////////////////////////////////////
+ /// Create the socket
+ ///
+ ////////////////////////////////////////////////////////////
+ void Create();
+
+ ////////////////////////////////////////////////////////////
+ // Member data
+ ////////////////////////////////////////////////////////////
+ SocketHelper::SocketType mySocket; ///< Socket descriptor
+};
+
+} // namespace sf
+
+
+#endif // SFML_SOCKETTCP_HPP
diff --git a/include/SFML/Network/SocketUDP.hpp b/include/SFML/Network/SocketUDP.hpp
new file mode 100755
index 0000000..cca2522
--- /dev/null
+++ b/include/SFML/Network/SocketUDP.hpp
@@ -0,0 +1,219 @@
+////////////////////////////////////////////////////////////
+//
+// SFML - Simple and Fast Multimedia Library
+// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
+//
+// This software is provided 'as-is', without any express or implied warranty.
+// In no event will the authors be held liable for any damages arising from the use of this software.
+//
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it freely,
+// subject to the following restrictions:
+//
+// 1. The origin of this software must not be misrepresented;
+// you must not claim that you wrote the original software.
+// If you use this software in a product, an acknowledgment
+// in the product documentation would be appreciated but is not required.
+//
+// 2. Altered source versions must be plainly marked as such,
+// and must not be misrepresented as being the original software.
+//
+// 3. This notice may not be removed or altered from any source distribution.
+//
+////////////////////////////////////////////////////////////
+
+#ifndef SFML_SOCKETUDP_HPP
+#define SFML_SOCKETUDP_HPP
+
+////////////////////////////////////////////////////////////
+// Headers
+////////////////////////////////////////////////////////////
+#include <SFML/Network/SocketHelper.hpp>
+#include <cstddef>
+
+
+namespace sf
+{
+class Packet;
+class IPAddress;
+template <typename> class Selector;
+
+////////////////////////////////////////////////////////////
+/// SocketUDP wraps a socket using UDP protocol to
+/// send data fastly (but with less safety)
+////////////////////////////////////////////////////////////
+class SFML_API SocketUDP
+{
+public :
+
+ ////////////////////////////////////////////////////////////
+ /// Default constructor
+ ///
+ ////////////////////////////////////////////////////////////
+ SocketUDP();
+
+ ////////////////////////////////////////////////////////////
+ /// Change the blocking state of the socket.
+ /// The default behaviour of a socket is blocking
+ ///
+ /// \param Blocking : Pass true to set the socket as blocking, or false for non-blocking
+ ///
+ ////////////////////////////////////////////////////////////
+ void SetBlocking(bool Blocking);
+
+ ////////////////////////////////////////////////////////////
+ /// Bind the socket to a specific port
+ ///
+ /// \param Port : Port to bind the socket to
+ ///
+ /// \return True if operation has been successful
+ ///
+ ////////////////////////////////////////////////////////////
+ bool Bind(unsigned short Port);
+
+ ////////////////////////////////////////////////////////////
+ /// Unbind the socket from its previous port, if any
+ ///
+ /// \return True if operation has been successful
+ ///
+ ////////////////////////////////////////////////////////////
+ bool Unbind();
+
+ ////////////////////////////////////////////////////////////
+ /// Send an array of bytes
+ ///
+ /// \param Data : Pointer to the bytes to send
+ /// \param Size : Number of bytes to send
+ /// \param Address : Address of the computer to send the packet to
+ /// \param Port : Port to send the data to
+ ///
+ /// \return Status code
+ ///
+ ////////////////////////////////////////////////////////////
+ Socket::Status Send(const char* Data, std::size_t Size, const IPAddress& Address, unsigned short Port);
+
+ ////////////////////////////////////////////////////////////
+ /// Receive an array of bytes.
+ /// This function will block if the socket is blocking
+ ///
+ /// \param Data : Pointer to a byte array to fill (make sure it is big enough)
+ /// \param MaxSize : Maximum number of bytes to read
+ /// \param SizeReceived : Number of bytes received
+ /// \param Address : Address of the computer which sent the data
+ ///
+ /// \return Status code
+ ///
+ ////////////////////////////////////////////////////////////
+ Socket::Status Receive(char* Data, std::size_t MaxSize, std::size_t& SizeReceived, IPAddress& Address);
+
+ ////////////////////////////////////////////////////////////
+ /// Send a packet of data
+ ///
+ /// \param PacketToSend : Packet to send
+ /// \param Address : Address of the computer to send the packet to
+ /// \param Port : Port to send the data to
+ ///
+ /// \return Status code
+ ///
+ ////////////////////////////////////////////////////////////
+ Socket::Status Send(Packet& PacketToSend, const IPAddress& Address, unsigned short Port);
+
+ ////////////////////////////////////////////////////////////
+ /// Receive a packet.
+ /// This function will block if the socket is blocking
+ ///
+ /// \param PacketToReceive : Packet to fill with received data
+ /// \param Address : Address of the computer which sent the packet
+ ///
+ /// \return Status code
+ ///
+ ////////////////////////////////////////////////////////////
+ Socket::Status Receive(Packet& PacketToReceive, IPAddress& Address);
+
+ ////////////////////////////////////////////////////////////
+ /// Close the socket
+ ///
+ /// \return True if operation has been successful
+ ///
+ ////////////////////////////////////////////////////////////
+ bool Close();
+
+ ////////////////////////////////////////////////////////////
+ /// Check if the socket is in a valid state ; this function
+ /// can be called any time to check if the socket is OK
+ ///
+ /// \return True if the socket is valid
+ ///
+ ////////////////////////////////////////////////////////////
+ bool IsValid() const;
+
+ ////////////////////////////////////////////////////////////
+ /// Get the port the socket is currently bound to
+ ///
+ /// \return Current port (0 means the socket is not bound)
+ ///
+ ////////////////////////////////////////////////////////////
+ unsigned short GetPort() const;
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator ==
+ ///
+ /// \param Other : Socket to compare
+ ///
+ /// \return True if *this == Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator ==(const SocketUDP& Other) const;
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator !=
+ ///
+ /// \param Other : Socket to compare
+ ///
+ /// \return True if *this != Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator !=(const SocketUDP& Other) const;
+
+ ////////////////////////////////////////////////////////////
+ /// Comparison operator <.
+ /// Provided for compatibility with standard containers, as
+ /// comparing two sockets doesn't make much sense...
+ ///
+ /// \param Other : Socket to compare
+ ///
+ /// \return True if *this < Other
+ ///
+ ////////////////////////////////////////////////////////////
+ bool operator <(const SocketUDP& Other) const;
+
+private :
+
+ friend class Selector<SocketUDP>;
+
+ ////////////////////////////////////////////////////////////
+ /// Construct the socket from a socket descriptor
+ /// (for internal use only)
+ ///
+ /// \param Descriptor : Socket descriptor
+ ///
+ ////////////////////////////////////////////////////////////
+ SocketUDP(SocketHelper::SocketType Descriptor);
+
+ ////////////////////////////////////////////////////////////
+ /// Create the socket
+ ///
+ ////////////////////////////////////////////////////////////
+ void Create();
+
+ ////////////////////////////////////////////////////////////
+ // Member data
+ ////////////////////////////////////////////////////////////
+ SocketHelper::SocketType mySocket; ///< Socket identifier
+ unsigned short myPort; ///< Port to which the socket is bound
+};
+
+} // namespace sf
+
+
+#endif // SFML_SOCKETUDP_HPP
diff --git a/include/SFML/Network/Sockets.hpp b/include/SFML/Network/Sockets.hpp
new file mode 100755
index 0000000..b0fb8ea
--- /dev/null
+++ b/include/SFML/Network/Sockets.hpp
@@ -0,0 +1,45 @@
+////////////////////////////////////////////////////////////
+//
+// SFML - Simple and Fast Multimedia Library
+// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
+//
+// This software is provided 'as-is', without any express or implied warranty.
+// In no event will the authors be held liable for any damages arising from the use of this software.
+//
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it freely,
+// subject to the following restrictions:
+//
+// 1. The origin of this software must not be misrepresented;
+// you must not claim that you wrote the original software.
+// If you use this software in a product, an acknowledgment
+// in the product documentation would be appreciated but is not required.
+//
+// 2. Altered source versions must be plainly marked as such,
+// and must not be misrepresented as being the original software.
+//
+// 3. This notice may not be removed or altered from any source distribution.
+//
+////////////////////////////////////////////////////////////
+
+#ifndef SFML_SOCKETS_HPP
+#define SFML_SOCKETS_HPP
+
+////////////////////////////////////////////////////////////
+// Headers
+////////////////////////////////////////////////////////////
+#include <SFML/Config.hpp>
+
+
+#ifdef SFML_SYSTEM_WINDOWS
+
+ #include <SFML/Network/Win32/Sockets.hpp>
+
+#else
+
+ #include <SFML/Network/Unix/Sockets.hpp>
+
+#endif
+
+
+#endif // SFML_SOCKETS_HPP
diff --git a/include/SFML/Network/Unix/SocketHelper.hpp b/include/SFML/Network/Unix/SocketHelper.hpp
new file mode 100755
index 0000000..caa58bb
--- /dev/null
+++ b/include/SFML/Network/Unix/SocketHelper.hpp
@@ -0,0 +1,95 @@
+////////////////////////////////////////////////////////////
+//
+// SFML - Simple and Fast Multimedia Library
+// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
+//
+// This software is provided 'as-is', without any express or implied warranty.
+// In no event will the authors be held liable for any damages arising from the use of this software.
+//
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it freely,
+// subject to the following restrictions:
+//
+// 1. The origin of this software must not be misrepresented;
+// you must not claim that you wrote the original software.
+// If you use this software in a product, an acknowledgment
+// in the product documentation would be appreciated but is not required.
+//
+// 2. Altered source versions must be plainly marked as such,
+// and must not be misrepresented as being the original software.
+//
+// 3. This notice may not be removed or altered from any source distribution.
+//
+////////////////////////////////////////////////////////////
+
+#ifndef SFML_SOCKETHELPERUNIX_HPP
+#define SFML_SOCKETHELPERUNIX_HPP
+
+////////////////////////////////////////////////////////////
+// Headers
+////////////////////////////////////////////////////////////
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <unistd.h>
+
+
+namespace sf
+{
+////////////////////////////////////////////////////////////
+/// This class defines helper functions to do all the
+/// non-portable socket stuff. This class is meant for internal
+/// use only
+////////////////////////////////////////////////////////////
+class SFML_API SocketHelper
+{
+public :
+
+ ////////////////////////////////////////////////////////////
+ // Define some socket types
+ ////////////////////////////////////////////////////////////
+ typedef int SocketType;
+ typedef socklen_t LengthType;
+
+ ////////////////////////////////////////////////////////////
+ /// Return the value of the invalid socket
+ ///
+ /// \return Unique value of the invalid socket
+ ///
+ ////////////////////////////////////////////////////////////
+ static SocketType InvalidSocket();
+
+ ////////////////////////////////////////////////////////////
+ /// Close / destroy a socket
+ ///
+ /// \param Socket : Socket to close
+ ///
+ /// \return True on success
+ ///
+ ////////////////////////////////////////////////////////////
+ static bool Close(SocketType Socket);
+
+ ////////////////////////////////////////////////////////////
+ /// Set a socket as blocking or non-blocking
+ ///
+ /// \param Socket : Socket to modify
+ /// \param Block : New blocking state of the socket
+ ///
+ ////////////////////////////////////////////////////////////
+ static void SetBlocking(SocketType Socket, bool Block);
+
+ ////////////////////////////////////////////////////////////
+ /// Get the last socket error status
+ ///
+ /// \return Status corresponding to the last socket error
+ ///
+ ////////////////////////////////////////////////////////////
+ static Socket::Status GetErrorStatus();
+};
+
+} // namespace sf
+
+
+#endif // SFML_SOCKETHELPERUNIX_HPP
diff --git a/include/SFML/Network/Win32/SocketHelper.hpp b/include/SFML/Network/Win32/SocketHelper.hpp
new file mode 100755
index 0000000..ec30f08
--- /dev/null
+++ b/include/SFML/Network/Win32/SocketHelper.hpp
@@ -0,0 +1,90 @@
+////////////////////////////////////////////////////////////
+//
+// SFML - Simple and Fast Multimedia Library
+// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
+//
+// This software is provided 'as-is', without any express or implied warranty.
+// In no event will the authors be held liable for any damages arising from the use of this software.
+//
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it freely,
+// subject to the following restrictions:
+//
+// 1. The origin of this software must not be misrepresented;
+// you must not claim that you wrote the original software.
+// If you use this software in a product, an acknowledgment
+// in the product documentation would be appreciated but is not required.
+//
+// 2. Altered source versions must be plainly marked as such,
+// and must not be misrepresented as being the original software.
+//
+// 3. This notice may not be removed or altered from any source distribution.
+//
+////////////////////////////////////////////////////////////
+
+#ifndef SFML_SOCKETHELPERWIN32_HPP
+#define SFML_SOCKETHELPERWIN32_HPP
+
+////////////////////////////////////////////////////////////
+// Headers
+////////////////////////////////////////////////////////////
+#include <winsock2.h>
+
+
+namespace sf
+{
+////////////////////////////////////////////////////////////
+/// This class defines helper functions to do all the
+/// non-portable socket stuff. This class is meant for internal
+/// use only
+////////////////////////////////////////////////////////////
+class SFML_API SocketHelper
+{
+public :
+
+ ////////////////////////////////////////////////////////////
+ // Define some socket types
+ ////////////////////////////////////////////////////////////
+ typedef SOCKET SocketType;
+ typedef int LengthType;
+
+ ////////////////////////////////////////////////////////////
+ /// Return the value of the invalid socket
+ ///
+ /// \return Unique value of the invalid socket
+ ///
+ ////////////////////////////////////////////////////////////
+ static SocketType InvalidSocket();
+
+ ////////////////////////////////////////////////////////////
+ /// Close / destroy a socket
+ ///
+ /// \param Socket : Socket to close
+ ///
+ /// \return True on success
+ ///
+ ////////////////////////////////////////////////////////////
+ static bool Close(SocketType Socket);
+
+ ////////////////////////////////////////////////////////////
+ /// Set a socket as blocking or non-blocking
+ ///
+ /// \param Socket : Socket to modify
+ /// \param Block : New blocking state of the socket
+ ///
+ ////////////////////////////////////////////////////////////
+ static void SetBlocking(SocketType Socket, bool Block);
+
+ ////////////////////////////////////////////////////////////
+ /// Get the last socket error status
+ ///
+ /// \return Status corresponding to the last socket error
+ ///
+ ////////////////////////////////////////////////////////////
+ static Socket::Status GetErrorStatus();
+};
+
+} // namespace sf
+
+
+#endif // SFML_SOCKETHELPERWIN32_HPP