Skip to content

Commit 3e87dcf

Browse files
arcaome-no-dev
authored andcommitted
beginPacket can be used without listening on socket (espressif#185)
Currently there is bug in WiFiUDP library when you want to use beginPacket(...) without listening on socket (without calling begin(...) first). You can't send any data because socket is not open and also tx_buffer is not allocated which cause crash while writing data to tx_buffer.
1 parent bfa979a commit 3e87dcf

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

libraries/WiFi/src/WiFiUdp.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,29 @@ int WiFiUDP::beginMulticastPacket(){
130130
int WiFiUDP::beginPacket(){
131131
if(!remote_port)
132132
return 0;
133+
134+
// allocate tx_buffer if is necessary
135+
if(!tx_buffer){
136+
tx_buffer = new char[1460];
137+
if(!tx_buffer){
138+
log_e("could not create tx buffer: %d", errno);
139+
return 0;
140+
}
141+
}
142+
133143
tx_buffer_len = 0;
144+
145+
// check whereas socket is already open
146+
if (udp_server != -1)
147+
return 1;
148+
149+
if ((udp_server=socket(AF_INET, SOCK_DGRAM, 0)) == -1){
150+
log_e("could not create socket: %d", errno);
151+
return 0;
152+
}
153+
154+
fcntl(udp_server, F_SETFL, O_NONBLOCK);
155+
134156
return 1;
135157
}
136158

0 commit comments

Comments
 (0)