AWS SDK for C++
AWS SDK for C++
Loading...
Searching...
No Matches
SimpleUDP.h
Go to the documentation of this file.
1
6#pragma once
7
10#include <cstdint>
11struct sockaddr;
12
13namespace Aws
14{
15 namespace Net
16 {
17 // 8K is aligned with default monitoring packet size.
18 const static size_t UDP_BUFFER_SIZE = 8192;
23 {
24 public:
33 SimpleUDP(int addressFamily, size_t sendBufSize = UDP_BUFFER_SIZE, size_t receiveBufSize = UDP_BUFFER_SIZE, bool nonBlocking = true);
34
43 SimpleUDP(bool IPV4 = true, size_t sendBufSize = UDP_BUFFER_SIZE, size_t receiveBufSize = UDP_BUFFER_SIZE, bool nonBlocking = true);
44
55 SimpleUDP(const char* host, unsigned short port, size_t sendBufSize = UDP_BUFFER_SIZE, size_t receiveBufSize = UDP_BUFFER_SIZE, bool nonBlocking = true);
56
58
65 int Connect(const sockaddr* address, size_t addressLength);
66
73 int ConnectToHost(const char* hostIP, unsigned short port) const;
74
78 int ConnectToLocalHost(unsigned short port) const;
79
86 int Bind(const sockaddr* address, size_t addressLength) const;
87
91 int BindToLocalHost(unsigned short port) const;
92
99 int SendData(const uint8_t* data, size_t dataLen) const;
100
109 int SendDataTo(const sockaddr* address, size_t addressLength, const uint8_t* data, size_t dataLen) const;
110
120 int SendDataToLocalHost(const uint8_t* data, size_t dataLen, unsigned short port) const;
121
129 int ReceiveData(uint8_t* buffer, size_t bufferLen) const;
130
139 int ReceiveDataFrom(sockaddr* address, size_t* addressLength, uint8_t* buffer, size_t bufferLen) const;
140
144 inline int GetAddressFamily() const { return m_addressFamily; }
145
149 inline bool IsConnected() const { return m_connected; }
150
151 private:
152 void CreateSocket(int addressFamily, size_t sendBufSize, size_t receiveBufSize, bool nonBlocking);
153 int GetUnderlyingSocket() const { return m_socket; }
154 void SetUnderlyingSocket(int socket) { m_socket = socket; }
155 int m_addressFamily;
156 // if not connected, you can't perform SendData, if connected, SendDataTo will call SendData
157 mutable bool m_connected;
158 int m_socket;
159 unsigned short m_port;
160 Aws::String m_hostIP;
161 };
162 }
163}
#define AWS_CORE_API
Definition: Core_EXPORTS.h:26
char * buffer
Definition: cJSON.h:174
int ConnectToHost(const char *hostIP, unsigned short port) const
An easy way to connect to host.
int ReceiveData(uint8_t *buffer, size_t bufferLen) const
Receive data from unique address specified in ConnectWithServer call. this function is equivalent to ...
int ConnectToLocalHost(unsigned short port) const
An easy way to connect to 127.0.0.1 or ::1 based on m_addressFamily.
int SendDataTo(const sockaddr *address, size_t addressLength, const uint8_t *data, size_t dataLen) const
Send data to server.
bool IsConnected() const
Definition: SimpleUDP.h:149
SimpleUDP(int addressFamily, size_t sendBufSize=UDP_BUFFER_SIZE, size_t receiveBufSize=UDP_BUFFER_SIZE, bool nonBlocking=true)
Constructor of SimpleUDP.
SimpleUDP(bool IPV4=true, size_t sendBufSize=UDP_BUFFER_SIZE, size_t receiveBufSize=UDP_BUFFER_SIZE, bool nonBlocking=true)
An easy constructor of an IPV4 or IPV6 SimpleUDP.
int ReceiveDataFrom(sockaddr *address, size_t *addressLength, uint8_t *buffer, size_t bufferLen) const
Receive data from network.
int BindToLocalHost(unsigned short port) const
An easy way to bind to localhost.
SimpleUDP(const char *host, unsigned short port, size_t sendBufSize=UDP_BUFFER_SIZE, size_t receiveBufSize=UDP_BUFFER_SIZE, bool nonBlocking=true)
An easy constructor of SimpleUDP based on host and port.
int Connect(const sockaddr *address, size_t addressLength)
Connect underlying udp socket to server specified in address.
int SendDataToLocalHost(const uint8_t *data, size_t dataLen, unsigned short port) const
An easy way to send data to localhost, when the underlying udp is connected, call this function will ...
int Bind(const sockaddr *address, size_t addressLength) const
Bind underlying udp socket to an address.
int GetAddressFamily() const
Definition: SimpleUDP.h:144
int SendData(const uint8_t *data, size_t dataLen) const
Send data to server without specifying address, only usable if hostIP and port are available.
static const size_t UDP_BUFFER_SIZE
Definition: SimpleUDP.h:18
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition: AWSString.h:97