summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/network.c8
-rw-r--r--testing/Lossless_UDP_testclient.c44
-rw-r--r--testing/Lossless_UDP_testserver.c43
3 files changed, 56 insertions, 39 deletions
diff --git a/core/network.c b/core/network.c
index a20b02f6..bbc1f570 100644
--- a/core/network.c
+++ b/core/network.c
@@ -25,7 +25,7 @@
25#include "network.h" 25#include "network.h"
26 26
27 27
28//returns current time in milleseconds since the epoch. 28//returns current time in milliseconds since the epoch.
29uint64_t current_time() 29uint64_t current_time()
30{ 30{
31 uint64_t time; 31 uint64_t time;
@@ -62,7 +62,7 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length)
62 62
63} 63}
64 64
65//Function to recieve data, ip and port of sender is put into ip_port 65//Function to receive data, ip and port of sender is put into ip_port
66//the packet data into data 66//the packet data into data
67//the packet length into length. 67//the packet length into length.
68//dump all empty packets. 68//dump all empty packets.
@@ -73,7 +73,7 @@ int recievepacket(IP_Port * ip_port, char * data, uint32_t * length)
73 (*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); 73 (*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
74 if(*(int *)length <= 0) 74 if(*(int *)length <= 0)
75 { 75 {
76 //nothing recieved 76 //nothing received
77 //or empty packet 77 //or empty packet
78 return -1; 78 return -1;
79 } 79 }
@@ -106,7 +106,7 @@ int init_networking(IP ip ,uint16_t port)
106 //initialize our socket 106 //initialize our socket
107 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 107 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
108 108
109 //Functions to increase the size of the send and recieve UDP buffers 109 //Functions to increase the size of the send and receive UDP buffers
110 //NOTE: uncomment if necessary 110 //NOTE: uncomment if necessary
111 /* 111 /*
112 int n = 1024 * 1024 * 2; 112 int n = 1024 * 1024 * 2;
diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c
index bd4b1e23..d092125b 100644
--- a/testing/Lossless_UDP_testclient.c
+++ b/testing/Lossless_UDP_testclient.c
@@ -3,7 +3,7 @@
3 * 3 *
4 * Best used in combination with Lossless_UDP_testserver 4 * Best used in combination with Lossless_UDP_testserver
5 * 5 *
6 * Compile with: gcc -O2 -Wall -o test ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testclient.c 6 * Compile with: gcc -O2 -Wall -o testclient ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testclient.c
7 * 7 *
8 * Command line arguments are the ip and port to cennect and send the file to. 8 * Command line arguments are the ip and port to cennect and send the file to.
9 * EX: ./test 127.0.0.1 33445 filename.txt 9 * EX: ./test 127.0.0.1 33445 filename.txt
@@ -40,6 +40,29 @@ void printpacket(char * data, uint32_t length, IP_Port ip_port)
40 printf("\n--------------------END-----------------------------\n\n\n"); 40 printf("\n--------------------END-----------------------------\n\n\n");
41} 41}
42 42
43//recieve packets and send them to the packethandler
44//run doLossless_UDP();
45void Lossless_UDP()
46{
47 IP_Port ip_port;
48 char data[MAX_UDP_PACKET_SIZE];
49 uint32_t length;
50 while(recievepacket(&ip_port, data, &length) != -1)
51 {
52 if(LosslessUDP_handlepacket(data, length, ip_port))
53 {
54 printpacket(data, length, ip_port);
55 }
56 else
57 {
58 printf("Received handled packet with length: %u\n", length);
59 }
60 }
61
62 doLossless_UDP();
63
64}
65
43 66
44int main(int argc, char *argv[]) 67int main(int argc, char *argv[])
45{ 68{
@@ -68,7 +91,7 @@ int main(int argc, char *argv[])
68 uint64_t timer = current_time(); 91 uint64_t timer = current_time();
69 while(1) 92 while(1)
70 { 93 {
71 94 Lossless_UDP();
72 if(is_connected(connection) == 3) 95 if(is_connected(connection) == 3)
73 { 96 {
74 printf("Connecting took: %llu us", (unsigned long long)(current_time() - timer)); 97 printf("Connecting took: %llu us", (unsigned long long)(current_time() - timer));
@@ -83,28 +106,13 @@ int main(int argc, char *argv[])
83 } 106 }
84 timer = current_time(); 107 timer = current_time();
85 108
86 IP_Port ip_port;
87 char data[MAX_UDP_PACKET_SIZE];
88 uint32_t length;
89 109
90 //read first part of file 110 //read first part of file
91 read = fread(buffer, 1, 128, file); 111 read = fread(buffer, 1, 128, file);
92 112
93 while(1) 113 while(1)
94 { 114 {
95 while(recievepacket(&ip_port, data, &length) != -1) 115 Lossless_UDP();
96 {
97 if(LosslessUDP_handlepacket(data, length, ip_port))
98 {
99 printpacket(data, length, ip_port);
100 }
101 else
102 {
103 printf("Received handled packet with length: %u\n", length);
104 }
105 }
106
107 doLossless_UDP();
108 116
109 if(is_connected(connection) == 1) 117 if(is_connected(connection) == 1)
110 { 118 {
diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c
index c898d887..5ae35574 100644
--- a/testing/Lossless_UDP_testserver.c
+++ b/testing/Lossless_UDP_testserver.c
@@ -3,7 +3,7 @@
3 * 3 *
4 * Best used in combination with Lossless_UDP_testclient 4 * Best used in combination with Lossless_UDP_testclient
5 * 5 *
6 * Compile with: gcc -O2 -Wall -o test ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testserver.c 6 * Compile with: gcc -O2 -Wall -o testserver ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testserver.c
7 * 7 *
8 * Command line argument is the name of the file to save what we recieve to. 8 * Command line argument is the name of the file to save what we recieve to.
9 * EX: ./test filename1.txt 9 * EX: ./test filename1.txt
@@ -41,6 +41,29 @@ void printpacket(char * data, uint32_t length, IP_Port ip_port)
41 printf("\n--------------------END-----------------------------\n\n\n"); 41 printf("\n--------------------END-----------------------------\n\n\n");
42} 42}
43 43
44//recieve packets and send them to the packethandler
45//run doLossless_UDP();
46void Lossless_UDP()
47{
48 IP_Port ip_port;
49 char data[MAX_UDP_PACKET_SIZE];
50 uint32_t length;
51 while(recievepacket(&ip_port, data, &length) != -1)
52 {
53 if(LosslessUDP_handlepacket(data, length, ip_port))
54 {
55 printpacket(data, length, ip_port);
56 }
57 else
58 {
59 printf("Received handled packet with length: %u\n", length);
60 }
61 }
62
63 doLossless_UDP();
64
65}
66
44 67
45int main(int argc, char *argv[]) 68int main(int argc, char *argv[])
46{ 69{
@@ -67,12 +90,10 @@ int main(int argc, char *argv[])
67 int connection; 90 int connection;
68 uint64_t timer = current_time(); 91 uint64_t timer = current_time();
69 92
70 IP_Port ip_port;
71 char data[MAX_UDP_PACKET_SIZE];
72 uint32_t length;
73 93
74 while(1) 94 while(1)
75 { 95 {
96 Lossless_UDP();
76 connection = incoming_connection(); 97 connection = incoming_connection();
77 if(connection != -1) 98 if(connection != -1)
78 { 99 {
@@ -89,19 +110,7 @@ int main(int argc, char *argv[])
89 110
90 while(1) 111 while(1)
91 { 112 {
92 while(recievepacket(&ip_port, data, &length) != -1) 113 Lossless_UDP();
93 {
94 if(LosslessUDP_handlepacket(data, length, ip_port))
95 {
96 printpacket(data, length, ip_port);
97 }
98 else
99 {
100 printf("Received handled packet with length: %u\n", length);
101 }
102 }
103
104 doLossless_UDP();
105 if(is_connected(connection) == 1) 114 if(is_connected(connection) == 1)
106 { 115 {
107 read = read_packet(connection, buffer); 116 read = read_packet(connection, buffer);