From 4cd14e4e51e4ec1cba0ad42120df5449244bb33a Mon Sep 17 00:00:00 2001 From: gbuddappagari Date: Mon, 5 Mar 2018 14:38:09 +0530 Subject: [PATCH] Add ping handler support for ping frame. --- src/nopoll_conn.c | 27 +++++++++++++++++++++++++++ src/nopoll_conn.h | 2 ++ src/nopoll_private.h | 6 ++++++ 3 files changed, 35 insertions(+) diff --git a/src/nopoll_conn.c b/src/nopoll_conn.c index 14b6ec8..00641a9 100644 --- a/src/nopoll_conn.c +++ b/src/nopoll_conn.c @@ -3569,6 +3569,8 @@ noPollMsg * nopoll_conn_get_msg (noPollConn * conn) if (msg->payload_size != 0 && msg->op_code == NOPOLL_PING_FRAME) { nopoll_log (conn->ctx, NOPOLL_LEVEL_DEBUG, "PING received over connection id=%d and payload_size=%d, replying PONG", conn->id, msg->payload_size); + /* Set the ping handler */ + conn->on_ping_msg (conn->ctx, conn, msg, conn->on_ping_msg_data); nopoll_conn_send_pong (conn, nopoll_msg_get_payload_size (msg), (noPollPtr)nopoll_msg_get_payload (msg)); nopoll_msg_unref (msg); return NULL; @@ -4072,6 +4074,31 @@ void nopoll_conn_set_on_msg (noPollConn * conn, return; } +/** + * @brief Allows to configure an on ping message handler + * that will be called when ping with payload is received. + * + * @param conn The connection to be configured with a particular on ping message handler. + * + * @param on_ping_msg The on ping message handler configured. + * + * @param user_data User defined pointer to be passed in into the on ping message handler when it is called. + * + */ +void nopoll_conn_set_on_ping_msg (noPollConn * conn, + noPollOnMessageHandler on_ping_msg, + noPollPtr user_data) +{ + if (conn == NULL) + return; + + /* configure on message handler */ + conn->on_ping_msg = on_ping_msg; + conn->on_ping_msg_data = user_data; + + return; +} + /** * @brief Allows to configure a handler that is called when the * connection provided is ready to send and receive because all diff --git a/src/nopoll_conn.h b/src/nopoll_conn.h index ce8d61c..637fbdf 100644 --- a/src/nopoll_conn.h +++ b/src/nopoll_conn.h @@ -110,6 +110,8 @@ noPollConn * nopoll_conn_accept (noPollCtx * ctx, noPollConn * listener); noPollConn * nopoll_conn_accept_socket (noPollCtx * ctx, noPollConn * listener, NOPOLL_SOCKET session); +void nopoll_conn_set_on_ping_msg (noPollConn * conn, noPollOnMessageHandler on_ping_msg, noPollPtr user_data); + nopoll_bool nopoll_conn_accept_complete (noPollCtx * ctx, noPollConn * listener, noPollConn * conn, diff --git a/src/nopoll_private.h b/src/nopoll_private.h index 021870e..7324686 100644 --- a/src/nopoll_private.h +++ b/src/nopoll_private.h @@ -233,6 +233,12 @@ struct _noPollConn { noPollOnMessageHandler on_msg; noPollPtr on_msg_data; + /** + * @internal Reference to the defined on ping message handling. + */ + noPollOnMessageHandler on_ping_msg; + noPollPtr on_ping_msg_data; + /** * @internal Reference to defined on ready handling. */