Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:14

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_SOCKET_HPP
0026 #define SFML_SOCKET_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Network/Export.hpp>
0032 #include <SFML/Network/SocketHandle.hpp>
0033 #include <SFML/System/NonCopyable.hpp>
0034 #include <vector>
0035 
0036 
0037 namespace sf
0038 {
0039 class SocketSelector;
0040 
0041 ////////////////////////////////////////////////////////////
0042 /// \brief Base class for all the socket types
0043 ///
0044 ////////////////////////////////////////////////////////////
0045 class SFML_NETWORK_API Socket : NonCopyable
0046 {
0047 public:
0048 
0049     ////////////////////////////////////////////////////////////
0050     /// \brief Status codes that may be returned by socket functions
0051     ///
0052     ////////////////////////////////////////////////////////////
0053     enum Status
0054     {
0055         Done,         //!< The socket has sent / received the data
0056         NotReady,     //!< The socket is not ready to send / receive data yet
0057         Partial,      //!< The socket sent a part of the data
0058         Disconnected, //!< The TCP socket has been disconnected
0059         Error         //!< An unexpected error happened
0060     };
0061 
0062     ////////////////////////////////////////////////////////////
0063     /// \brief Some special values used by sockets
0064     ///
0065     ////////////////////////////////////////////////////////////
0066     enum
0067     {
0068         AnyPort = 0 //!< Special value that tells the system to pick any available port
0069     };
0070 
0071 public:
0072 
0073     ////////////////////////////////////////////////////////////
0074     /// \brief Destructor
0075     ///
0076     ////////////////////////////////////////////////////////////
0077     virtual ~Socket();
0078 
0079     ////////////////////////////////////////////////////////////
0080     /// \brief Set the blocking state of the socket
0081     ///
0082     /// In blocking mode, calls will not return until they have
0083     /// completed their task. For example, a call to Receive in
0084     /// blocking mode won't return until some data was actually
0085     /// received.
0086     /// In non-blocking mode, calls will always return immediately,
0087     /// using the return code to signal whether there was data
0088     /// available or not.
0089     /// By default, all sockets are blocking.
0090     ///
0091     /// \param blocking True to set the socket as blocking, false for non-blocking
0092     ///
0093     /// \see isBlocking
0094     ///
0095     ////////////////////////////////////////////////////////////
0096     void setBlocking(bool blocking);
0097 
0098     ////////////////////////////////////////////////////////////
0099     /// \brief Tell whether the socket is in blocking or non-blocking mode
0100     ///
0101     /// \return True if the socket is blocking, false otherwise
0102     ///
0103     /// \see setBlocking
0104     ///
0105     ////////////////////////////////////////////////////////////
0106     bool isBlocking() const;
0107 
0108 protected:
0109 
0110     ////////////////////////////////////////////////////////////
0111     /// \brief Types of protocols that the socket can use
0112     ///
0113     ////////////////////////////////////////////////////////////
0114     enum Type
0115     {
0116         Tcp, //!< TCP protocol
0117         Udp  //!< UDP protocol
0118     };
0119 
0120     ////////////////////////////////////////////////////////////
0121     /// \brief Default constructor
0122     ///
0123     /// This constructor can only be accessed by derived classes.
0124     ///
0125     /// \param type Type of the socket (TCP or UDP)
0126     ///
0127     ////////////////////////////////////////////////////////////
0128     Socket(Type type);
0129 
0130     ////////////////////////////////////////////////////////////
0131     /// \brief Return the internal handle of the socket
0132     ///
0133     /// The returned handle may be invalid if the socket
0134     /// was not created yet (or already destroyed).
0135     /// This function can only be accessed by derived classes.
0136     ///
0137     /// \return The internal (OS-specific) handle of the socket
0138     ///
0139     ////////////////////////////////////////////////////////////
0140     SocketHandle getHandle() const;
0141 
0142     ////////////////////////////////////////////////////////////
0143     /// \brief Create the internal representation of the socket
0144     ///
0145     /// This function can only be accessed by derived classes.
0146     ///
0147     ////////////////////////////////////////////////////////////
0148     void create();
0149 
0150     ////////////////////////////////////////////////////////////
0151     /// \brief Create the internal representation of the socket
0152     ///        from a socket handle
0153     ///
0154     /// This function can only be accessed by derived classes.
0155     ///
0156     /// \param handle OS-specific handle of the socket to wrap
0157     ///
0158     ////////////////////////////////////////////////////////////
0159     void create(SocketHandle handle);
0160 
0161     ////////////////////////////////////////////////////////////
0162     /// \brief Close the socket gracefully
0163     ///
0164     /// This function can only be accessed by derived classes.
0165     ///
0166     ////////////////////////////////////////////////////////////
0167     void close();
0168 
0169 private:
0170 
0171     friend class SocketSelector;
0172 
0173     ////////////////////////////////////////////////////////////
0174     // Member data
0175     ////////////////////////////////////////////////////////////
0176     Type         m_type;       //!< Type of the socket (TCP or UDP)
0177     SocketHandle m_socket;     //!< Socket descriptor
0178     bool         m_isBlocking; //!< Current blocking mode of the socket
0179 };
0180 
0181 } // namespace sf
0182 
0183 
0184 #endif // SFML_SOCKET_HPP
0185 
0186 
0187 ////////////////////////////////////////////////////////////
0188 /// \class sf::Socket
0189 /// \ingroup network
0190 ///
0191 /// This class mainly defines internal stuff to be used by
0192 /// derived classes.
0193 ///
0194 /// The only public features that it defines, and which
0195 /// is therefore common to all the socket classes, is the
0196 /// blocking state. All sockets can be set as blocking or
0197 /// non-blocking.
0198 ///
0199 /// In blocking mode, socket functions will hang until
0200 /// the operation completes, which means that the entire
0201 /// program (well, in fact the current thread if you use
0202 /// multiple ones) will be stuck waiting for your socket
0203 /// operation to complete.
0204 ///
0205 /// In non-blocking mode, all the socket functions will
0206 /// return immediately. If the socket is not ready to complete
0207 /// the requested operation, the function simply returns
0208 /// the proper status code (Socket::NotReady).
0209 ///
0210 /// The default mode, which is blocking, is the one that is
0211 /// generally used, in combination with threads or selectors.
0212 /// The non-blocking mode is rather used in real-time
0213 /// applications that run an endless loop that can poll
0214 /// the socket often enough, and cannot afford blocking
0215 /// this loop.
0216 ///
0217 /// \see sf::TcpListener, sf::TcpSocket, sf::UdpSocket
0218 ///
0219 ////////////////////////////////////////////////////////////