feat(proto): add client handshake implementation

This commit is contained in:
Jindřich Moravec 2023-12-23 00:29:40 +01:00
parent df4c4166d9
commit 7b2dce4dfb
6 changed files with 116 additions and 0 deletions

View file

@ -0,0 +1,34 @@
use crate::handshake::client::error::ClientHandshakeError;
use crate::handshake::client::request::ClientHandshakeRequest;
use crate::handshake::client::response::ClientHandshakeResponse;
use crate::message::backend::{AuthenticationOkData, BackendMessage};
use crate::message::special::StartupMessageData;
use crate::reader::backend::BackendProtoReader;
use crate::writer::frontend::FrontendProtoWriter;
use crate::writer::protowriter::ProtoFlush;
pub async fn do_client_handshake(
writer: &mut (impl FrontendProtoWriter + ProtoFlush),
reader: &mut impl BackendProtoReader,
request: ClientHandshakeRequest,
) -> Result<ClientHandshakeResponse, ClientHandshakeError> {
let startup_message: StartupMessageData = request.into();
writer.write_startup_message(startup_message).await?;
let auth = reader.read_proto().await?;
if !matches!(auth, BackendMessage::AuthenticationOk(AuthenticationOkData { status: 0 })) {
return Err(ClientHandshakeError::UnexpectedAuthResponse(auth));
}
let mut messages = Vec::new();
loop {
let msg = reader.read_proto().await?;
if matches!(msg, BackendMessage::ReadyForQuery(_)) {
break;
}
messages.push(msg);
}
ClientHandshakeResponse::from_backend_messages(&messages)
}