2023-05-26 21:11:08 +03:00
|
|
|
#pragma once
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* Template implementations (as opposed to mere declarations).
|
|
|
|
*
|
|
|
|
* This file is an exmample of the "impl.hh" pattern. See the
|
|
|
|
* contributing guide.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serve-protocol.hh"
|
|
|
|
#include "length-prefixed-protocol-helper.hh"
|
2022-02-20 21:24:07 +02:00
|
|
|
#include "store-api.hh"
|
2023-05-26 21:11:08 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
/* protocol-agnostic templates */
|
|
|
|
|
|
|
|
#define SERVE_USE_LENGTH_PREFIX_SERIALISER(TEMPLATE, T) \
|
2022-03-18 17:35:45 +02:00
|
|
|
TEMPLATE T ServeProto::Serialise< T >::read(const StoreDirConfig & store, ServeProto::ReadConn conn) \
|
2023-05-26 21:11:08 +03:00
|
|
|
{ \
|
|
|
|
return LengthPrefixedProtoHelper<ServeProto, T >::read(store, conn); \
|
|
|
|
} \
|
2022-03-18 17:35:45 +02:00
|
|
|
TEMPLATE void ServeProto::Serialise< T >::write(const StoreDirConfig & store, ServeProto::WriteConn conn, const T & t) \
|
2023-05-26 21:11:08 +03:00
|
|
|
{ \
|
|
|
|
LengthPrefixedProtoHelper<ServeProto, T >::write(store, conn, t); \
|
|
|
|
}
|
|
|
|
|
|
|
|
SERVE_USE_LENGTH_PREFIX_SERIALISER(template<typename T>, std::vector<T>)
|
|
|
|
SERVE_USE_LENGTH_PREFIX_SERIALISER(template<typename T>, std::set<T>)
|
|
|
|
SERVE_USE_LENGTH_PREFIX_SERIALISER(template<typename... Ts>, std::tuple<Ts...>)
|
|
|
|
|
|
|
|
#define COMMA_ ,
|
|
|
|
SERVE_USE_LENGTH_PREFIX_SERIALISER(
|
|
|
|
template<typename K COMMA_ typename V>,
|
|
|
|
std::map<K COMMA_ V>)
|
|
|
|
#undef COMMA_
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use `CommonProto` where possible.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
struct ServeProto::Serialise
|
|
|
|
{
|
2022-03-18 17:35:45 +02:00
|
|
|
static T read(const StoreDirConfig & store, ServeProto::ReadConn conn)
|
2023-05-26 21:11:08 +03:00
|
|
|
{
|
|
|
|
return CommonProto::Serialise<T>::read(store,
|
|
|
|
CommonProto::ReadConn { .from = conn.from });
|
|
|
|
}
|
2022-03-18 17:35:45 +02:00
|
|
|
static void write(const StoreDirConfig & store, ServeProto::WriteConn conn, const T & t)
|
2023-05-26 21:11:08 +03:00
|
|
|
{
|
|
|
|
CommonProto::Serialise<T>::write(store,
|
|
|
|
CommonProto::WriteConn { .to = conn.to },
|
|
|
|
t);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* protocol-specific templates */
|
|
|
|
|
2022-02-20 21:24:07 +02:00
|
|
|
struct ServeProto::BasicClientConnection
|
|
|
|
{
|
|
|
|
FdSink to;
|
|
|
|
FdSource from;
|
|
|
|
ServeProto::Version remoteVersion;
|
|
|
|
|
2022-02-20 21:24:07 +02:00
|
|
|
/**
|
|
|
|
* Establishes connection, negotiating version.
|
|
|
|
*
|
|
|
|
* @return the version provided by the other side of the
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
* @param to Taken by reference to allow for various error handling
|
|
|
|
* mechanisms.
|
|
|
|
*
|
|
|
|
* @param from Taken by reference to allow for various error
|
|
|
|
* handling mechanisms.
|
|
|
|
*
|
|
|
|
* @param localVersion Our version which is sent over
|
|
|
|
*
|
|
|
|
* @param host Just used to add context to thrown exceptions.
|
|
|
|
*/
|
|
|
|
static ServeProto::Version handshake(
|
|
|
|
BufferedSink & to,
|
|
|
|
Source & from,
|
|
|
|
ServeProto::Version localVersion,
|
|
|
|
std::string_view host);
|
|
|
|
|
2022-02-20 21:24:07 +02:00
|
|
|
/**
|
|
|
|
* Coercion to `ServeProto::ReadConn`. This makes it easy to use the
|
|
|
|
* factored out serve protocol serializers with a
|
|
|
|
* `LegacySSHStore::Connection`.
|
|
|
|
*
|
|
|
|
* The serve protocol connection types are unidirectional, unlike
|
|
|
|
* this type.
|
|
|
|
*/
|
|
|
|
operator ServeProto::ReadConn ()
|
|
|
|
{
|
|
|
|
return ServeProto::ReadConn {
|
|
|
|
.from = from,
|
|
|
|
.version = remoteVersion,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Coercion to `ServeProto::WriteConn`. This makes it easy to use the
|
|
|
|
* factored out serve protocol serializers with a
|
|
|
|
* `LegacySSHStore::Connection`.
|
|
|
|
*
|
|
|
|
* The serve protocol connection types are unidirectional, unlike
|
|
|
|
* this type.
|
|
|
|
*/
|
|
|
|
operator ServeProto::WriteConn ()
|
|
|
|
{
|
|
|
|
return ServeProto::WriteConn {
|
|
|
|
.to = to,
|
|
|
|
.version = remoteVersion,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
StorePathSet queryValidPaths(
|
|
|
|
const Store & remoteStore,
|
|
|
|
bool lock, const StorePathSet & paths,
|
|
|
|
SubstituteFlag maybeSubstitute);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just the request half, because Hydra may do other things between
|
|
|
|
* issuing the request and reading the `BuildResult` response.
|
|
|
|
*/
|
|
|
|
void putBuildDerivationRequest(
|
|
|
|
const Store & store,
|
|
|
|
const StorePath & drvPath, const BasicDerivation & drv,
|
|
|
|
const ServeProto::BuildOptions & options);
|
|
|
|
};
|
|
|
|
|
2024-01-19 23:38:08 +02:00
|
|
|
struct ServeProto::BasicServerConnection
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Establishes connection, negotiating version.
|
|
|
|
*
|
|
|
|
* @return the version provided by the other side of the
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
* @param to Taken by reference to allow for various error handling
|
|
|
|
* mechanisms.
|
|
|
|
*
|
|
|
|
* @param from Taken by reference to allow for various error
|
|
|
|
* handling mechanisms.
|
|
|
|
*
|
|
|
|
* @param localVersion Our version which is sent over
|
|
|
|
*/
|
|
|
|
static ServeProto::Version handshake(
|
|
|
|
BufferedSink & to,
|
|
|
|
Source & from,
|
|
|
|
ServeProto::Version localVersion);
|
|
|
|
};
|
|
|
|
|
2023-05-26 21:11:08 +03:00
|
|
|
}
|