1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/* Messenger.c
*
* An implementation of a simple text chat only messenger on the tox network core.
*
*/
#include "Messenger.h"
typedef struct
{
uint8_t client_id[CLIENT_ID_SIZE];
}Friend;
#define MAX_NUM_FRIENDS 256
Friend friendlist[MAX_NUM_FRIENDS];
//add a friend
//returns the friend number if success
//return -1 if failure.
int m_addfriend(uint8_t * client_id)
{
//add friend to the DHT
addfriend(uint8_t * client_id);
send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length);
}
//remove a friend
int m_delfriend(int friendnumber)
{
//delete friend from DHT
delfriend(uint8_t * client_id);
}
//return 1 if friend is online
//return 0 if he is not
int m_friendonline(int friendnumber)
{
}
//send a text chat message to a friend.
int m_sendmessage(int friendnumber)
{
write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length);
}
#define PORT 33445
//run this at startup
void initMessenger();
{
new_keys();
IP ip;
ip.i = 0;
init_networking(ip, PORT);
}
//the main loop that needs to be run at least 200 times per second.
void doMessenger();
{
IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length;
while(recievepacket(&ip_port, data, &length) != -1)
{
//if(rand() % 3 != 1)//simulate packet loss
//{
if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
{
//if packet is discarded
printf("Received unhandled packet with length: %u\n", length);
}
else
{
printf("Received handled packet with length: %u\n", length);
}
//}
}
doDHT();
doLossless_UDP();
doNetCrypto();
}
|