From e960b2823091f7c6685b55d5f1ad8d7612130009 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 19 Jan 2024 16:38:08 -0500 Subject: [PATCH] Factor our `ServeProto::BasicServerConnection::handshake` We'll need this for unit testing. Co-authored-by: Robert Hensing --- src/libstore/serve-protocol-impl.cc | 12 ++++++++++++ src/libstore/serve-protocol-impl.hh | 22 ++++++++++++++++++++++ src/libstore/serve-protocol.hh | 1 + src/nix-store/nix-store.cc | 8 +++----- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/libstore/serve-protocol-impl.cc b/src/libstore/serve-protocol-impl.cc index 6bf6c8cf6..b39212884 100644 --- a/src/libstore/serve-protocol-impl.cc +++ b/src/libstore/serve-protocol-impl.cc @@ -22,6 +22,18 @@ ServeProto::Version ServeProto::BasicClientConnection::handshake( return remoteVersion; } +ServeProto::Version ServeProto::BasicServerConnection::handshake( + BufferedSink & to, + Source & from, + ServeProto::Version localVersion) +{ + unsigned int magic = readInt(from); + if (magic != SERVE_MAGIC_1) throw Error("protocol mismatch"); + to << SERVE_MAGIC_2 << localVersion; + to.flush(); + return readInt(from); +} + StorePathSet ServeProto::BasicClientConnection::queryValidPaths( const Store & store, diff --git a/src/libstore/serve-protocol-impl.hh b/src/libstore/serve-protocol-impl.hh index 8cd241fd3..fd8d94697 100644 --- a/src/libstore/serve-protocol-impl.hh +++ b/src/libstore/serve-protocol-impl.hh @@ -132,4 +132,26 @@ struct ServeProto::BasicClientConnection const ServeProto::BuildOptions & options); }; +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); +}; + } diff --git a/src/libstore/serve-protocol.hh b/src/libstore/serve-protocol.hh index 632c4b6bd..8c112bb74 100644 --- a/src/libstore/serve-protocol.hh +++ b/src/libstore/serve-protocol.hh @@ -65,6 +65,7 @@ struct ServeProto * @todo remove once Hydra uses Store abstraction consistently. */ struct BasicClientConnection; + struct BasicServerConnection; /** * Data type for canonical pairs of serialisers for the serve protocol. diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index 0a0a3ab1a..40378e123 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -828,11 +828,9 @@ static void opServe(Strings opFlags, Strings opArgs) FdSink out(STDOUT_FILENO); /* Exchange the greeting. */ - unsigned int magic = readInt(in); - if (magic != SERVE_MAGIC_1) throw Error("protocol mismatch"); - out << SERVE_MAGIC_2 << SERVE_PROTOCOL_VERSION; - out.flush(); - ServeProto::Version clientVersion = readInt(in); + ServeProto::Version clientVersion = + ServeProto::BasicServerConnection::handshake( + out, in, SERVE_PROTOCOL_VERSION); ServeProto::ReadConn rconn { .from = in,