Skip to content

Commit

Permalink
Support function calls on event queues (useful for #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
LubosD committed Jul 7, 2015
1 parent db51799 commit 415aa34
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> 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
///////////////////////////////////////////////////////////
Expand Down Expand Up @@ -545,6 +561,12 @@ void t_event_queue::push_quit(void) {
push(event);
}

void t_event_queue::push_fncall(std::function<void()> 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);
Expand Down
12 changes: 12 additions & 0 deletions src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -627,6 +628,14 @@ class t_event_tcp_ping : public t_event {
//@}
};

class t_event_fncall : public t_event {
public:
t_event_fncall(std::function<void()> fn);
void invoke();
virtual t_event_type get_type(void) const override;
private:
std::function<void()> m_fn;
};

/**
* Event queue.
Expand Down Expand Up @@ -663,6 +672,9 @@ class t_event_queue {
/** Push a quit event into the queue. */
void push_quit(void);

/** Push a queued function call */
void push_fncall(std::function<void()> fn);

/**
* Create a network event and push it into the queue.
* @param m [in] SIP message.
Expand Down
3 changes: 3 additions & 0 deletions src/userintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2242,6 +2242,9 @@ void t_userintf::process_events(void) {
case EV_QUIT:
quit = true;
break;
case EV_FN_CALL:
static_cast<t_event_fncall*>(event)->invoke();
break;
default:
assert(false);
break;
Expand Down

0 comments on commit 415aa34

Please sign in to comment.