Skip to content

Commit

Permalink
Switch to std::function for callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tasssadar committed Jan 7, 2019
1 parent e027731 commit 4c22463
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ A library faciliating communication with RbController Android app: https://play.
#include "rbprotocol.h"
#include "rbwebserver.h"

void onPktReceived(rb::Protocol &protocol, void *cookie, const std::string& command, rbjson::Object *pkt) {
void onPktReceived(const std::string& command, rbjson::Object *pkt) {
if(command == "joy") {
printf("Joy: ");
rbjson::Array *data = pkt->getArray("data");
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "rbprotocol.h"
#include "rbwebserver.h"

void onPktReceived(rb::Protocol &protocol, void *cookie, const std::string& command, rbjson::Object *pkt) {
void onPktReceived(const std::string& command, rbjson::Object *pkt) {
if(command == "joy") {
printf("Joy: ");
rbjson::Array *data = pkt->getArray("data");
Expand Down
7 changes: 3 additions & 4 deletions src/rbprotocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ class SemaphoreHolder {
namespace rb {

Protocol::Protocol(const char *owner, const char *name, const char *description,
ProtocolCallback onPacketReceivedCallback, void *callback_cookie) {
std::function<void(const std::string& cmd, rbjson::Object* pkt)> callback) {
m_owner = owner;
m_name = name;
m_desc = description;
m_callback = onPacketReceivedCallback;
m_callback_cookie = callback_cookie;
m_callback = callback;

m_socket = -1;
m_mutex = xSemaphoreCreateMutex();
Expand Down Expand Up @@ -387,7 +386,7 @@ void Protocol::handle_msg(struct sockaddr_in *addr, char *buf, ssize_t size) {
ESP_LOGI(TAG, "We are possessed!");
send_log("The device %s has been possessed!\n", m_name);
} else if(m_callback != NULL) {
m_callback(*this, m_callback_cookie, cmd, pkt.get());
m_callback(cmd, pkt.get());
}
}

Expand Down
11 changes: 3 additions & 8 deletions src/rbprotocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>
#include <vector>
#include <sys/socket.h>
#include <functional>

#include "rbjson.h"

Expand All @@ -17,11 +18,6 @@ namespace rb {

class Protocol;

/**
* \brief This is the type for onPacketReceived callback
*/
typedef void (*ProtocolCallback)(Protocol& protocol, void *cookie, const std::string& command, rbjson::Object *pkt);

/**
* \brief Class that manages the RBProtocol communication
*/
Expand All @@ -32,7 +28,7 @@ class Protocol {
* It runs on a separate task, only single packet is processed at a time.
*/
Protocol(const char *owner, const char *name, const char *description,
ProtocolCallback onPacketReceivedCallback = NULL, void *callback_cookie = NULL);
std::function<void(const std::string& cmd, rbjson::Object* pkt)> callback = nullptr);
~Protocol();

void start(int port = RBPROTOCOL_PORT); //!< Start listening for UDP packets on port
Expand Down Expand Up @@ -79,8 +75,7 @@ class Protocol {
const char *m_name;
const char *m_desc;

ProtocolCallback m_callback;
void *m_callback_cookie;
std::function<void(const std::string& cmd, rbjson::Object* pkt)> m_callback;

int m_socket;
int m_read_counter;
Expand Down

0 comments on commit 4c22463

Please sign in to comment.