OILS / cpp / fanos.cc View on Github | oilshell.org

57 lines, 34 significant
1// fanos.cc
2
3#include "cpp/fanos.h"
4
5#include <assert.h>
6#include <errno.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <sys/socket.h>
10#include <unistd.h>
11
12#include "cpp/fanos_shared.h"
13
14namespace fanos {
15
16void send(int sock_fd, BigStr* blob) {
17 // TODO: Support this argument
18 int fds[FANOS_NUM_FDS] = {-1, -1, -1};
19
20 FanosError err = {0};
21 fanos_send(sock_fd, blob->data(), len(blob), fds, &err);
22 if (err.err_code != 0) {
23 throw Alloc<IOError>(err.err_code);
24 }
25 if (err.value_err != NULL) {
26 throw Alloc<ValueError>(StrFromC(err.value_err));
27 }
28}
29
30BigStr* recv(int sock_fd, List<int>* fd_out) {
31 FanosError err = {0};
32 FanosResult res = {nullptr, FANOS_INVALID_LEN};
33 int fds[FANOS_NUM_FDS] = {-1, -1, -1};
34
35 fanos_recv(sock_fd, fds, &res, &err);
36
37 if (err.err_code != 0) {
38 throw Alloc<IOError>(err.err_code);
39 }
40 if (err.value_err != nullptr) {
41 throw Alloc<ValueError>(StrFromC(err.value_err));
42 }
43
44 if (res.len == FANOS_EOF) {
45 return nullptr; // EOF sentinel
46 }
47 for (int i = 0; i < 3; i++) {
48 fd_out->append(fds[i]);
49 }
50
51 DCHECK(res.data != nullptr);
52 BigStr* ret = StrFromC(res.data, res.len);
53 free(res.data);
54 return ret;
55}
56
57} // namespace fanos