From 2c3f49a18ef611ca1493139419a1aef5fd55b3b9 Mon Sep 17 00:00:00 2001 From: Nukoooo <89713806+Nukoooo@users.noreply.github.com> Date: Thu, 9 Nov 2023 07:06:49 +0800 Subject: [PATCH] core: add on_any (#402) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add on_any * Update style in sio_socket.cpp To keep the style. "ifs" with element can be put in one line if they fit. Otherwise they should use the bracket style. --------- Co-authored-by: Joan Marcè i Igual --- .gitignore | 3 ++- src/sio_socket.cpp | 27 +++++++++++++++++++++++++++ src/sio_socket.h | 4 ++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ec9c2f5b..6c464daa 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ Makefile cmake_install.cmake install_manifest.txt libsioclient.a -.DS_Store \ No newline at end of file +.DS_Store +.cache/ \ No newline at end of file diff --git a/src/sio_socket.cpp b/src/sio_socket.cpp index db2ebb7c..f89429f7 100644 --- a/src/sio_socket.cpp +++ b/src/sio_socket.cpp @@ -115,6 +115,10 @@ namespace sio void on(std::string const& event_name,event_listener const& func); + void on_any(event_listener_aux const& func); + + void on_any(event_listener const& func); + void off(std::string const& event_name); void off_all(); @@ -179,6 +183,8 @@ namespace sio std::map m_event_binding; + event_listener m_event_listener; + error_listener m_error_listener; std::unique_ptr m_connection_timer; @@ -203,6 +209,16 @@ namespace sio m_event_binding[event_name] = func; } + void socket::impl::on_any(event_listener_aux const& func) + { + m_event_listener = event_adapter::do_adapt(func); + } + + void socket::impl::on_any(event_listener const& func) + { + m_event_listener = func; + } + void socket::impl::off(std::string const& event_name) { std::lock_guard guard(m_event_mutex); @@ -443,6 +459,7 @@ namespace sio event ev = event_adapter::create_event(nsp,name, std::move(message),needAck); event_listener func = this->get_bind_listener_locked(name); if(func)func(ev); + if (m_event_listener) m_event_listener(ev); if(needAck) { this->ack(msgId, name, ev.get_ack_message()); @@ -545,6 +562,16 @@ namespace sio m_impl->on(event_name, func); } + void socket::on_any(event_listener_aux const& func) + { + m_impl->on_any(func); + } + + void socket::on_any(event_listener const& func) + { + m_impl->on_any(func); + } + void socket::off(std::string const& event_name) { m_impl->off(event_name); diff --git a/src/sio_socket.h b/src/sio_socket.h index bf760340..9e2e5294 100644 --- a/src/sio_socket.h +++ b/src/sio_socket.h @@ -62,6 +62,10 @@ namespace sio void off(std::string const& event_name); + void on_any(event_listener const& func); + + void on_any(event_listener_aux const& func); + void off_all(); void close();