Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 compatiblity: AsyncUDPPacket inherit from Stream #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/AsyncUDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ AsyncUDPPacket::AsyncUDPPacket(AsyncUDP *udp, ip_addr_t *localIp, uint16_t local
_remotePort = remotePort;
_data = data;
_len = len;
_index = 0;
}

AsyncUDPPacket::~AsyncUDPPacket()
Expand All @@ -95,6 +96,40 @@ size_t AsyncUDPPacket::length()
return _len;
}

int AsyncUDPPacket::available(){
return _len - _index;
}

int AsyncUDPPacket::read(uint8_t *data, size_t len){
size_t i;
size_t a = _len - _index;
if(len > a){
len = a;
}
for(i=0;i<len;i++){
data[i] = read();
}
return len;
}

int AsyncUDPPacket::read(){
if(_index < _len){
return _data[_index++];
}
return -1;
}

int AsyncUDPPacket::peek(){
if(_index < _len){
return _data[_index];
}
return -1;
}

void AsyncUDPPacket::flush(){
_index = _len;
}

IPAddress AsyncUDPPacket::localIP()
{
return IPAddress(_localIp->addr);
Expand Down
9 changes: 8 additions & 1 deletion src/ESPAsyncUDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AsyncUDPMessage : public Print
}
};

class AsyncUDPPacket : public Print
class AsyncUDPPacket : public Stream
{
protected:
AsyncUDP *_udp;
Expand All @@ -50,6 +50,7 @@ class AsyncUDPPacket : public Print
uint16_t _remotePort;
uint8_t *_data;
size_t _len;
size_t _index;
public:
AsyncUDPPacket(AsyncUDP *udp, ip_addr_t *localIp, uint16_t localPort, ip_addr_t *remoteIp, uint16_t remotePort, uint8_t *data, size_t len);
virtual ~AsyncUDPPacket();
Expand All @@ -66,6 +67,12 @@ class AsyncUDPPacket : public Print

size_t send(AsyncUDPMessage &message);

int available();
int read(uint8_t *data, size_t len);
int read();
int peek();
void flush();

size_t write(const uint8_t *data, size_t len);
size_t write(uint8_t data);
};
Expand Down