2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2021-09-16 18:09:39 +02:00
|
|
|
#include "td/mtproto/ConnectionManager.h"
|
2019-01-31 03:23:40 +01:00
|
|
|
#include "td/mtproto/PacketInfo.h"
|
2019-01-31 03:13:59 +01:00
|
|
|
#include "td/mtproto/TransportType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
2021-10-22 18:59:15 +02:00
|
|
|
#include "td/utils/BufferedFd.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
2021-03-27 03:19:22 +01:00
|
|
|
#include "td/utils/port/IPAddress.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/SocketFd.h"
|
|
|
|
#include "td/utils/Status.h"
|
2019-02-12 17:48:52 +01:00
|
|
|
#include "td/utils/StorerBase.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
|
|
|
|
2019-02-12 17:48:52 +01:00
|
|
|
class AuthKey;
|
2019-01-31 03:05:40 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class RawConnection {
|
|
|
|
public:
|
|
|
|
class StatsCallback {
|
|
|
|
public:
|
|
|
|
virtual ~StatsCallback() = default;
|
|
|
|
virtual void on_read(uint64 bytes) = 0;
|
|
|
|
virtual void on_write(uint64 bytes) = 0;
|
|
|
|
|
|
|
|
virtual void on_pong() = 0; // called when we know that connection is alive
|
2019-07-22 04:04:18 +02:00
|
|
|
virtual void on_error() = 0; // called on RawConnection error. Such error should be very rare on good connections.
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void on_mtproto_error() = 0;
|
|
|
|
};
|
2021-03-27 03:19:22 +01:00
|
|
|
RawConnection() = default;
|
|
|
|
RawConnection(const RawConnection &) = delete;
|
|
|
|
RawConnection &operator=(const RawConnection &) = delete;
|
2023-06-16 14:16:03 +02:00
|
|
|
virtual ~RawConnection();
|
2021-03-27 03:19:22 +01:00
|
|
|
|
2021-10-22 18:59:15 +02:00
|
|
|
static unique_ptr<RawConnection> create(IPAddress ip_address, BufferedFd<SocketFd> buffered_socket_fd,
|
|
|
|
TransportType transport_type, unique_ptr<StatsCallback> stats_callback);
|
2021-03-26 16:04:01 +01:00
|
|
|
|
2021-09-16 18:09:39 +02:00
|
|
|
virtual void set_connection_token(ConnectionManager::ConnectionToken connection_token) = 0;
|
2021-03-26 16:04:01 +01:00
|
|
|
|
|
|
|
virtual bool can_send() const = 0;
|
|
|
|
virtual TransportType get_transport_type() const = 0;
|
2023-05-14 22:00:56 +02:00
|
|
|
virtual size_t send_crypto(const Storer &storer, uint64 session_id, int64 salt, const AuthKey &auth_key,
|
2022-09-20 16:46:10 +02:00
|
|
|
uint64 quick_ack_token) = 0;
|
2021-03-26 16:04:01 +01:00
|
|
|
virtual uint64 send_no_crypto(const Storer &storer) = 0;
|
|
|
|
|
|
|
|
virtual PollableFdInfo &get_poll_info() = 0;
|
|
|
|
virtual StatsCallback *stats_callback() = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
class Callback {
|
|
|
|
public:
|
|
|
|
Callback() = default;
|
|
|
|
Callback(const Callback &) = delete;
|
|
|
|
Callback &operator=(const Callback &) = delete;
|
|
|
|
virtual ~Callback() = default;
|
2023-09-09 23:04:55 +02:00
|
|
|
virtual Status on_raw_packet(const PacketInfo &packet_info, BufferSlice packet) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual Status on_quick_ack(uint64 quick_ack_token) {
|
2021-10-24 12:23:47 +02:00
|
|
|
return Status::Error("Quick acknowledgements are unsupported by the callback");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
virtual Status before_write() {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-11-20 13:07:27 +01:00
|
|
|
virtual void on_read(size_t size) {
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// NB: After first returned error, all subsequent calls will return error too.
|
2021-03-26 16:04:01 +01:00
|
|
|
virtual Status flush(const AuthKey &auth_key, Callback &callback) TD_WARN_UNUSED_RESULT = 0;
|
|
|
|
virtual bool has_error() const = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-03-26 16:04:01 +01:00
|
|
|
virtual void close() = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-03-26 16:04:01 +01:00
|
|
|
struct PublicFields {
|
|
|
|
uint32 extra{0};
|
|
|
|
string debug_str;
|
|
|
|
double rtt{0};
|
|
|
|
};
|
2018-06-17 19:52:40 +02:00
|
|
|
|
2021-03-26 16:04:01 +01:00
|
|
|
virtual PublicFields &extra() = 0;
|
|
|
|
virtual const PublicFields &extra() const = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|