2020-12-20 07:46:07 +08:00
|
|
|
/*
|
|
|
|
* SimExternalConnection.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <boost/asio.hpp>
|
2020-12-20 10:09:30 +08:00
|
|
|
#include <boost/range.hpp>
|
2020-12-20 11:57:03 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2020-12-20 07:46:07 +08:00
|
|
|
|
|
|
|
#include "fdbrpc/SimExternalConnection.h"
|
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
|
|
|
|
|
|
|
using namespace boost::asio;
|
|
|
|
|
2020-12-20 09:24:36 +08:00
|
|
|
static io_service ios;
|
2020-12-20 07:46:07 +08:00
|
|
|
|
2020-12-20 09:24:36 +08:00
|
|
|
void SimExternalConnection::close() {
|
|
|
|
socket.close();
|
|
|
|
}
|
2020-12-20 07:46:07 +08:00
|
|
|
|
|
|
|
Future<Void> SimExternalConnection::acceptHandshake() {
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Void> SimExternalConnection::connectHandshake() {
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Void> SimExternalConnection::onWritable() {
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Void> SimExternalConnection::onReadable() {
|
2020-12-20 12:15:02 +08:00
|
|
|
return onReadableTrigger.onTrigger();
|
2020-12-20 07:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int SimExternalConnection::read(uint8_t* begin, uint8_t* end) {
|
2020-12-20 10:12:19 +08:00
|
|
|
size_t toRead = end - begin;
|
2020-12-20 11:57:03 +08:00
|
|
|
ASSERT(toRead <= readBuffer.size());
|
|
|
|
// TODO: Improve performance
|
|
|
|
for (int i = 0; i < toRead; ++i) {
|
|
|
|
*(begin + i) = readBuffer.front();
|
|
|
|
readBuffer.pop_front();
|
|
|
|
}
|
|
|
|
return toRead;
|
2020-12-20 07:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int SimExternalConnection::write(SendBuffer const* buffer, int limit) {
|
2020-12-20 10:09:30 +08:00
|
|
|
boost::system::error_code err;
|
2020-12-20 12:15:02 +08:00
|
|
|
bool triggerReaders = (socket.available() == 0);
|
2020-12-20 11:57:03 +08:00
|
|
|
int bytesSent = socket.write_some(
|
2020-12-20 10:09:30 +08:00
|
|
|
boost::iterator_range<SendBufferIterator>(SendBufferIterator(buffer, limit), SendBufferIterator()), err);
|
|
|
|
ASSERT(!err);
|
2020-12-20 11:57:03 +08:00
|
|
|
ASSERT(bytesSent > 0);
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
const auto bytesReadable = socket.available();
|
|
|
|
std::vector<uint8_t> tempReadBuffer(bytesReadable);
|
|
|
|
for (int index = 0; index < bytesReadable;) {
|
|
|
|
index += socket.read_some(mutable_buffers_1(&tempReadBuffer[index], bytesReadable), err);
|
|
|
|
}
|
|
|
|
std::copy(tempReadBuffer.begin(), tempReadBuffer.end(), std::inserter(readBuffer, readBuffer.end()));
|
|
|
|
ASSERT(!err);
|
|
|
|
ASSERT(socket.available() == 0);
|
2020-12-20 12:15:02 +08:00
|
|
|
if (triggerReaders) {
|
|
|
|
onReadableTrigger.trigger();
|
|
|
|
}
|
2020-12-20 11:57:03 +08:00
|
|
|
return bytesSent;
|
2020-12-20 07:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NetworkAddress SimExternalConnection::getPeerAddress() const {
|
2020-12-20 09:31:08 +08:00
|
|
|
auto endpoint = socket.remote_endpoint();
|
|
|
|
auto addr = endpoint.address();
|
|
|
|
if (addr.is_v6()) {
|
|
|
|
return NetworkAddress(IPAddress(addr.to_v6().to_bytes()), endpoint.port());
|
|
|
|
} else {
|
|
|
|
return NetworkAddress(addr.to_v4().to_ulong(), endpoint.port());
|
|
|
|
}
|
2020-12-20 07:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UID SimExternalConnection::getDebugID() const {
|
2020-12-20 11:57:03 +08:00
|
|
|
return dbgid;
|
2020-12-20 07:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<std::vector<NetworkAddress>> SimExternalConnection::resolveTCPEndpoint(const std::string& host,
|
|
|
|
const std::string& service) {
|
|
|
|
ip::tcp::resolver resolver(ios);
|
|
|
|
ip::tcp::resolver::query query(host, service);
|
|
|
|
auto iter = resolver.resolve(query);
|
|
|
|
decltype(iter) end;
|
|
|
|
std::vector<NetworkAddress> addrs;
|
|
|
|
while (iter != end) {
|
|
|
|
auto endpoint = iter->endpoint();
|
|
|
|
auto addr = endpoint.address();
|
|
|
|
if (addr.is_v6()) {
|
|
|
|
addrs.emplace_back(IPAddress(addr.to_v6().to_bytes()), endpoint.port());
|
|
|
|
} else {
|
|
|
|
addrs.emplace_back(addr.to_v4().to_ulong(), endpoint.port());
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
return addrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Reference<IConnection>> SimExternalConnection::connect(NetworkAddress toAddr) {
|
2020-12-20 09:24:36 +08:00
|
|
|
ip::tcp::socket socket(ios);
|
|
|
|
auto ip = toAddr.ip;
|
|
|
|
ip::address address;
|
|
|
|
if (ip.isV6()) {
|
|
|
|
address = boost::asio::ip::address_v6(ip.toV6());
|
|
|
|
} else {
|
|
|
|
address = boost::asio::ip::address_v4(ip.toV4());
|
|
|
|
}
|
|
|
|
socket.connect(ip::tcp::endpoint(address, toAddr.port));
|
|
|
|
return Reference<IConnection>(new SimExternalConnection(std::move(socket)));
|
2020-12-20 07:46:07 +08:00
|
|
|
}
|
2020-12-20 09:24:36 +08:00
|
|
|
|
2020-12-20 11:57:03 +08:00
|
|
|
SimExternalConnection::SimExternalConnection(ip::tcp::socket&& socket)
|
|
|
|
: socket(std::move(socket)), dbgid(deterministicRandom()->randomUniqueID()) {}
|