scclib
Stable Cloud Computing C++ Library
inet_stream.cc
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <gtest/gtest.h>
32 #include <system_error>
33 #include <future>
34 #include <sstream>
35 #include <chrono>
36 #include <thread>
37 
38 #include <util/event.h>
39 #include <util/poller.h>
40 #include <net/inet.h>
41 #include <util/iostream.h>
42 #include <util/logger.h>
43 
50 using std::cout;
51 using std::endl;
52 using std::string;
53 using std::stringstream;
54 using std::ios;
56 using scc::util::Logger;
57 using scc::util::Event;
58 using scc::util::Poller;
59 using scc::net::InetAddr;
61 
62 struct InetServerStreamTest : public testing::Test
63 {
64  Event startev, quitev;
65  InetTcpSock ssock, csock;
66  InetAddr caddr, saddr;
67  Logger log;
68 
69  InetServerStreamTest()
70  {
71  log.add_cout();
72  log.max_line(2048);
73  saddr.host("::");
74  saddr.port(9876);
75  ssock.reuse_addr(true);
76  ssock.bind(saddr);
77  caddr.host("::1");
78  caddr.port(9876);
79  }
80  virtual ~InetServerStreamTest()
81  {
82  }
83 
84  static void set_all_polls(Poller& p, int fd)
85  {
86  p.set(fd, Poller::PollFlag::input|Poller::PollFlag::output|Poller::PollFlag::read_hup|Poller::PollFlag::priority);
87  }
88  static std::string str_polls(int ev)
89  {
90  stringstream s;
91  if (ev & Poller::PollFlag::input) s << " input";
92  if (ev & Poller::PollFlag::output) s << " output";
93  if (ev & Poller::PollFlag::read_hup) s << " read_hup";
94  if (ev & Poller::PollFlag::priority) s << " priority";
95  if (ev & Poller::PollFlag::hup) s << " hup";
96  if (ev & Poller::PollFlag::error) s << " error";
97  return s.str();
98  }
99 
100  static std::string server(InetTcpSock& sock, Event& start, Event& quit)
101  {
102  using scc::util::Poller;
103  Logger log;
104  log.add_cout();
105  log.id("*serv*");
106 
107  stringstream s;
108 
109  try
110  {
111  sock.listen();
112  log << "signal start" << endl;
113  start.write(1);
114 
115  Poller p;
116  p.set(quit, Poller::PollFlag::input);
117  set_all_polls(p, sock);
118 
119  //sock.non_blocking(true);
120  //error_code ec;
121 
122  log << "poll before accept" << endl;
123  s << "*accept_poll:";
124  while (1)
125  {
126  p.wait();
127  if (p.event(quit))
128  {
129  log << "quit event" << endl;
130  s << " quit";
131  return s.str();
132  }
133 
134  int ev = p.event(sock);
135 
136  if (ev & ~Poller::PollFlag::output)
137  {
138  cout << "sock events: " << str_polls(ev) << endl;
139  s << str_polls(ev);
140  }
141 
142  if (ev & Poller::PollFlag::input) break;
143  }
144 
145  log << "calling accept" << endl;
146  InetAddr from;
147  auto conn = sock.accept(from);
148  log << "connect from " << from << endl;
149 
150  IoStream st(conn, conn);
151  st.exceptions(ios::failbit); // throw on eof or bad
152  try
153  {
154  set_all_polls(p, conn);
155  while (1)
156  {
157  s << " *conn_read_poll:";
158  while (1)
159  {
160  p.wait();
161  if (p.event(quit))
162  {
163  log << "quit event" << endl;
164  s << " quit";
165  return s.str();
166  }
167 
168  int ev = p.event(conn);
169  if (ev & ~Poller::PollFlag::output)
170  {
171  cout << "conn events: " << str_polls(ev) << endl;
172  s << " *conn_ev:" << str_polls(ev);
173  }
174 
175  if (ev & Poller::PollFlag::read_hup)
176  {
177  log << "connection sock peer hung up" << endl;
178  s << " *conn_peer_hup";
179  return s.str();
180  }
181 
182  if (ev & Poller::PollFlag::input) break;
183  }
184 
185  log << "getline waiting" << endl;
186  string got;
187  if (!std::getline(st, got))
188  {
189  log << "getline returned failed" << endl;
190  s << " *getline_failed";
191  return s.str();
192  }
193  log << "got=" << got << endl;
194 
195  s << " *conn_write_poll:";
196  while (1)
197  {
198  p.wait();
199  if (p.event(quit))
200  {
201  log << "quit event" << endl;
202  s << " quit";
203  return s.str();
204  }
205 
206  int ev = p.event(conn);
207  if (ev & ~Poller::PollFlag::input)
208  {
209  cout << "conn events: " << str_polls(ev) << endl;
210  s << " *conn_ev:" << str_polls(ev);
211  }
212 
213  if (ev & Poller::PollFlag::read_hup)
214  {
215  log << "connection sock peer hung up" << endl;
216  s << " *conn_peer_hup";
217  return s.str();
218  }
219 
220  if (ev & Poller::PollFlag::output) break;
221  }
222 
223  st << got << endl;
224  if (got == "quit")
225  {
226  log << "getline stream got quit" << endl;
227  s << " *getline_quit";
228  return s.str();
229  }
230  }
231  log << "returned from stream loop" << endl;
232  s << " *stream_loop_returned";
233  }
234  catch (const std::exception& e)
235  {
236  log << "stream exception: " << e.what() << endl;
237 
238  if (st.eof())
239  {
240  log << "stream eof" << endl;
241  s << " *stream_eof";
242  }
243  if (st.bad())
244  {
245  log << "stream eof" << endl;
246  s << " *stream_bad";
247  }
248  if (st.send_fail().size())
249  {
250  log << "stream send failmsg=" << st.send_fail() << endl;
251  s << " *stream_send_fail=" << st.send_fail();
252  }
253  if (st.recv_fail().size())
254  {
255  log << "stream recv failmsg=" << st.recv_fail() << endl;
256  s << " *stream_recv_fail=" << st.recv_fail();
257  }
258  return s.str();
259  }
260  }
261  catch (const std::exception& e)
262  {
263  log << "pre-accept ex: " << e.what() << endl;
264  start.write(1);
265  return s.str()+" ex="+e.what();
266  }
267  start.write(1);
268  return s.str();
269  }
270 
271  void clientstart()
272  {
273  log << "wait for start" << endl;
274  startev.read();
275 
276  //this_thread::sleep_for(chrono::milliseconds(100));
277 
278  log << "connect to " << endl;
279  csock.connect(caddr);
280  }
281 };
282 
283 TEST_F(InetServerStreamTest, send_quit)
284 {
285  log.id("send quit and recv");
286 
287  auto fut = async(server, std::ref(ssock), std::ref(startev), std::ref(quitev));
288 
289  clientstart();
290 
291  IoStream st(csock, csock);
292 
293  st << "quit" << endl;
294  string got;
295  getline(st, got);
296  log << "got=" << got << endl;
297 
298  auto ret = fut.get();
299  log << "server response: " << ret << endl;
300 }
301 
302 TEST_F(InetServerStreamTest, reset)
303 {
304  log.id("send recv and reset");
305 
306  auto fut = async(server, std::ref(ssock), std::ref(startev), std::ref(quitev));
307 
308  clientstart();
309 
310  csock.reset();
311 
312  auto ret = fut.get();
313  log << "server response: " << ret << endl;
314 }
315 
316 TEST_F(InetServerStreamTest, send_and_reset_clientsock)
317 {
318  log.id("send recv and reset client socket");
319 
320  auto fut = async(server, std::ref(ssock), std::ref(startev), std::ref(quitev));
321 
322  clientstart();
323 
324  IoStream st(csock, csock);
325 
326  st << "don't quit" << endl;
327  string got;
328  getline(st, got);
329  log << "got=" << got << endl;
330 
331  csock.reset();
332 
333  auto ret = fut.get();
334  log << "server response: " << ret << endl;
335 }
336 
337 TEST_F(InetServerStreamTest, quit)
338 {
339  log.id("send recv and reset");
340 
341  auto fut = async(server, std::ref(ssock), std::ref(startev), std::ref(quitev));
342 
343  log << "wait for start" << endl;
344  startev.read();
345 
346  quitev.write(1);
347 
348  auto ret = fut.get();
349  log << "server response: " << ret << endl;
350 }
Ipv6 internet address.
Definition: inet.h:120
unsigned port() const
Get the port.
Definition: inet.cc:159
virtual std::string host() const
Get host.
Definition: inet.cc:188
Internet transmission control protocol (tcp) socket.
Definition: inet.h:251
virtual void reset()
Close the connection and reset the socket.
Definition: inet.cc:418
void bind(const SockaddrBase &)
Bind an address to the socket.
Definition: socket.cc:313
void reuse_addr(bool r=true)
Set address reusable.
Definition: socket.cc:162
void connect(SockaddrBase &)
Connect to a socket address.
Definition: socket.cc:397
Signaling kernel event counter.
Definition: event.h:79
uint64_t read()
Read from (decrement) the event counter.
Definition: event.cc:82
void write(uint64_t)
Write to (increment) the event counter.
Definition: event.cc:94
Input/output stream wrapper for reader/writer.
Definition: iostream.h:157
Thread-safe stream logger.
Definition: logger.h:86
void max_line(unsigned int)
Set the maximum line length (default is 256).
void id(const std::string &id="")
Set the id.
Definition: logger.cc:425
void add_cout()
Add std::cout console stream.
Definition: logger.cc:418
Poller which allows polling of generic file descriptors for various events.
Definition: poller.h:66
void set(int, int)
Add a file desriptor to poller.
Definition: poller.cc:54
int event(int)
Return flags which were polled for this file descriptor.
Definition: poller.cc:171
Signaling kernel event counter.
Internet tcp and udp networking.
Base input/output stream classes.
Thread safe logging.
Linux kernel i/o event notification (poller).