2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* FlowTransport.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* 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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FLOW_TRANSPORT_H
|
|
|
|
#define FLOW_TRANSPORT_H
|
|
|
|
#pragma once
|
|
|
|
|
2018-10-25 05:59:50 +08:00
|
|
|
#include <algorithm>
|
2017-05-26 04:48:44 +08:00
|
|
|
#include "flow/network.h"
|
|
|
|
|
|
|
|
#pragma pack(push, 4)
|
|
|
|
class Endpoint {
|
|
|
|
public:
|
|
|
|
// Endpoint represents a particular service (e.g. a serialized Promise<T> or PromiseStream<T>)
|
|
|
|
// An endpoint is either "local" (used for receiving data) or "remote" (used for sending data)
|
|
|
|
typedef UID Token;
|
2018-10-31 04:44:37 +08:00
|
|
|
NetworkAddressList addresses;
|
2017-05-26 04:48:44 +08:00
|
|
|
Token token;
|
|
|
|
|
2019-03-24 08:54:46 +08:00
|
|
|
Endpoint() {}
|
2018-10-31 04:44:37 +08:00
|
|
|
Endpoint(const NetworkAddressList& addresses, Token token) : addresses(addresses), token(token) {
|
2019-02-01 10:20:14 +08:00
|
|
|
choosePrimaryAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
void choosePrimaryAddress() {
|
2019-03-24 08:54:46 +08:00
|
|
|
if(addresses.secondaryAddress.present() && !g_network->getLocalAddresses().secondaryAddress.present() && (addresses.address.isTLS() != g_network->getLocalAddresses().address.isTLS())) {
|
|
|
|
std::swap(addresses.address, addresses.secondaryAddress.get());
|
|
|
|
}
|
2018-10-31 04:44:37 +08:00
|
|
|
}
|
2018-10-25 05:59:50 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
bool isValid() const { return token.isValid(); }
|
|
|
|
bool isLocal() const;
|
|
|
|
|
2018-10-31 04:44:37 +08:00
|
|
|
// Return the primary network address, which is the first network address among
|
|
|
|
// all addresses this endpoint listens to.
|
|
|
|
const NetworkAddress& getPrimaryAddress() const {
|
2019-03-24 08:54:46 +08:00
|
|
|
return addresses.address;
|
2018-10-31 04:44:37 +08:00
|
|
|
}
|
|
|
|
|
2018-10-25 05:59:50 +08:00
|
|
|
bool operator == (Endpoint const& r) const {
|
2019-01-09 23:41:02 +08:00
|
|
|
return getPrimaryAddress() == r.getPrimaryAddress() && token == r.token;
|
2018-10-25 05:59:50 +08:00
|
|
|
}
|
|
|
|
bool operator != (Endpoint const& r) const {
|
|
|
|
return !(*this == r);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator < (Endpoint const& r) const {
|
2019-01-09 23:41:02 +08:00
|
|
|
const NetworkAddress& left = getPrimaryAddress();
|
|
|
|
const NetworkAddress& right = r.getPrimaryAddress();
|
2018-10-25 05:59:50 +08:00
|
|
|
if (left != right)
|
|
|
|
return left < right;
|
|
|
|
else
|
|
|
|
return token < r.token;
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
2019-02-01 10:20:14 +08:00
|
|
|
if (ar.isDeserializing && ar.protocolVersion() < 0x0FDB00B061020001LL) {
|
2019-03-24 08:54:46 +08:00
|
|
|
addresses.secondaryAddress = Optional<NetworkAddress>();
|
|
|
|
serializer(ar, addresses.address, token);
|
2019-02-01 10:20:14 +08:00
|
|
|
} else {
|
|
|
|
serializer(ar, addresses, token);
|
|
|
|
if (ar.isDeserializing) {
|
|
|
|
choosePrimaryAddress();
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
2018-10-25 05:59:50 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
class NetworkMessageReceiver {
|
|
|
|
public:
|
|
|
|
virtual void receive( ArenaReader& ) = 0;
|
|
|
|
virtual bool isStream() const { return false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct NetworkPacket* PacketID;
|
|
|
|
|
|
|
|
class FlowTransport {
|
|
|
|
public:
|
|
|
|
FlowTransport(uint64_t transportId);
|
|
|
|
~FlowTransport();
|
|
|
|
|
|
|
|
static void createInstance(uint64_t transportId = 0);
|
|
|
|
// Creates a new FlowTransport and makes FlowTransport::transport() return it. This uses g_network->global() variables,
|
|
|
|
// so it will be private to a simulation.
|
|
|
|
|
|
|
|
void initMetrics();
|
|
|
|
// Metrics must be initialized after FlowTransport::createInstance has been called
|
|
|
|
|
|
|
|
Future<Void> bind( NetworkAddress publicAddress, NetworkAddress listenAddress );
|
|
|
|
// Starts a server listening on the given listenAddress, and sets publicAddress to be the public
|
|
|
|
// address of this server. Returns only errors.
|
|
|
|
|
2018-10-31 04:44:37 +08:00
|
|
|
NetworkAddress getLocalAddress() const;
|
|
|
|
// Returns first local NetworkAddress.
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-02-01 10:20:14 +08:00
|
|
|
NetworkAddressList getLocalAddresses() const;
|
|
|
|
// Returns all local NetworkAddress.
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
std::map<NetworkAddress, std::pair<uint64_t, double>>* getIncompatiblePeers();
|
|
|
|
// Returns the same of all peers that have attempted to connect, but have incompatible protocol versions
|
|
|
|
|
2018-07-11 04:10:29 +08:00
|
|
|
void addPeerReference( const Endpoint&, NetworkMessageReceiver* );
|
|
|
|
// Signal that a peer connection is being used, even if no messages are currently being sent to the peer
|
|
|
|
|
|
|
|
void removePeerReference( const Endpoint&, NetworkMessageReceiver* );
|
|
|
|
// Signal that a peer connection is no longer being used
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
void addEndpoint( Endpoint& endpoint, NetworkMessageReceiver*, uint32_t taskID );
|
|
|
|
// Sets endpoint to be a new local endpoint which delivers messages to the given receiver
|
|
|
|
|
|
|
|
void removeEndpoint( const Endpoint&, NetworkMessageReceiver* );
|
|
|
|
// The given local endpoint no longer delivers messages to the given receiver or uses resources
|
|
|
|
|
|
|
|
void addWellKnownEndpoint( Endpoint& endpoint, NetworkMessageReceiver*, uint32_t taskID );
|
|
|
|
// Sets endpoint to a new local endpoint (without changing its token) which delivers messages to the given receiver
|
|
|
|
// Implementations may have limitations on when this function is called and what endpoint.token may be!
|
|
|
|
|
|
|
|
PacketID sendReliable( ISerializeSource const& what, const Endpoint& destination );
|
|
|
|
// sendReliable will keep trying to deliver the data to the destination until cancelReliable is
|
|
|
|
// called. It will retry sending if the connection is closed or the failure manager reports
|
|
|
|
// the destination become available (edge triggered).
|
|
|
|
|
|
|
|
void cancelReliable( PacketID );
|
|
|
|
// Makes PacketID "unreliable" (either the data or a connection close event will be delivered
|
|
|
|
// eventually). It can still be used safely to send a reply to a "reliable" request.
|
|
|
|
|
2018-07-09 01:26:41 +08:00
|
|
|
void sendUnreliable( ISerializeSource const& what, const Endpoint& destination, bool openConnection = true );// { cancelReliable(sendReliable(what,destination)); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
int getEndpointCount();
|
|
|
|
// for tracing only
|
|
|
|
|
2017-11-10 03:20:35 +08:00
|
|
|
bool incompatibleOutgoingConnectionsPresent();
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
static FlowTransport& transport() { return *static_cast<FlowTransport*>((void*) g_network->global(INetwork::enFlowTransport)); }
|
|
|
|
static NetworkAddress getGlobalLocalAddress() { return transport().getLocalAddress(); }
|
2019-02-01 10:20:14 +08:00
|
|
|
static NetworkAddressList getGlobalLocalAddresses() { return transport().getLocalAddresses(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-03-27 03:05:43 +08:00
|
|
|
Endpoint loadedEndpoint(const UID& token);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
class TransportData* self;
|
|
|
|
};
|
|
|
|
|
2019-01-07 19:38:35 +08:00
|
|
|
inline bool Endpoint::isLocal() const {
|
2019-03-24 08:54:46 +08:00
|
|
|
const auto& localAddrs = FlowTransport::transport().getLocalAddresses();
|
|
|
|
return addresses.address == localAddrs.address || (localAddrs.secondaryAddress.present() && addresses.address == localAddrs.secondaryAddress.get());
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 07:46:52 +08:00
|
|
|
#endif
|