REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
native_ethernet_udp.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef _WIN64
4#include <winsock2.h>
5#pragma comment(lib, "ws2_32.lib")
6#else
7#include <netinet/in.h>
8#include <netinet/tcp.h>
9#include <sys/ioctl.h>
10#include <unistd.h>
11#endif
12
13#include <Arduino.h>
14#include <Udp.h>
15#include <IPAddress.h>
16#include <stdexcept>
17
18#include <net/ethernet_client_hal.h>
19
20namespace net {
21
22class EthernetServer;
23
24#ifdef _WIN32
25#include <winsock2.h>
26#include <ws2tcpip.h>
27#pragma comment(lib, "ws2_32.lib")
28typedef int socklen_t;
29#else
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <netdb.h>
34#include <unistd.h>
35#include <fcntl.h>
36#define SOCKET int
37#define INVALID_SOCKET -1
38#define SOCKET_ERROR -1
39#define closesocket close
40#endif
41
42class NativeEthernetUDP : public VirtualUDP {
43private:
44 SOCKET _socket;
45 struct sockaddr_in _localAddr;
46 struct sockaddr_in _remoteAddr;
47 uint16_t _localPort;
48 uint8_t* _sendBuffer;
49 size_t _sendBufferSize;
50 size_t _sendBufferPos;
51 uint8_t* _recvBuffer;
52 size_t _recvBufferSize;
53 size_t _recvBufferPos;
54 size_t _recvDataSize;
55 bool _hasData;
56
57 static bool _wsaInitialized;
58public:
60 virtual ~NativeEthernetUDP();
61
62 uint8_t begin(uint16_t) override; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
63 uint8_t beginMulticast(IPAddress, uint16_t) override;
64 void stop() override; // Finish with the UDP socket
65
66 // Sending UDP packets
67
68 // Start building up a packet to send to the remote host specific in ip and port
69 // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
70 int beginPacket(IPAddress ip, uint16_t port) override;
71 // Start building up a packet to send to the remote host specific in host and port
72 // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
73 int beginPacket(const char *host, uint16_t port) override;
74 // Finish off this packet and send it
75 // Returns 1 if the packet was sent successfully, 0 if there was an error
76 int endPacket() override;
77 // Write a single byte into the packet
78 size_t write(uint8_t) override;
79 // Write size bytes from buffer into the packet
80 size_t write(const uint8_t *buffer, size_t size) override;
81
82 // Start processing the next available incoming packet
83 // Returns the size of the packet in bytes, or 0 if no packets are available
84 int parsePacket() override;
85 // Number of bytes remaining in the current packet
86 int available() override;
87 // Read a single byte from the current packet
88 int read() override;
89 // Read up to len bytes from the current packet and place them into buffer
90 // Returns the number of bytes read, or 0 if none are available
91 int read(unsigned char* buffer, size_t len) override;
92 // Read up to len characters from the current packet and place them into buffer
93 // Returns the number of characters read, or 0 if none are available
94 int read(char* buffer, size_t len) override;
95 // Return the next byte from the current packet without moving on to the next byte
96 int peek() override;
97 void flush() override; // Finish reading the current packet
98
99 // Return the IP address of the host who sent the current incoming packet
100 IPAddress remoteIP() override;
101 // Return the port of the host who sent the current incoming packet
102 uint16_t remotePort() override;
103
105};
106
107} // namespace net
IPAddress remoteIP() override
size_t write(uint8_t) override
int beginPacket(IPAddress ip, uint16_t port) override
uint8_t begin(uint16_t) override
uint16_t remotePort() override
uint8_t beginMulticast(IPAddress, uint16_t) override
uint32_t uint32_t size
Definition flasher.cpp:63
#define SOCKET