Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add User Data #36

Closed
wants to merge 1 commit into from

4 participants

@IzayoyiSakuya

Since libpomelo uses callback between request and response, and in practice the callback will do in a separate thread context, user sometimes needs to save context information between callbacks. For example, in C++, a user can save this pointer into user defined data, during callback this pointer can be retrieved easily.

Below is some example code:

in request:

// register this object to pomelo client.
pc_set_user_data(this->client, 0, (void*)this);
...

in callback:
void on_request_cb(pc_request_t *req, int status, json_t *resp) {

pc_client_t * client = pc_get_client(req);
if (!client) {
    printf("[ERROR]: client is not available after request.\n");
    return;
}

void* userData = pc_get_user_data(client, 0);
WorkDescription* wd = (WorkDescription*)userData;

...

@walkmansk walkmansk add user defined data. these data can be set and get by user, in orde…
…r to keep some context information between callbacks (e.g. c++ this pointer)
22bafb8
@qiankanglai

Defining array like userData[USER_DATA_COUNT]; is not flexible. Why not use something like hashmap?

@cynron cynron closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 17, 2013
  1. @walkmansk

    add user defined data. these data can be set and get by user, in orde…

    walkmansk authored
    …r to keep some context information between callbacks (e.g. c++ this pointer)
This page is out of date. Refresh to see the latest.
Showing with 93 additions and 0 deletions.
  1. +36 −0 include/pomelo.h
  2. +57 −0 src/util.c
View
36 include/pomelo.h
@@ -31,6 +31,7 @@ extern "C" {
#define PC_EVENT_DISCONNECT "disconnect"
#define PC_EVENT_KICK "onKick"
+#define USER_DATA_COUNT 5
typedef struct pc_client_s pc_client_t;
typedef struct pc_listener_s pc_listener_t;
@@ -262,6 +263,9 @@ struct pc_client_s {
uv_cond_t cond;
uv_mutex_t listener_mutex;
uv_thread_t worker;
+
+ /* customized user data, used in callback */
+ void* userData[USER_DATA_COUNT];
};
/**
@@ -470,6 +474,38 @@ void pc_remove_listener(pc_client_t *client, const char *event,
*/
void pc_emit_event(pc_client_t *client, const char *event, void *data);
+
+/**
+ * set customized user data, can be used in callback (especially in C++)
+ *
+ * @param client client instance.
+ * @param slot which slot this user data goes in.
+ * @param data user data set by user.
+ *
+ */
+void pc_set_user_data(pc_client_t *client, unsigned int slot, void *data);
+
+/**
+ * get customized user data, can be used in callback (especially in C++)
+ *
+ * @param client client instance.
+ * @param slot which slot this user data goes in.
+ *
+ */
+void* pc_get_user_data(pc_client_t *client, unsigned int slot);
+
+
+/**
+ * get client instance from request instance.
+ *
+ * @param req request instance.
+ *
+ *
+ */
+pc_client_t* pc_get_client(pc_request_t* req);
+
+
+
/* Don't export the private CPP symbols. */
#undef PC_TCP_REQ_FIELDS
#undef PC_REQ_FIELDS
View
57 src/util.c
@@ -0,0 +1,57 @@
+//
+// pomelo
+// util.c
+//
+//
+//
+//
+//
+
+#include <stdio.h>
+#include "pomelo.h"
+
+/**
+ * set customized user data, can be used in callback (especially in C++)
+ *
+ * @param client client instance.
+ * @param slot which slot this user data goes in.
+ * @param data user data set by user.
+ *
+ */
+void pc_set_user_data(pc_client_t *client, unsigned int slot, void *data)
+{
+ if (slot >= USER_DATA_COUNT) {
+ return;
+ }
+
+ client->userData[slot] = data;
+}
+
+
+/**
+ * get customized user data, can be used in callback (especially in C++)
+ *
+ * @param client client instance.
+ * @param slot which slot this user data goes in.
+ *
+ */
+void* pc_get_user_data(pc_client_t *client, unsigned int slot)
+{
+ if (slot >= USER_DATA_COUNT) {
+ return NULL;
+ }
+ return client->userData[slot];
+}
+
+
+
+/**
+ * get client instance from request instance.
+ *
+ * @param req request instance.
+ *
+ */
+pc_client_t* pc_get_client(pc_request_t* req)
+{
+ return req->client;
+}
Something went wrong with that request. Please try again.