Fixed TcpListener and TcpSocket not behaving as documented when calling listen or connect while the underlying socket object already exists, also adjusted UdpSocket to be consistent with connect and listen behaviour when calling bind while the underlying socket object already exists. Fixes #1346

This commit is contained in:
binary1248 2018-04-08 04:42:56 +02:00 committed by Lukas Dürrenberger
parent 9da895da8b
commit 9bdd6d46dd
6 changed files with 24 additions and 7 deletions

View File

@ -65,14 +65,16 @@ public:
unsigned short getLocalPort() const; unsigned short getLocalPort() const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Start listening for connections /// \brief Start listening for incoming connection attempts
/// ///
/// This functions makes the socket listen to the specified /// This function makes the socket start listening on the
/// port, waiting for new connections. /// specified port, waiting for incoming connection attempts.
/// If the socket was previously listening to another port,
/// it will be stopped first and bound to the new port.
/// ///
/// \param port Port to listen for new connections /// If the socket is already listening on a port when this
/// function is called, it will stop listening on the old
/// port before starting to listen on the new port.
///
/// \param port Port to listen on for incoming connection attempts
/// \param address Address of the interface to listen on /// \param address Address of the interface to listen on
/// ///
/// \return Status code /// \return Status code

View File

@ -97,7 +97,8 @@ public:
/// In blocking mode, this function may take a while, especially /// In blocking mode, this function may take a while, especially
/// if the remote peer is not reachable. The last parameter allows /// if the remote peer is not reachable. The last parameter allows
/// you to stop trying to connect after a given timeout. /// you to stop trying to connect after a given timeout.
/// If the socket was previously connected, it is first disconnected. /// If the socket is already connected, the connection is
/// forcibly disconnected before attempting to connect again.
/// ///
/// \param remoteAddress Address of the remote peer /// \param remoteAddress Address of the remote peer
/// \param remotePort Port of the remote peer /// \param remotePort Port of the remote peer

View File

@ -82,6 +82,11 @@ public:
/// system to automatically pick an available port, and then /// system to automatically pick an available port, and then
/// call getLocalPort to retrieve the chosen port. /// call getLocalPort to retrieve the chosen port.
/// ///
/// 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.
///
/// \param port Port to bind the socket to /// \param port Port to bind the socket to
/// \param address Address of the interface to bind to /// \param address Address of the interface to bind to
/// ///

View File

@ -63,6 +63,9 @@ unsigned short TcpListener::getLocalPort() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Socket::Status TcpListener::listen(unsigned short port, const IpAddress& address) Socket::Status TcpListener::listen(unsigned short port, const IpAddress& address)
{ {
// Close the socket if it is already bound
close();
// Create the internal socket if it doesn't exist // Create the internal socket if it doesn't exist
create(); create();

View File

@ -118,6 +118,9 @@ unsigned short TcpSocket::getRemotePort() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Socket::Status TcpSocket::connect(const IpAddress& remoteAddress, unsigned short remotePort, Time timeout) Socket::Status TcpSocket::connect(const IpAddress& remoteAddress, unsigned short remotePort, Time timeout)
{ {
// Disconnect the socket if it is already connected
disconnect();
// Create the internal socket if it doesn't exist // Create the internal socket if it doesn't exist
create(); create();

View File

@ -66,6 +66,9 @@ unsigned short UdpSocket::getLocalPort() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Socket::Status UdpSocket::bind(unsigned short port, const IpAddress& address) Socket::Status UdpSocket::bind(unsigned short port, const IpAddress& address)
{ {
// Close the socket if it is already bound
close();
// Create the internal socket if it doesn't exist // Create the internal socket if it doesn't exist
create(); create();