Skip to content

Commit 4b29d8a

Browse files
committed
1 parent 9175e73 commit 4b29d8a

File tree

5 files changed

+62
-29
lines changed

5 files changed

+62
-29
lines changed

README.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ Simple to use implementation of WebSockets for microcontrollers.
3939
- [Installation](#installation)
4040
- [Config.h](#configh)
4141
- [Physical connection](#physical-connection)
42-
- [Prerequisite](#prerequisite)
4342
- [Usage examples](#usage-examples)
4443
- [Server](#server)
4544
- [Verify clients](#verify-clients)
@@ -111,20 +110,6 @@ If you have a **WeMos D1** in the size of **Arduino Uno** simply attaching a shi
111110
| SCS | PIN 10 | PIN 53 |
112111
| SCLK | PIN 13 | PIN 52 |
113112
114-
## Prerequisite
115-
116-
You need to provide your own implementation of `fetchRemoteIp`:
117-
118-
```cpp
119-
// This is necessary because:
120-
// 1. Not every implementation of {Etherent/Wifi}Client implements remoteIP() function
121-
// 2. Arduino does not have <type_traits> which could allow use of SFINAE
122-
123-
IPAddress fetchRemoteIp(const EthernetClient &client) {
124-
return client.remoteIP();
125-
}
126-
```
127-
128113
## Usage examples
129114
130115
```cpp

src/Config.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
//#define _DUMP_FRAME_DATA
1919

2020
/** Maximum size of data buffer - frame payload (in bytes). */
21-
constexpr uint16_t kBufferMaxSize{512};
21+
constexpr uint16_t kBufferMaxSize{256};
2222
/** Maximum time to wait for endpoint response (in milliseconds). */
2323
constexpr uint16_t kTimeoutInterval{5000};

src/ExampleBoilerplate.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@ using NetClient = EthernetClient;
3535
using NetServer = EthernetServer;
3636
#endif
3737

38-
template <typename NetClient> IPAddress fetchRemoteIp(const NetClient &client) {
39-
#if (PLATFORM_ARCH == PLATFORM_ARCHITECTURE_ESP8266) && \
40-
(NETWORK_CONTROLLER == ETHERNET_CONTROLLER_W5X00)
41-
// EthernetClient class in ESP8266 doesn't implement remoteIP()
42-
return {};
43-
#else
44-
// EthernetClient class is not "const correct"
45-
return const_cast<NetClient &>(client).remoteIP();
46-
#endif
47-
}
48-
4938
inline void setupNetwork() {
5039
#if NETWORK_CONTROLLER == NETWORK_CONTROLLER_WIFI
5140
//_SERIAL.setDebugOutput(true);

src/TypeTraits.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
3+
#include "Platform.hpp"
4+
#if PLATFORM_ARCH != PLATFORM_ARCHITECTURE_AVR
5+
# include <type_traits>
6+
#else
7+
namespace std {
8+
9+
template <class _Ty, _Ty _Val> struct integral_constant {
10+
static constexpr _Ty value = _Val;
11+
12+
using value_type = _Ty;
13+
using type = integral_constant;
14+
15+
constexpr operator value_type() const noexcept { return value; }
16+
17+
constexpr value_type operator()() const noexcept { return value; }
18+
};
19+
20+
template <bool _Val> using bool_constant = integral_constant<bool, _Val>;
21+
22+
using true_type = bool_constant<true>;
23+
using false_type = bool_constant<false>;
24+
25+
template <class, class> inline constexpr bool is_same_v = false;
26+
template <class _Ty> inline constexpr bool is_same_v<_Ty, _Ty> = true;
27+
28+
template <class _Ty1, class _Ty2>
29+
struct is_same : bool_constant<is_same_v<_Ty1, _Ty2>> {};
30+
31+
} // namespace std
32+
#endif
33+
34+
#include <IPAddress.h>
35+
36+
namespace net {
37+
38+
template <typename T> struct has_remoteIP {
39+
template <typename U> static constexpr std::false_type test(...) {
40+
return {};
41+
}
42+
template <typename U>
43+
static constexpr auto test(U *u) ->
44+
typename std::is_same<IPAddress, decltype(u->remoteIP())>::type {
45+
return {};
46+
}
47+
48+
static constexpr bool value{test<T>(nullptr)};
49+
};
50+
51+
} // namespace net

src/WebSocket.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
/** @file */
44

5+
#include <Arduino.h>
56
#include "Config.hpp"
67
#include "Utility.hpp"
7-
#include <Arduino.h>
8-
#include <IPAddress.h>
8+
#include "TypeTraits.hpp"
99

1010
namespace net {
1111

@@ -292,6 +292,14 @@ void encodeSecKey(const char *key, char output[]);
292292
/** @param[out] output Array of 4 elements (without NULL). */
293293
void generateMask(char output[]);
294294

295+
template <typename NetClient>
296+
inline IPAddress fetchRemoteIp(const NetClient &client) {
297+
if constexpr (has_remoteIP<NetClient>::value)
298+
return client.remoteIP();
299+
else
300+
return {};
301+
}
302+
295303
} // namespace net
296304

297305
#include "WebSocket.inl"

0 commit comments

Comments
 (0)