mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Couple fixes.
This commit is contained in:
parent
457feeed0b
commit
fd44c68801
|
@ -25,7 +25,7 @@
|
||||||
#include "network.h"
|
#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 current_time()
|
||||||
{
|
{
|
||||||
uint64_t 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 data into data
|
||||||
//the packet length into length.
|
//the packet length into length.
|
||||||
//dump all empty packets.
|
//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);
|
(*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
||||||
if(*(int *)length <= 0)
|
if(*(int *)length <= 0)
|
||||||
{
|
{
|
||||||
//nothing recieved
|
//nothing received
|
||||||
//or empty packet
|
//or empty packet
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,7 @@ int init_networking(IP ip ,uint16_t port)
|
||||||
//initialize our socket
|
//initialize our socket
|
||||||
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
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
|
//NOTE: uncomment if necessary
|
||||||
/*
|
/*
|
||||||
int n = 1024 * 1024 * 2;
|
int n = 1024 * 1024 * 2;
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
*
|
*
|
||||||
* Best used in combination with Lossless_UDP_testserver
|
* 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.
|
* Command line arguments are the ip and port to cennect and send the file to.
|
||||||
* EX: ./test 127.0.0.1 33445 filename.txt
|
* 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");
|
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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
@ -68,7 +91,7 @@ int main(int argc, char *argv[])
|
||||||
uint64_t timer = current_time();
|
uint64_t timer = current_time();
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
|
Lossless_UDP();
|
||||||
if(is_connected(connection) == 3)
|
if(is_connected(connection) == 3)
|
||||||
{
|
{
|
||||||
printf("Connecting took: %llu us", (unsigned long long)(current_time() - timer));
|
printf("Connecting took: %llu us", (unsigned long long)(current_time() - timer));
|
||||||
|
@ -83,28 +106,13 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
timer = current_time();
|
timer = current_time();
|
||||||
|
|
||||||
IP_Port ip_port;
|
|
||||||
char data[MAX_UDP_PACKET_SIZE];
|
|
||||||
uint32_t length;
|
|
||||||
|
|
||||||
//read first part of file
|
//read first part of file
|
||||||
read = fread(buffer, 1, 128, file);
|
read = fread(buffer, 1, 128, file);
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
while(recievepacket(&ip_port, data, &length) != -1)
|
Lossless_UDP();
|
||||||
{
|
|
||||||
if(LosslessUDP_handlepacket(data, length, ip_port))
|
|
||||||
{
|
|
||||||
printpacket(data, length, ip_port);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("Received handled packet with length: %u\n", length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
doLossless_UDP();
|
|
||||||
|
|
||||||
if(is_connected(connection) == 1)
|
if(is_connected(connection) == 1)
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
*
|
*
|
||||||
* Best used in combination with Lossless_UDP_testclient
|
* 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.
|
* Command line argument is the name of the file to save what we recieve to.
|
||||||
* EX: ./test filename1.txt
|
* 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");
|
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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
@ -67,12 +90,10 @@ int main(int argc, char *argv[])
|
||||||
int connection;
|
int connection;
|
||||||
uint64_t timer = current_time();
|
uint64_t timer = current_time();
|
||||||
|
|
||||||
IP_Port ip_port;
|
|
||||||
char data[MAX_UDP_PACKET_SIZE];
|
|
||||||
uint32_t length;
|
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
|
Lossless_UDP();
|
||||||
connection = incoming_connection();
|
connection = incoming_connection();
|
||||||
if(connection != -1)
|
if(connection != -1)
|
||||||
{
|
{
|
||||||
|
@ -89,19 +110,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
while(recievepacket(&ip_port, data, &length) != -1)
|
Lossless_UDP();
|
||||||
{
|
|
||||||
if(LosslessUDP_handlepacket(data, length, ip_port))
|
|
||||||
{
|
|
||||||
printpacket(data, length, ip_port);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("Received handled packet with length: %u\n", length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
doLossless_UDP();
|
|
||||||
if(is_connected(connection) == 1)
|
if(is_connected(connection) == 1)
|
||||||
{
|
{
|
||||||
read = read_packet(connection, buffer);
|
read = read_packet(connection, buffer);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user