servicepoint-binding-c/example/src/helpers.h
2025-06-18 00:11:18 +02:00

45 lines
881 B
C

#pragma once
#ifndef SERVICEPOINT_BINDING_C_MSLEEP_H
#define SERVICEPOINT_BINDING_C_MSLEEP_H
#include <time.h>
#include <errno.h>
#include <stdlib.h>
#include "servicepoint.h"
static UdpSocket *sock = NULL;
void sock_free() {
sp_udp_free(sock);
}
void sock_init() {
//sock = sp_udp_open_ipv4(127, 0, 0, 1, 2342);
sock = sp_udp_open_ipv4(172, 23, 42, 29, 2342);
if (sock == NULL)
exit(-1);
atexit(sock_free);
}
/// TODO: all of this for sleeping n ms? There should be a better way!
int msleep(long msec) {
if (msec < 0) {
errno = EINVAL;
return -1;
}
struct timespec ts = {
.tv_sec = msec / 1000,
.tv_nsec = (msec % 1000) * 1000000,
};
int res;
do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);
return res;
}
#endif //SERVICEPOINT_BINDING_C_MSLEEP_H