fix(handler): do call handler methods when initialize server (#118)
This commit is contained in:
parent
d0962ec9ee
commit
31da5ff02d
3 changed files with 40 additions and 14 deletions
|
|
@ -504,15 +504,16 @@ where
|
||||||
T: IntoTransport<R, E, A>,
|
T: IntoTransport<R, E, A>,
|
||||||
E: std::error::Error + Send + Sync + 'static,
|
E: std::error::Error + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
serve_inner(service, transport, peer_info, Default::default(), ct).await
|
let (peer, peer_rx) = Peer::new(Arc::new(AtomicU32RequestIdProvider::default()), peer_info);
|
||||||
|
serve_inner(service, transport, peer, peer_rx, ct).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
async fn serve_inner<R, S, T, E, A>(
|
async fn serve_inner<R, S, T, E, A>(
|
||||||
mut service: S,
|
mut service: S,
|
||||||
transport: T,
|
transport: T,
|
||||||
peer_info: R::PeerInfo,
|
peer: Peer<R>,
|
||||||
id_provider: Arc<AtomicU32Provider>,
|
mut peer_rx: tokio::sync::mpsc::Receiver<PeerSinkMessage<R>>,
|
||||||
ct: CancellationToken,
|
ct: CancellationToken,
|
||||||
) -> Result<RunningService<R, S>, E>
|
) -> Result<RunningService<R, S>, E>
|
||||||
where
|
where
|
||||||
|
|
@ -525,14 +526,13 @@ where
|
||||||
const SINK_PROXY_BUFFER_SIZE: usize = 64;
|
const SINK_PROXY_BUFFER_SIZE: usize = 64;
|
||||||
let (sink_proxy_tx, mut sink_proxy_rx) =
|
let (sink_proxy_tx, mut sink_proxy_rx) =
|
||||||
tokio::sync::mpsc::channel::<TxJsonRpcMessage<R>>(SINK_PROXY_BUFFER_SIZE);
|
tokio::sync::mpsc::channel::<TxJsonRpcMessage<R>>(SINK_PROXY_BUFFER_SIZE);
|
||||||
|
let peer_info = peer.peer_info();
|
||||||
if R::IS_CLIENT {
|
if R::IS_CLIENT {
|
||||||
tracing::info!(?peer_info, "Service initialized as client");
|
tracing::info!(?peer_info, "Service initialized as client");
|
||||||
} else {
|
} else {
|
||||||
tracing::info!(?peer_info, "Service initialized as server");
|
tracing::info!(?peer_info, "Service initialized as server");
|
||||||
}
|
}
|
||||||
|
|
||||||
let (peer, mut peer_proxy) = <Peer<R>>::new(id_provider, peer_info);
|
|
||||||
service.set_peer(peer.clone());
|
service.set_peer(peer.clone());
|
||||||
let mut local_responder_pool = HashMap::new();
|
let mut local_responder_pool = HashMap::new();
|
||||||
let mut local_ct_pool = HashMap::<RequestId, CancellationToken>::new();
|
let mut local_ct_pool = HashMap::<RequestId, CancellationToken>::new();
|
||||||
|
|
@ -576,7 +576,7 @@ where
|
||||||
break QuitReason::Closed
|
break QuitReason::Closed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m = peer_proxy.recv() => {
|
m = peer_rx.recv() => {
|
||||||
if let Some(m) = m {
|
if let Some(m) = m {
|
||||||
Event::ProxyMessage(m)
|
Event::ProxyMessage(m)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -174,7 +174,8 @@ where
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
sink.send(notification).await?;
|
sink.send(notification).await?;
|
||||||
serve_inner(service, (sink, stream), initialize_result, id_provider, ct).await
|
let (peer, peer_rx) = Peer::new(id_provider, initialize_result);
|
||||||
|
serve_inner(service, (sink, stream), peer, peer_rx, ct).await
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! method {
|
macro_rules! method {
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use super::*;
|
||||||
use crate::model::{
|
use crate::model::{
|
||||||
CancelledNotification, CancelledNotificationParam, ClientInfo, ClientJsonRpcMessage,
|
CancelledNotification, CancelledNotificationParam, ClientInfo, ClientJsonRpcMessage,
|
||||||
ClientNotification, ClientRequest, ClientResult, CreateMessageRequest,
|
ClientNotification, ClientRequest, ClientResult, CreateMessageRequest,
|
||||||
CreateMessageRequestParam, CreateMessageResult, ListRootsRequest, ListRootsResult,
|
CreateMessageRequestParam, CreateMessageResult, ErrorData, ListRootsRequest, ListRootsResult,
|
||||||
LoggingMessageNotification, LoggingMessageNotificationParam, ProgressNotification,
|
LoggingMessageNotification, LoggingMessageNotificationParam, ProgressNotification,
|
||||||
ProgressNotificationParam, PromptListChangedNotification, ResourceListChangedNotification,
|
ProgressNotificationParam, PromptListChangedNotification, ResourceListChangedNotification,
|
||||||
ResourceUpdatedNotification, ResourceUpdatedNotificationParam, ServerInfo, ServerNotification,
|
ResourceUpdatedNotification, ResourceUpdatedNotificationParam, ServerInfo, ServerNotification,
|
||||||
|
|
@ -41,6 +41,12 @@ pub enum ServerError {
|
||||||
#[error("connection closed: {0}")]
|
#[error("connection closed: {0}")]
|
||||||
ConnectionClosed(String),
|
ConnectionClosed(String),
|
||||||
|
|
||||||
|
#[error("unexpected initialize result: {0:?}")]
|
||||||
|
UnexpectedInitializeResponse(ServerResult),
|
||||||
|
|
||||||
|
#[error("initialize failed: {0}")]
|
||||||
|
InitializeFailed(ErrorData),
|
||||||
|
|
||||||
#[error("IO error: {0}")]
|
#[error("IO error: {0}")]
|
||||||
Io(#[from] std::io::Error),
|
Io(#[from] std::io::Error),
|
||||||
}
|
}
|
||||||
|
|
@ -144,14 +150,34 @@ where
|
||||||
.await
|
.await
|
||||||
.map_err(handle_server_error)?;
|
.map_err(handle_server_error)?;
|
||||||
|
|
||||||
let ClientRequest::InitializeRequest(peer_info) = request else {
|
let ClientRequest::InitializeRequest(peer_info) = &request else {
|
||||||
return Err(handle_server_error(ServerError::ExpectedInitRequest(Some(
|
return Err(handle_server_error(ServerError::ExpectedInitRequest(Some(
|
||||||
ClientJsonRpcMessage::request(request, id),
|
ClientJsonRpcMessage::request(request, id),
|
||||||
))));
|
))));
|
||||||
};
|
};
|
||||||
|
let (peer, peer_rx) = Peer::new(id_provider, peer_info.params.clone());
|
||||||
|
let context = RequestContext {
|
||||||
|
ct: ct.child_token(),
|
||||||
|
id: id.clone(),
|
||||||
|
meta: request.get_meta().clone(),
|
||||||
|
extensions: request.extensions().clone(),
|
||||||
|
peer: peer.clone(),
|
||||||
|
};
|
||||||
// Send initialize response
|
// Send initialize response
|
||||||
let mut init_response = service.get_info();
|
let init_response = service.handle_request(request.clone(), context).await;
|
||||||
|
let mut init_response = match init_response {
|
||||||
|
Ok(ServerResult::InitializeResult(init_response)) => init_response,
|
||||||
|
Ok(result) => {
|
||||||
|
return Err(handle_server_error(
|
||||||
|
ServerError::UnexpectedInitializeResponse(result),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
sink.send(ServerJsonRpcMessage::error(e.clone(), id))
|
||||||
|
.await?;
|
||||||
|
return Err(handle_server_error(ServerError::InitializeFailed(e)));
|
||||||
|
}
|
||||||
|
};
|
||||||
let protocol_version = match peer_info
|
let protocol_version = match peer_info
|
||||||
.params
|
.params
|
||||||
.protocol_version
|
.protocol_version
|
||||||
|
|
@ -174,15 +200,14 @@ where
|
||||||
let notification = expect_notification(&mut stream, "initialize notification")
|
let notification = expect_notification(&mut stream, "initialize notification")
|
||||||
.await
|
.await
|
||||||
.map_err(handle_server_error)?;
|
.map_err(handle_server_error)?;
|
||||||
|
|
||||||
let ClientNotification::InitializedNotification(_) = notification else {
|
let ClientNotification::InitializedNotification(_) = notification else {
|
||||||
return Err(handle_server_error(ServerError::ExpectedInitNotification(
|
return Err(handle_server_error(ServerError::ExpectedInitNotification(
|
||||||
Some(ClientJsonRpcMessage::notification(notification)),
|
Some(ClientJsonRpcMessage::notification(notification)),
|
||||||
)));
|
)));
|
||||||
};
|
};
|
||||||
|
let _ = service.handle_notification(notification).await;
|
||||||
// Continue processing service
|
// Continue processing service
|
||||||
serve_inner(service, (sink, stream), peer_info.params, id_provider, ct).await
|
serve_inner(service, (sink, stream), peer, peer_rx, ct).await
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! method {
|
macro_rules! method {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue