Skip to content

Commit 495425a

Browse files
committed
WiFiServer: Return and check error from begin().
1 parent 90e1715 commit 495425a

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

arduino/libraries/WiFi/src/WiFiServer.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,50 +26,48 @@
2626
#include "WiFiServer.h"
2727

2828
WiFiServer::WiFiServer() :
29-
WiFiServer(0)
30-
{
31-
}
32-
33-
WiFiServer::WiFiServer(uint16_t port) :
34-
_port(port),
29+
_port(0),
3530
_socket(-1)
3631
{
3732
for (int i = 0; i < CONFIG_LWIP_MAX_SOCKETS; i++) {
3833
_spawnedSockets[i] = -1;
3934
}
4035
}
4136

42-
void WiFiServer::begin()
37+
uint8_t WiFiServer::begin(uint16_t port)
4338
{
4439
_socket = lwip_socket(AF_INET, SOCK_STREAM, 0);
4540

4641
if (_socket < 0) {
47-
return;
42+
return 0;
4843
}
4944

5045
struct sockaddr_in addr;
5146
memset(&addr, 0x00, sizeof(addr));
5247

5348
addr.sin_family = AF_INET;
5449
addr.sin_addr.s_addr = (uint32_t)0;
55-
addr.sin_port = htons(_port);
50+
addr.sin_port = htons(port);
5651

5752
if (lwip_bind(_socket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
5853
lwip_close_r(_socket);
5954
_socket = -1;
60-
return;
55+
return 0;
6156
}
6257

6358
if (lwip_listen(_socket, 1) < 0) {
6459
lwip_close_r(_socket);
6560
_socket = -1;
66-
return;
61+
return 0;
6762
}
6863

6964
int nonBlocking = 1;
7065
lwip_ioctl_r(_socket, FIONBIO, &nonBlocking);
7166

72-
return;
67+
// Set port.
68+
_port = port;
69+
70+
return 1;
7371
}
7472

7573
WiFiClient WiFiServer::available(uint8_t* status)

arduino/libraries/WiFi/src/WiFiServer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ class WiFiClient;
3030
class WiFiServer /*: public Server*/ {
3131
public:
3232
WiFiServer();
33-
WiFiServer(uint16_t);
3433
WiFiClient available(uint8_t* status = NULL);
35-
void begin();
34+
uint8_t begin(uint16_t port);
3635
virtual size_t write(uint8_t);
3736
virtual size_t write(const uint8_t *buf, size_t size);
3837
uint8_t status();

main/CommandHandler.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,7 @@ int startServerTcp(const uint8_t command[], uint8_t response[])
400400
response[2] = 1; // number of parameters
401401
response[3] = 1; // parameter 1 length
402402

403-
if (type == 0x00) {
404-
tcpServers[socket] = WiFiServer(port);
405-
406-
tcpServers[socket].begin();
407-
403+
if (type == 0x00 && tcpServers[socket].begin(port)) {
408404
socketTypes[socket] = 0x00;
409405
response[4] = 1;
410406
} else if (type == 0x01 && udps[socket].begin(port)) {

0 commit comments

Comments
 (0)