summaryrefslogtreecommitdiff
path: root/testing/Lossless_UDP_testclient.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/Lossless_UDP_testclient.c')
-rw-r--r--testing/Lossless_UDP_testclient.c260
1 files changed, 0 insertions, 260 deletions
diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c
deleted file mode 100644
index 535509e0..00000000
--- a/testing/Lossless_UDP_testclient.c
+++ /dev/null
@@ -1,260 +0,0 @@
1/* Lossless_UDP testclient
2 * A program that connects and sends a file using our lossless UDP algorithm.
3 * NOTE: this program simulates a 33% packet loss.
4 *
5 * Best used in combination with Lossless_UDP_testserver
6 *
7 * Compile with: gcc -O2 -Wall -lsodium -o testclient ../toxcore/network.c ../toxcore/Lossless_UDP.c ../toxcore/util.c Lossless_UDP_testclient.c
8 *
9 * Command line arguments are the ip and port to connect and send the file to.
10 * EX: ./testclient --ipv4 127.0.0.1 33445 filename.txt
11 *
12 * Copyright (C) 2013 Tox project All Rights Reserved.
13 *
14 * This file is part of Tox.
15 *
16 * Tox is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * Tox is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
28 *
29 */
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include "../toxcore/network.h"
36#include "../toxcore/Lossless_UDP.h"
37#include "../toxcore/util.h"
38#include "misc_tools.c"
39
40#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
41
42#define c_sleep(x) Sleep(1*x)
43
44#else
45#include <unistd.h>
46#include <arpa/inet.h>
47#define c_sleep(x) usleep(1000*x)
48
49#endif
50
51#define PORT 33446
52
53void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port)
54{
55 uint32_t i;
56 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
57 printf("--------------------BEGIN-----------------------------\n");
58
59 for (i = 0; i < length; i++) {
60 if (data[i] < 16)
61 printf("0");
62
63 printf("%hhX", data[i]);
64 }
65
66 printf("\n--------------------END-----------------------------\n\n\n");
67}
68
69void printip(IP_Port ip_port)
70{
71 printf("\nIP: %s Port: %u", ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
72}
73/*
74void printpackets(Data test)
75{
76 int i;
77 if(test.size == 0)
78 return;
79 printf("SIZE: %u\n", test.size);
80 for(i =0; i < test.size; i++)
81 {
82 printf("%hhX", test.data[i]);
83 }
84 printf("\n");
85}
86
87void printconnection(int connection_id)
88{
89 printf("--------------------BEGIN---------------------\n");
90 IP_Port ip_port = connections[connection_id].ip_port;
91 printf("IP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port));
92 printf("status: %u, inbound: %u, SYNC_rate: %u\n", connections[connection_id].status,
93 connections[connection_id].inbound, connections[connection_id].SYNC_rate);
94 printf("data rate: %u, last sync: %llu, last sent: %llu, last recv: %llu \n", connections[connection_id].data_rate,
95 connections[connection_id].last_SYNC, connections[connection_id].last_sent, connections[connection_id].last_recv);
96 int i;
97 for(i =0; i < MAX_QUEUE_NUM; i++)
98 {
99 printf(" %u ",i);
100 printpackets(connections[connection_id].sendbuffer[i]);
101 }
102 for(i =0; i < MAX_QUEUE_NUM; i++)
103 {
104 printf(" %u ",i);
105 printpackets(connections[connection_id].recvbuffer[i]);
106 }
107 Data sendbuffer[MAX_QUEUE_NUM];
108 Data recvbuffer[MAX_QUEUE_NUM];
109 printf("recv_num: %u, orecv_num: %u, sent_packetnum %u, osent_packetnum: %u, successful_sent: %u, successful_read: %u\n",
110 connections[connection_id].recv_packetnum,
111 connections[connection_id].orecv_packetnum, connections[connection_id].sent_packetnum, connections[connection_id].osent_packetnum,
112 connections[connection_id].successful_sent,
113 connections[connection_id].successful_read);
114
115 printf("req packets: \n");
116 for(i = 0; i < BUFFER_PACKET_NUM; i++)
117 {
118 printf(" %u ", connections[connection_id].req_packets[i]);
119 }
120 printf("\nNumber: %u recv_counter: %u, send_counter: %u\n", connections[connection_id].num_req_paquets,
121 connections[connection_id].recv_counter, connections[connection_id].send_counter);
122
123 printf("--------------------END---------------------\n");
124
125}
126*/
127
128/*( receive packets and send them to the packethandler */
129/*run doLossless_UDP(); */
130//void Lossless_UDP()
131//{
132/* IP_Port ip_port;
133 uint8_t data[MAX_UDP_PACKET_SIZE];
134 uint32_t length;
135 while (receivepacket(&ip_port, data, &length) != -1) {
136 printf("packet with length: %u\n", length); */
137/* if(rand() % 3 != 1)//add packet loss
138 { */
139/*
140 if (LosslessUDP_handlepacket(data, length, ip_port))
141 printpacket(data, length, ip_port);
142 else
143 printf("Received handled packet with length: %u\n", length); //printconnection(0); */
144
145/* } */
146/* }*/
147
148//networking_poll();
149
150//doLossless_UDP();
151
152//}
153
154int main(int argc, char *argv[])
155{
156 /* let user override default by cmdline */
157 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
158 int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
159
160 if (argvoffset < 0)
161 exit(1);
162
163 if (argc < argvoffset + 4) {
164 printf("Usage: %s [--ipv4|--ipv6] ip port filename\n", argv[0]);
165 exit(0);
166 }
167
168 uint8_t buffer[MAX_DATA_SIZE];
169 int read;
170
171 FILE *file = fopen(argv[argvoffset + 3], "rb");
172
173 if (file == NULL) {
174 printf("Failed to open file \"%s\".\n", argv[argvoffset + 3]);
175 return 1;
176 }
177
178
179 /* initialize networking */
180 /* bind to ip 0.0.0.0:PORT */
181 IP ip;
182 ip_init(&ip, ipv6enabled);
183
184 Lossless_UDP *ludp = new_lossless_udp(new_networking(ip, PORT));
185 perror("Initialization");
186
187 IP_Port serverip;
188 ip_init(&serverip.ip, ipv6enabled);
189
190 if (!addr_resolve(argv[argvoffset + 1], &serverip.ip, NULL)) {
191 printf("Failed to convert \"%s\" into an IP address.\n", argv[argvoffset + 1]);
192 return 1;
193 }
194
195 serverip.port = htons(atoi(argv[argvoffset + 2]));
196 printip(serverip);
197
198 int connection = new_connection(ludp, serverip);
199 uint64_t timer = current_time();
200
201 while (1) {
202 /* printconnection(connection); */
203 networking_poll(ludp->net);
204 do_lossless_udp(ludp);
205
206 if (is_connected(ludp, connection) == LUDP_ESTABLISHED) {
207 printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
208 break;
209 }
210
211 if (is_connected(ludp, connection) == LUDP_NO_CONNECTION) {
212 printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
213 return 1;
214 }
215
216 c_sleep(1);
217 }
218
219 timer = current_time();
220 unsigned long long bytes_sent = 0;
221
222 /*read first part of file */
223 read = fread(buffer, 1, MAX_DATA_SIZE, file);
224
225 while (1) {
226 /* printconnection(connection); */
227 networking_poll(ludp->net);
228 do_lossless_udp(ludp);
229
230 if (is_connected(ludp, connection) == LUDP_ESTABLISHED) {
231
232 while (write_packet(ludp, connection, buffer, read)) {
233 bytes_sent += read;
234 /* printf("Wrote data.\n"); */
235 read = fread(buffer, 1, MAX_DATA_SIZE, file);
236
237 }
238
239 /* printf("%u\n", sendqueue(connection)); */
240 if (sendqueue(ludp, connection) == 0) {
241 if (read == 0) {
242 unsigned long long us = (unsigned long long)(current_time() - timer);
243 printf("Sent file successfully in: %llu us = %llu seconds. Average speed: %llu KB/s\n", us, us / 1000000UL,
244 bytes_sent / (us / 1024UL));
245 //printf("Total bytes sent: %llu B, Total data sent: %llu B, overhead: %llu B\n", total_bytes_sent, bytes_sent, total_bytes_sent-bytes_sent);
246 break;
247 }
248 }
249 } else {
250 printf("%u Client Connecting Lost after: %llu us\n", is_connected(ludp, connection),
251 (unsigned long long)(current_time() - timer));
252 return 0;
253 }
254
255 }
256
257 c_sleep(25);
258
259 return 0;
260}