Skip to content

Commit

Permalink
Change Of API
Browse files Browse the repository at this point in the history
  • Loading branch information
Me No Dev committed Jul 18, 2016
1 parent 26cc6d4 commit 4b654b8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/AsyncUDPClient/AsyncUDPClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ void loop()
{
delay(1000);
//Send broadcast on port 1234
udp.broadcast("Anyone here?", 1234);
udp.broadcastTo("Anyone here?", 1234);
}
39 changes: 22 additions & 17 deletions src/AsyncUDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ bool AsyncUDPPacket::isMulticast()

size_t AsyncUDPPacket::write(const uint8_t *data, size_t len)
{
return _udp->write(data, len, _remoteIp, _remotePort);
return _udp->writeTo(data, len, _remoteIp, _remotePort);
}

size_t AsyncUDPPacket::write(uint8_t data)
Expand All @@ -156,6 +156,11 @@ AsyncUDP::~AsyncUDP()
close();
}

AsyncUDP::operator bool()
{
return _connected;
}

bool AsyncUDP::connected()
{
return _connected;
Expand Down Expand Up @@ -282,7 +287,7 @@ void AsyncUDP::close()
}
}

size_t AsyncUDP::write(const uint8_t *data, size_t len, ip_addr_t *addr, uint16_t port)
size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, ip_addr_t *addr, uint16_t port)
{
if(!_pcb && !connect(addr, port)) {
return 0;
Expand Down Expand Up @@ -330,39 +335,39 @@ bool AsyncUDP::connect(const IPAddress addr, uint16_t port)
return connect(&daddr, port);
}

size_t AsyncUDP::write(const uint8_t *data, size_t len, const IPAddress addr, uint16_t port)
size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const IPAddress addr, uint16_t port)
{
ip_addr_t daddr;
daddr.addr = addr;
return write(data, len, &daddr, port);
return writeTo(data, len, &daddr, port);
}

size_t AsyncUDP::write(const uint8_t *data, size_t len)
{
return write(data, len, &(_pcb->remote_ip), _pcb->remote_port);
return writeTo(data, len, &(_pcb->remote_ip), _pcb->remote_port);
}

size_t AsyncUDP::write(uint8_t data)
{
return write(&data, 1);
}

size_t AsyncUDP::broadcast(uint8_t *data, size_t len, uint16_t port)
size_t AsyncUDP::broadcastTo(uint8_t *data, size_t len, uint16_t port)
{
ip_addr_t daddr;
daddr.addr = 0xFFFFFFFF;
return write(data, len, &daddr, port);
return writeTo(data, len, &daddr, port);
}

size_t AsyncUDP::broadcast(const char * data, uint16_t port)
size_t AsyncUDP::broadcastTo(const char * data, uint16_t port)
{
return broadcast((uint8_t *)data, strlen(data), port);
return broadcastTo((uint8_t *)data, strlen(data), port);
}

size_t AsyncUDP::broadcast(uint8_t *data, size_t len)
{
if(_pcb->local_port != 0) {
return broadcast(data, len, _pcb->local_port);
return broadcastTo(data, len, _pcb->local_port);
}
return 0;
}
Expand All @@ -373,36 +378,36 @@ size_t AsyncUDP::broadcast(const char * data)
}


size_t AsyncUDP::send(AsyncUDPMessage &message, ip_addr_t *addr, uint16_t port)
size_t AsyncUDP::sendTo(AsyncUDPMessage &message, ip_addr_t *addr, uint16_t port)
{
if(!message) {
return 0;
}
return write(message.data(), message.length(), addr, port);
return writeTo(message.data(), message.length(), addr, port);
}

size_t AsyncUDP::send(AsyncUDPMessage &message, const IPAddress addr, uint16_t port)
size_t AsyncUDP::sendTo(AsyncUDPMessage &message, const IPAddress addr, uint16_t port)
{
if(!message) {
return 0;
}
return write(message.data(), message.length(), addr, port);
return writeTo(message.data(), message.length(), addr, port);
}

size_t AsyncUDP::send(AsyncUDPMessage &message)
{
if(!message) {
return 0;
}
return write(message.data(), message.length(), &(_pcb->remote_ip), _pcb->remote_port);
return writeTo(message.data(), message.length(), &(_pcb->remote_ip), _pcb->remote_port);
}

size_t AsyncUDP::broadcast(AsyncUDPMessage &message, uint16_t port)
size_t AsyncUDP::broadcastTo(AsyncUDPMessage &message, uint16_t port)
{
if(!message) {
return 0;
}
return broadcast(message.data(), message.length(), port);
return broadcastTo(message.data(), message.length(), port);
}

size_t AsyncUDP::broadcast(AsyncUDPMessage &message)
Expand Down
19 changes: 8 additions & 11 deletions src/ESPAsyncUDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,25 @@ class AsyncUDP : public Print

void close();

size_t write(const uint8_t *data, size_t len, ip_addr_t *addr, uint16_t port);
size_t write(const uint8_t *data, size_t len, const IPAddress addr, uint16_t port);
size_t writeTo(const uint8_t *data, size_t len, ip_addr_t *addr, uint16_t port);
size_t writeTo(const uint8_t *data, size_t len, const IPAddress addr, uint16_t port);
size_t write(const uint8_t *data, size_t len);
size_t write(uint8_t data);

size_t broadcast(uint8_t *data, size_t len, uint16_t port);
size_t broadcast(const char * data, uint16_t port);
size_t broadcastTo(uint8_t *data, size_t len, uint16_t port);
size_t broadcastTo(const char * data, uint16_t port);
size_t broadcast(uint8_t *data, size_t len);
size_t broadcast(const char * data);

size_t send(AsyncUDPMessage &message, ip_addr_t *addr, uint16_t port);
size_t send(AsyncUDPMessage &message, const IPAddress addr, uint16_t port);
size_t sendTo(AsyncUDPMessage &message, ip_addr_t *addr, uint16_t port);
size_t sendTo(AsyncUDPMessage &message, const IPAddress addr, uint16_t port);
size_t send(AsyncUDPMessage &message);

size_t broadcast(AsyncUDPMessage &message, uint16_t port);
size_t broadcastTo(AsyncUDPMessage &message, uint16_t port);
size_t broadcast(AsyncUDPMessage &message);

bool connected();
operator bool()
{
return connected();
}
operator bool();
};

#endif

0 comments on commit 4b654b8

Please sign in to comment.