Factor our `ServeProto::BasicServerConnection::handshake`

We'll need this for unit testing.

Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
This commit is contained in:
John Ericson 2024-01-19 16:38:08 -05:00
parent 4a5ca576da
commit e960b28230
4 changed files with 38 additions and 5 deletions

View File

@ -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,

View File

@ -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);
};
}

View File

@ -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.

View File

@ -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,