Skip to content

Commit

Permalink
feat: order id
Browse files Browse the repository at this point in the history
  • Loading branch information
i007c committed Dec 1, 2024
1 parent 731e55b commit 02f799f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion shah-macros/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ pub(crate) fn api(args: TokenStream, code: TokenStream) -> TokenStream {

#input_result

let reply = taker.take(&order)?;
let reply = taker.take(&mut order)?;
// let reply_head = taker.reply_head();
// let reply_body = taker.reply_body(reply_head.size as usize);
Ok((#output_result))
Expand Down
1 change: 1 addition & 0 deletions shah/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl From<std::io::Error> for ErrorCode {
#[derive(Debug, Clone, Copy)]
#[repr(u16)]
pub enum SystemError {
BadOrderId,
Database,
Io,
ZeroGeneId,
Expand Down
5 changes: 2 additions & 3 deletions shah/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,13 @@ pub struct OrderHead {
pub scope: u8,
pub route: u8,
_pad: [u8; 2],
pub id: u64,
}

#[crate::model]
#[derive(Debug)]
pub struct ReplyHead {
pub scope: u8,
pub route: u8,
_pad: [u8; 6],
pub id: u64,
pub size: u32,
pub error: u32,
pub elapsed: u64,
Expand Down
3 changes: 1 addition & 2 deletions shah/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ pub fn run<T: Debug>(
let order_head = OrderHead::from_binary(order_head);
let order_body = &order_body[..order_size - OrderHead::S];

reply.head.route = order_head.route;
reply.head.scope = order_head.scope;
reply.head.id = order_head.id;

let route = match routes
.get(order_head.scope as usize)
Expand Down
11 changes: 8 additions & 3 deletions shah/src/taker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
time::Duration,
};

use crate::{Binary, ErrorCode, Reply};
use crate::{error::SystemError, Binary, ErrorCode, OrderHead, Reply};

/// Order Taker
pub struct Taker {
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Taker {
// &self.reply[ReplyHead::S..ReplyHead::S + size]
// }

pub fn take(&self, order: &[u8]) -> Result<Reply, ErrorCode> {
pub fn take(&self, order: &mut [u8]) -> Result<Reply, ErrorCode> {
let mut reply = Reply::default();
// self.reply[0..ReplyHead::S].fill(0);

Expand All @@ -44,6 +44,8 @@ impl Taker {
*count = 0;
}
*count += 1;
let order_head = OrderHead::from_binary_mut(order);
order_head.id = *count;

if let Err(e) = self.conn.send(order) {
log::error!("send error: {e:#?}");
Expand All @@ -62,7 +64,10 @@ impl Taker {
}
}
self.conn.recv(reply.as_binary_mut())?;
drop(count);

if reply.head.id != *count {
Err(SystemError::BadOrderId)?;
}

// let order_head = OrderHead::from_binary(order);
// assert_eq!(reply.head.scope, order_head.scope);
Expand Down

0 comments on commit 02f799f

Please sign in to comment.