Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ping handler support for ping frame. #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/nopoll_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/nopoll_conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/nopoll_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down