-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUdpSocketServer.cpp
More file actions
44 lines (33 loc) · 1020 Bytes
/
Copy pathUdpSocketServer.cpp
File metadata and controls
44 lines (33 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "UdpSocketServer.h"
static Thread thread(osPriorityNormal, 1024);
UdpSocketServer::UdpSocketServer(int port): SocketServer(port) {
memset(buffer, 0, (sizeof buffer / sizeof buffer[0]));
}
osStatus UdpSocketServer::run() {
osStatus result = thread.start(callback(this, &UdpSocketServer::loop));
return result;
}
void UdpSocketServer::loop() {
UDPSocket listener;
bool running = true;
listener.open(net);
nsapi_error_t error = listener.bind(_port);
if (error != NSAPI_ERROR_OK) {
NDBG("error %d \r\n", error);
running = false;
} else {
NDBG("Listening on port %d \r\n", _port);
}
while (running) {
nsapi_size_or_error_t size = listener.recv(buffer, sizeof(buffer));
if (size > 0) {
buffer[size] = '\0';
NDBG("size %d\r\n", size);
_mycb.call(size); // callback
}
if (size < 0) {
NDBG("error %d\r\n", size);
}
}
listener.close();
}