2018-02-26 19:08:47 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2018-02-28 00:09:23 +01:00
|
|
|
#include "td/telegram/TdDotNetApi.h"
|
2018-02-26 19:08:47 +01:00
|
|
|
|
|
|
|
#include "td/telegram/Client.h"
|
|
|
|
|
|
|
|
#include "td/utils/port/CxCli.h"
|
|
|
|
|
2018-02-28 11:25:07 +01:00
|
|
|
#include <cstdint>
|
|
|
|
|
2018-02-28 02:11:29 +01:00
|
|
|
namespace Telegram {
|
|
|
|
namespace Td {
|
2018-02-28 00:09:23 +01:00
|
|
|
|
2018-02-26 19:08:47 +01:00
|
|
|
using namespace CxCli;
|
|
|
|
|
|
|
|
public interface class ClientResultHandler {
|
2018-02-28 19:38:21 +01:00
|
|
|
void OnResult(Api::BaseObject^ object);
|
2018-02-26 19:08:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
public ref class Client sealed {
|
|
|
|
public:
|
2018-02-28 19:38:21 +01:00
|
|
|
void Send(Api::Function^ function, ClientResultHandler^ handler) {
|
2018-02-26 19:08:47 +01:00
|
|
|
if (function == nullptr) {
|
|
|
|
throw REF_NEW NullReferenceException("Function can't be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::uint64_t queryId = Increment(currentId);
|
|
|
|
if (handler != nullptr) {
|
|
|
|
handlers[queryId] = handler;
|
|
|
|
}
|
|
|
|
td::Client::Request request;
|
|
|
|
request.id = queryId;
|
|
|
|
request.function = td::td_api::move_object_as<td::td_api::Function>(ToUnmanaged(function)->get_object_ptr());
|
|
|
|
client->send(std::move(request));
|
|
|
|
}
|
|
|
|
|
2018-02-28 19:38:21 +01:00
|
|
|
Api::BaseObject^ Execute(Api::Function^ function) {
|
2018-02-26 19:08:47 +01:00
|
|
|
if (function == nullptr) {
|
|
|
|
throw REF_NEW NullReferenceException("Function can't be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
td::Client::Request request;
|
|
|
|
request.id = 0;
|
|
|
|
request.function = td::td_api::move_object_as<td::td_api::Function>(ToUnmanaged(function)->get_object_ptr());
|
2018-02-28 19:38:21 +01:00
|
|
|
return Api::FromUnmanaged(*client->execute(std::move(request)).object);
|
2018-02-26 19:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetUpdatesHandler(ClientResultHandler^ handler) {
|
|
|
|
handlers[0] = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Run() {
|
|
|
|
while (true) {
|
|
|
|
auto response = client->receive(10.0);
|
|
|
|
if (response.object != nullptr) {
|
2018-02-28 19:38:21 +01:00
|
|
|
ProcessResult(response.id, Api::FromUnmanaged(*response.object));
|
2018-02-26 19:08:47 +01:00
|
|
|
|
|
|
|
if (response.object->get_id() == td::td_api::updateAuthorizationState::ID &&
|
|
|
|
static_cast<td::td_api::updateAuthorizationState &>(*response.object).authorization_state_->get_id() ==
|
|
|
|
td::td_api::authorizationStateClosed::ID) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Client^ Create(ClientResultHandler^ updatesHandler) {
|
|
|
|
return REF_NEW Client(updatesHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Client(ClientResultHandler^ updatesHandler) {
|
|
|
|
client = new td::Client();
|
|
|
|
handlers[0] = updatesHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Client() {
|
|
|
|
delete client;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::int64_t currentId = 0;
|
|
|
|
ConcurrentDictionary<std::uint64_t, ClientResultHandler^> handlers;
|
|
|
|
td::Client *client = nullptr;
|
|
|
|
|
2018-02-28 19:38:21 +01:00
|
|
|
void ProcessResult(std::uint64_t id, Api::BaseObject^ object) {
|
2018-02-26 19:08:47 +01:00
|
|
|
ClientResultHandler^ handler;
|
|
|
|
// update handler stays forever
|
|
|
|
if (id == 0 ? handlers.TryGetValue(id, handler) : handlers.TryRemove(id, handler)) {
|
|
|
|
// TODO try/catch
|
|
|
|
handler->OnResult(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-28 02:11:29 +01:00
|
|
|
} // namespace Td
|
|
|
|
} // namespace Telegram
|