Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
FdUtils.hxx
Go to the documentation of this file.
1
35#ifndef _UTILS_FD_UTILS_HXX_
36#define _UTILS_FD_UTILS_HXX_
37
38#include <unistd.h>
39
40#include "utils/logging.h"
41#include "utils/macros.h"
42
43struct FdUtils
44{
50 static void repeated_read(int fd, void *buf, size_t size)
51 {
52 uint8_t *dst = static_cast<uint8_t *>(buf);
53 while (size)
54 {
55 ssize_t ret = ::read(fd, dst, size);
56 ERRNOCHECK("read", ret);
57 if (ret == 0)
58 {
59 DIE("Unexpected EOF reading the config file.");
60 }
61 size -= ret;
62 dst += ret;
63 }
64 }
65
71 static void repeated_write(int fd, const void *buf, size_t size)
72 {
73 const uint8_t *dst = static_cast<const uint8_t *>(buf);
74 while (size)
75 {
76 ssize_t ret = ::write(fd, dst, size);
77 ERRNOCHECK("write_config", ret);
78 if (ret == 0)
79 {
80 DIE("Unexpected EOF writing the config file.");
81 }
82 size -= ret;
83 dst += ret;
84 }
85 }
86
90 static void optimize_socket_fd(int fd);
91
95 static void optimize_tty_fd(int fd);
96
99 static void optimize_fd(int fd);
100};
101
102#endif // _UTILS_FD_UTILS_HXX_
#define ERRNOCHECK(where, x...)
Calls the function x, and if the return value is negative, prints errno as error message to stderr an...
Definition logging.h:174
#define DIE(MSG)
Unconditionally terminates the current process with a message.
Definition macros.h:143
static void optimize_socket_fd(int fd)
Optimizes the kernel settings like socket and TCP options for an fd that is an outgoing TCP socket.
Definition FdUtils.cxx:67
static void repeated_read(int fd, void *buf, size_t size)
Performs a reliable read from the given FD.
Definition FdUtils.hxx:50
static void optimize_tty_fd(int fd)
Sets the kernel settings like queuing and terminal settings for an fd that is an outgoing tty.
Definition FdUtils.cxx:102
static void optimize_fd(int fd)
For an fd that is an outgoing link, detects what kind of file descriptor this is and calls the approp...
Definition FdUtils.cxx:119
static void repeated_write(int fd, const void *buf, size_t size)
Performs a reliable write to the given FD.
Definition FdUtils.hxx:71