Free Electron
MessageUDP.h
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 #ifndef __MESSAGEUDP_H__
6 #define __MESSAGEUDP_H__
7 
8 #include "osheader.h"
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 /**************************************************************************//**
16  @brief Message Sender/Receiver over UDP
17 
18  @ingroup message
19 *//***************************************************************************/
20 class MessageUDP : virtual public MessageI
21 {
22 public:
23  MessageUDP();
24  virtual ~MessageUDP();
25 
26  virtual bool recvFrom(Messagegram *) override;
27  virtual bool recvFrom(Messagegram *, long seconds, long uSeconds) override;
28  virtual bool recvLargeFrom(Messagegram *) override;
29  virtual void getRecvAddress(uint32_t &address) override;
30  virtual void convertAddressToStr(const uint32_t address,
31  char *addStr) override;
32  virtual bool replyLast(const Messagegram &) override;
33  virtual bool sendTo(const Messagegram &, const char *, uint16_t) override;
34  virtual bool sendLargeTo(const Messagegram &,
35  const char *, uint16_t) override;
36  virtual bool bind(const char *, uint16_t) override;
37  virtual bool bind(uint16_t) override;
38  virtual uint16_t bindRandom() override;
39  virtual bool postInit() override;
40  virtual bool setReceiveBufferSize(int bufferSize) override;
41  virtual bool setSendBufferSize(int bufferSize) override;
42  virtual bool setBroadcast(bool boardcast) override;
43  virtual bool setReusePort(bool reuse) override;
44  virtual bool setReuseAddress(bool reuse) override;
45  virtual void shutdown() override;
46 
47 private:
48  bool receiveFrom(char *mesg, uint16_t *bytesRecv,
49  uint16_t maxLen, sockaddr_in *recvFrom,
50  long seconds, long uSeconds, int flags = 0);
51  bool receiveFrom(char *mesg, uint16_t *bytesRecv, uint16_t maxLen,
52  sockaddr_in *recvFrom, int flags = 0);
53  bool receiveLargeFrom(char *mesg, uint16_t *bytesRecv, uint16_t maxLen,
54  sockaddr_in *recvFrom, int flags = 0);
55  bool sendTo(sockaddr_in &addr, const char *mesg, uint16_t mesgLen,
56  int flags = 0);
57  bool bindTo(const char *host, uint16_t port);
58  bool bindTo(uint16_t port);
59 
60 private:
61  class MessageAddress
62  {
63  public:
64  MessageAddress() : m_isValid(false) {}
65 
66  MessageAddress(const char *host, unsigned short port)
67  : m_host(host), m_port(port)
68  {
69  int addrLen = sizeof(m_addr);
70  memset(&m_addr, 0, addrLen);
71  m_addr.sin_family = AF_INET;
72  m_addr.sin_port = htons(m_port);
73  inet_pton(AF_INET, m_host.c_str(), &m_addr.sin_addr);
74  m_isValid = true;
75  }
76 
77  MessageAddress(struct sockaddr_in addr) : m_addr(addr)
78  {
79  char ip[256];
80  memset(ip, 0, 256);
81  inet_ntop(AF_INET, &m_addr.sin_addr, ip, 256);
82  m_port = ntohs(m_addr.sin_port);
83  m_host = ip;
84  m_isValid = true;
85  }
86 
87  MessageAddress &operator=(const MessageAddress &other)
88  {
89  m_host = other.m_host;
90  m_port = other.m_port;
91  m_addr = other.m_addr;
92  m_isValid = other.m_isValid;
93  return *this;
94  }
95 
96  const std::string& getHost() const { return m_host; }
97  unsigned short getPort() const { return m_port; }
98  struct sockaddr_in getNetAddress() const { return m_addr; }
99  bool isValid() const { return m_isValid; }
100 
101  private:
102  bool m_isValid;
103  std::string m_host;
104  unsigned short m_port;
105  struct sockaddr_in m_addr;
106  };
107 
108  SOCKET m_socket;
109  MessageAddress m_replyAddr;
110 };
111 
112 } // namespace ext
113 } // namespace fe
114 
115 #endif // __MESSAGEUDP_H__
Message Sender/Receiver.
Definition: MessageI.h:35
kernel
Definition: namespace.dox:3
Message Sender/Receiver over UDP.
Definition: MessageUDP.h:20