refactor server

This commit is contained in:
Robert Jördens 2019-05-28 10:59:26 +00:00
parent 1cff400d00
commit b0921f67eb
1 changed files with 51 additions and 34 deletions

View File

@ -652,6 +652,7 @@ fn main() -> ! {
}); });
let mut last = 0; let mut last = 0;
let mut server = Server::new();
loop { loop {
// if ETHERNET_PENDING.swap(false, Ordering::Relaxed) { } // if ETHERNET_PENDING.swap(false, Ordering::Relaxed) { }
let time = TIME.load(Ordering::Relaxed); let time = TIME.load(Ordering::Relaxed);
@ -661,14 +662,7 @@ fn main() -> ! {
socket.listen(1234).unwrap_or_else(|e| warn!("TCP listen error: {:?}", e)); socket.listen(1234).unwrap_or_else(|e| warn!("TCP listen error: {:?}", e));
} else if last != time && socket.can_send() { } else if last != time && socket.can_send() {
last = time; last = time;
let s = unsafe { Status{ handle_status(socket, time);
t: time,
x0: IIR_STATE[0][0],
y0: IIR_STATE[0][2],
x1: IIR_STATE[1][0],
y1: IIR_STATE[1][2],
}};
send_response(socket, &s);
} }
} }
{ {
@ -676,7 +670,7 @@ fn main() -> ! {
if !(socket.is_open() || socket.is_listening()) { if !(socket.is_open() || socket.is_listening()) {
socket.listen(1235).unwrap_or_else(|e| warn!("TCP listen error: {:?}", e)); socket.listen(1235).unwrap_or_else(|e| warn!("TCP listen error: {:?}", e));
} else { } else {
handle_command(socket); server.handle_command(socket);
} }
} }
@ -702,37 +696,48 @@ struct Response<'a> {
message: &'a str, message: &'a str,
} }
fn send_response<T: Serialize>(socket: &mut net::socket::TcpSocket, msg: &T) { fn reply<T: Serialize>(socket: &mut net::socket::TcpSocket, msg: &T) {
let mut u: String<U128> = to_string(msg).unwrap(); let mut u: String<U128> = to_string(msg).unwrap();
u.push('\n').unwrap(); u.push('\n').unwrap();
socket.write_str(&u).unwrap(); socket.write_str(&u).unwrap();
} }
fn handle_command(socket: &mut net::socket::TcpSocket) { struct Server {
let mut data: Vec<u8, U256> = Vec::new(); data: Vec<u8, U256>,
let mut discard: bool = false; discard: bool,
while socket.can_recv() { }
if socket.recv(|buf| {
let (len, found) = match buf.iter().position(|&c| c as char == '\n') { impl Server {
Some(end) => (end + 1, true), fn new() -> Self {
None => (buf.len(), false), Self { data: Vec::new(), discard: false }
}; }
if data.len() + len >= data.capacity() {
discard = true; fn handle_command(&mut self, socket: &mut net::socket::TcpSocket) {
data.clear(); while socket.can_recv() {
} else if !discard && len > 0 { let found = socket.recv(|buf| {
data.extend_from_slice(&buf[..len - 1]).unwrap(); let (len, found) = match buf.iter().position(|&c| c as char == '\n') {
Some(end) => (end + 1, true),
None => (buf.len(), false),
};
if self.data.len() + len >= self.data.capacity() {
self.discard = true;
self.data.clear();
} else if !self.discard && len > 0 {
self.data.extend_from_slice(&buf[..len - 1]).unwrap();
}
(len, found)
}).unwrap();
if !found {
continue;
} }
(len, found) let resp = if self.discard {
}).unwrap() { self.discard = false;
let resp = if discard { Response{ code: 520, message: "command buffer overflow" }
discard = false;
Response{ code: 500, message: "command buffer overflow" }
} else { } else {
match from_slice::<Request>(&data) { match from_slice::<Request>(&self.data) {
Ok(request) => { Ok(request) => {
if request.channel > 1 { if request.channel > 1 {
Response{ code: 500, message: "invalid channel" } Response{ code: 530, message: "invalid channel" }
} else { } else {
cortex_m::interrupt::free(|_| { cortex_m::interrupt::free(|_| {
unsafe { IIR_CH[request.channel as usize] = request.iir; }; unsafe { IIR_CH[request.channel as usize] = request.iir; };
@ -741,17 +746,29 @@ fn handle_command(socket: &mut net::socket::TcpSocket) {
} }
}, },
Err(err) => { Err(err) => {
warn!("parse error {}", err); warn!("parse error {:?}", err);
Response{ code: 550, message: "parse error" } Response{ code: 550, message: "parse error" }
}, },
} }
}; };
send_response(socket, &resp); self.data.clear();
reply(socket, &resp);
socket.close(); socket.close();
} }
} }
} }
fn handle_status(socket: &mut net::socket::TcpSocket, time: u32) {
let s = unsafe { Status{
t: time,
x0: IIR_STATE[0][0],
y0: IIR_STATE[0][2],
x1: IIR_STATE[1][0],
y1: IIR_STATE[1][2],
}};
reply(socket, &s);
}
#[derive(Serialize)] #[derive(Serialize)]
struct Status { struct Status {
t: u32, t: u32,
@ -765,7 +782,7 @@ const SCALE: f32 = ((1 << 15) - 1) as f32;
static mut IIR_STATE: [IIRState; 2] = [[0.; 5]; 2]; static mut IIR_STATE: [IIRState; 2] = [[0.; 5]; 2];
static mut IIR_CH: [IIR; 2] = [ static mut IIR_CH: [IIR; 2] = [
IIR{ ba: [0., 0., 0., 0., 0.], y_offset: 0., IIR{ ba: [0., 0., 0., 0., 0.], y_offset: 0.,
y_min: -SCALE, y_max: SCALE }; 2]; y_min: -SCALE - 1., y_max: SCALE }; 2];
// seems to slow it down // seems to slow it down
// #[link_section = ".data.spi1"] // #[link_section = ".data.spi1"]