Couple fixes.

This commit is contained in:
irungentoo 2013-06-28 09:15:19 -04:00
parent 457feeed0b
commit fd44c68801
3 changed files with 56 additions and 39 deletions

View File

@ -25,7 +25,7 @@
#include "network.h"
//returns current time in milleseconds since the epoch.
//returns current time in milliseconds since the epoch.
uint64_t current_time()
{
uint64_t time;
@ -62,7 +62,7 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length)
}
//Function to recieve data, ip and port of sender is put into ip_port
//Function to receive data, ip and port of sender is put into ip_port
//the packet data into data
//the packet length into length.
//dump all empty packets.
@ -73,7 +73,7 @@ int recievepacket(IP_Port * ip_port, char * data, uint32_t * length)
(*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
if(*(int *)length <= 0)
{
//nothing recieved
//nothing received
//or empty packet
return -1;
}
@ -106,7 +106,7 @@ int init_networking(IP ip ,uint16_t port)
//initialize our socket
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
//Functions to increase the size of the send and recieve UDP buffers
//Functions to increase the size of the send and receive UDP buffers
//NOTE: uncomment if necessary
/*
int n = 1024 * 1024 * 2;

View File

@ -3,7 +3,7 @@
*
* Best used in combination with Lossless_UDP_testserver
*
* Compile with: gcc -O2 -Wall -o test ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testclient.c
* Compile with: gcc -O2 -Wall -o testclient ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testclient.c
*
* Command line arguments are the ip and port to cennect and send the file to.
* EX: ./test 127.0.0.1 33445 filename.txt
@ -40,6 +40,29 @@ void printpacket(char * data, uint32_t length, IP_Port ip_port)
printf("\n--------------------END-----------------------------\n\n\n");
}
//recieve packets and send them to the packethandler
//run doLossless_UDP();
void Lossless_UDP()
{
IP_Port ip_port;
char data[MAX_UDP_PACKET_SIZE];
uint32_t length;
while(recievepacket(&ip_port, data, &length) != -1)
{
if(LosslessUDP_handlepacket(data, length, ip_port))
{
printpacket(data, length, ip_port);
}
else
{
printf("Received handled packet with length: %u\n", length);
}
}
doLossless_UDP();
}
int main(int argc, char *argv[])
{
@ -68,7 +91,7 @@ int main(int argc, char *argv[])
uint64_t timer = current_time();
while(1)
{
Lossless_UDP();
if(is_connected(connection) == 3)
{
printf("Connecting took: %llu us", (unsigned long long)(current_time() - timer));
@ -83,28 +106,13 @@ int main(int argc, char *argv[])
}
timer = current_time();
IP_Port ip_port;
char data[MAX_UDP_PACKET_SIZE];
uint32_t length;
//read first part of file
read = fread(buffer, 1, 128, file);
while(1)
{
while(recievepacket(&ip_port, data, &length) != -1)
{
if(LosslessUDP_handlepacket(data, length, ip_port))
{
printpacket(data, length, ip_port);
}
else
{
printf("Received handled packet with length: %u\n", length);
}
}
doLossless_UDP();
Lossless_UDP();
if(is_connected(connection) == 1)
{

View File

@ -3,7 +3,7 @@
*
* Best used in combination with Lossless_UDP_testclient
*
* Compile with: gcc -O2 -Wall -o test ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testserver.c
* Compile with: gcc -O2 -Wall -o testserver ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testserver.c
*
* Command line argument is the name of the file to save what we recieve to.
* EX: ./test filename1.txt
@ -41,6 +41,29 @@ void printpacket(char * data, uint32_t length, IP_Port ip_port)
printf("\n--------------------END-----------------------------\n\n\n");
}
//recieve packets and send them to the packethandler
//run doLossless_UDP();
void Lossless_UDP()
{
IP_Port ip_port;
char data[MAX_UDP_PACKET_SIZE];
uint32_t length;
while(recievepacket(&ip_port, data, &length) != -1)
{
if(LosslessUDP_handlepacket(data, length, ip_port))
{
printpacket(data, length, ip_port);
}
else
{
printf("Received handled packet with length: %u\n", length);
}
}
doLossless_UDP();
}
int main(int argc, char *argv[])
{
@ -67,12 +90,10 @@ int main(int argc, char *argv[])
int connection;
uint64_t timer = current_time();
IP_Port ip_port;
char data[MAX_UDP_PACKET_SIZE];
uint32_t length;
while(1)
{
Lossless_UDP();
connection = incoming_connection();
if(connection != -1)
{
@ -89,19 +110,7 @@ int main(int argc, char *argv[])
while(1)
{
while(recievepacket(&ip_port, data, &length) != -1)
{
if(LosslessUDP_handlepacket(data, length, ip_port))
{
printpacket(data, length, ip_port);
}
else
{
printf("Received handled packet with length: %u\n", length);
}
}
doLossless_UDP();
Lossless_UDP();
if(is_connected(connection) == 1)
{
read = read_packet(connection, buffer);