Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
tsn-measures
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
tsn-measures
Commits
04802a76
Commit
04802a76
authored
May 28, 2020
by
Joanne Hugé
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create separate file for receiving UDP packets and clean the code
parent
6274d4a9
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
91 additions
and
49 deletions
+91
-49
packet-exchange/build/Makefile
packet-exchange/build/Makefile
+2
-0
packet-exchange/src/client.c
packet-exchange/src/client.c
+1
-1
packet-exchange/src/recv_packet.c
packet-exchange/src/recv_packet.c
+73
-0
packet-exchange/src/recv_packet.h
packet-exchange/src/recv_packet.h
+7
-0
packet-exchange/src/send_packet.c
packet-exchange/src/send_packet.c
+1
-1
packet-exchange/src/send_packet.h
packet-exchange/src/send_packet.h
+1
-1
packet-exchange/src/server.c
packet-exchange/src/server.c
+6
-46
No files found.
packet-exchange/build/Makefile
View file @
04802a76
...
...
@@ -6,6 +6,8 @@ CLIENT_PROG = client
SRCDIR
=
../src
SERVER_SRCS
=
server.c
SERVER_SRCS
+=
recv_packet.c
CLIENT_SRCS
=
client.c
CLIENT_SRCS
+=
send_packet.c
...
...
packet-exchange/src/client.c
View file @
04802a76
...
...
@@ -129,7 +129,7 @@ int main(int argc, char *argv[]) {
// Process bash options
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
);
...
...
packet-exchange/src/recv_packet.c
0 → 100644
View file @
04802a76
#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
}
packet-exchange/src/recv_packet.h
0 → 100644
View file @
04802a76
#ifndef RECV_PACKET
#define RECV_PACKET
int
init_udp_recv
();
void
recv_udp_packet
(
int
fd
);
#endif
packet-exchange/src/send_packet.c
View file @
04802a76
...
...
@@ -90,7 +90,7 @@ static void init_tx_buffer(size_t _tx_buffer_len) {
/*
* 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
)
{
int
index
;
...
...
packet-exchange/src/send_packet.h
View file @
04802a76
...
...
@@ -4,7 +4,7 @@
#include <stdint.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
);
#endif
packet-exchange/src/server.c
View file @
04802a76
...
...
@@ -29,6 +29,8 @@
#include <time.h>
#include <unistd.h>
#include "recv_packet.h"
#define NSEC_PER_SEC UINT64_C(1000000000)
#define SERVER_PORT "50000"
...
...
@@ -71,13 +73,6 @@ static void *packet_receiving_thread(void *p) {
uint64_t
diff
=
0
;
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
->
max_interval
=
0
;
...
...
@@ -96,12 +91,10 @@ static void *packet_receiving_thread(void *p) {
// Packet receiving loop
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
)
error
(
EXIT_FAILURE
,
errno
,
"Error while attempting to receive packets"
);
recv_udp_packet
(
param
->
sockfd
);
clock_gettime
(
CLOCK_MONOTONIC
,
&
current
);
if
(
stats
->
packets_received
)
{
diff
=
calcdiff_ns
(
current
,
previous
);
...
...
@@ -129,7 +122,7 @@ int main(int argc, char *argv[]) {
// Process bash options
process_options
(
argc
,
argv
,
&
param
,
&
main_param
);
init_server
(
&
param
);
param
.
sockfd
=
init_udp_recv
(
);
usleep
(
10000
);
...
...
@@ -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
)
{
uint64_t
diff
;
diff
=
NSEC_PER_SEC
*
(
uint64_t
)((
int
)
t1
.
tv_sec
-
(
int
)
t2
.
tv_sec
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment