From 4ec85b932e3771f38fcfea20577c626fe4bba6f2 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Tue, 22 Aug 2023 21:08:57 -0600 Subject: [PATCH] Unify SocketImpl headers The two headers only differed in their inclusions and two type aliases. It's easier to use the preprocessor than the maintain two nearly identical files. --- src/SFML/Network/CMakeLists.txt | 2 - src/SFML/Network/SocketImpl.hpp | 87 ++++++++++++++++++++- src/SFML/Network/Unix/SocketImpl.cpp | 2 +- src/SFML/Network/Unix/SocketImpl.hpp | 104 -------------------------- src/SFML/Network/Win32/SocketImpl.cpp | 2 +- src/SFML/Network/Win32/SocketImpl.hpp | 101 ------------------------- 6 files changed, 85 insertions(+), 213 deletions(-) delete mode 100644 src/SFML/Network/Unix/SocketImpl.hpp delete mode 100644 src/SFML/Network/Win32/SocketImpl.hpp diff --git a/src/SFML/Network/CMakeLists.txt b/src/SFML/Network/CMakeLists.txt index 881ed0860..ec3c8ea5a 100644 --- a/src/SFML/Network/CMakeLists.txt +++ b/src/SFML/Network/CMakeLists.txt @@ -31,12 +31,10 @@ set(SRC if(SFML_OS_WINDOWS) list(APPEND SRC ${SRCROOT}/Win32/SocketImpl.cpp - ${SRCROOT}/Win32/SocketImpl.hpp ) else() list(APPEND SRC ${SRCROOT}/Unix/SocketImpl.cpp - ${SRCROOT}/Unix/SocketImpl.hpp ) endif() diff --git a/src/SFML/Network/SocketImpl.hpp b/src/SFML/Network/SocketImpl.hpp index 236f9868d..7bbe9f224 100644 --- a/src/SFML/Network/SocketImpl.hpp +++ b/src/SFML/Network/SocketImpl.hpp @@ -27,15 +27,94 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// -#include - +#include #if defined(SFML_SYSTEM_WINDOWS) -#include +#include + +#include +#include #else -#include +#include +#include +#include +#include +#include +#include +#include #endif + +#include + + +namespace sf::priv +{ +//////////////////////////////////////////////////////////// +/// \brief Helper class implementing all the non-portable +/// socket stuff +/// +//////////////////////////////////////////////////////////// +class SocketImpl +{ +public: + //////////////////////////////////////////////////////////// + // Types + //////////////////////////////////////////////////////////// +#if defined(SFML_SYSTEM_WINDOWS) + using AddrLength = int; + using Size = int; +#else + using AddrLength = socklen_t; + using Size = std::size_t; +#endif + + //////////////////////////////////////////////////////////// + /// \brief Create an internal sockaddr_in address + /// + /// \param address Target address + /// \param port Target port + /// + /// \return sockaddr_in ready to be used by socket functions + /// + //////////////////////////////////////////////////////////// + static sockaddr_in createAddress(std::uint32_t address, unsigned short port); + + //////////////////////////////////////////////////////////// + /// \brief Return the value of the invalid socket + /// + /// \return Special value of the invalid socket + /// + //////////////////////////////////////////////////////////// + static SocketHandle invalidSocket(); + + //////////////////////////////////////////////////////////// + /// \brief Close and destroy a socket + /// + /// \param sock Handle of the socket to close + /// + //////////////////////////////////////////////////////////// + static void close(SocketHandle sock); + + //////////////////////////////////////////////////////////// + /// \brief Set a socket as blocking or non-blocking + /// + /// \param sock Handle of the socket + /// \param block New blocking state of the socket + /// + //////////////////////////////////////////////////////////// + static void setBlocking(SocketHandle sock, bool block); + + //////////////////////////////////////////////////////////// + /// Get the last socket error status + /// + /// \return Status corresponding to the last socket error + /// + //////////////////////////////////////////////////////////// + static Socket::Status getErrorStatus(); +}; + +} // namespace sf::priv diff --git a/src/SFML/Network/Unix/SocketImpl.cpp b/src/SFML/Network/Unix/SocketImpl.cpp index 72ed32a91..d66d50286 100644 --- a/src/SFML/Network/Unix/SocketImpl.cpp +++ b/src/SFML/Network/Unix/SocketImpl.cpp @@ -25,7 +25,7 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// -#include +#include #include diff --git a/src/SFML/Network/Unix/SocketImpl.hpp b/src/SFML/Network/Unix/SocketImpl.hpp deleted file mode 100644 index f2bc26128..000000000 --- a/src/SFML/Network/Unix/SocketImpl.hpp +++ /dev/null @@ -1,104 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org) -// -// 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. -// -//////////////////////////////////////////////////////////// - -#pragma once - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - - -namespace sf::priv -{ -//////////////////////////////////////////////////////////// -/// \brief Helper class implementing all the non-portable -/// socket stuff; this is the Unix version -/// -//////////////////////////////////////////////////////////// -class SocketImpl -{ -public: - //////////////////////////////////////////////////////////// - // Types - //////////////////////////////////////////////////////////// - using AddrLength = socklen_t; - using Size = std::size_t; - - //////////////////////////////////////////////////////////// - /// \brief Create an internal sockaddr_in address - /// - /// \param address Target address - /// \param port Target port - /// - /// \return sockaddr_in ready to be used by socket functions - /// - //////////////////////////////////////////////////////////// - static sockaddr_in createAddress(std::uint32_t address, unsigned short port); - - //////////////////////////////////////////////////////////// - /// \brief Return the value of the invalid socket - /// - /// \return Special value of the invalid socket - /// - //////////////////////////////////////////////////////////// - static SocketHandle invalidSocket(); - - //////////////////////////////////////////////////////////// - /// \brief Close and destroy a socket - /// - /// \param sock Handle of the socket to close - /// - //////////////////////////////////////////////////////////// - static void close(SocketHandle sock); - - //////////////////////////////////////////////////////////// - /// \brief Set a socket as blocking or non-blocking - /// - /// \param sock Handle of the socket - /// \param block New blocking state of the socket - /// - //////////////////////////////////////////////////////////// - static void setBlocking(SocketHandle sock, bool block); - - //////////////////////////////////////////////////////////// - /// Get the last socket error status - /// - /// \return Status corresponding to the last socket error - /// - //////////////////////////////////////////////////////////// - static Socket::Status getErrorStatus(); -}; - -} // namespace sf::priv diff --git a/src/SFML/Network/Win32/SocketImpl.cpp b/src/SFML/Network/Win32/SocketImpl.cpp index bea19a07a..b74ff738f 100644 --- a/src/SFML/Network/Win32/SocketImpl.cpp +++ b/src/SFML/Network/Win32/SocketImpl.cpp @@ -25,7 +25,7 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// -#include +#include #include diff --git a/src/SFML/Network/Win32/SocketImpl.hpp b/src/SFML/Network/Win32/SocketImpl.hpp deleted file mode 100644 index 10d8c32c0..000000000 --- a/src/SFML/Network/Win32/SocketImpl.hpp +++ /dev/null @@ -1,101 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org) -// -// 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. -// -//////////////////////////////////////////////////////////// - -#pragma once - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - -#include - -#include -#include - -#include - - -namespace sf::priv -{ -//////////////////////////////////////////////////////////// -/// \brief Helper class implementing all the non-portable -/// socket stuff; this is the Windows version -/// -//////////////////////////////////////////////////////////// -class SocketImpl -{ -public: - //////////////////////////////////////////////////////////// - // Types - //////////////////////////////////////////////////////////// - using AddrLength = int; - using Size = int; - - //////////////////////////////////////////////////////////// - /// \brief Create an internal sockaddr_in address - /// - /// \param address Target address - /// \param port Target port - /// - /// \return sockaddr_in ready to be used by socket functions - /// - //////////////////////////////////////////////////////////// - static sockaddr_in createAddress(std::uint32_t address, unsigned short port); - - //////////////////////////////////////////////////////////// - /// \brief Return the value of the invalid socket - /// - /// \return Special value of the invalid socket - /// - //////////////////////////////////////////////////////////// - static SocketHandle invalidSocket(); - - //////////////////////////////////////////////////////////// - /// \brief Close and destroy a socket - /// - /// \param sock Handle of the socket to close - /// - //////////////////////////////////////////////////////////// - static void close(SocketHandle sock); - - //////////////////////////////////////////////////////////// - /// \brief Set a socket as blocking or non-blocking - /// - /// \param sock Handle of the socket - /// \param block New blocking state of the socket - /// - //////////////////////////////////////////////////////////// - static void setBlocking(SocketHandle sock, bool block); - - //////////////////////////////////////////////////////////// - /// Get the last socket error status - /// - /// \return Status corresponding to the last socket error - /// - //////////////////////////////////////////////////////////// - static Socket::Status getErrorStatus(); -}; - -} // namespace sf::priv