forked from flightaware/dump978
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_output.cc
152 lines (121 loc) · 4.44 KB
/
socket_output.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2019, FlightAware LLC.
// All rights reserved.
// Licensed under the 2-clause BSD license; see the LICENSE file
#include <iomanip>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/ip/v6_only.hpp>
#include "message_dispatch.h"
#include "socket_output.h"
namespace asio = boost::asio;
using boost::asio::ip::tcp;
using namespace flightaware::uat;
SocketOutput::SocketOutput(asio::io_service &service, tcp::socket &&socket) : service_(service), strand_(service), socket_(std::move(socket)), peer_(socket_.remote_endpoint()), flush_pending_(false) {}
void SocketOutput::Start() { ReadAndDiscard(); }
void SocketOutput::ReadAndDiscard() {
auto self(shared_from_this());
auto buf = std::make_shared<Bytes>(512);
socket_.async_read_some(asio::buffer(*buf), strand_.wrap([this, self, buf](const boost::system::error_code &ec, std::size_t len) {
if (ec) {
HandleError(ec);
} else {
ReadAndDiscard();
}
}));
}
void SocketOutput::Write(SharedMessageVector messages) {
auto self(shared_from_this());
strand_.dispatch([this, self, messages]() {
if (IsOpen()) {
InternalWrite(messages);
Flush();
}
});
}
void SocketOutput::Flush() {
if (flush_pending_)
return;
auto writebuf = std::make_shared<std::string>(outbuf_.str());
if (writebuf->empty())
return;
flush_pending_ = true;
outbuf_.str(std::string());
auto self(shared_from_this());
async_write(socket_, boost::asio::buffer(*writebuf), strand_.wrap([this, self, writebuf](const boost::system::error_code &ec, size_t len) {
flush_pending_ = false;
if (ec) {
HandleError(ec);
return;
}
Flush(); // maybe some more data arrived
}));
}
void SocketOutput::HandleError(const boost::system::error_code &ec) {
if (ec == boost::asio::error::eof) {
std::cerr << peer_ << ": connection closed" << std::endl;
} else if (ec != boost::asio::error::operation_aborted) {
std::cerr << peer_ << ": connection error: " << ec.message() << std::endl;
}
Close();
}
void SocketOutput::Close() {
socket_.close();
if (close_notifier_) {
close_notifier_();
}
}
//////////////
void RawOutput::Start() {
SocketOutput::Start();
if (header_)
Write(header_);
}
void RawOutput::InternalWrite(SharedMessageVector messages) {
for (const auto &message : *messages) {
Buf() << message << '\n';
}
}
//////////////
void JsonOutput::InternalWrite(SharedMessageVector messages) {
for (const auto &message : *messages) {
if (message.Type() == MessageType::DOWNLINK_SHORT || message.Type() == MessageType::DOWNLINK_LONG) {
Buf() << AdsbMessage(message).ToJson() << '\n';
}
}
}
//////////////
SocketListener::SocketListener(asio::io_service &service, const tcp::endpoint &endpoint, MessageDispatch &dispatch, ConnectionFactory factory) : service_(service), acceptor_(service), endpoint_(endpoint), socket_(service), dispatch_(dispatch), factory_(factory) {}
void SocketListener::Start() {
acceptor_.open(endpoint_.protocol());
acceptor_.set_option(asio::socket_base::reuse_address(true));
acceptor_.set_option(tcp::acceptor::reuse_address(true));
// We are v6 aware and bind separately to v4 and v6 addresses
if (endpoint_.protocol() == tcp::v6())
acceptor_.set_option(asio::ip::v6_only(true));
acceptor_.bind(endpoint_);
acceptor_.listen();
Accept();
}
void SocketListener::Close() {
acceptor_.cancel();
socket_.close();
}
void SocketListener::Accept() {
auto self(shared_from_this());
acceptor_.async_accept(socket_, peer_, [this, self](const boost::system::error_code &ec) {
if (!ec) {
std::cerr << endpoint_ << ": accepted a connection from " << peer_ << std::endl;
auto new_output = factory_(service_, std::move(socket_));
if (new_output) {
auto handle = dispatch_.AddClient(std::bind(&SocketOutput::Write, new_output, std::placeholders::_1));
new_output->SetCloseNotifier([this, self, handle] { dispatch_.RemoveClient(handle); });
new_output->Start();
}
} else {
if (ec == boost::system::errc::operation_canceled)
return;
std::cerr << endpoint_ << ": accept error: " << ec.message() << std::endl;
}
Accept();
});
}