2014-02-10 14:43:13 +02:00
|
|
|
#pragma once
|
2023-04-01 06:18:41 +03:00
|
|
|
///@file
|
2014-02-10 14:43:13 +02:00
|
|
|
|
2023-05-26 21:11:08 +03:00
|
|
|
#include "common-protocol.hh"
|
|
|
|
|
2014-02-10 14:43:13 +02:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
#define SERVE_MAGIC_1 0x390c9deb
|
|
|
|
#define SERVE_MAGIC_2 0x5452eecb
|
|
|
|
|
2021-08-24 00:47:29 +03:00
|
|
|
#define SERVE_PROTOCOL_VERSION (2 << 8 | 7)
|
2014-02-10 14:43:13 +02:00
|
|
|
#define GET_PROTOCOL_MAJOR(x) ((x) & 0xff00)
|
|
|
|
#define GET_PROTOCOL_MINOR(x) ((x) & 0x00ff)
|
|
|
|
|
2023-05-26 21:11:08 +03:00
|
|
|
|
|
|
|
class Store;
|
|
|
|
struct Source;
|
|
|
|
|
|
|
|
|
2023-05-26 18:22:24 +03:00
|
|
|
/**
|
|
|
|
* The "serve protocol", used by ssh:// stores.
|
|
|
|
*
|
|
|
|
* This `struct` is basically just a `namespace`; We use a type rather
|
|
|
|
* than a namespace just so we can use it as a template argument.
|
|
|
|
*/
|
|
|
|
struct ServeProto
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Enumeration of all the request types for the protocol.
|
|
|
|
*/
|
|
|
|
enum struct Command : uint64_t;
|
2023-05-26 21:11:08 +03:00
|
|
|
|
2023-10-17 18:15:36 +03:00
|
|
|
/**
|
|
|
|
* Version type for the protocol.
|
|
|
|
*
|
|
|
|
* @todo Convert to struct with separate major vs minor fields.
|
|
|
|
*/
|
|
|
|
using Version = unsigned int;
|
|
|
|
|
2023-05-26 21:11:08 +03:00
|
|
|
/**
|
|
|
|
* A unidirectional read connection, to be used by the read half of the
|
|
|
|
* canonical serializers below.
|
|
|
|
*/
|
|
|
|
struct ReadConn {
|
|
|
|
Source & from;
|
2022-03-25 06:40:49 +02:00
|
|
|
Version version;
|
2023-05-26 21:11:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A unidirectional write connection, to be used by the write half of the
|
|
|
|
* canonical serializers below.
|
|
|
|
*/
|
|
|
|
struct WriteConn {
|
|
|
|
Sink & to;
|
2022-03-25 06:40:49 +02:00
|
|
|
Version version;
|
2023-05-26 21:11:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data type for canonical pairs of serialisers for the serve protocol.
|
|
|
|
*
|
|
|
|
* See https://en.cppreference.com/w/cpp/language/adl for the broader
|
|
|
|
* concept of what is going on here.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
struct Serialise;
|
|
|
|
// This is the definition of `Serialise` we *want* to put here, but
|
|
|
|
// do not do so.
|
|
|
|
//
|
|
|
|
// See `worker-protocol.hh` for a longer explanation.
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
static T read(const Store & store, ReadConn conn);
|
|
|
|
static void write(const Store & store, WriteConn conn, const T & t);
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper function around `ServeProto::Serialise<T>::write` that allows us to
|
|
|
|
* infer the type instead of having to write it down explicitly.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
static void write(const Store & store, WriteConn conn, const T & t)
|
|
|
|
{
|
|
|
|
ServeProto::Serialise<T>::write(store, conn, t);
|
|
|
|
}
|
2023-05-26 18:22:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
enum struct ServeProto::Command : uint64_t
|
|
|
|
{
|
|
|
|
QueryValidPaths = 1,
|
|
|
|
QueryPathInfos = 2,
|
|
|
|
DumpStorePath = 3,
|
|
|
|
ImportPaths = 4,
|
|
|
|
ExportPaths = 5,
|
|
|
|
BuildPaths = 6,
|
|
|
|
QueryClosure = 7,
|
|
|
|
BuildDerivation = 8,
|
|
|
|
AddToStoreNar = 9,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience for sending operation codes.
|
|
|
|
*
|
|
|
|
* @todo Switch to using `ServeProto::Serialize` instead probably. But
|
|
|
|
* this was not done at this time so there would be less churn.
|
|
|
|
*/
|
|
|
|
inline Sink & operator << (Sink & sink, ServeProto::Command op)
|
|
|
|
{
|
|
|
|
return sink << (uint64_t) op;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience for debugging.
|
|
|
|
*
|
|
|
|
* @todo Perhaps render known opcodes more nicely.
|
|
|
|
*/
|
|
|
|
inline std::ostream & operator << (std::ostream & s, ServeProto::Command op)
|
|
|
|
{
|
|
|
|
return s << (uint64_t) op;
|
|
|
|
}
|
2014-02-10 14:43:13 +02:00
|
|
|
|
2023-05-26 21:11:08 +03:00
|
|
|
/**
|
|
|
|
* Declare a canonical serialiser pair for the worker protocol.
|
|
|
|
*
|
|
|
|
* We specialise the struct merely to indicate that we are implementing
|
|
|
|
* the function for the given type.
|
|
|
|
*
|
|
|
|
* Some sort of `template<...>` must be used with the caller for this to
|
|
|
|
* be legal specialization syntax. See below for what that looks like in
|
|
|
|
* practice.
|
|
|
|
*/
|
|
|
|
#define DECLARE_SERVE_SERIALISER(T) \
|
|
|
|
struct ServeProto::Serialise< T > \
|
|
|
|
{ \
|
|
|
|
static T read(const Store & store, ServeProto::ReadConn conn); \
|
|
|
|
static void write(const Store & store, ServeProto::WriteConn conn, const T & t); \
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
DECLARE_SERVE_SERIALISER(std::vector<T>);
|
|
|
|
template<typename T>
|
|
|
|
DECLARE_SERVE_SERIALISER(std::set<T>);
|
|
|
|
template<typename... Ts>
|
|
|
|
DECLARE_SERVE_SERIALISER(std::tuple<Ts...>);
|
|
|
|
|
|
|
|
#define COMMA_ ,
|
|
|
|
template<typename K, typename V>
|
|
|
|
DECLARE_SERVE_SERIALISER(std::map<K COMMA_ V>);
|
|
|
|
#undef COMMA_
|
|
|
|
|
2014-02-10 14:43:13 +02:00
|
|
|
}
|