Commit 04802a76 authored by Joanne Hugé's avatar Joanne Hugé

Create separate file for receiving UDP packets and clean the code

parent 6274d4a9
...@@ -6,6 +6,8 @@ CLIENT_PROG = client ...@@ -6,6 +6,8 @@ CLIENT_PROG = client
SRCDIR = ../src SRCDIR = ../src
SERVER_SRCS = server.c SERVER_SRCS = server.c
SERVER_SRCS += recv_packet.c
CLIENT_SRCS = client.c CLIENT_SRCS = client.c
CLIENT_SRCS += send_packet.c CLIENT_SRCS += send_packet.c
......
...@@ -129,7 +129,7 @@ int main(int argc, char *argv[]) { ...@@ -129,7 +129,7 @@ int main(int argc, char *argv[]) {
// Process bash options // Process bash options
process_options(argc, argv, &param, &main_param); process_options(argc, argv, &param, &main_param);
init_udp_etf(param.enable_etf, param.enable_timestamps, main_param.packet_priority, param.network_if, main_param.tx_buffer_len); init_udp_send(param.enable_etf, param.enable_timestamps, main_param.packet_priority, param.network_if, main_param.tx_buffer_len);
usleep(10000); usleep(10000);
......
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <error.h>
#include <inttypes.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define SERVER_PORT "50000"
#define BUFFER_SIZE 1024
static char rx_buffer[BUFFER_SIZE];
int init_udp_recv() {
int status;
int fd = -1;
struct addrinfo hints, *servinfo, *servinfo_it;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
status = getaddrinfo(NULL, SERVER_PORT, &hints, &servinfo);
if (status != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
exit(EXIT_FAILURE);
}
for (servinfo_it = servinfo; servinfo_it;
servinfo_it = servinfo_it->ai_next) {
fd = socket(servinfo->ai_family, servinfo->ai_socktype,
servinfo->ai_protocol);
if (bind(fd, servinfo_it->ai_addr, servinfo_it->ai_addrlen) == -1) {
close(fd);
continue;
}
break;
}
freeaddrinfo(servinfo);
if(fd == -1)
error(EXIT_FAILURE, errno, "Couldn't create receive socket");
printf("waiting to receive...\n");
return fd;
}
void recv_udp_packet(int fd) {
#ifdef DEBUG
int bytes_received = 0;
bytes_received = recvfrom(fd, rx_buffer, BUFFER_SIZE - 1, 0, NULL, NULL);
if (bytes_received == -1)
error(EXIT_FAILURE, errno, "Error while attempting to receive packets");
#else
recvfrom(fd, rx_buffer, BUFFER_SIZE - 1, 0, NULL, NULL);
#endif
}
#ifndef RECV_PACKET
#define RECV_PACKET
int init_udp_recv();
void recv_udp_packet(int fd);
#endif
...@@ -90,7 +90,7 @@ static void init_tx_buffer(size_t _tx_buffer_len) { ...@@ -90,7 +90,7 @@ static void init_tx_buffer(size_t _tx_buffer_len) {
/* /*
* Init UDP socket * Init UDP socket
*/ */
void init_udp_etf(int use_etf, int use_timestamps, int packet_priority, void init_udp_send(int use_etf, int use_timestamps, int packet_priority,
char *network_if, size_t _tx_buffer_len) { char *network_if, size_t _tx_buffer_len) {
int index; int index;
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
void init_udp_etf(int use_etf, int use_timestamps, int so_priority, char * network_if, size_t tx_buffer_len); void init_udp_send(int use_etf, int use_timestamps, int so_priority, char * network_if, size_t tx_buffer_len);
void send_udp_packet(int use_etf, int use_timestamps, uint64_t txtime, const char *server_ip); void send_udp_packet(int use_etf, int use_timestamps, uint64_t txtime, const char *server_ip);
#endif #endif
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include "recv_packet.h"
#define NSEC_PER_SEC UINT64_C(1000000000) #define NSEC_PER_SEC UINT64_C(1000000000)
#define SERVER_PORT "50000" #define SERVER_PORT "50000"
...@@ -71,13 +73,6 @@ static void *packet_receiving_thread(void *p) { ...@@ -71,13 +73,6 @@ static void *packet_receiving_thread(void *p) {
uint64_t diff = 0; uint64_t diff = 0;
cpu_set_t mask; cpu_set_t mask;
char buf[BUFFER_SIZE];
int bytes_received = 0;
struct sockaddr_storage client_addr;
socklen_t addr_len;
addr_len = sizeof client_addr;
stats->min_interval = UINT64_MAX; stats->min_interval = UINT64_MAX;
stats->max_interval = 0; stats->max_interval = 0;
...@@ -96,12 +91,10 @@ static void *packet_receiving_thread(void *p) { ...@@ -96,12 +91,10 @@ static void *packet_receiving_thread(void *p) {
// Packet receiving loop // Packet receiving loop
for (stats->packets_received = 0;; stats->packets_received++) { for (stats->packets_received = 0;; stats->packets_received++) {
bytes_received = recvfrom(param->sockfd, buf, BUFFER_SIZE - 1, 0,
(struct sockaddr *)&client_addr, &addr_len);
clock_gettime(CLOCK_MONOTONIC, &current);
if (bytes_received == -1) recv_udp_packet(param->sockfd);
error(EXIT_FAILURE, errno, "Error while attempting to receive packets");
clock_gettime(CLOCK_MONOTONIC, &current);
if (stats->packets_received) { if (stats->packets_received) {
diff = calcdiff_ns(current, previous); diff = calcdiff_ns(current, previous);
...@@ -129,7 +122,7 @@ int main(int argc, char *argv[]) { ...@@ -129,7 +122,7 @@ int main(int argc, char *argv[]) {
// Process bash options // Process bash options
process_options(argc, argv, &param, &main_param); process_options(argc, argv, &param, &main_param);
init_server(&param); param.sockfd = init_udp_recv();
usleep(10000); usleep(10000);
...@@ -172,39 +165,6 @@ static void process_options(int argc, char *argv[], thread_param_t *param, ...@@ -172,39 +165,6 @@ static void process_options(int argc, char *argv[], thread_param_t *param,
} }
} }
void init_server(thread_param_t *param) {
int status;
struct addrinfo hints, *servinfo, *servinfo_it;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
status = getaddrinfo(NULL, SERVER_PORT, &hints, &servinfo);
if (status != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
exit(EXIT_FAILURE);
}
for (servinfo_it = servinfo; servinfo_it;
servinfo_it = servinfo_it->ai_next) {
param->sockfd = socket(servinfo->ai_family, servinfo->ai_socktype,
servinfo->ai_protocol);
if (bind(param->sockfd, servinfo_it->ai_addr, servinfo_it->ai_addrlen) ==
-1) {
close(param->sockfd);
continue;
}
break;
}
freeaddrinfo(servinfo);
printf("waiting to receive...\n");
}
static inline uint64_t calcdiff_ns(struct timespec t1, struct timespec t2) { static inline uint64_t calcdiff_ns(struct timespec t1, struct timespec t2) {
uint64_t diff; uint64_t diff;
diff = NSEC_PER_SEC * (uint64_t)((int)t1.tv_sec - (int)t2.tv_sec); diff = NSEC_PER_SEC * (uint64_t)((int)t1.tv_sec - (int)t2.tv_sec);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment