From 415aa345125e48f5be1ee47296b00e49a587f2ae Mon Sep 17 00:00:00 2001 From: Lubos Dolezel Date: Tue, 7 Jul 2015 21:20:20 +0200 Subject: Support function calls on event queues (useful for #17) --- src/events.cpp | 22 ++++++++++++++++++++++ src/events.h | 12 ++++++++++++ src/userintf.cpp | 3 +++ 3 files changed, 37 insertions(+) (limited to 'src') diff --git a/src/events.cpp b/src/events.cpp index 45c4f1d..a034d4d 100644 --- a/src/events.cpp +++ b/src/events.cpp @@ -505,6 +505,22 @@ unsigned short t_event_tcp_ping::get_dst_port(void) const { return dst_port_; } +/////////////////////////////////////////////////////////// +// class t_event_fncall +/////////////////////////////////////////////////////////// + +t_event_fncall::t_event_fncall(std::function fn) + : m_fn(fn) { + +} +void t_event_fncall::invoke() { + m_fn(); +} + +t_event_type t_event_fncall::get_type(void) const { + return EV_FN_CALL; +} + /////////////////////////////////////////////////////////// // class t_event_queue /////////////////////////////////////////////////////////// @@ -545,6 +561,12 @@ void t_event_queue::push_quit(void) { push(event); } +void t_event_queue::push_fncall(std::function fn) { + t_event_fncall* event = new t_event_fncall(fn); + MEMMAN_NEW(event); + push(event); +} + void t_event_queue::push_network(t_sip_message *m, const t_ip_port &ip_port) { t_event_network *event = new t_event_network(m); MEMMAN_NEW(event); diff --git a/src/events.h b/src/events.h index c13e9c0..460ae95 100644 --- a/src/events.h +++ b/src/events.h @@ -58,6 +58,7 @@ enum t_event_type { EV_ASYNC_RESPONSE, /**< Response on an asynchronous question */ EV_BROKEN_CONNECTION, /**< Persitent connection to SIP proxy broken */ EV_TCP_PING, /**< Send a TCP ping (double CRLF) */ + EV_FN_CALL, /**< Queued function call */ }; /** Abstract parent class for all events */ @@ -627,6 +628,14 @@ public: //@} }; +class t_event_fncall : public t_event { +public: + t_event_fncall(std::function fn); + void invoke(); + virtual t_event_type get_type(void) const override; +private: + std::function m_fn; +}; /** * Event queue. @@ -663,6 +672,9 @@ public: /** Push a quit event into the queue. */ void push_quit(void); + /** Push a queued function call */ + void push_fncall(std::function fn); + /** * Create a network event and push it into the queue. * @param m [in] SIP message. diff --git a/src/userintf.cpp b/src/userintf.cpp index 71ca14a..bbd7fe6 100644 --- a/src/userintf.cpp +++ b/src/userintf.cpp @@ -2242,6 +2242,9 @@ void t_userintf::process_events(void) { case EV_QUIT: quit = true; break; + case EV_FN_CALL: + static_cast(event)->invoke(); + break; default: assert(false); break; -- cgit v1.2.3