Skip to content

Commit 5384647

Browse files
committed
ControlPoint connection corrections
1 parent 3072c0a commit 5384647

File tree

7 files changed

+79
-23
lines changed

7 files changed

+79
-23
lines changed

src/dlna/clients/DLNAControlPoint.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ class DLNAControlPoint : public IControlPoint {
201201
}
202202

203203
// instantiate subscription manager
204-
subscription_mgr.setup(http, udp, local_url, getDevice());
204+
if (local_url && p_http_server) {
205+
subscription_mgr.setup(http, udp, local_url, getDevice());
206+
}
205207

206208
// If we exited early because a device was found, deactivate the MSearch
207209
// schedule so it will stop repeating. The scheduler will clean up
@@ -656,8 +658,9 @@ class DLNAControlPoint : public IControlPoint {
656658

657659
/// checks if the usn contains the search target
658660
bool matches(const char* usn) {
661+
if (search_target == nullptr || *search_target == '\0') return true;
659662
if (StrView(search_target).equals("ssdp:all")) return true;
660-
return StrView(usn).contains(search_target);
663+
return StrView(usn).contains(search_target) || StrView(search_target).contains(usn);
661664
}
662665

663666
/// processes a bye-bye message
@@ -792,7 +795,9 @@ class DLNAControlPoint : public IControlPoint {
792795
(void)ref;
793796
return createSoapXML(action, out);
794797
};
795-
p_http->stop();
798+
if (!p_http->isKeepAlive()) {
799+
p_http->stop();
800+
}
796801
p_http->request().put("SOAPACTION", soapAction);
797802
int rc = p_http->post(post_url, xmlLen, printXML, "text/xml");
798803

src/dlna/clients/SubscriptionMgrControlPoint.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ class SubscriptionMgrControlPoint {
373373
for (auto& service : device.getServices()) {
374374
if (!subscribeToService(service)) {
375375
DlnaLogger.log(DlnaLogLevel::Error, "Subscription to service %s failed",
376-
service.service_id);
376+
service.service_id.c_str());
377377
ok = false;
378378
}
379379
}
@@ -397,7 +397,7 @@ class SubscriptionMgrControlPoint {
397397
if (!unsubscribeFromService(service)) {
398398
DlnaLogger.log(DlnaLogLevel::Warning,
399399
"Unsubscribe from service %s failed",
400-
service.service_id);
400+
service.service_id.c_str());
401401
ok = false;
402402
}
403403
}
@@ -422,7 +422,8 @@ class SubscriptionMgrControlPoint {
422422
if (StrView(service.event_sub_url).isEmpty()) {
423423
return false;
424424
}
425-
Url url(service.event_sub_url);
425+
426+
Url url(getURLString(service).c_str());
426427

427428
// If already subscribed and not expired, nothing to do
428429
if (service.subscription_state == SubscriptionState::Subscribed &&
@@ -463,7 +464,7 @@ class SubscriptionMgrControlPoint {
463464
// failed to subscribe
464465
DlnaLogger.log(DlnaLogLevel::Error,
465466
"Failed to subscribe to service %s, rc=%d",
466-
service.service_id, rc);
467+
service.service_id.c_str(), rc);
467468
return false;
468469
}
469470
return true;
@@ -489,7 +490,7 @@ class SubscriptionMgrControlPoint {
489490
return true;
490491
}
491492

492-
Url url(service.event_sub_url);
493+
Url url(getURLString(service).c_str());
493494
int rc = p_http->unsubscribe(url, service.subscription_id.c_str());
494495
if (rc == 200) {
495496
DlnaLogger.log(DlnaLogLevel::Info, "Unsubscribe %s -> rc=%d", url.url(),
@@ -503,7 +504,7 @@ class SubscriptionMgrControlPoint {
503504
} else {
504505
DlnaLogger.log(DlnaLogLevel::Error,
505506
"Failed to unsubscribe from service %s, rc=%d",
506-
service.service_id, rc);
507+
service.service_id.c_str(), rc);
507508
return false;
508509
}
509510
return true;
@@ -572,6 +573,20 @@ class SubscriptionMgrControlPoint {
572573
// reply OK to the NOTIFY
573574
client.replyOK();
574575
}
576+
577+
Str getURLString(DLNAServiceInfo& service) {
578+
Str url_str{100};
579+
if (service.event_sub_url.startsWith("/")) {
580+
// relative URL: need to build full URL using device base URL
581+
url_str = p_device->getBaseURL();
582+
url_str += service.event_sub_url.c_str();
583+
url_str.replace("//", "/");
584+
585+
} else {
586+
url_str = service.event_sub_url.c_str();
587+
}
588+
return url_str;
589+
}
575590
};
576591

577592
} // namespace tiny_dlna

src/dlna_config.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
#define DLNA_RUN_SUBSCRIPTIONS_EVERY_MS 300
1616
#endif
1717

18+
/// Define the default http request timeout
19+
#ifndef DLNA_HTTP_READ_TIMEOUT_MS
20+
#define DLNA_HTTP_READ_TIMEOUT_MS 100
21+
#endif
22+
1823
/// Define the default http request timeout
1924
#ifndef DLNA_HTTP_REQUEST_TIMEOUT_MS
20-
#define DLNA_HTTP_REQUEST_TIMEOUT_MS 60
25+
#define DLNA_HTTP_REQUEST_TIMEOUT_MS 6000
2126
#endif
2227

2328
/// Define XML parse buffer size

src/http/Server/HttpClientHandler.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,9 @@ class HttpClientHandler : public IClientHandler {
178178
}
179179

180180
void endClient() override {
181-
if (p_client) {
182-
p_client->flush();
183-
p_client->stop();
184-
}
181+
if (!p_client) return;
182+
p_client->flush();
183+
if (!p_client->isKeepAlive()) p_client->stop();
185184
}
186185

187186
void crlf() override {

src/http/Server/HttpRequest.h

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "HttpHeader.h"
88
#include "IHttpRequest.h"
99
#include "basic/StrPrint.h"
10+
#ifdef ESP32
11+
#include <WiFi.h>
12+
#include <lwip/sockets.h>
13+
#endif
1014

1115
namespace tiny_dlna {
1216

@@ -71,6 +75,7 @@ class HttpRequest : public IHttpRequest {
7175
DlnaLogger.log(DlnaLogLevel::Info, "HttpRequest::stop");
7276
if (client_ptr != nullptr) {
7377
client_ptr->stop();
78+
// delay(300);
7479
}
7580
}
7681

@@ -183,6 +188,8 @@ class HttpRequest : public IHttpRequest {
183188
/// Sets the timeout.
184189
void setTimeout(int ms) override { client_ptr->setTimeout(ms); }
185190

191+
bool isKeepAlive() { return StrView(connection).equals(CON_KEEP_ALIVE); }
192+
186193
protected:
187194
ClientType default_client;
188195
Client* client_ptr = nullptr;
@@ -201,12 +208,38 @@ class HttpRequest : public IHttpRequest {
201208

202209
/// opens a connection to the indicated host
203210
virtual int connect(const char* ip, uint16_t port) {
204-
DlnaLogger.log(DlnaLogLevel::Info, "HttpRequest::connect %s", ip);
205-
int rc = this->client_ptr->connect(ip, port);
211+
this->client_ptr->setTimeout(DLNA_HTTP_REQUEST_TIMEOUT_MS);
212+
if (!isKeepAlive() && this->client_ptr->connected()) {
213+
stop();
214+
}
215+
#ifdef ESP32
216+
// clear input buffer
217+
static_cast<ClientType*>(client_ptr)->clear();
218+
// static_cast<ClientType*>(client_ptr)
219+
// ->setConnectionTimeout(DLNA_HTTP_REQUEST_TIMEOUT_MS);
220+
static_cast<ClientType*>(client_ptr)->setNoDelay(true);
221+
int enable = 1;
222+
static_cast<ClientType*>(client_ptr)
223+
->setSocketOption(SOL_SOCKET, SO_REUSEADDR, &enable,
224+
sizeof(enable)); // Address reuse
225+
#endif
226+
DlnaLogger.log(DlnaLogLevel::Info, "HttpRequest::connect %s:%d", ip, port);
206227
uint64_t end = millis() + client_ptr->getTimeout();
207-
DlnaLogger.log(DlnaLogLevel::Info, "Connected: %s (rc=%d) with timeout %ld",
208-
connected() ? "true" : "false", rc,
209-
client_ptr->getTimeout());
228+
bool rc = false;
229+
for (int j=0;j < 3; j++) {
230+
rc = this->client_ptr->connect(ip, port);
231+
if (rc) break;
232+
delay(200);
233+
}
234+
if (!connected()) {
235+
DlnaLogger.log(
236+
DlnaLogLevel::Error, "Connected: %s (rc=%d) with timeout %ld",
237+
connected() ? "true" : "false", rc, client_ptr->getTimeout());
238+
} else {
239+
DlnaLogger.log(
240+
DlnaLogLevel::Debug, "Connected: %s (rc=%d) with timeout %ld",
241+
connected() ? "true" : "false", rc, client_ptr->getTimeout());
242+
}
210243
return rc;
211244
}
212245

@@ -281,9 +314,6 @@ class HttpRequest : public IHttpRequest {
281314
DlnaLogger.log(DlnaLogLevel::Info, "%s %s", methods[method], url.url());
282315

283316
if (!connected()) {
284-
DlnaLogger.log(DlnaLogLevel::Info,
285-
"HttpRequest::connecting to host %s port %d", url.host(),
286-
url.port());
287317
connect(url.host(), url.port());
288318
}
289319

src/http/Server/HttpServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class HttpServer : public IHttpServer {
242242
ClientType client = server_ptr->accept();
243243
if (client.connected()) {
244244
DlnaLogger.log(DlnaLogLevel::Info, "copy: accepted new client");
245-
client.setTimeout(DLNA_HTTP_REQUEST_TIMEOUT_MS);
245+
client.setTimeout(DLNA_HTTP_READ_TIMEOUT_MS);
246246
#ifdef ESP32
247247
client.setNoDelay(true); // disables Nagle
248248
#endif

src/http/Server/IHttpRequest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class IHttpRequest {
3232
virtual bool connected() = 0;
3333
/// Get number of bytes available to read
3434
virtual int available() = 0;
35+
/// Do not close the connection after request
36+
virtual bool isKeepAlive() = 0;
3537
/// Stop the connection
3638
virtual void stop() = 0;
3739
/// Send POST request with string data

0 commit comments

Comments
 (0)