coral
A C++ library for distributed co-simulation
udp.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_NET_UDP_HPP
11 #define CORAL_NET_UDP_HPP
12 
13 #ifdef _WIN32
14 # include <winsock2.h>
15 #else
16 # include <netinet/in.h>
17 #endif
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 
23 #include "coral/config.h"
24 #include "coral/net.hpp"
25 
26 
27 namespace coral
28 {
29 namespace net
30 {
31 
33 namespace udp
34 {
35 
36 
39 {
40 public:
42 #ifdef _WIN32
43  typedef SOCKET NativeSocket;
44 #else
45  typedef int NativeSocket;
46 #endif
47 
49  enum Flags
50  {
57  };
58 
73  const ip::Address& networkInterface,
74  ip::Port port,
75  int flags = 0);
76 
78  ~BroadcastSocket() CORAL_NOEXCEPT;
79 
80  BroadcastSocket(const BroadcastSocket&) = delete;
81  BroadcastSocket& operator=(const BroadcastSocket&) = delete;
82 
84  BroadcastSocket(BroadcastSocket&&) CORAL_NOEXCEPT;
86  BroadcastSocket& operator=(BroadcastSocket&&) CORAL_NOEXCEPT;
87 
98  void Send(const char* buffer, std::size_t msgSize);
99 
118  std::size_t Receive(
119  char* buffer,
120  std::size_t bufferSize,
121  ip::Address* sender);
122 
124  NativeSocket NativeHandle() const CORAL_NOEXCEPT;
125 
126 private:
127  class Private;
128  std::unique_ptr<Private> m_private;
129 };
130 
131 
132 }}} // namespace
133 #endif // header guard
An object which represents an internet port number.
Definition: net.hpp:156
BroadcastSocket(const ip::Address &networkInterface, ip::Port port, int flags=0)
Constructor.
std::size_t Receive(char *buffer, std::size_t bufferSize, ip::Address *sender)
Receives a message.
Main module header for coral::net.
Flags
Flags that control the operation of this class.
Definition: udp.hpp:49
STL namespace.
void Send(const char *buffer, std::size_t msgSize)
Broadcasts a message.
An object which identifies an internet host or network interface as either an IPv4 address or a textu...
Definition: net.hpp:80
~BroadcastSocket() CORAL_NOEXCEPT
Destructor.
int NativeSocket
The native socket handle type (SOCKET on Windows, int on *NIX).
Definition: udp.hpp:45
Definition: variable_io.hpp:28
NativeSocket NativeHandle() const CORAL_NOEXCEPT
The native socket handle.
Only send, don&#39;t receive (i.e., don&#39;t bind the socket).
Definition: udp.hpp:56
A class for sending and receiving UDP broadcast messages.
Definition: udp.hpp:38