Create struct for client agent
This commit is contained in:
parent
3f6b55dcc4
commit
b6c3c69777
2 changed files with 48 additions and 33 deletions
|
@ -16,7 +16,7 @@ use ws::{Message, WebSocket};
|
|||
use path::Tail;
|
||||
|
||||
use net::{
|
||||
agent::run_client,
|
||||
agent::ClientAgent,
|
||||
server::{Handle, Server},
|
||||
ClientMessage, ServerMessage,
|
||||
};
|
||||
|
@ -114,5 +114,5 @@ async fn handle_socket(game_server: Handle, websocket: WebSocket) -> Result<()>
|
|||
})
|
||||
});
|
||||
|
||||
run_client(game_server, &mut websocket).await
|
||||
ClientAgent::new(game_server).run(&mut websocket).await
|
||||
}
|
||||
|
|
|
@ -4,40 +4,55 @@ use futures::{channel::mpsc::*, prelude::*, Stream};
|
|||
use super::server::Handle;
|
||||
use super::*;
|
||||
|
||||
pub async fn run_client(
|
||||
handle: Handle,
|
||||
socket: &mut (impl Stream<Item = Result<ClientMessage, Error>>
|
||||
+ Sink<ServerMessage, Error = Error>
|
||||
+ Send
|
||||
+ Unpin),
|
||||
) -> Result<()> {
|
||||
let (sender, mut receiver) = channel(CHANNEL_BUFFER);
|
||||
pub struct ClientAgent {
|
||||
server: Handle,
|
||||
}
|
||||
|
||||
// register player
|
||||
let player_id = handle.lock().await.add_player(sender, &handle).await?;
|
||||
impl ClientAgent {
|
||||
pub fn new(server: Handle) -> ClientAgent {
|
||||
ClientAgent { server }
|
||||
}
|
||||
|
||||
// main message loop
|
||||
let result: Result<()> = async {
|
||||
loop {
|
||||
tokio::select! {
|
||||
client_message = socket.next() => {
|
||||
match client_message {
|
||||
Some(msg) =>
|
||||
handle.lock().await.process_message(player_id, msg?),
|
||||
None => break Ok(()),
|
||||
}
|
||||
},
|
||||
Some(server_message) = receiver.next() => {
|
||||
socket.send(server_message).await?
|
||||
},
|
||||
else => break Ok(()),
|
||||
pub async fn run(
|
||||
&mut self,
|
||||
socket: &mut (impl Stream<Item = Result<ClientMessage, Error>>
|
||||
+ Sink<ServerMessage, Error = Error>
|
||||
+ Send
|
||||
+ Unpin),
|
||||
) -> Result<()> {
|
||||
let (sender, mut receiver) = channel(CHANNEL_BUFFER);
|
||||
|
||||
// register player
|
||||
let player_id = self
|
||||
.server
|
||||
.lock()
|
||||
.await
|
||||
.add_player(sender, &self.server)
|
||||
.await?;
|
||||
|
||||
// main message loop
|
||||
let result: Result<()> = async {
|
||||
loop {
|
||||
tokio::select! {
|
||||
client_message = socket.next() => {
|
||||
match client_message {
|
||||
Some(msg) =>
|
||||
self.server.lock().await.process_message(player_id, msg?),
|
||||
None => break Ok(()),
|
||||
}
|
||||
},
|
||||
Some(server_message) = receiver.next() => {
|
||||
socket.send(server_message).await?
|
||||
},
|
||||
else => break Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
.await;
|
||||
|
||||
// deregister player, whether normally or due to error
|
||||
self.server.lock().await.remove_player(player_id);
|
||||
|
||||
result
|
||||
}
|
||||
.await;
|
||||
|
||||
// deregister player, whether normally or due to error
|
||||
handle.lock().await.remove_player(player_id);
|
||||
|
||||
result
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue