This commit is contained in:
ntr 2019-06-12 23:40:28 +10:00
parent 59320da37d
commit abe8a445ae
2 changed files with 10 additions and 3 deletions

View File

@ -44,6 +44,7 @@ impl Actor for MnmlSocket {
impl StreamHandler<ws::Message, ws::ProtocolError> for MnmlSocket { impl StreamHandler<ws::Message, ws::ProtocolError> for MnmlSocket {
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
// process websocket messages // process websocket messages
let begin = Instant::now();
println!("msg: {:?}", msg); println!("msg: {:?}", msg);
match msg { match msg {
ws::Message::Ping(msg) => { ws::Message::Ping(msg) => {
@ -60,7 +61,7 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for MnmlSocket {
ws::Message::Nop => (), ws::Message::Nop => (),
ws::Message::Binary(bin) => { ws::Message::Binary(bin) => {
let db_connection = self.pool.get().expect("unable to get db connection"); let db_connection = self.pool.get().expect("unable to get db connection");
match receive(bin.to_vec(), &db_connection, ctx) { match receive(bin.to_vec(), &db_connection, ctx, begin) {
Ok(reply) => { Ok(reply) => {
let response = to_vec(&reply) let response = to_vec(&reply)
.expect("failed to serialize response"); .expect("failed to serialize response");

View File

@ -1,7 +1,9 @@
use std::time::{Instant};
use actix_web_actors::ws; use actix_web_actors::ws;
use postgres::transaction::Transaction; use postgres::transaction::Transaction;
use serde_cbor::{from_slice, to_vec}; use serde_cbor::{from_slice};
use uuid::Uuid; use uuid::Uuid;
use failure::Error; use failure::Error;
use failure::err_msg; use failure::err_msg;
@ -18,7 +20,7 @@ use item::{Item, ItemInfoCtr, item_info};
type MnmlWs = ws::WebsocketContext<MnmlSocket>; type MnmlWs = ws::WebsocketContext<MnmlSocket>;
pub fn receive(data: Vec<u8>, db: &Db, client: &mut MnmlWs) -> Result<RpcResponse, Error> { pub fn receive(data: Vec<u8>, db: &Db, client: &mut MnmlWs, begin: Instant) -> Result<RpcResponse, Error> {
// cast the data to this type to receive method name // cast the data to this type to receive method name
match from_slice::<RpcMessage>(&data) { match from_slice::<RpcMessage>(&data) {
Ok(v) => { Ok(v) => {
@ -87,6 +89,8 @@ pub fn receive(data: Vec<u8>, db: &Db, client: &mut MnmlWs) -> Result<RpcRespons
tx.commit()?; tx.commit()?;
info!("method={:?} account={:?} duration={:?}", v.method, account_name, begin.elapsed());
return response; return response;
}, },
Err(e) => { Err(e) => {
@ -144,6 +148,8 @@ fn handle_game_ready(data: Vec<u8>, tx: &mut Transaction, account: Account, _cli
fn handle_construct_spawn(data: Vec<u8>, tx: &mut Transaction, account: Account, client: &mut MnmlWs) -> Result<RpcResponse, Error> { fn handle_construct_spawn(data: Vec<u8>, tx: &mut Transaction, account: Account, client: &mut MnmlWs) -> Result<RpcResponse, Error> {
let msg = from_slice::<ConstructSpawnMsg>(&data).or(Err(err_msg("invalid params")))?; let msg = from_slice::<ConstructSpawnMsg>(&data).or(Err(err_msg("invalid params")))?;
construct_spawn(msg.params, tx, &account)?;
let construct_list = RpcResponse { let construct_list = RpcResponse {
method: "account_constructs".to_string(), method: "account_constructs".to_string(),
params: RpcResult::ConstructList(account_constructs(tx, &account)?) params: RpcResult::ConstructList(account_constructs(tx, &account)?)