arduino-audio-tools
Loading...
Searching...
No Matches
WiFiServerZephyr.h
Go to the documentation of this file.
1#pragma once
2
3#include <zephyr/net/socket.h>
4
5#include <cstring>
6
7namespace audio_tools {
8
18 public:
19 explicit WiFiServerZephyr(int port) : port(port), listen_sock(-1) {}
20
23 void begin() {
25
26 if (listen_sock < 0) {
27 return;
28 }
29
30 int opt = 1;
31
32 zsock_setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
33
35 std::memset(&addr, 0, sizeof(addr));
36
37 addr.sin_family = AF_INET;
38 addr.sin_port = htons(port);
39 addr.sin_addr.s_addr = INADDR_ANY;
40
41 zsock_bind(listen_sock, (sockaddr*)&addr, sizeof(addr));
42
43 zsock_listen(listen_sock, 5);
44 }
45
48 if (listen_sock < 0) {
49 return WiFiClient();
50 }
51
53 socklen_t len = sizeof(client_addr);
54
55 int s = zsock_accept(listen_sock, (sockaddr*)&client_addr, &len);
56
57 if (s < 0) {
58 return WiFiClient();
59 }
60
61 return WiFiClient(s);
62 }
63
65 WiFiClient accept() { return available(); }
66
68 void end() {
69 if (listen_sock >= 0) {
70 zsock_close(listen_sock);
71 listen_sock = -1;
72 }
73 }
74
76 int portNumber() const { return port; }
77
78 private:
79 int port;
80 int listen_sock;
81};
82
84using EthernetServer = WiFiServerZephyr; // EthernetServer API is identical to WiFiServer
85
86} // namespace audio_tools
#define htons(x)
Definition Net.h:15
Definition WiFiClientZephyr.h:44
WiFiServer (Zephyr zsock version) This class provides a TCP server interface using Zephyr's socket AP...
Definition WiFiServerZephyr.h:17
~WiFiServerZephyr()
Definition WiFiServerZephyr.h:21
WiFiClient accept()
Accept an incoming client connection (blocking)
Definition WiFiServerZephyr.h:65
WiFiClient available()
Check for and return an incoming client connection.
Definition WiFiServerZephyr.h:47
WiFiServerZephyr(int port)
Definition WiFiServerZephyr.h:19
void end()
Stop the server.
Definition WiFiServerZephyr.h:68
void begin()
Start the server.
Definition WiFiServerZephyr.h:23
int portNumber() const
Get the port number the server is listening on.
Definition WiFiServerZephyr.h:76
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
WiFiClientZephyr WiFiClient
Definition WiFiClientZephyr.h:311
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508