Skip to content

Commit

Permalink
fix: change server's state after sending (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
lauti7 authored May 2, 2023
1 parent 5fec703 commit 113e0da
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dcl-rpc"
version = "2.0.1"
version = "2.1.1"
edition = "2021"
description = "Decentraland RPC Implementation"
repository = "https://github.com/decentraland/rpc-rust"
Expand Down
23 changes: 13 additions & 10 deletions rpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,6 @@ impl<Context: Send + Sync + 'static, T: Transport + ?Sized + 'static> RpcServer<
payload: Vec<u8>,
) -> ServerResult<()> {
let port_id = self.next_port_id;
self.next_port_id += 1;
let create_port = CreatePort::decode(payload.as_slice())
.map_err(|_| ServerResultError::External(ServerError::ProtocolError))?;
let port_name = create_port.port_name;
Expand All @@ -671,12 +670,6 @@ impl<Context: Send + Sync + 'static, T: Transport + ?Sized + 'static> RpcServer<
handler(&mut port);
}

self.ports.insert(port_id, port);
self.ports_by_transport_id
.entry(transport_id)
.and_modify(|ports| ports.push(port_id))
.or_insert_with(|| vec![port_id]);

let response = CreatePortResponse {
message_identifier: build_message_identifier(
RpcMessageTypes::CreatePortResponse as u32,
Expand All @@ -685,11 +678,19 @@ impl<Context: Send + Sync + 'static, T: Transport + ?Sized + 'static> RpcServer<
port_id,
};
let response = response.encode_to_vec();

transport
.send(response)
.await
.map_err(|_| ServerResultError::Internal(ServerInternalError::TransportError))?;

self.next_port_id += 1;
self.ports.insert(port_id, port);
self.ports_by_transport_id
.entry(transport_id)
.and_modify(|ports| ports.push(port_id))
.or_insert_with(|| vec![port_id]);

Ok(())
}

Expand Down Expand Up @@ -785,6 +786,8 @@ pub struct RpcServerPort<Context> {
loaded_modules: HashMap<String, ServerModuleDeclaration>,
/// Procedures contains the id and the handler for each procedure
procedures: HashMap<u32, ProcedureDefinition<Context>>,
/// Global Procedure ID
next_procedure_id: u32,
}

impl<Context> RpcServerPort<Context> {
Expand All @@ -794,6 +797,7 @@ impl<Context> RpcServerPort<Context> {
registered_modules: HashMap::new(),
loaded_modules: HashMap::new(),
procedures: HashMap::new(),
next_procedure_id: 1,
}
}

Expand Down Expand Up @@ -827,10 +831,9 @@ impl<Context> RpcServerPort<Context> {
};

let definitions = module_generator.get_definitions();
let mut procedure_id = 1;

for (procedure_name, procedure_definition) in definitions {
let current_id = procedure_id;
let current_id = self.next_procedure_id;
self.procedures
.insert(current_id, procedure_definition.clone());
server_module_declaration
Expand All @@ -839,7 +842,7 @@ impl<Context> RpcServerPort<Context> {
procedure_name: procedure_name.clone(),
procedure_id: current_id,
});
procedure_id += 1
self.next_procedure_id += 1;
}

self.loaded_modules
Expand Down

0 comments on commit 113e0da

Please sign in to comment.