coral
A C++ library for distributed co-simulation
net.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_NET_HPP
11 #define CORAL_NET_HPP
12 
13 #ifdef _WIN32
14 # include <winsock2.h>
15 #else
16 # include <netinet/in.h>
17 #endif
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <string>
22 
23 #include <coral/config.h>
24 
25 
26 namespace coral
27 {
29 namespace net
30 {
31 
32 
34 class Endpoint
35 {
36 public:
38  Endpoint() CORAL_NOEXCEPT;
39 
41  explicit Endpoint(const std::string& url);
42 
44  Endpoint(const std::string& transport, const std::string& address);
45 
47  std::string Transport() const CORAL_NOEXCEPT;
48 
50  std::string Address() const CORAL_NOEXCEPT;
51 
53  std::string URL() const CORAL_NOEXCEPT;
54 
55 private:
56  std::string m_transport;
57  std::string m_address;
58 };
59 
60 
65 namespace ip
66 {
67 
80  class Address
81  {
82  public:
84  Address() CORAL_NOEXCEPT;
85 
94  /* implicit */ Address(const std::string& address);
95 
96  // C-style version of the above.
97  /* implicit */ Address(const char* address);
98 
100  /* implicit */ Address(in_addr address) CORAL_NOEXCEPT;
101 
103  bool IsAnyAddress() const CORAL_NOEXCEPT;
104 
106  bool IsName() const CORAL_NOEXCEPT;
107 
109  std::string ToString() const CORAL_NOEXCEPT;
110 
121  in_addr ToInAddr() const;
122 
123  private:
124  friend bool operator==(const Address&, const Address&);
125  std::string m_strAddr;
126  in_addr m_inAddr;
127  };
128 
135  bool operator==(const Address& a1, const Address& a2);
136 
143  inline bool operator!=(const Address& a1, const Address& a2)
144  {
145  return !operator==(a1, a2);
146  }
147 
148 
156  class Port
157  {
158  public:
160  /* implicit */ Port(std::uint16_t port = 0u) CORAL_NOEXCEPT;
161 
171  /* implicit */ Port(const std::string& port);
172 
173  // C-style version of the above.
174  /* implicit */ Port(const char* port);
175 
177  bool IsNumber() const CORAL_NOEXCEPT;
178 
180  bool IsAnyPort() const CORAL_NOEXCEPT;
181 
186  std::uint16_t ToNumber() const;
187 
189  std::string ToString() const CORAL_NOEXCEPT;
190 
195  std::uint16_t ToNetworkByteOrder() const;
196 
198  static Port FromNetworkByteOrder(std::uint16_t nPort) CORAL_NOEXCEPT;
199 
200  private:
201  std::int32_t m_port;
202  };
203 
204 
209  class Endpoint
210  {
211  public:
213  Endpoint() CORAL_NOEXCEPT;
214 
216  Endpoint(
217  const ip::Address& address,
218  const ip::Port& port)
219  CORAL_NOEXCEPT;
220 
230  explicit Endpoint(const std::string& specification);
231 
233  explicit Endpoint(const sockaddr_in& sin);
234 
241  explicit Endpoint(const sockaddr& sa);
242 
244  const ip::Address& Address() const CORAL_NOEXCEPT;
245 
247  void SetAddress(const ip::Address& value) CORAL_NOEXCEPT;
248 
250  const ip::Port& Port() const CORAL_NOEXCEPT;
251 
258  void SetPort_(const ip::Port& value) CORAL_NOEXCEPT;
259 
261  std::string ToString() const CORAL_NOEXCEPT;
262 
272  coral::net::Endpoint ToEndpoint(const std::string& transport) const;
273 
281  sockaddr_in ToSockaddrIn() const;
282 
283  private:
284  ip::Address m_address;
285  ip::Port m_port;
286  };
287 
288 } // namespace ip
289 
290 
293 {
294 public:
295  explicit SlaveLocator(
296  const Endpoint& controlEndpoint = Endpoint{},
297  const Endpoint& dataPubEndpoint = Endpoint{}) CORAL_NOEXCEPT;
298 
299  const Endpoint& ControlEndpoint() const CORAL_NOEXCEPT;
300  const Endpoint& DataPubEndpoint() const CORAL_NOEXCEPT;
301 
302 private:
303  Endpoint m_controlEndpoint;
304  Endpoint m_dataPubEndpoint;
305 };
306 
307 
308 }} // namespace
309 #endif // header guard
An object which represents an internet port number.
Definition: net.hpp:156
STL class.
An object which identifies an internet host or network interface as either an IPv4 address or a textu...
Definition: net.hpp:80
std::string URL() const CORAL_NOEXCEPT
Returns a URL on the form "transport://address".
bool operator!=(const Address &a1, const Address &a2)
Inequality operator for coral::net::ip::Address.
Definition: net.hpp:143
An object which identifies an endpoint for Internet communication as a combination of an address and ...
Definition: net.hpp:209
A protocol/transport independent endpoint address specification.
Definition: net.hpp:34
Definition: variable_io.hpp:28
std::string Address() const CORAL_NOEXCEPT
Returns the address.
Endpoint() CORAL_NOEXCEPT
Default constructor; leaves both transport and adress empty.
std::string Transport() const CORAL_NOEXCEPT
Returns the transport.
bool operator==(const Address &a1, const Address &a2)
Equality operator for coral::net::ip::Address.
Class which represents the network location(s) of a slave.
Definition: net.hpp:292