beginPacket can be used without listening on socket (#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.
This commit is contained in:
Martin Sloup 2017-02-08 22:10:16 +01:00 committed by Me No Dev
parent bfa979a900
commit 3e87dcf748

View File

@ -130,7 +130,29 @@ int WiFiUDP::beginMulticastPacket(){
int WiFiUDP::beginPacket(){ int WiFiUDP::beginPacket(){
if(!remote_port) if(!remote_port)
return 0; return 0;
// allocate tx_buffer if is necessary
if(!tx_buffer){
tx_buffer = new char[1460];
if(!tx_buffer){
log_e("could not create tx buffer: %d", errno);
return 0;
}
}
tx_buffer_len = 0; tx_buffer_len = 0;
// check whereas socket is already open
if (udp_server != -1)
return 1;
if ((udp_server=socket(AF_INET, SOCK_DGRAM, 0)) == -1){
log_e("could not create socket: %d", errno);
return 0;
}
fcntl(udp_server, F_SETFL, O_NONBLOCK);
return 1; return 1;
} }