From 507f46739006b596b3754822879680985f4f1f2a Mon Sep 17 00:00:00 2001 From: LaurentGom Date: Mon, 29 Mar 2010 17:02:10 +0000 Subject: [PATCH] Added TcpListener::GetLocalPort() git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1480 4e206d99-4929-0410-ac5d-dfc041789085 --- include/SFML/Graphics/Font.hpp | 3 +++ include/SFML/Network/TcpListener.hpp | 13 +++++++++++++ src/SFML/Network/IpAddress.cpp | 2 +- src/SFML/Network/TcpListener.cpp | 19 +++++++++++++++++++ src/SFML/Network/TcpSocket.cpp | 4 ++-- 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/include/SFML/Graphics/Font.hpp b/include/SFML/Graphics/Font.hpp index 9fb284c2b..e5b357816 100644 --- a/include/SFML/Graphics/Font.hpp +++ b/include/SFML/Graphics/Font.hpp @@ -99,6 +99,9 @@ public : /// Note that this function know nothing about the standard /// fonts installed on the user's system, thus you can't /// load them directly. + /// Warning: SFML cannot preload all the font data in this + /// function, so the buffer pointed by \a data has to remain + /// valid as long as the font is used. /// /// \param data Pointer to the file data in memory /// \param sizeInBytes Size of the data to load, in bytes diff --git a/include/SFML/Network/TcpListener.hpp b/include/SFML/Network/TcpListener.hpp index 6fa803f74..4d2eb8f7e 100644 --- a/include/SFML/Network/TcpListener.hpp +++ b/include/SFML/Network/TcpListener.hpp @@ -49,6 +49,19 @@ public : //////////////////////////////////////////////////////////// TcpListener(); + //////////////////////////////////////////////////////////// + /// \brief Get the port to which the socket is bound locally + /// + /// If the socket is not listening to a port, this function + /// returns 0. + /// + /// \return Port to which the socket is bound + /// + /// \see Listen + /// + //////////////////////////////////////////////////////////// + unsigned short GetLocalPort() const; + //////////////////////////////////////////////////////////// /// \brief Start listening for connections /// diff --git a/src/SFML/Network/IpAddress.cpp b/src/SFML/Network/IpAddress.cpp index 1d59378b4..e85af8442 100644 --- a/src/SFML/Network/IpAddress.cpp +++ b/src/SFML/Network/IpAddress.cpp @@ -159,7 +159,7 @@ IpAddress IpAddress::GetLocalAddress() priv::SocketImpl::Close(sock); // Finally build the IP address - localAddress.myAddress = address.sin_addr.s_addr; + localAddress = IpAddress(ntohl(address.sin_addr.s_addr)); return localAddress; } diff --git a/src/SFML/Network/TcpListener.cpp b/src/SFML/Network/TcpListener.cpp index 615ecd0de..89fd19ca3 100644 --- a/src/SFML/Network/TcpListener.cpp +++ b/src/SFML/Network/TcpListener.cpp @@ -41,6 +41,25 @@ Socket(Tcp) } +//////////////////////////////////////////////////////////// +unsigned short TcpListener::GetLocalPort() const +{ + if (GetHandle() != priv::SocketImpl::InvalidSocket()) + { + // Retrieve informations about the local end of the socket + sockaddr_in address; + priv::SocketImpl::AddrLength size = sizeof(address); + if (getsockname(GetHandle(), reinterpret_cast(&address), &size) != -1) + { + return ntohs(address.sin_port); + } + } + + // We failed to retrieve the port + return 0; +} + + //////////////////////////////////////////////////////////// Socket::Status TcpListener::Listen(unsigned short port) { diff --git a/src/SFML/Network/TcpSocket.cpp b/src/SFML/Network/TcpSocket.cpp index 4c23e4fcf..46879a44c 100644 --- a/src/SFML/Network/TcpSocket.cpp +++ b/src/SFML/Network/TcpSocket.cpp @@ -30,8 +30,8 @@ #include #include #include -#include -#include +#include +#include #ifdef _MSC_VER #pragma warning(disable : 4127) // "conditional expression is constant" generated by the FD_SET macro