-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_select_trudp.c
822 lines (660 loc) · 24.9 KB
/
main_select_trudp.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/**
* \file main_select.c
* \author Kirill Scherba <kirill@scherba.ru>
*
* \example main_select.c
*
* This is example of Teocli library which use select to check that data is
* available ready to read. This application connect to network
* L0 server, initialize (login) at the L0 server, and send and receive data to
* from network peer.
*
* See server example parameters at:
* https://gitlab.ksproject.org/teonet/teocli/blob/master/README.md#basic-teocli-example
*
* ### This application parameters:
*
* **Usage:** ./teocli_s <client_name> <server_address> <server_port> <peer_name> [message]
*
* **Example:** ./teocli_s C3 127.0.0.1 9000 teostream "Story about this world!"
*
* ### This application algorithm:
*
* * Connect to L0 server with server_address and server_port parameters
* * Send ClientLogin request with client_name parameter
* * Send CMD_L_PEERS request to peer_name server
* * Receive peers data from peer_name server
* * Send CMD_L_ECHO request to peer_name server
* * Receive peers data from peer_name server
* * Close connection
*
* Created on October 19, 2015, 3:51 PM
*/
#if defined(_WIN32) || defined(_WIN64)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !(defined(_WIN32) || defined(_WIN64))
#include <unistd.h>
#endif
#include <errno.h>
#include <stdarg.h>
#include "libteol0/teonet_l0_client.h"
#include "libtrudp/src/trudp.h"
#include "libtrudp/src/trudp_utils.h"
#include "teobase/logging.h"
#define DEBUG 0
#define TL0CNS_VERSION "0.0.2"
// Application constants
#define SEND_MESSAGE_AFTER_MIN 15000 /* 16667 */ // uSec (mSec * 1000)
#define SEND_MESSAGE_AFTER 1000000
#define RECONNECT_AFTER 3000000 // uSec (mSec * 1000)
#define SHOW_STATISTIC_AFTER 500000 // uSec (mSec * 1000)
// Send L0 event to L0 event loop
void* _param = NULL;
#define send_l0_event(tcd, event, data, data_length, user_data) \
event_cb(tcd, event, data, data_length, _param);
/**
* Show error and exit
*
* @param fmt
* @param ...
*/
static void die(char *fmt, ...)
{
va_list ap;
fflush(stdout);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
/**
* Show debug message
*
* @param fmt
* @param ...
*/
static void debug(const void *tru, int mode, char *fmt, ...)
{
static unsigned long idx = 0;
va_list ap;
if(DEBUG) {
fflush(stdout);
fprintf(stderr, "%lu %.3f debug: ", ++idx, trudpGetTimestamp() / 1000.0);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fflush(stderr);
}
}
/**
* Application parameters structure
*/
struct app_parameters {
const char *host_name;
const char *tcp_server;
int tcp_port;
const char *peer_name;
const char *msg;
};
static const int BUFFER_SIZE = 2048;
static int connected_flag = 0;
/**
* Teonet L0 client event callback
*
* @param con
* @param event
* @param data
* @param data_len
* @param user_data
*/
void event_cb(void *tcd, teoLNullEvents event, void *data,
size_t data_len, void *user_data) {
const struct app_parameters *param = user_data;
switch(event) {
case EV_L_CONNECTED:
{
int *fd = data;
if(*fd > 0) {
printf("Successfully connect to server\n");
// // Send (1) Initialization packet to L0 server
// ssize_t snd = teoLNullLogin(con, param->host_name);
// if(snd == -1) perror(strerror(errno));
// printf("\nSend %d bytes packet to L0 server, "
// "Initialization packet\n",
// (int)snd);
// Send (2) peer list request to peer, command CMD_L_PEERS
//snd = teoLNullSend(con, CMD_L_PEERS, param->peer_name, NULL, 0);
//ssize_t snd = 0;
char buf[BUFFER_SIZE];
// Send peers request
size_t pkg_length = teoLNullPacketCreate(buf, BUFFER_SIZE, /*79*/ CMD_L_PEERS, param->peer_name, NULL, 0);
ssize_t snd = trudpChannelSendData(tcd, buf, pkg_length);
//
printf("Send %d bytes packet to L0 server to peer %s, "
"cmd = %d (CMD_L_PEERS)\n",
(int)snd, param->peer_name, CMD_L_PEERS);
// Send lients request
pkg_length = teoLNullPacketCreate(buf, BUFFER_SIZE, CMD_L_L0_CLIENTS, param->peer_name, NULL, 0);
snd = trudpChannelSendData(tcd, buf, pkg_length);
//
printf("Send %d bytes packet to L0 server to peer %s, "
"cmd = %d (CMD_L_L0_CLIENTS)\n",
(int)snd, param->peer_name, CMD_L_L0_CLIENTS);
// // Send (3) echo request to peer, command CMD_L_ECHO
// //
// // Add current time to the end of message (it should be return
// // back by server)
// snd = teoLNullSendEcho(con, param->peer_name, param->msg);
// if(snd == -1) perror(strerror(errno));
// printf("Send %d bytes packet to L0 server to peer %s, "
// "cmd = %d (CMD_L_ECHO), "
// "data: %s\n",
// (int)snd, param->peer_name, CMD_L_ECHO, param->msg);
// Show empty line
printf("\n");
}
else {
printf("Can't connect to server\n");
}
} break;
case EV_L_DISCONNECTED:
printf("Disconnected ...\n");
break;
case EV_L_RECEIVED_UNRELIABLE: // fallthrough
case EV_L_RECEIVED:
{
// Receive answer from server
const size_t rc = data_len;
teoLNullCPacket *cp = (teoLNullCPacket*) data;
printf("Receive %d bytes: %hu bytes data from L0 server, "
"from peer %s, cmd = %hhu\n",
(int)rc, cp->data_length, cp->peer_name, cp->cmd);
// Process commands
switch(cp->cmd) {
case CMD_L_PEERS_ANSWER:
{
// Show peer list
if(cp->data_length > 1) {
ksnet_arp_data_ar *arp_data_ar = (ksnet_arp_data_ar *)
(cp->peer_name + cp->peer_name_length);
const char *ln =
"--------------------------------------------"
"---------\n";
printf("%sPeers (%u): \n%s", ln, arp_data_ar->length, ln);
int i;
for(i = 0; i < (int)arp_data_ar->length; i++) {
printf("%-12s(%2d) %-15s %d %8.3f ms\n",
arp_data_ar->arp_data[i].name,
arp_data_ar->arp_data[i].data.mode,
arp_data_ar->arp_data[i].data.addr,
arp_data_ar->arp_data[i].data.port,
arp_data_ar->arp_data[i].data.last_triptime);
}
printf("%s", ln);
}
} break;
case CMD_L_L0_CLIENTS_ANSWER: {
// Show peer list
teonet_client_data_ar *client_data_ar = (teonet_client_data_ar *)
(cp->peer_name + cp->peer_name_length);
const char *ln = "--------------------------------------------"
"---------\n";
printf("%sClients (%u): \n%s", ln, client_data_ar->length, ln);
int i;
for(i = 0; i < (int)client_data_ar->length; i++) {
printf("%-12s\n", client_data_ar->client_data[i].name);
}
printf("%s", ln);
} break;
case CMD_L_ECHO_ANSWER:
{
printf("Got echo answer command\n");
data = cp->peer_name + cp->peer_name_length;
int trip_time = teoLNullProccessEchoAnswer(data);
// Show data
printf("Data: %s\n", (char*)data);
// Show trip time
printf("Trip time: %d ms\n\n", trip_time);
} break;
case CMD_L_ECHO:
{
printf("Got echo command\n");
} break;
case CMD_L_AUTH_LOGIN_ANSWER: {
printf("Got answer from authentication server\n");
const char *auth_data = (const char *)
(cp->peer_name + cp->peer_name_length);
// Show data
printf("Data: %s\n\n", auth_data);
connected_flag = 1;
send_l0_event(tcd, EV_L_CONNECTED, &((trudpChannelData *)tcd)->fd, sizeof(((trudpChannelData *)tcd)->fd), NULL);
}
break;
default:
printf("Got unknown command\n");
break;
}
} break;
default:
break;
}
}
static char *remote_address;
static int remote_port_i;
// Read buffer
static uint8_t *buffer;
/**
* TR-UDP event callback
*
* @param tcd_pointer
* @param event
* @param data
* @param data_length
* @param user_data
*/
static void trudpEventCback(void *tcd_pointer, int event, void *data, size_t data_length,
void *user_data) {
trudpChannelData *tcd = (trudpChannelData *)tcd_pointer;
void *tru = user_data;
switch(event) {
// CONNECTED event
// @param data NULL
// @param user_data NULL
case CONNECTED: {
debug(tru, DEBUG, "Connect channel %s\n", tcd->channel_key);
} break;
// DISCONNECTED event
// @param tcd Pointer to trudpData
// @param data Last packet received
// @param user_data NULL
case DISCONNECTED: {
if(data_length == sizeof(uint32_t)) {
uint32_t last_received = *(uint32_t*)data;
debug(tru, DEBUG,
"Disconnect channel %s, last received: %.6f sec\n",
tcd->channel_key, last_received / 1000000.0);
trudpChannelDestroy(tcd);
}
else debug(tru, DEBUG, "Disconnected channel %s\n", tcd->channel_key);
connected_flag = 0;
} break;
// GOT_RESET event
// @param data NULL
// @param user_data NULL
case GOT_RESET: {
debug(tru, DEBUG, "got TRU_RESET packet from channel %s\n", tcd->channel_key);
connected_flag = 0;
} break;
// SEND_RESET event
// @param data Pointer to uint32_t id or NULL (data_size == 0)
// @param user_data NULL
case SEND_RESET: {
if(!data) debug(tru, DEBUG, "Send reset: to channel %s\n", tcd->channel_key);
else {
uint32_t id = (data_length == sizeof(uint32_t)) ? *(uint32_t*)data:0;
if(!id)
debug(tru, DEBUG,
"Send reset: "
"Not expected packet with id = 0 received from channel %s\n",
tcd->channel_key);
else
debug(tru, DEBUG,
"Send reset: "
"High send packet number (%d) at channel %s\n",
id, tcd->channel_key);
}
} break;
// GOT_ACK_RESET event: got ACK to reset command
// @param data NULL
// @param user_data NULL
case GOT_ACK_RESET: {
debug(tru, DEBUG, "Got ACK to RESET packet at channel %s\n", tcd->channel_key);
} break;
// GOT_ACK_PING event: got ACK to ping command
// @param data Pointer to ping data (usually it is a string)
// @param user_data NULL
case GOT_ACK_PING: {
debug(tru, DEBUG,
"got ACK to PING packet at channel %s, data: %s, %.3f(%.3f) ms\n",
tcd->channel_key, (char*)data,
(tcd->triptime)/1000.0, (tcd->triptimeMiddle)/1000.0);
} break;
// GOT_PING event: got PING packet, data
// @param data Pointer to ping data (usually it is a string)
// @param user_data NULL
case GOT_PING: {
debug(tru, DEBUG,
"got PING packet at channel %s, data: %s\n",
tcd->channel_key, (char*)data);
} break;
// Got ACK event
// @param data Pointer to ACK packet
// @param data_length Length of data
// @param user_data NULL
case GOT_ACK: {
debug(tru, DEBUG, "got ACK id=%u at channel %s, %.3f(%.3f) ms\n",
trudpPacketGetId(data/*trudpPacketGetPacket(data)*/),
tcd->channel_key, (tcd->triptime)/1000.0, (tcd->triptimeMiddle)/1000.0);
#if USE_LIBEV
// trudp_start_send_queue_cb(&psd, 0);
#endif
} break;
// Got DATA event
// @param data Pointer to data
// @param data_length Length of data
// @param user_data NULL Pointer to teoLNullConnectData
case GOT_DATA: {
teoLNullConnectData *con = user_data;
trudpPacket * packet = (trudpPacket *)data;
uint32_t id = trudpPacketGetId(packet);
size_t block_len = trudpPacketGetDataLength(packet);
void* block = trudpPacketGetData(packet);
ssize_t ready_count = teoLNullRecvCheck(con, block, block_len);
if (ready_count <= 0) {
LTRACK_I("TeonetClient", "Got pktid=%u of %d bytes", id, (int)block_len);
break;
}
teoLNullCPacket *cp = (teoLNullCPacket *)con->read_buffer;
debug(tru, DEBUG,
"got %d byte data at channel %s [%.3f(%.3f) ms], id=%u, "
"peer: %s, cmd: %u, data length: %u\n",
(int)block_len, tcd->channel_key,
(double)tcd->triptime / 1000.0,
(double)tcd->triptimeMiddle / 1000.0,
id, cp->peer_name, (uint32_t)cp->cmd,
(uint32_t)cp->data_length);
// Process ECHO command
if(cp->cmd == CMD_L_ECHO) {
cp->cmd = CMD_L_ECHO_ANSWER;
teoLNullPacketUpdateChecksums(cp);
trudpChannelSendData(tcd, cp, ready_count);
}
// Send other commands to L0 event loop
else send_l0_event(tcd, EV_L_RECEIVED, cp, ready_count, NULL);
} break;
// Process received data
// @param tcd Pointer to trudpData
// @param data Pointer to receive buffer
// @param data_length Receive buffer length
// @param user_data NULL
case PROCESS_RECEIVE: {
trudpData *td = (trudpData *)tcd;
trudpProcessReceived(td, data, data_length);
} break;
// Process send data
// @param data Pointer to send data
// @param data_length Length of send
// @param user_data NULL
case PROCESS_SEND: {
//if(isWritable(tcd->td->fd, timeout) > 0) {
// Send to UDP
trudpUdpSendto(tcd->td->fd, data, data_length,
(__CONST_SOCKADDR_ARG) &tcd->remaddr, tcd->addrlen);
//}
// Debug message
if(/*o.debug*/ 1) {
int port,type;
uint32_t id = trudpPacketGetId(data);
const char *addr = trudpUdpGetAddr((__CONST_SOCKADDR_ARG)&tcd->remaddr, tcd->addrlen, &port);
if(!(type = trudpPacketGetType(data))) {
teoLNullCPacket *cp = trudpPacketGetData(data);
debug(tru, DEBUG, "send %d bytes, id=%u, to %s:%d, %.3f(%.3f) ms, peer: %s, cmd: %d, data: %s\n",
(int)data_length, id, addr, port,
tcd->triptime / 1000.0, tcd->triptimeMiddle / 1000.0, cp->peer_name, cp->cmd, cp->peer_name + cp->peer_name_length);
}
else {
debug(tru, DEBUG, "send %d bytes %s id=%u, to %s:%d\n",
(int)data_length,
type == 1 ? "ACK" :
type == 2 ? "RESET" :
type == 3 ? "ACK to RESET" :
type == 4 ? "PING" : "ACK to PING"
, id, addr, port);
}
}
#if USE_LIBEV
// trudpSendQueueCbStart(&psd, 0);
#endif
} break;
default: break;
}
}
/**
* The TR-UDP cat network loop with select function
*
* @param td Pointer to trudpData
* @param delay Default read data timeout
*/
static void trudpNetworkSelectLoop(trudpData *td, int timeout) {
int rv = 1;
fd_set rfds, wfds;
struct timeval tv;
uint64_t /*tt, next_et = UINT64_MAX,*/ ts = teoGetTimestampFull();
// while(rv > 0) {
// Watch server_socket to see when it has input.
FD_ZERO(&wfds);
FD_ZERO(&rfds);
FD_SET(td->fd, &rfds);
// Process write queue
if(trudpGetWriteQueueSize(td)) {
FD_SET(td->fd, &wfds);
}
uint32_t timeout_sq = trudpGetSendQueueTimeout(td, ts);
// debug("set timeout: %.3f ms; default: %.3f ms, send_queue: %.3f ms%s\n",
// (timeout_sq < timeout ? timeout_sq : timeout) / 1000.0,
// timeout / 1000.0,
// timeout_sq / 1000.0,
// timeout_sq == UINT32_MAX ? "(queue is empty)" : ""
// );
// Wait up to ~50 ms. */
uint32_t t = timeout_sq < timeout ? timeout_sq : timeout;
usecToTv(&tv, t);
rv = select((int)td->fd + 1, &rfds, &wfds, NULL, &tv);
// Error
if (rv == -1) {
fprintf(stderr, "select() handle error\n");
return;
}
// Timeout
else if(!rv) { // Idle or Timeout event
// Process send queue
if(timeout_sq != UINT32_MAX) {
int rv = trudpProcessSendQueue(td, 0);
debug(NULL, DEBUG, "process send queue ... %d\n", rv);
}
}
// There is a data in fd
else {
// Process read fd
if(FD_ISSET(td->fd, &rfds)) {
struct sockaddr_in remaddr; // remote address
socklen_t addr_len = sizeof(remaddr);
int error_code = 0;
size_t recvlen = 0;
// TODO: Handle errors in recvfrom.
// TODO: All of this is big mistake! Fix it!
teosockRecvfromResult recvfrom_result = trudpUdpRecvfrom(td->fd, buffer, BUFFER_SIZE,
(__SOCKADDR_ARG)&remaddr, &addr_len, &recvlen, &error_code);
// Process received packet
if(recvlen > 0) {
trudpChannelData *tcd = trudpGetChannelCreate(td, (__SOCKADDR_ARG)&remaddr, addr_len, 0);
trudpChannelProcessReceivedPacket(tcd, buffer, recvlen);
}
}
// Process write fd
if(FD_ISSET(td->fd, &wfds)) {
// Process write queue
while(trudpProcessWriteQueue(td));
//trudpProcessWriteQueue(td);
}
}
// }
}
/**
* Connect (login) to peer
*
* @param td
* @return
*/
static trudpChannelData *trudpLNullLogin(trudpData *td, const char * host_name) {
trudpChannelData *tcd = NULL;
// ssize_t snd;
const size_t buf_len = teoLNullBufferSize(1, strlen(host_name) + 1);
// Buffer
#if defined(_WIN32) || defined(_WIN64)
char *buf = malloc(buf_len);
#else
char buf[buf_len];
#endif
size_t pkg_length = teoLNullPacketCreateLogin(buf, buf_len, host_name);
if(!pkg_length) return NULL;
tcd = trudpChannelNew(td, remote_address, remote_port_i, 0);
trudpChannelSendData(tcd, buf, pkg_length);
fprintf(stderr, "Connecting to %s:%u:%u\n", remote_address, remote_port_i, 0);
//connected_flag = 1;
tcd->fd = 1;
//send_l0_event(tcd, EV_L_CONNECTED, &tcd->fd, sizeof(tcd->fd), NULL);
//if ((snd = teoLNullPacketSend((int)con->fd, buf, pkg_length)) >= 0) {};
// Free buffer
#if defined(_WIN32) || defined(_WIN64)
free(buf);
#endif
// // Start connection and Send "connect" packet
// char *connect = "Connect with TR-UDP!";
// size_t connect_length = strlen(connect) + 1;
// tcd = trudp_ChannelNew(td, remote_address, remote_port_i, 0);
// trudp_ChannelSendData(tcd, connect, connect_length);
// fprintf(stderr, "Connecting to %s:%u:%u\n", remote_address, remote_port_i, 0);
// connected_flag = 1;
return tcd;
}
static teoLNullConnectData* trudpLNullConnect(void *user_data) {
teoLNullConnectData *con = malloc(sizeof(teoLNullConnectData));
if(con == NULL) return con;
con->last_packet_offset = 0;
con->read_buffer = NULL;
con->read_buffer_offset = 0;
con->read_buffer_size = 0;
con->event_cb = event_cb;
con->user_data = user_data;
con->fd = 0;
return con;
}
static void trudpLNullFree(teoLNullConnectData* con) {
if(con) {
free(con);
}
}
/**
* Main L0 Native client example function
*
* @param argc Number of arguments
* @param argv Arguments array
*
* @return
*/
int main(int argc, char** argv) {
// Welcome message
printf("Teonet L0 client with Select and Event Loop Callback example "
"version " TL0CN_VERSION " (Native TCP/UDP Client)\n\n");
// Check application parameters
if(argc < 5) {
printf("Usage: "
"%s <client_name> <server_address> <server_port> <peer_name> "
"[message]\n", argv[0]);
exit(EXIT_SUCCESS);
}
// Teonet L0 server parameters
struct app_parameters param;
param.host_name = argv[1]; //"C3";
param.tcp_server = argv[2]; //"127.0.0.1"; //"10.12.35.53"; //
param.tcp_port = atoi(argv[3]); //9000;
param.peer_name = argv[4]; //"teostream";
if(argc > 5) param.msg = argv[5];
else param.msg = "Hello"; // from TRUdp client :)";
// Initialize L0 Client library
//teoLNullInit();
// Connect to L0 TR-UDP server
// Bind UDP port and get FD (start listening at port)
_param = ¶m;
int port = 9090; //atoi(o_local_port); // Local port
int fd = trudpUdpBindRaw(&port, 1);
if(fd <= 0) die("Can't bind UDP port ...\n");
else fprintf(stderr, "Start listening at UDP port %d\n", port);
if(fd > 0) {
// Initialize TR-UDP
remote_port_i = param.tcp_port;
remote_address = (char*)param.tcp_server;
teoLNullConnectData* con = trudpLNullConnect(NULL);
trudpData *td = trudpInit(fd, port, trudpEventCback, con);
printf("TR-UDP port created, fd = %d\n", td->fd);
// Create read buffer
buffer = malloc(BUFFER_SIZE);
uint32_t tt, tt_s = 0, tt_c = 0;
const int DELAY = 500000; // uSec
unsigned long num = 0;
int quit_flag = 0;
trudpChannelData *tcd = NULL;
// Event loop
while(!quit_flag) {
trudpNetworkSelectLoop(td, SEND_MESSAGE_AFTER < DELAY ?
SEND_MESSAGE_AFTER : DELAY);
// Current timestamp
tt = trudpGetTimestamp();
// Connect
if(!connected_flag && (tt - tt_c) > RECONNECT_AFTER) {
tcd = trudpLNullLogin(td, param.host_name);
tt_c = tt;
}
// When connected
if(connected_flag) {
// Send Echo command every 1 second
if((tt - tt_s) > SEND_MESSAGE_AFTER * 1) {
//printf("tick...\n");
char buf[BUFFER_SIZE];
// Send ping
size_t pkg_length = teoLNullPacketCreateEcho(buf, BUFFER_SIZE, param.peer_name, param.msg);
trudpChannelSendData(tcd, buf, pkg_length);
// // Send cmd 129
// pkg_length = teoLNullPacketCreate(buf, BUFFER_SIZE, 129, "teo-wg-users"/*param.peer_name*/, NULL, 0);
// trudpChannelSendData(tcd, buf, pkg_length);
tt_s = tt;
}
else trudpProcessKeepConnection(td);
}
num++;
}
// Destroy TR-UDP
trudpLNullFree(con);
trudpDestroy(td);
free(buffer);
}
// // Connect to L0 server
// teoLNullConnectData *con = teoLNullConnectE(param.tcp_server, param.tcp_port,
// event_cb, ¶m);
// if(con->fd > 0) {
//
// unsigned long num = 0;
// const int timeout = 50;
//
// // Event loop
// while(teoLNullReadEventLoop(con, timeout)) {
//
// // Send Echo command every second
// if( !(num % (1000 / timeout)) )
// teoLNullSendEcho(con, param.peer_name, param.msg);
//
// num++;
// }
//
// // Close connection
// teoLNullDisconnect(con);
// }
// Cleanup L0 Client library
//teoLNullCleanup();
return (EXIT_SUCCESS);
}