hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
netio.H
Go to the documentation of this file.
1 /*
2  * netio : shorthand for basic network I/O
3  */
4 
5 #ifndef HOG_NETIO_H_INCLUDED
6 #define HOG_NETIO_H_INCLUDED
7 
8 #include <hobbes/util/perf.H>
9 #include <hobbes/util/time.H>
10 #include <string>
11 #include <vector>
12 #include <stdexcept>
13 #include <iostream>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 
17 namespace hog {
18 
19 #define out std::cout << "[" << hobbes::showDateTime(hobbes::time() / 1000) << "]: "
20 
21 inline void ssend(int c, const uint8_t* d, size_t dsz) {
22  int rc = 0;
23  size_t o = 0;
24  while (dsz > 0) {
25  rc = ::send(c, d + o, dsz, 0);
26  if (rc <= 0) {
27  if (errno != EINTR) {
28  break;
29  }
30  } else {
31  o += rc;
32  dsz -= rc;
33  }
34  }
35 
36  if (rc <= 0) {
37  throw std::runtime_error("error sending on connection: " + std::string(strerror(errno)));
38  }
39 }
40 
41 inline void ssend(int c, const std::string& s) {
42  size_t n = s.size();
43  ssend(c, (const uint8_t*)&n, sizeof(n));
44  ssend(c, (const uint8_t*)s.data(), n);
45 }
46 
47 inline void srecv(int c, uint8_t* d, size_t dsz) {
48  int rc = 0;
49  size_t o = 0;
50  while (dsz > 0) {
51  rc = recv(c, d + o, dsz, 0);
52  if (rc <= 0) {
53  if (errno != EINTR) {
54  break;
55  }
56  } else {
57  o += rc;
58  dsz -= rc;
59  }
60  }
61 
62  if (rc <= 0) {
63  throw std::runtime_error("error receiving on connection: " + std::string(strerror(errno)));
64  }
65 }
66 
67 inline void srecv(int c, std::vector<uint8_t>* d) {
68  size_t sz = 0;
69  srecv(c, (uint8_t*)&sz, sizeof(sz));
70  d->resize(sz);
71  srecv(c, &(*d)[0], sz);
72 }
73 
74 inline void srecv(int c, std::string* s) {
75  size_t sz = 0;
76  srecv(c, (uint8_t*)&sz, sizeof(sz));
77  s->resize(sz);
78  srecv(c, (uint8_t*)&(*s)[0], sz);
79 }
80 
81 inline size_t uptoPow2(size_t x) {
82  size_t i = 1;
83  while (i < x) {
84  size_t j = i << 1;
85  if (j < i) {
86  return x;
87  } else {
88  i = j;
89  }
90  }
91  return i;
92 }
93 
94 struct buffer {
95  size_t allocsz;
96  uint8_t* data;
97  size_t size;
98 
99  buffer() {
100  this->allocsz = 0;
101  this->data = 0;
102  this->size = 0;
103  }
104 
106  free(this->data);
107  }
108 
109  void reserve(size_t sz) {
110  if (sz > this->allocsz) {
111  free(this->data);
112  size_t psz = uptoPow2(sz);
113  this->data = (uint8_t*)malloc(psz);
114  this->allocsz = psz;
115  if (!this->data) {
116  printf("failed to allocate %d bytes from %d bytes!\n", (int)psz, (int)sz);
117  exit(-1);
118  }
119  }
120  }
121 };
122 
123 inline void srecv(int c, buffer* b) {
124  size_t sz = 0;
125  srecv(c, (uint8_t*)&sz, sizeof(sz));
126  b->reserve(sz);
127  srecv(c, b->data, sz);
128  b->size = sz;
129 }
130 
131 }
132 
133 #endif
134 
uint8_t * data
Definition: netio.H:96
buffer()
Definition: netio.H:99
void srecv(int c, uint8_t *d, size_t dsz)
Definition: netio.H:47
Definition: batchrecv.C:20
void * malloc(YYSIZE_T)
void reserve(size_t sz)
Definition: netio.H:109
size_t allocsz
Definition: netio.H:95
size_t uptoPow2(size_t x)
Definition: netio.H:81
size_t size
Definition: netio.H:97
Definition: netio.H:94
~buffer()
Definition: netio.H:105
void free(void *)
void ssend(int c, const uint8_t *d, size_t dsz)
Definition: netio.H:21