@@ -46,7 +46,7 @@ namespace net {
4646 friend class tcp ::Listener;
4747
4848 private:
49- using Listeners = std::map<tcp::port_t , std::unique_ptr<tcp::Listener>>;
49+ using Listeners = std::map<tcp::Socket , std::unique_ptr<tcp::Listener>>;
5050 using Connections = std::map<tcp::Connection::Tuple, tcp::Connection_ptr>;
5151
5252 public:
@@ -70,7 +70,10 @@ namespace net {
7070 *
7171 * @return a TCP Listener
7272 */
73- tcp::Listener& bind (const tcp::port_t port, ConnectCallback cb = nullptr );
73+ tcp::Listener& bind (const tcp::port_t port, ConnectCallback cb = nullptr )
74+ { return bind ({0 , port}, std::move (cb)); }
75+
76+ tcp::Listener& bind (tcp::Socket socket, ConnectCallback cb = nullptr );
7477
7578 /* *
7679 * @brief Unbind (and close) a Listener
@@ -80,7 +83,7 @@ namespace net {
8083 * @param port listening port
8184 * @return whether the listener had a port
8285 */
83- bool unbind (const tcp::port_t port );
86+ bool unbind (tcp::Socket socket );
8487
8588 /* *
8689 * @brief Make an outgoing connection to a TCP remote (IP:port).
@@ -341,7 +344,7 @@ namespace net {
341344 *
342345 * @return An IP4 address
343346 */
344- tcp::Address address ()
347+ tcp::Address address () const noexcept
345348 { return inet_.ip_addr (); }
346349
347350 /* *
@@ -446,6 +449,38 @@ namespace net {
446449
447450 // INTERNALS - Handling of collections
448451
452+ /* *
453+ * @brief Try to find the listener bound to socket.
454+ * If none is found directly, try any address (0).
455+ *
456+ * @param[in] socket The socket the listener is bound to
457+ *
458+ * @return A listener iterator
459+ */
460+ Listeners::iterator find_listener (const tcp::Socket socket)
461+ {
462+ Listeners::iterator it = listeners_.find (socket);
463+ if (it == listeners_.end ())
464+ it = listeners_.find ({0 , socket.port ()});
465+ return it;
466+ }
467+
468+ /* *
469+ * @brief Try to find the listener bound to socket.
470+ * If none is found directly, try any address (0).
471+ *
472+ * @param[in] socket The socket the listener is bound to
473+ *
474+ * @return A listener const iterator
475+ */
476+ Listeners::const_iterator cfind_listener (const tcp::Socket socket)
477+ {
478+ Listeners::const_iterator it = listeners_.find (socket);
479+ if (it == listeners_.cend ())
480+ it = listeners_.find ({0 , socket.port ()});
481+ return it;
482+ }
483+
449484 /* *
450485 * @brief Adds a connection.
451486 *
@@ -456,13 +491,13 @@ namespace net {
456491 /* *
457492 * @brief Creates a connection.
458493 *
459- * @param[in] local_port The local port
460- * @param[in] remote The remote
461- * @param[in] cb Connect callback
494+ * @param[in] local The local socket
495+ * @param[in] remote The remote socket
496+ * @param[in] cb Connect callback
462497 *
463498 * @return A ptr to the Connection whos created
464499 */
465- tcp::Connection_ptr create_connection (tcp::port_t local_port ,
500+ tcp::Connection_ptr create_connection (tcp::Socket local ,
466501 tcp::Socket remote,
467502 ConnectCallback cb = nullptr );
468503
@@ -480,7 +515,7 @@ namespace net {
480515 * @param[in] listener A Listener
481516 */
482517 void close_listener (tcp::Listener& listener)
483- { listeners_.erase (listener.port ()); }
518+ { listeners_.erase (listener.local ()); }
484519
485520
486521 // WRITEQ HANDLING
0 commit comments