rohdeschwarz  0.2.0
TCPIP socket library for Rohde & Schwarz instrument control
socket.cpp
Go to the documentation of this file.
1 
7 // rohdeschwarz
10 using namespace rohdeschwarz::busses::socket;
11 
12 
13 // boost
15 
16 
17 // std lib
18 #include <sstream>
19 
20 
21 // types
22 // Note: do I need WS2tcpip.h on windows?
23 // See https://stackoverflow.com/a/4454036/1390788
24 using char_p = char*;
25 using const_char_p = const char*;
26 using socklen_t_p = socklen_t*;
27 
28 
29 Socket::Socket(const char* host, int port) :
30  _host(host),
31  _port(port),
32  _socket(_io_context),
33  _native_handle(_socket.native_handle())
34 {
35  open();
36 }
37 
38 
39 Socket::Socket(const std::string& host, int port) :
40  _host(host),
41  _port(port),
42  _socket(_io_context),
43  _native_handle(_socket.native_handle())
44 {
45  open();
46 }
47 
48 
50 {
51  close();
52 }
53 
54 
55 std::string Socket::host() const
56 {
57  return _host;
58 }
59 
60 
61 int Socket::port() const
62 {
63  return _port;
64 }
65 
66 
67 std::string Socket::endpoint() const
68 {
69  std::stringstream stream;
70  stream << host() << ":" << port();
71  return stream.str();
72 }
73 
74 
75 int Socket::timeout_ms() const
76 {
77  // use read timeout
78  int timeout_ms;
79  int bytes_read;
80  getsockopt(
81  _native_handle,
82  SOL_SOCKET, SO_RCVTIMEO,
84  socklen_t_p(&bytes_read)
85  );
86  return timeout_ms;
87 }
88 
89 
90 bool Socket::setTimeout(int timeout_ms)
91 {
92  // set read timeout
93  int result = setsockopt
94  (
95  _native_handle,
96  SOL_SOCKET, SO_RCVTIMEO,
98  sizeof(timeout_ms)
99  );
100  if (result != 0)
101  {
102  // error
103  return false;
104  }
105 
106  // set write timeout
107  result = setsockopt(
108  _native_handle,
109  SOL_SOCKET, SO_SNDTIMEO,
111  sizeof(timeout_ms)
112  );
113 
114  // success?
115  return result == 0;
116 }
117 
118 
119 bool Socket::readData(unsigned char* buffer, std::size_t bufferSize, std::size_t* readSize)
120 {
121  // read
122  boost::asio::mutable_buffer _buffer
123  = boost::asio::buffer(char_p(buffer), bufferSize);
124  std::size_t _readSize;
125  try
126  {
127  _readSize = boost::asio::read(_socket, _buffer);
128  }
129 
130  // error?
131  catch (const system_error& error)
132  {
133  return false;
134  }
135 
136  // return read size?
137  if (readSize != nullptr)
138  {
139  *readSize = _readSize;
140  }
141 
142  // success
143  return true;
144 }
145 
146 
147 bool Socket::writeData(const unsigned char* data, std::size_t dataSize, std::size_t* writeSize)
148 {
149  // write
150  boost::asio::const_buffer _buffer
151  = boost::asio::buffer(const_char_p(data), dataSize);
152  std::size_t _writeSize;
153  try
154  {
155  _writeSize = boost::asio::write(_socket, _buffer);
156  }
157 
158  // error?
159  catch (const system_error& error)
160  {
161  return false;
162  }
163 
164  // return write size?
165  if (writeSize != nullptr)
166  {
167  *writeSize = _writeSize;
168  }
169 
170  // success
171  return true;
172 }
173 
174 
175 bool Socket::isError() const
176 {
177  return _socket.is_open();
178 }
179 
180 
181 std::string Socket::statusMessage() const
182 {
183  return _socket.is_open()? "connection is open" : "warning: connection is closed";
184 }
185 
186 
187 bool Socket::open()
188 {
189  auto endpoints = resolve(_host, _port, _io_context);
190  boost::asio::connect(_socket, endpoints);
191  return _socket.is_open();
192 }
193 
194 
195 void Socket::close()
196 {
197  _socket.shutdown(shutdown_both);
198  _socket.close();
199 }
char * char_p
Definition: bus.cpp:21
rohdeschwarz::busses::socket helper function definitions
std::vector< unsigned char > * buffer()
Definition: bus.cpp:53
virtual bool isError() const
Checks socket state for error.
Definition: socket.cpp:175
virtual bool readData(unsigned char *buffer, std::size_t bufferSize, std::size_t *readSize=nullptr)
read data into buffer
Definition: socket.cpp:119
virtual int timeout_ms() const
Get timeout, in ms.
Definition: socket.cpp:75
int port() const
returns the port
Definition: socket.cpp:61
virtual std::string endpoint() const
Returns string '{host}:{port}'.
Definition: socket.cpp:67
Socket(const char *host, int port=5025)
Constructor.
Definition: socket.cpp:29
virtual std::string statusMessage() const
human-readable bus status message
Definition: socket.cpp:181
std::string host() const
returns the host or ip address
Definition: socket.cpp:55
virtual bool writeData(const unsigned char *data, std::size_t dataSize, std::size_t *writeSize=nullptr)
read data into buffer
Definition: socket.cpp:147
virtual bool setTimeout(int timeout_ms)
Set timeout, in ms.
Definition: socket.cpp:90
virtual ~Socket()
Destructor.
Definition: socket.cpp:49
boost::system::system_error system_error
System error exception.
Definition: socket.hpp:32
boost::asio::ip::basic_resolver< boost::asio::ip::tcp >::results_type resolve(const std::string &host, int port, boost::asio::io_context &io_context)
Resolves TCP IP endpoint(s) from host, port.
Definition: helpers.cpp:16
const char * const_char_p
Definition: socket.cpp:25
const auto shutdown_both
Definition: socket.cpp:14
char * char_p
Definition: socket.cpp:24
socklen_t * socklen_t_p
Definition: socket.cpp:26
rohdeschwarz::busses::socket::Socket class definition