-
Notifications
You must be signed in to change notification settings - Fork 1
/
testclient.c
184 lines (145 loc) · 3.83 KB
/
testclient.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* very basic client to test tox-group */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ncurses.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <time.h>
#include <arpa/inet.h>
#define debug(...) wprintw(win, __VA_ARGS__); wrefresh(win);
#define countof(x) (sizeof(x)/sizeof(*(x)))
#define DEFAULT_PORT 0x707
#define msec(x) ((uint64_t)x * 1000 * 1000)
WINDOW *win, *win_info, *win_input;
uint64_t get_time(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return ((uint64_t)ts.tv_sec * (1000 * 1000 * 1000)) + (uint64_t)ts.tv_nsec;
}
#include "toxgroup.c"
void update_info(ToxGroup *g)
{
wmove(win_info, 0, 0);
werase(win_info);
wprintw(win_info, "npeer: %u | nconn %u | info %u\n", g->npeer, g->nconn, g->info);
int i = 0;
while(i < 4) {
if(i < g->npeer) {
PEER *p = &g->peerlist[i];
CONN *c;
c = conn_find(g, p->ip, p->port);
char ip[16];
inet_ntop(AF_INET, &p->ip, ip, 16);
if(!c) {
wprintw(win_info, "%s:%u (%u) (%X)\n", ip, p->port, p->timeout, p->info);
} else {
wprintw(win_info, "%s:%u (%u) (%u) (%u) (%X, %X)\n",
ip, p->port, p->timeout, c->timeout, c->connect, p->info, c->info);
}
} else {
wprintw(win_info, "\n");
}
i++;
}
wrefresh(win_info);
}
_Bool send_audio;
void do_input(ToxGroup *g)
{
static char inputstr[256] = {0};
static int inputlen = 0;
int ch;
while((ch = wgetch(win_input)) != ERR) {
switch(ch) {
case 127: {
if(inputlen != 0) {inputlen--;}
break;
}
case '\n': {
if(!memcmp("special", inputstr, 7)) {
toxgroup_beginaudio(g);
send_audio = 1;
break;
}
toxgroup_sendchat(g, (uint8_t*)inputstr, inputlen);
debug("Sent: %.*s\n", inputlen, inputstr);
//group_write_message(inputstr, inputlen);
inputlen = 0;
break;
}
default: {
inputstr[inputlen++] = ch;
break;
}
}
}
wmove(win_input, 0, 0);
werase(win_input);
wprintw(win_input, "say: %.*s", inputlen, inputstr);
wrefresh(win_input);
}
void curses_init(void)
{
initscr();
noecho();
cbreak();
int h, w;
getmaxyx(stdscr, h, w);
win = newwin(h - 6, w, 5, 0);
win_info = newwin(5, w, 0, 0);
win_input = newwin(1, w, h - 1, 0);
scrollok(win, TRUE);
nodelay(win_input, TRUE);
}
_Bool info_change;
void peer_callback(ToxGroup *g, uint8_t id, uint8_t change)
{
info_change = 1;
}
void message_callback(ToxGroup *g, const uint8_t *msg, uint16_t length)
{
debug("Anon: %.*s\n", length, msg);
}
int main(int argc, char** argv)
{
ToxGroup *g;
if(argc != 1 && argc != 3) {
printf("Usage: %s [ip port]\n", argv[0]);
return 1;
}
curses_init();
if(argc == 3) {
uint32_t ip;
uint16_t port;
inet_pton(AF_INET, argv[1], &ip);
port = strtol(argv[2], NULL, 0);
g = toxgroup_new_bootstrap(ip, port);
} else {
g = toxgroup_new();
}
update_info(g);
g->peer_callback = peer_callback;
g->message_callback = message_callback;
int z = 0;
while(1) {
toxgroup_do(g);
do_input(g);
if(info_change) {
update_info(g);
info_change = 0;
}
usleep(500);
z++;
if(z == 2000) {
if(send_audio) {toxgroup_sendaudio(g);}
z = 0;
}
}
return 0;
}