diff --git a/include/opendht/dht_proxy_client.h b/include/opendht/dht_proxy_client.h index 54edb517..0d59c754 100644 --- a/include/opendht/dht_proxy_client.h +++ b/include/opendht/dht_proxy_client.h @@ -314,7 +314,7 @@ private: struct Listener; struct ProxySearch; - size_t listener_token_ {0}; + size_t listenerToken_ {0}; std::map searches_; mutable std::mutex searchLock_; diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h index f15e05bc..9371b0cc 100644 --- a/include/opendht/dht_proxy_server.h +++ b/include/opendht/dht_proxy_server.h @@ -239,7 +239,7 @@ private: struct PushListener; std::mutex lockPushListeners_; std::map pushListeners_; - unsigned tokenPushNotif_ {0}; + uint64_t tokenPushNotif_ {0}; void cancelPushListen(const std::string& pushToken, const InfoHash& key, unsigned token); #endif //OPENDHT_PUSH_NOTIFICATIONS diff --git a/include/opendht/value.h b/include/opendht/value.h index 056d5bf9..4a950cda 100644 --- a/include/opendht/value.h +++ b/include/opendht/value.h @@ -996,6 +996,10 @@ unpackVector(const std::vector>& vals) { return ret; } +#if OPENDHT_PROXY_SERVER || OPENDHT_PROXY_CLIENT +uint64_t unpackId(const Json::Value& json, const std::string& key); +#endif + } MSGPACK_ADD_ENUM(dht::Value::Field) diff --git a/src/dht_proxy_client.cpp b/src/dht_proxy_client.cpp index b2c689ec..0f436e32 100644 --- a/src/dht_proxy_client.cpp +++ b/src/dht_proxy_client.cpp @@ -639,7 +639,7 @@ DhtProxyClient::doListen(const InfoHash& key, ValueCallback cb, Value::Filter fi } DHT_LOG.d(key, "[search %s] sending %s", key.to_c_str(), deviceKey_.empty() ? "listen" : "subscribe"); - auto token = ++listener_token_; + auto token = ++listenerToken_; auto l = search->second.listeners.find(token); if (l == search->second.listeners.end()) { auto f = filter; @@ -719,8 +719,7 @@ DhtProxyClient::doListen(const InfoHash& key, ValueCallback cb, Value::Filter fi auto* char_data = reinterpret_cast(&body[0]); if (reader->parse(char_data, char_data + body.size(), &json, &err)) { - if (!json.isMember("token")) return; - *pushNotifToken = json["token"].asLargestUInt(); + *pushNotifToken = unpackId(json, "token"); } else { state->ok = false; } @@ -926,14 +925,15 @@ DhtProxyClient::pushNotificationReceived(const std::map lock(searchLock_); - auto token = std::stoul(notification.at("token")); + bool timeout = notification.find("timeout") == notification.cend(); + uint64_t token = std::stoull(notification.at("token")); for (auto& search: searches_) { for (auto& list : search.second.listeners) { auto& listener = list.second; if (*listener.pushNotifToken!= token) continue; DHT_LOG.d(search.first, "[search %s] handling push notification", search.first.to_c_str()); - if (notification.find("timeout") == notification.cend()) { + if (timeout) { // Wake up daemon and get values auto cb = listener.cb; auto filter = listener.filter; @@ -993,7 +993,7 @@ DhtProxyClient::resubscribe(const InfoHash& key, Listener& listener) auto reader = std::unique_ptr(rbuilder.newCharReader()); if (reader->parse(char_data, char_data + body.size(), &json, &err)) { if (!json.isMember("token")) return; - *pushNotifToken = json["token"].asLargestUInt(); + *pushNotifToken = unpackId(json, "token"); } } catch (std::runtime_error&) { // NOTE: Http::close() can occurs here. Ignore this. diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp index bd3a877e..9bf1a9db 100644 --- a/src/dht_proxy_server.cpp +++ b/src/dht_proxy_server.cpp @@ -341,12 +341,12 @@ DhtProxyServer::subscribe(const std::shared_ptr& session) } auto pushToken = root["key"].asString(); if (pushToken.empty()) return; - auto tokenFromReq = root.isMember("token") ? root["token"].asLargestUInt() : 0; + auto tokenFromReq = unpackId(root, "token"); auto platform = root["platform"].asString(); auto isAndroid = platform == "android"; auto clientId = root.isMember("client_id") ? root["client_id"].asString() : std::string(); - auto token = 0; + uint64_t token = 0; { std::lock_guard lock(lockListener_); // Check if listener is already present and refresh timeout if launched @@ -376,7 +376,7 @@ DhtProxyServer::subscribe(const std::shared_ptr& session) // Build message content. Json::Value json; json["to"] = clientId; - json["token"] = token; + json["token"] = std::to_string(token); sendPushNotification(pushToken, json, isAndroid); return true; } @@ -389,7 +389,7 @@ DhtProxyServer::subscribe(const std::shared_ptr& session) Json::Value json; json["timeout"] = infoHash.toString(); json["to"] = clientId; - json["token"] = token; + json["token"] = std::to_string(token); sendPushNotification(pushToken, json, isAndroid); } ); @@ -431,7 +431,7 @@ DhtProxyServer::unsubscribe(const std::shared_ptr& session) } auto pushToken = root["key"].asString(); if (pushToken.empty()) return; - auto token = std::stoull(root["token"].asString()); + auto token = unpackId(root, "token"); if (token == 0) return; cancelPushListen(pushToken, infoHash, token); diff --git a/src/value.cpp b/src/value.cpp index a6f66c5d..aea02953 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -175,45 +175,37 @@ Value::msgpack_unpack_body(const msgpack::object& o) #if OPENDHT_PROXY_SERVER || OPENDHT_PROXY_CLIENT Value::Value(Json::Value& json) { - try { - if (json.isMember("id")) { - if (json["id"].isString()) { - id = Value::Id(std::stoull(json["id"].asString())); - } else { - id = Value::Id(json["id"].asLargestUInt()); - } - } - } catch (...) { } - if (json.isMember("cypher")) { - auto cypherStr = json["cypher"].asString(); - cypherStr = base64_decode(cypherStr); - cypher = std::vector(cypherStr.begin(), cypherStr.end()); - } - if (json.isMember("sig")) { - auto sigStr = json["sig"].asString(); - sigStr = base64_decode(sigStr); - signature = std::vector(sigStr.begin(), sigStr.end()); - } - if (json.isMember("seq")) - seq = json["seq"].asInt(); - if (json.isMember("owner")) { - auto ownerStr = json["owner"].asString(); - auto ownerBlob = std::vector(ownerStr.begin(), ownerStr.end()); - owner = std::make_shared(ownerBlob); - } - if (json.isMember("to")) { - auto toStr = json["to"].asString(); - recipient = InfoHash(toStr); - } - if (json.isMember("type")) - type = json["type"].asInt(); - if (json.isMember("data")){ - auto dataStr = json["data"].asString(); - dataStr = base64_decode(dataStr); - data = std::vector(dataStr.begin(), dataStr.end()); - } - if (json.isMember("utype")) - user_type = json["utype"].asString(); + id = Value::Id(unpackId(json, "id")); + if (json.isMember("cypher")) { + auto cypherStr = json["cypher"].asString(); + cypherStr = base64_decode(cypherStr); + cypher = std::vector(cypherStr.begin(), cypherStr.end()); + } + if (json.isMember("sig")) { + auto sigStr = json["sig"].asString(); + sigStr = base64_decode(sigStr); + signature = std::vector(sigStr.begin(), sigStr.end()); + } + if (json.isMember("seq")) + seq = json["seq"].asInt(); + if (json.isMember("owner")) { + auto ownerStr = json["owner"].asString(); + auto ownerBlob = std::vector(ownerStr.begin(), ownerStr.end()); + owner = std::make_shared(ownerBlob); + } + if (json.isMember("to")) { + auto toStr = json["to"].asString(); + recipient = InfoHash(toStr); + } + if (json.isMember("type")) + type = json["type"].asInt(); + if (json.isMember("data")){ + auto dataStr = json["data"].asString(); + dataStr = base64_decode(dataStr); + data = std::vector(dataStr.begin(), dataStr.end()); + } + if (json.isMember("utype")) + user_type = json["utype"].asString(); } Json::Value @@ -240,6 +232,22 @@ Value::toJson() const } return val; } + +uint64_t +unpackId(const Json::Value& json, const std::string& key) { + uint64_t ret = 0; + try { + if (json.isMember(key)) { + const auto& t = json[key]; + if (t.isString()) { + ret = std::stoull(t.asString()); + } else { + ret = t.asLargestUInt(); + } + } + } catch (...) {} + return ret; +} #endif //OPENDHT_PROXY_SERVER bool