2
This file is part of TON Blockchain source code.
4
TON Blockchain is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License
6
as published by the Free Software Foundation; either version 2
7
of the License, or (at your option) any later version.
9
TON Blockchain is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with TON Blockchain. If not, see <http://www.gnu.org/licenses/>.
17
In addition, as a special exception, the copyright holders give permission
18
to link the code of portions of this program with the OpenSSL library.
19
You must obey the GNU General Public License in all respects for all
20
of the code used other than OpenSSL. If you modify file(s) with this
21
exception, you may extend this exception to your version of the file(s),
22
but you are not obligated to do so. If you do not wish to do so, delete this
23
exception statement from your version. If you delete this exception statement
24
from all source files in the program, then also delete it here.
26
Copyright 2017-2020 Telegram Systems LLP
28
#include "td/actor/actor.h"
30
#include "td/utils/BufferedFd.h"
31
#include "td/utils/OptionParser.h"
32
#include "td/utils/port/SocketFd.h"
33
#include "td/utils/port/ServerSocketFd.h"
34
#include "td/utils/Observer.h"
36
#include "td/net/TcpListener.h"
38
class PingClient : public td::actor::Actor, td::ObserverBase {
40
PingClient(td::SocketFd fd) : buffered_fd_(std::move(fd)) {
44
td::BufferedFd<td::SocketFd> buffered_fd_;
45
td::actor::ActorId<PingClient> self_;
46
void notify() override {
47
// NB: Interface will be changed
48
send_closure_later(self_, &PingClient::on_net);
54
void start_up() override {
55
self_ = actor_id(this);
57
// Subscribe for socket updates
58
// NB: Interface will be changed
59
td::actor::SchedulerContext::get()->get_poll().subscribe(buffered_fd_.get_poll_info().extract_pollable_fd(this),
60
td::PollFlags::ReadWrite());
62
alarm_timestamp() = td::Timestamp::now();
65
void tear_down() override {
67
// unsubscribe from socket updates
68
// nb: interface will be changed
69
td::actor::SchedulerContext::get()->get_poll().unsubscribe(buffered_fd_.get_poll_info().get_pollable_fd_ref());
72
void loop() override {
74
TRY_STATUS(buffered_fd_.flush_read());
75
auto &input = buffered_fd_.input_buffer();
76
while (input.size() >= 12) {
77
auto query = input.cut_head(12).move_as_buffer_slice();
78
LOG(INFO) << "Got query " << td::format::escaped(query.as_slice());
79
if (query[5] == 'i') {
80
LOG(INFO) << "Send ping";
81
buffered_fd_.output_buffer().append("magkpongpong");
83
LOG(INFO) << "Got pong";
87
TRY_STATUS(buffered_fd_.flush_write());
88
if (td::can_close(buffered_fd_)) {
91
return td::Status::OK();
93
if (status.is_error()) {
94
LOG(ERROR) << "Client got error " << status;
99
void alarm() override {
100
alarm_timestamp() = td::Timestamp::in(5);
101
LOG(INFO) << "Send ping";
102
buffered_fd_.output_buffer().append("magkpingping");
107
int main(int argc, char *argv[]) {
108
td::OptionParser options_parser;
109
options_parser.set_description("Tcp ping server/client (based on td::actors2)");
112
bool is_client = false;
113
options_parser.add_option('p', "port", "listen/connect to tcp port (8081 by default)",
114
[&](td::Slice arg) { port = td::to_integer<int>(arg); });
115
options_parser.add_option('c', "client", "Work as client (server by default)", [&]() { is_client = true; });
116
auto status = options_parser.run(argc, argv);
117
if (status.is_error()) {
118
LOG(ERROR) << status.error();
119
LOG(INFO) << options_parser;
123
// NB: Interface will be changed
124
td::actor::Scheduler scheduler({2});
125
scheduler.run_in_context([&] {
127
td::IPAddress ip_address;
128
ip_address.init_ipv4_port("127.0.0.1", port).ensure();
129
td::actor::create_actor<PingClient>(td::actor::ActorOptions().with_name("TcpClient").with_poll(),
130
td::SocketFd::open(ip_address).move_as_ok())
133
class Callback : public td::TcpListener::Callback {
135
void accept(td::SocketFd fd) override {
136
td::actor::create_actor<PingClient>(td::actor::ActorOptions().with_name("TcpClient").with_poll(),
141
td::actor::create_actor<td::TcpListener>(td::actor::ActorOptions().with_name("TcpServer").with_poll(), port,
142
std::make_unique<Callback>())