Merge pull request #439 from Alek900/master

Added support for userdata in callbacks
This commit is contained in:
irungentoo 2013-08-12 13:33:20 -07:00
commit f9bfb42504
8 changed files with 84 additions and 67 deletions

View File

@ -65,13 +65,13 @@ void do_tox(void)
doMessenger(m);
}
void parent_confirm_message(Messenger *m, int num, uint8_t *data, uint16_t length)
void parent_confirm_message(Messenger *m, int num, uint8_t *data, uint16_t length, void* userdata)
{
puts("OK");
request_flags |= SECOND_FLAG;
}
void parent_confirm_status(Messenger *m, int num, uint8_t *data, uint16_t length)
void parent_confirm_status(Messenger *m, int num, uint8_t *data, uint16_t length, void* userdata)
{
puts("OK");
request_flags |= FIRST_FLAG;
@ -108,7 +108,7 @@ int parent_friend_request(void)
return 0;
}
void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length)
void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
{
fputs("OK\nsending status to parent", stdout);
fflush(stdout);
@ -116,7 +116,7 @@ void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length)
request_flags |= FIRST_FLAG;
}
void child_got_statuschange(Messenger *m, int friend_num, uint8_t *string, uint16_t length)
void child_got_statuschange(Messenger *m, int friend_num, uint8_t *string, uint16_t length, void* userdata)
{
request_flags |= SECOND_FLAG;
}
@ -175,8 +175,8 @@ int main(int argc, char *argv[])
Messenger_save(m, child_id);
msync(child_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);
m_callback_friendrequest(m, child_got_request);
m_callback_statusmessage(m, child_got_statuschange);
m_callback_friendrequest(m, child_got_request, NULL);
m_callback_statusmessage(m, child_got_statuschange, NULL);
/* wait on the friend request */
while(!(request_flags & FIRST_FLAG))
@ -207,8 +207,8 @@ int main(int argc, char *argv[])
m = initMessenger();
msync(parent_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);
m_callback_statusmessage(m, parent_confirm_status);
m_callback_friendmessage(m, parent_confirm_message);
m_callback_statusmessage(m, parent_confirm_status, NULL);
m_callback_friendmessage(m, parent_confirm_message, NULL);
/* hacky way to give the child time to set up */
c_sleep(50);

View File

@ -395,52 +395,59 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
static uint8_t friend_request_isset = 0; */
/* set the function that will be executed when a friend request is received. */
void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t))
void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata)
{
callback_friendrequest(function);
callback_friendrequest(function, userdata);
}
/* set the function that will be executed when a message from a friend is received. */
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t))
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
{
m->friend_message = function;
m->friend_message_isset = 1;
m->friend_message_userdata = userdata;
}
void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t))
void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
{
m->friend_action = function;
m->friend_action_isset = 1;
m->friend_action_userdata = userdata;
}
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t))
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
{
m->friend_namechange = function;
m->friend_namechange_isset = 1;
m->friend_namechange_userdata = userdata;
}
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t))
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
{
m->friend_statusmessagechange = function;
m->friend_statusmessagechange_isset = 1;
m->friend_statuschange_userdata = userdata;
}
void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS))
void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata)
{
m->friend_userstatuschange = function;
m->friend_userstatuschange_isset = 1;
m->friend_userstatuschange_userdata = userdata;
}
void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t))
void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata)
{
m->read_receipt = function;
m->read_receipt_isset = 1;
m->read_receipt_userdata = userdata;
}
void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t))
void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata)
{
m->friend_connectionstatuschange = function;
m->friend_connectionstatuschange_isset = 1;
m->friend_connectionstatuschange_userdata = userdata;
}
static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status)
@ -452,7 +459,7 @@ static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_
const uint8_t was_connected = m->friendlist[friendnumber].status == FRIEND_ONLINE;
const uint8_t is_connected = status == FRIEND_ONLINE;
if (is_connected != was_connected)
m->friend_connectionstatuschange(m, friendnumber, is_connected);
m->friend_connectionstatuschange(m, friendnumber, is_connected, m->friend_connectionstatuschange_userdata);
}
void set_friend_status(Messenger *m, int friendnumber, uint8_t status)
@ -569,7 +576,7 @@ void doFriends(Messenger *m)
if (data_length >= MAX_NAME_LENGTH || data_length == 0)
break;
if(m->friend_namechange_isset)
m->friend_namechange(m, i, data, data_length);
m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata);
memcpy(m->friendlist[i].name, data, data_length);
m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */
break;
@ -580,7 +587,8 @@ void doFriends(Messenger *m)
uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1);
memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
if (m->friend_statusmessagechange_isset)
m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH),
m->friend_statuschange_userdata);
set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
free(status);
break;
@ -590,7 +598,7 @@ void doFriends(Messenger *m)
break;
USERSTATUS status = data[0];
if (m->friend_userstatuschange_isset)
m->friend_userstatuschange(m, i, status);
m->friend_userstatuschange(m, i, status, m->friend_userstatuschange_userdata);
set_friend_userstatus(m, i, status);
break;
}
@ -603,12 +611,12 @@ void doFriends(Messenger *m)
write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
}
if (m->friend_message_isset)
(*m->friend_message)(m, i, message, message_length);
(*m->friend_message)(m, i, message, message_length, m->friend_message_userdata);
break;
}
case PACKET_ID_ACTION: {
if (m->friend_action_isset)
(*m->friend_action)(m, i, data, data_length);
(*m->friend_action)(m, i, data, data_length, m->friend_action_userdata);
break;
}
case PACKET_ID_RECEIPT: {
@ -618,7 +626,7 @@ void doFriends(Messenger *m)
memcpy(&msgid, data, sizeof(msgid));
msgid = ntohl(msgid);
if (m->read_receipt_isset)
(*m->read_receipt)(m, i, msgid);
(*m->read_receipt)(m, i, msgid, m->read_receipt_userdata);
break;
}
}

View File

@ -105,22 +105,30 @@ typedef struct Messenger {
Friend *friendlist;
uint32_t numfriends;
void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t);
void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
uint8_t friend_message_isset;
void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t);
void* friend_message_userdata;
void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
uint8_t friend_action_isset;
void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t);
void* friend_action_userdata;
void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
uint8_t friend_namechange_isset;
void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t);
void* friend_namechange_userdata;
void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
uint8_t friend_statusmessagechange_isset;
void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS);
void* friend_statusmessagechange_userdata;
void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS, void*);
uint8_t friend_userstatuschange_isset;
void (*read_receipt)(struct Messenger *m, int, uint32_t);
void* friend_userstatuschange_userdata;
void (*read_receipt)(struct Messenger *m, int, uint32_t, void*);
uint8_t read_receipt_isset;
void (*friend_statuschange)(struct Messenger *m, int, uint8_t);
void* read_receipt_userdata;
void (*friend_statuschange)(struct Messenger *m, int, uint8_t, void*);
uint8_t friend_statuschange_isset;
void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t);
void* friend_statuschange_userdata;
void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void*);
uint8_t friend_connectionstatuschange_isset;
void* friend_connectionstatuschange_userdata;
} Messenger;
@ -230,29 +238,29 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno);
/* set the function that will be executed when a friend request is received.
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t));
void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata);
/* set the function that will be executed when a message from a friend is received.
function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t));
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
/* set the function that will be executed when an action from a friend is received.
function format is: function(int friendnumber, uint8_t * action, uint32_t length) */
void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t));
void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
/* set the callback for name changes
function(int friendnumber, uint8_t *newname, uint16_t length)
you are not responsible for freeing newname */
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t));
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
/* set the callback for status message changes
function(int friendnumber, uint8_t *newstatus, uint16_t length)
you are not responsible for freeing newstatus */
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t));
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
/* set the callback for status type changes
function(int friendnumber, USERSTATUS kind) */
void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS));
void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata);
/* set the callback for read receipts
function(int friendnumber, uint32_t receipt)
@ -261,7 +269,7 @@ void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USE
has been received on the other side. since core doesn't
track ids for you, receipt may not correspond to any message
in that case, you should discard it. */
void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t));
void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata);
/* set the callback for connection status changes
function(int friendnumber, uint8_t status)
@ -271,7 +279,7 @@ void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, u
note that this callback is not called when adding friends, thus the "after
being previously online" part. it's assumed that when adding friends,
their connection status is offline. */
void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t));
void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata);
/* run this at startup
* returns allocated instance of Messenger on success

View File

@ -57,14 +57,15 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
return num;
}
static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t);
static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void*);
static uint8_t handle_friendrequest_isset = 0;
static void* handle_friendrequest_userdata;
/* set the function that will be executed when a friend request is received. */
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata)
{
handle_friendrequest = function;
handle_friendrequest_isset = 1;
handle_friendrequest_userdata = userdata;
}
@ -121,7 +122,7 @@ static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t len
return 1;
addto_receivedlist(public_key);
(*handle_friendrequest)(public_key, data, len);
(*handle_friendrequest)(public_key, data, len, handle_friendrequest_userdata);
} else { /* if request is not for us, try routing it. */
if(route_packet(packet + 1, packet, length) == length)
return 0;

View File

@ -37,7 +37,7 @@ int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length);
/* set the function that will be executed when a friend request for us is received.
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata);
/* sets up friendreq packet handlers */
void friendreq_init(void);

View File

@ -55,7 +55,7 @@
* networking_requesthandler and so cannot take a Messenger * */
static Messenger *m;
void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
void print_request(uint8_t * public_key, uint8_t * data, uint16_t length, void* userdata)
{
printf("Friend request received from: \n");
printf("ClientID: ");
@ -80,7 +80,7 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
}
}
void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length)
void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length, void* userdata)
{
printf("Message with length %u received from %u: %s \n", length, friendnumber, string);
m_sendmessage(m, friendnumber, (uint8_t*)"Test1", 6);
@ -114,8 +114,8 @@ int main(int argc, char *argv[])
fclose(file);
}
m_callback_friendrequest(m, print_request);
m_callback_friendmessage(m, print_message);
m_callback_friendrequest(m, print_request, NULL);
m_callback_friendmessage(m, print_message, NULL);
printf("OUR ID: ");
uint32_t i;

View File

@ -336,7 +336,7 @@ void do_refresh()
refresh();
}
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length)
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
{
new_lines("[i] received friend request with message:");
new_lines((char *)data);
@ -349,12 +349,12 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length)
do_refresh();
}
void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length)
void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length, void* userdata)
{
new_lines(format_message(m, (char*)string, friendnumber));
}
void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length)
void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
char name[MAX_NAME_LENGTH];
if(getname(m, friendnumber, (uint8_t*)name) != -1) {
@ -364,7 +364,7 @@ void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t
}
}
void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length)
void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
char name[MAX_NAME_LENGTH];
if(getname(m, friendnumber, (uint8_t*)name) != -1) {
@ -467,10 +467,10 @@ int main(int argc, char *argv[])
load_key(m, filename);
m_callback_friendrequest(m, print_request);
m_callback_friendmessage(m, print_message);
m_callback_namechange(m, print_nickchange);
m_callback_statusmessage(m, print_statuschange);
m_callback_friendrequest(m, print_request, NULL);
m_callback_friendmessage(m, print_message, NULL);
m_callback_namechange(m, print_nickchange, NULL);
m_callback_statusmessage(m, print_statuschange, NULL);
initscr();
noecho();

View File

@ -46,7 +46,7 @@ int w_num;
int active_window;
/* CALLBACKS START */
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length)
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
{
int n = add_req(public_key);
wprintw(prompt->window, "\nFriend request from:\n");
@ -65,7 +65,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length)
}
}
void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length)
void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
@ -74,7 +74,7 @@ void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length
}
}
void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length)
void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
@ -83,7 +83,7 @@ void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length)
}
}
void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length)
void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
int i;
@ -93,7 +93,7 @@ void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t len
}
}
void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length)
void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
int i;
@ -134,11 +134,11 @@ static void init_tox()
m = initMessenger();
/* Callbacks */
m_callback_friendrequest(m, on_request);
m_callback_friendmessage(m, on_message);
m_callback_namechange(m, on_nickchange);
m_callback_statusmessage(m, on_statuschange);
m_callback_action(m, on_action);
m_callback_friendrequest(m, on_request, NULL);
m_callback_friendmessage(m, on_message, NULL);
m_callback_namechange(m, on_nickchange, NULL);
m_callback_statusmessage(m, on_statuschange, NULL);
m_callback_action(m, on_action, NULL);
}
#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */