Skip to content

Commit 6df5aab

Browse files
committed
feat: support unsubscribe()
1 parent 0cf20fb commit 6df5aab

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

ArduinoOSC/ArduinoOSCCommon.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,45 @@ namespace osc {
6666
#endif
6767
}
6868

69+
bool unsubscribe(const uint16_t port, const String& addr) {
70+
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
71+
if (WiFi.getMode() != WIFI_OFF)
72+
return OscServerManager<S>::getInstance().unsubscribe(port, addr);
73+
else {
74+
LOG_ERROR(F("WiFi is not enabled. Unsubscribing OSC failed."));
75+
return false;
76+
}
77+
#else
78+
return OscServerManager<S>::getInstance().unsubscribe(port, addr);
79+
#endif
80+
}
81+
82+
bool unsubscribe(const uint16_t port) {
83+
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
84+
if (WiFi.getMode() != WIFI_OFF)
85+
return OscServerManager<S>::getInstance().unsubscribe(port);
86+
else {
87+
LOG_ERROR(F("WiFi is not enabled. Unsubscribing OSC failed."));
88+
return false;
89+
}
90+
#else
91+
return OscServerManager<S>::getInstance().unsubscribe(port);
92+
#endif
93+
}
94+
95+
bool unsubscribe() {
96+
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
97+
if (WiFi.getMode() != WIFI_OFF)
98+
return OscServerManager<S>::getInstance().unsubscribeAll();
99+
else {
100+
LOG_ERROR(F("WiFi is not enabled. Unsubscribing OSC failed."));
101+
return false;
102+
}
103+
#else
104+
return OscServerManager<S>::getInstance().unsubscribeAll();
105+
#endif
106+
}
107+
69108
void parse() {
70109
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
71110
if (this->isWiFiConnected() || this->isWiFiModeAP()) {

ArduinoOSC/OSCServer.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,23 @@ namespace osc {
204204
callbacks.insert({addr, ref});
205205
}
206206

207+
bool unsubscribe(const String& addr) {
208+
auto it = callbacks.find(addr);
209+
if (it != callbacks.end()) {
210+
callbacks.erase(it);
211+
return true;
212+
}
213+
return false;
214+
}
215+
216+
bool unsubscribeAll() {
217+
if (!callbacks.empty()) {
218+
callbacks.clear();
219+
return true;
220+
}
221+
return false;
222+
}
223+
207224
bool parse() {
208225
auto stream = UdpMapManager<S>::getInstance().getUdp(port);
209226
const size_t size = stream->parsePacket();
@@ -263,6 +280,32 @@ namespace osc {
263280
getServer(port).subscribe(addr, std::forward<Ts>(ts)...);
264281
}
265282

283+
bool unsubscribe(const uint16_t port, const String& addr) {
284+
auto it = server_map.find(port);
285+
if (it != server_map.end()) {
286+
return it->second->unsubscribe(addr);
287+
}
288+
return false;
289+
}
290+
291+
bool unsubscribe(const uint16_t port) {
292+
auto it = server_map.find(port);
293+
if (it != server_map.end()) {
294+
return it->second->unsubscribeAll();
295+
}
296+
return false;
297+
}
298+
299+
bool unsubscribeAll() {
300+
if (!server_map.empty()) {
301+
for (auto& m : server_map) {
302+
m.second->unsubscribeAll();
303+
}
304+
return true;
305+
}
306+
return false;
307+
}
308+
266309
void parse() {
267310
for (auto& m : server_map)
268311
m.second->parse();

0 commit comments

Comments
 (0)