Common
common packages for kyubic
 
Loading...
Searching...
No Matches
udp.hpp
Go to the documentation of this file.
1
10#ifndef _UDP_HPP
11#define _UDP_HPP
12
13#include <netinet/in.h>
14
15#include <cstdint>
16#include <string>
17#include <vector>
18
19namespace common
20{
21
27{
28public:
32 UdpSocket();
33
37 ~UdpSocket();
38
39 // --- Configuration ---
40
45 void setTimeout(int sec);
46
51 void setBroadcast(bool enable);
52
53 // --- Listener Methods ---
54
60 bool bind(int port);
61
67 std::vector<uint8_t> receive(size_t max_len = 1024);
68
69 // --- Sender Methods ---
70
78 bool setDestination(const std::string & host, int port);
79
85 ssize_t send(const std::vector<uint8_t> & data);
86
95 ssize_t sendTo(const std::vector<uint8_t> & data, const std::string & host, int port);
96
97private:
98 int sockfd_;
99 struct sockaddr_in remote_addr_;
100 bool is_connected_;
101};
102
103} // namespace common
104
105#endif // _UDP_SOCKET_HPP
UDP (Sender/Listener) Class.
Definition: udp.hpp:27
void setBroadcast(bool enable)
Enables or disables broadcast.
Definition: udp.cpp:53
std::vector< uint8_t > receive(size_t max_len=1024)
Receives data.
Definition: udp.cpp:74
ssize_t sendTo(const std::vector< uint8_t > &data, const std::string &host, int port)
Sends data to a specified destination (uses sendto). Can be used without calling setDestination().
Definition: udp.cpp:130
bool setDestination(const std::string &host, int port)
Sets a fixed destination address (uses connect). This enables the use of the send() method.
Definition: udp.cpp:100
ssize_t send(const std::vector< uint8_t > &data)
Sends data to the fixed destination.
Definition: udp.cpp:121
~UdpSocket()
Closes the socket.
Definition: udp.cpp:37
void setTimeout(int sec)
Sets the send/receive timeout.
Definition: udp.cpp:44
bool bind(int port)
Binds to a specified port and prepares for receiving data.
Definition: udp.cpp:59
UdpSocket()
Creates a socket and configures basic options.
Definition: udp.cpp:24
For common.
Definition: udp.hpp:20