From 88293a4284de16ed096df95851c0fd805b6d2087 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Mon, 22 May 2017 15:27:34 +0300 Subject: [PATCH] Implement WiFiClient.peek() Thanks @miomir1981 --- libraries/WiFi/src/WiFiClient.cpp | 14 ++++++++++++++ libraries/WiFi/src/WiFiClient.h | 5 +---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp index b1b6e72ce..d6f27354c 100644 --- a/libraries/WiFi/src/WiFiClient.cpp +++ b/libraries/WiFi/src/WiFiClient.cpp @@ -237,6 +237,20 @@ int WiFiClient::read(uint8_t *buf, size_t size) return res; } +int WiFiClient::peek() +{ + if(!available()) { + return -1; + } + uint8_t data = 0; + int res = recv(fd(), &data, 1, MSG_PEEK); + if(res < 0 && errno != EWOULDBLOCK) { + log_e("%d", errno); + stop(); + } + return data; +} + int WiFiClient::available() { if(!_connected) { diff --git a/libraries/WiFi/src/WiFiClient.h b/libraries/WiFi/src/WiFiClient.h index 5b10db7dc..5472c74dd 100644 --- a/libraries/WiFi/src/WiFiClient.h +++ b/libraries/WiFi/src/WiFiClient.h @@ -47,10 +47,7 @@ public: int available(); int read(); int read(uint8_t *buf, size_t size); - int peek() - { - return -1; - } + int peek(); void flush(); void stop(); uint8_t connected(); -- GitLab