Max OS 0.3
Loading...
Searching...
No Matches
udp.h
Go to the documentation of this file.
1
9#ifndef MAXOS_NET_UDP_H
10#define MAXOS_NET_UDP_H
11
12
13#include <cstdint>
14#include <common/eventHandler.h>
15#include <net/ipv4.h>
17
18
19namespace MaxOS::net {
20
28 typedef struct PACKED UDPHeader {
29
30 uint16_t source_port;
32 uint16_t length;
33 uint16_t checksum;
34
35 } udp_header_t;
36
41 enum class UDPEvents {
42 DATA_RECEIVED,
43 };
44
45 //Predefine
46 class UDPSocket;
47
48 class UserDatagramProtocolHandler;
49
50 typedef uint16_t UserDatagramProtocolPort;
51
56 class UDPDataReceivedEvent : public common::Event<UDPEvents> {
57 public:
59 uint8_t* data;
60 uint16_t size;
61
62 UDPDataReceivedEvent(UDPSocket* socket, uint8_t* data, uint16_t size);
64 };
65
70 class UDPPayloadHandler : public common::EventHandler<UDPEvents> {
71 public:
74
76
77 virtual void handle_user_datagram_protocol_message(UDPSocket* socket, uint8_t* data, uint16_t size);
78
79 };
80
85 class UDPSocket : public common::EventManager<UDPEvents> {
86 friend class UserDatagramProtocolHandler;
87
88 protected:
89 bool listening;
90
91 uint16_t local_port = 0;
92 uint16_t remote_port = 0;
93
94 uint32_t local_ip = 0;
95 uint32_t remote_ip = 0;
96
98
99 public:
100 UDPSocket();
101 ~UDPSocket();
102
103 virtual void handle_user_datagram_protocol_payload(uint8_t* data, uint16_t size);
104 virtual void send(uint8_t* data, uint16_t size);
105 virtual void disconnect();
106
107 };
108
114 protected:
118
119 public:
122
123 bool handle_internet_protocol_payload(net::InternetProtocolAddress source_ip, net::InternetProtocolAddress destination_ip, uint8_t* payload_data, uint32_t size) override;
124
125 UDPSocket* connect(uint32_t ip, uint16_t port);
126 static UDPSocket* connect(const string& address);
127
128 UDPSocket* listen(uint16_t port);
129
130 void disconnect(UDPSocket* socket);
131 void send(UDPSocket* socket, const uint8_t* data, uint16_t size);
132
133 static void bind(UDPSocket* socket, UDPPayloadHandler* udp_payload_handler);
134 };
135
136}
137
138
139#endif //MAXOS_NET_UDP_H
Used to handle an event.
Manages the m_handlers for a type of event, raises events and calls the m_handlers.
Used to store information about an event, has a type and a return value.
A stream that strings can be written to.
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Handles the payload of a specific IP protocol.
Definition ipv4.h:66
InternetProtocolHandler * internet_protocol_handler
The Internet protocol handler this payload handler is connected to.
Definition ipv4.h:70
Handles IPv4 packets over Ethernet frames.
Definition ipv4.h:85
Event fired when data is received on a UDP socket.
Definition udp.h:56
UDPSocket * socket
The socket that received the data.
Definition udp.h:58
uint16_t size
The size of the data received.
Definition udp.h:60
uint8_t * data
The data received.
Definition udp.h:59
Handles the payload of a UDP packet.
Definition udp.h:70
common::Event< UDPEvents > * on_event(common::Event< UDPEvents > *event) override
Event handler for UDP payload events.
Definition udp.cpp:39
virtual void handle_user_datagram_protocol_message(UDPSocket *socket, uint8_t *data, uint16_t size)
Handle the recivement of a UDP message.
Definition udp.cpp:29
A UDP socket.
Definition udp.h:85
uint32_t remote_ip
The IP of the remote device.
Definition udp.h:95
UDPSocket()
Socket
Definition udp.cpp:57
uint16_t remote_port
The port on the remote device.
Definition udp.h:92
virtual void disconnect()
disconnect the UDP socket
Definition udp.cpp:96
uint16_t local_port
The port on this device.
Definition udp.h:91
UserDatagramProtocolHandler * user_datagram_protocol_handler
The UDP handler this socket is connected to.
Definition udp.h:97
virtual void send(uint8_t *data, uint16_t size)
send data through the UDP socket
Definition udp.cpp:87
bool listening
Wether the port is waiting for incoming connections.
Definition udp.h:89
uint32_t local_ip
The IP of this device.
Definition udp.h:94
virtual void handle_user_datagram_protocol_payload(uint8_t *data, uint16_t size)
Handle the recivement of a UDP payload.
Definition udp.cpp:72
Handles the UDP protocol.
Definition udp.h:113
static UserDatagramProtocolPort free_ports
The next free port number.
Definition udp.h:116
void disconnect(UDPSocket *socket)
Disconnects the socket from the remote IP and port.
Definition udp.cpp:248
UDPSocket * connect(uint32_t ip, uint16_t port)
Definition udp.cpp:181
common::Vector< UDPSocket * > sockets
The list of UDP sockets.
Definition udp.h:115
UDPSocket * listen(uint16_t port)
Listens for incoming packets on the port.
Definition udp.cpp:222
static void bind(UDPSocket *socket, UDPPayloadHandler *udp_payload_handler)
Binds a handler to the socket.
Definition udp.cpp:308
bool handle_internet_protocol_payload(net::InternetProtocolAddress source_ip, net::InternetProtocolAddress destination_ip, uint8_t *payload_data, uint32_t size) override
Handle the recivement of an UDP packet.
Definition udp.cpp:129
common::OutputStream * errorMessages
Where to write error messages.
Definition udp.h:117
void send(UDPSocket *socket, const uint8_t *data, uint16_t size)
Sends a packet to the remote IP and port.
Definition udp.cpp:269
Defines classes for handling events and event managers.
Defines classes and structures for handling Internet Protocol version 4 (IPv4) packets.
uint32_t InternetProtocolAddress
An IPv4 address.
Definition ipv4.h:18
Defines a MemoryManager class for handling memory allocation and deallocation.
The header of a UDP packet.
Definition udp.h:28
uint16_t length
The length of the UDP header and data.
Definition udp.h:32
uint16_t source_port
The port of the sender.
Definition udp.h:30
uint16_t destination_port
The port of the receiver.
Definition udp.h:31
uint16_t checksum
The checksum of the header and data.
Definition udp.h:33
uint16_t UserDatagramProtocolPort
UDP port.
Definition udp.h:50
UDPEvents
The events that can be fired by the UDP protocol.
Definition udp.h:41