This is what MeshLink uses internally as well, and prevents confusion over which units the old int
return value had.
};
char buf[102400];
- int timeout = utcp_timeout(u);
+ struct timeval timeout = utcp_timeout(u);
while(dir) {
- poll(fds, 2, timeout);
+ poll(fds, 2, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
if(fds[0].revents) {
int len = read(0, buf, sizeof buf);
* The return value is the time to the next timeout in milliseconds,
* or maybe a negative value if the timeout is infinite.
*/
-int utcp_timeout(struct utcp *utcp) {
+struct timeval utcp_timeout(struct utcp *utcp) {
struct timeval now;
gettimeofday(&now, NULL);
struct timeval next = {now.tv_sec + 3600, now.tv_usec};
struct timeval diff;
timersub(&next, &now, &diff);
- if(diff.tv_sec < 0)
- return 0;
- return diff.tv_sec * 1000 + diff.tv_usec / 1000;
+ return diff;
}
struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) {
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
+// TODO: Windows
+#include <sys/time.h>
#ifndef UTCP_INTERNAL
struct utcp {
extern int utcp_close(struct utcp_connection *connection);
extern int utcp_abort(struct utcp_connection *connection);
extern int utcp_shutdown(struct utcp_connection *connection, int how);
-extern int utcp_timeout(struct utcp *utcp);
+extern struct timeval utcp_timeout(struct utcp *utcp);
extern void utcp_set_recv_cb(struct utcp_connection *connection, utcp_recv_t recv);
extern void utcp_set_poll_cb(struct utcp_connection *connection, utcp_poll_t poll);