tdlight/td/telegram/ClientDotNet.cpp

101 lines
2.8 KiB
C++
Raw Normal View History

//
// 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)
//
#include "td/telegram/TdDotNetApi.h"
#include "td/telegram/Client.h"
#include "td/utils/port/CxCli.h"
#include <cstdint>
namespace Telegram {
namespace Td {
using namespace CxCli;
public interface class ClientResultHandler {
void OnResult(Api::BaseObject^ object);
};
public ref class Client sealed {
public:
void Send(Api::Function^ function, ClientResultHandler^ handler) {
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));
}
Api::BaseObject^ Execute(Api::Function^ function) {
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());
return Api::FromUnmanaged(*client->execute(std::move(request)).object);
}
void SetUpdatesHandler(ClientResultHandler^ handler) {
handlers[0] = handler;
}
void Run() {
while (true) {
auto response = client->receive(10.0);
if (response.object != nullptr) {
ProcessResult(response.id, Api::FromUnmanaged(*response.object));
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;
void ProcessResult(std::uint64_t id, Api::BaseObject^ object) {
ClientResultHandler^ handler;
// update handler stays forever
if (id == 0 ? handlers.TryGetValue(id, handler) : handlers.TryRemove(id, handler)) {
// TODO try/catch
handler->OnResult(object);
}
}
};
} // namespace Td
} // namespace Telegram