mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
More stuff done in group chats.
This commit is contained in:
parent
7722d420e7
commit
00c9c8ab57
|
@ -278,7 +278,7 @@ static int delpeer(Group_Chat *chat, uint8_t *client_id)
|
||||||
#define PING_TIMEOUT 5
|
#define PING_TIMEOUT 5
|
||||||
static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum)
|
static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum)
|
||||||
{
|
{
|
||||||
if (peernum < 0 || peernum >= chat->numpeers)
|
if ((uint32_t)peernum >= chat->numpeers)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
uint64_t temp_time = unix_time();
|
uint64_t temp_time = unix_time();
|
||||||
|
@ -296,7 +296,7 @@ static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum)
|
||||||
|
|
||||||
static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid)
|
static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid)
|
||||||
{
|
{
|
||||||
if (peernum < 0 || peernum >= chat->numpeers)
|
if ((uint32_t)peernum >= chat->numpeers)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
sendnodes_data contents;
|
sendnodes_data contents;
|
||||||
|
@ -321,7 +321,7 @@ static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_
|
||||||
if (len != sizeof(getnodes_data))
|
if (len != sizeof(getnodes_data))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (peernum < 0 || peernum >= chat->numpeers)
|
if ((uint32_t)peernum >= chat->numpeers)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
getnodes_data contents;
|
getnodes_data contents;
|
||||||
|
@ -336,7 +336,7 @@ static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_
|
||||||
|
|
||||||
static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len)
|
static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len)
|
||||||
{
|
{
|
||||||
if (peernum < 0 || peernum >= chat->numpeers)
|
if ((uint32_t)peernum >= chat->numpeers)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (len > sizeof(sendnodes_data) || len < sizeof(uint64_t))
|
if (len > sizeof(sendnodes_data) || len < sizeof(uint64_t))
|
||||||
|
@ -384,13 +384,17 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
|
||||||
//TODO:
|
//TODO:
|
||||||
int peernum = peer_in_chat(chat, data);
|
int peernum = peer_in_chat(chat, data);
|
||||||
|
|
||||||
if (peernum == -1) { /*NOTE: This is just for testing and will be removed later.*/
|
|
||||||
peernum = addpeer(chat, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (peernum == -1)
|
if (peernum == -1)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
uint64_t temp_time = unix_time();
|
||||||
|
/* Spam prevention (1 message per peer per second limit.)
|
||||||
|
|
||||||
|
if (chat->group[peernum].last_recv == temp_time)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
chat->group[peernum].last_recv = temp_time;
|
||||||
|
*/
|
||||||
uint32_t message_num;
|
uint32_t message_num;
|
||||||
memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
|
memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
|
||||||
message_num = ntohl(message_num);
|
message_num = ntohl(message_num);
|
||||||
|
@ -403,14 +407,34 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
|
||||||
|
|
||||||
chat->group[peernum].last_message_number = message_num;
|
chat->group[peernum].last_message_number = message_num;
|
||||||
|
|
||||||
int handled = 0;
|
int handled = 1;
|
||||||
|
uint8_t *contents = data + GROUP_DATA_MIN_SIZE;
|
||||||
|
uint16_t contents_len = len - GROUP_DATA_MIN_SIZE;
|
||||||
|
|
||||||
if (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] == 64) {
|
switch (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)]) {
|
||||||
if (chat->group_message != NULL) /* If message is chat message */
|
case 0: /* If message is ping */
|
||||||
(*chat->group_message)(chat, peernum, data + GROUP_DATA_MIN_SIZE, len - GROUP_DATA_MIN_SIZE,
|
if (contents_len != 0)
|
||||||
chat->group_message_userdata);
|
return 1;
|
||||||
|
|
||||||
|
chat->group[peernum].last_recv_msgping = temp_time;
|
||||||
|
|
||||||
|
case 16: /* If message is new peer */
|
||||||
|
if (contents_len != crypto_box_PUBLICKEYBYTES)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
addpeer(chat, contents);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 64: /* If message is chat message */
|
||||||
|
if (chat->group_message != NULL)
|
||||||
|
(*chat->group_message)(chat, peernum, contents, contents_len, chat->group_message_userdata);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
handled = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
handled = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (handled == 1) {
|
if (handled == 1) {
|
||||||
|
@ -465,9 +489,6 @@ int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, ui
|
||||||
|
|
||||||
int peernum = peer_in_chat(chat, public_key);
|
int peernum = peer_in_chat(chat, public_key);
|
||||||
|
|
||||||
if (peernum == -1)/*NOTE: This is just for testing and will be removed later.*/
|
|
||||||
peernum = addpeer(chat, public_key);
|
|
||||||
|
|
||||||
if (peernum == -1)
|
if (peernum == -1)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
@ -493,6 +514,12 @@ uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length)
|
||||||
return send_data(chat, message, length, 64); //TODO: better return values?
|
return send_data(chat, message, length, 64); //TODO: better return values?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id)
|
||||||
|
{
|
||||||
|
addpeer(chat, client_id);
|
||||||
|
return send_data(chat, client_id, crypto_box_PUBLICKEYBYTES, 16); //TODO: better return values?
|
||||||
|
}
|
||||||
|
|
||||||
void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *),
|
void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *),
|
||||||
void *userdata)
|
void *userdata)
|
||||||
{
|
{
|
||||||
|
|
|
@ -33,9 +33,11 @@ extern "C" {
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t client_id[crypto_box_PUBLICKEYBYTES];
|
uint8_t client_id[crypto_box_PUBLICKEYBYTES];
|
||||||
uint64_t last_recv;
|
|
||||||
uint64_t pingid;
|
uint64_t pingid;
|
||||||
uint64_t last_pinged;
|
uint64_t last_pinged;
|
||||||
|
|
||||||
|
uint64_t last_recv;
|
||||||
|
uint64_t last_recv_msgping;
|
||||||
uint32_t last_message_number;
|
uint32_t last_message_number;
|
||||||
} Group_Peer;
|
} Group_Peer;
|
||||||
|
|
||||||
|
@ -78,6 +80,14 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat,
|
||||||
*/
|
*/
|
||||||
uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length);
|
uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tell everyone about a new peer (a person we are inviting for example.)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id);
|
||||||
|
|
||||||
|
|
||||||
/* Create a new group chat.
|
/* Create a new group chat.
|
||||||
*
|
*
|
||||||
* Returns a new group chat instance if success.
|
* Returns a new group chat instance if success.
|
||||||
|
|
|
@ -84,6 +84,7 @@ int main()
|
||||||
ip_port.port = htons(12745);
|
ip_port.port = htons(12745);
|
||||||
|
|
||||||
for (i = 0; i < NUM_CHATS; ++i) {
|
for (i = 0; i < NUM_CHATS; ++i) {
|
||||||
|
group_newpeer(chats[0], chats[i]->self_public_key);
|
||||||
chat_bootstrap(chats[i], ip_port, chats[0]->self_public_key);
|
chat_bootstrap(chats[i], ip_port, chats[0]->self_public_key);
|
||||||
printf("%u\n", i);
|
printf("%u\n", i);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user