Skip to content

Commit

Permalink
optimizing for latency
Browse files Browse the repository at this point in the history
  • Loading branch information
majek committed Jun 29, 2015
1 parent 0a85d5e commit 57078ae
Show file tree
Hide file tree
Showing 8 changed files with 1,081 additions and 0 deletions.
10 changes: 10 additions & 0 deletions how-to-receive-a-packet/.clang-format
@@ -0,0 +1,10 @@
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
AlwaysBreakBeforeMultilineStrings: true
AllowShortBlocksOnASingleLine: false

ContinuationIndentWidth: 8
13 changes: 13 additions & 0 deletions how-to-receive-a-packet/build.sh
@@ -0,0 +1,13 @@
#!/bin/sh
set +e


clang -O3 -Wall -Wextra -Wno-unused-parameter -Wpointer-arith -Wl,-z,now -Wl,-z,relro -pie -fPIC -D_FORTIFY_SOURCE=2\
-ggdb -g -pthread \
-o udpserver \
udpserver.c net.c

clang -O3 -Wall -Wextra -Wno-unused-parameter -Wpointer-arith -Wl,-z,now -Wl,-z,relro -pie -fPIC -D_FORTIFY_SOURCE=2\
-ggdb -g -pthread -lm \
-o udpclient \
udpclient.c net.c
58 changes: 58 additions & 0 deletions how-to-receive-a-packet/common.h
@@ -0,0 +1,58 @@
#define ERRORF(x...) fprintf(stderr, x)

#define FATAL(x...) \
do { \
ERRORF("[-] PROGRAM ABORT : " x); \
ERRORF("\n\tLocation : %s(), %s:%u\n\n", __FUNCTION__, \
__FILE__, __LINE__); \
exit(EXIT_FAILURE); \
} while (0)

#define PFATAL(x...) \
do { \
ERRORF("[-] SYSTEM ERROR : " x); \
ERRORF("\n\tLocation : %s(), %s:%u\n", __FUNCTION__, __FILE__, \
__LINE__); \
perror(" OS message "); \
ERRORF("\n"); \
exit(EXIT_FAILURE); \
} while (0)

#define TIMESPEC_NSEC(ts) ((ts)->tv_sec * 1000000000ULL + (ts)->tv_nsec)
#define TIMEVAL_NSEC(ts) \
((ts)->tv_sec * 1000000000ULL + (ts)->tv_usec * 1000ULL)
#define NSEC_TIMESPEC(ns) \
(struct timespec) { (ns) / 1000000000ULL, (ns) % 1000000000ULL }
#define NSEC_TIMEVAL(ns) \
(struct timeval) \
{ \
(ns) / 1000000000ULL, ((ns) % 1000000000ULL) / 1000ULL \
}
#define MSEC_NSEC(ms) ((ms)*1000000ULL)

/* net.c */
struct net_addr
{
int ipver;
struct sockaddr_in sin4;
struct sockaddr_in6 sin6;
struct sockaddr *sockaddr;
int sockaddr_len;
};

void parse_addr(struct net_addr *netaddr, const char *addr);
const char *addr_to_str(struct net_addr *addr);
int net_bind_udp(struct net_addr *addr, int reuseport, int busy_poll);
void net_set_buffer_size(int cd, int max, int send);
void net_gethostbyname(struct net_addr *shost, const char *host, int port);
int net_connect_udp(struct net_addr *addr, int src_port, int busy_poll);

struct thread;
struct thread *thread_spawn(void (*callback)(void *), void *userdata);

uint64_t realtime_now();

const char *optstring_from_long_options(const struct option *opt);

int net_connect_tcp(struct net_addr *shost, int src_port, int busy_poll);
int net_bind_tcp(struct net_addr *addr, int reuseport);

0 comments on commit 57078ae

Please sign in to comment.