MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
socket2stream.cc File Reference
#include "mbconfig.h"
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <sys/un.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <iostream>
#include <sstream>
#include <vector>
#include "sock.h"
#include "s2s.h"
Include dependency graph for socket2stream.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 58 of file socket2stream.cc.

References s2s_t::buf, s2s_t::dbuf, done, s2s_t::is_blocking(), s2s_t::nChannels, s2s_t::parse(), s2s_t::prepare(), s2s_t::recv(), s2s_t::shutdown(), and s2s_t::sock.

59 {
60  s2s_t s2s;
61 
62  try {
63  s2s.parse(argc, argv);
64  s2s.prepare();
65 
66  } catch (...) {
67  s2s.shutdown();
68  exit(EXIT_FAILURE);
69  }
70 
71  if (s2s.nChannels == 0) {
72  s2s.nChannels = 1;
73  }
74 
75  s2s.dbuf.resize(s2s.nChannels);
76  const char *sep = " ";
77  while (true) {
78  int len = s2s.recv(0);
79 
80  switch (len) {
81  case -1: {
82  int save_errno = errno;
83 
84  if (save_errno == EAGAIN && !s2s.is_blocking()) {
85  continue;
86  }
87 
88  const char *err_msg = strerror(save_errno);
89  silent_cerr("recv(" << s2s.sock << ",\"" << s2s.buf << "\") "
90  "failed (" << save_errno << ": " << err_msg << ")"
91  << std::endl);
92 
93  // fallthru
94  }
95 
96  case 0:
97  goto done;
98 
99  default:
100  break;
101  }
102 
103  for (int i = 0; i < s2s.nChannels - 1; i++) {
104  std::cout << s2s.dbuf[i] << sep;
105  }
106  std::cout << s2s.dbuf[s2s.nChannels - 1] << std::endl;
107  }
108 
109 done:;
110  s2s.shutdown();
111  exit(EXIT_SUCCESS);
112 }
std::string buf
Definition: s2s.h:68
void prepare(void)
ssize_t recv(int flags)
int nChannels
Definition: s2s.h:56
bool is_blocking(void) const
std::vector< double > dbuf
Definition: s2s.h:69
int sock
Definition: s2s.h:66
static unsigned done
Definition: gust.cc:209
Definition: s2s.h:54
void parse(int argc, char *argv[])
void shutdown(void)

Here is the call graph for this function: