2014-09-19 17:15:03 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2015-01-19 18:33:48 +03:00
|
|
|
//! Low-level wire protocol implementation. Currently only supports
|
|
|
|
//! [JSON packets]
|
|
|
|
//! (https://wiki.mozilla.org/Remote_Debugging_Protocol_Stream_Transport#JSON_Packets).
|
2014-09-19 17:15:03 +04:00
|
|
|
|
2016-03-31 16:06:13 +03:00
|
|
|
use serde::Serialize;
|
|
|
|
use serde_json::{self, Value};
|
2015-08-06 23:00:26 +03:00
|
|
|
use std::error::Error;
|
|
|
|
use std::io::{Read, Write};
|
2015-03-25 18:03:51 +03:00
|
|
|
use std::net::TcpStream;
|
2014-09-19 17:15:03 +04:00
|
|
|
|
2014-11-13 00:42:35 +03:00
|
|
|
pub trait JsonPacketStream {
|
2016-03-31 16:06:13 +03:00
|
|
|
fn write_json_packet<T: Serialize>(&mut self, obj: &T);
|
|
|
|
fn read_json_packet(&mut self) -> Result<Option<Value>, String>;
|
2014-09-19 17:15:03 +04:00
|
|
|
}
|
|
|
|
|
2014-11-13 00:42:35 +03:00
|
|
|
impl JsonPacketStream for TcpStream {
|
2016-03-31 16:06:13 +03:00
|
|
|
fn write_json_packet<T: Serialize>(&mut self, obj: &T) {
|
|
|
|
let s = serde_json::to_string(obj).unwrap();
|
2015-01-04 00:39:46 +03:00
|
|
|
println!("<- {}", s);
|
2015-09-02 21:32:44 +03:00
|
|
|
write!(self, "{}:{}", s.len(), s).unwrap();
|
2014-09-19 17:15:03 +04:00
|
|
|
}
|
2014-11-13 00:42:35 +03:00
|
|
|
|
2016-03-31 16:06:13 +03:00
|
|
|
fn read_json_packet(&mut self) -> Result<Option<Value>, String> {
|
2014-11-13 00:42:35 +03:00
|
|
|
// https://wiki.mozilla.org/Remote_Debugging_Protocol_Stream_Transport
|
|
|
|
// In short, each JSON packet is [ascii length]:[JSON data of given length]
|
|
|
|
let mut buffer = vec!();
|
|
|
|
loop {
|
2015-03-25 18:03:51 +03:00
|
|
|
let mut buf = [0];
|
2015-08-06 23:00:26 +03:00
|
|
|
let byte = match self.read(&mut buf) {
|
|
|
|
Ok(0) => return Ok(None), // EOF
|
|
|
|
Ok(1) => buf[0],
|
|
|
|
Ok(_) => unreachable!(),
|
2015-09-02 21:32:44 +03:00
|
|
|
Err(e) => return Err(e.description().to_owned()),
|
2015-03-25 18:03:51 +03:00
|
|
|
};
|
|
|
|
match byte {
|
|
|
|
b':' => {
|
2015-08-06 23:00:26 +03:00
|
|
|
let packet_len_str = match String::from_utf8(buffer) {
|
|
|
|
Ok(packet_len) => packet_len,
|
2015-09-02 21:32:44 +03:00
|
|
|
Err(_) => return Err("nonvalid UTF8 in packet length".to_owned()),
|
2015-08-06 23:00:26 +03:00
|
|
|
};
|
|
|
|
let packet_len = match u64::from_str_radix(&packet_len_str, 10) {
|
|
|
|
Ok(packet_len) => packet_len,
|
2015-09-02 21:32:44 +03:00
|
|
|
Err(_) => return Err("packet length missing / not parsable".to_owned()),
|
2015-08-06 23:00:26 +03:00
|
|
|
};
|
2015-03-25 18:03:51 +03:00
|
|
|
let mut packet = String::new();
|
|
|
|
self.take(packet_len).read_to_string(&mut packet).unwrap();
|
2015-01-04 00:39:46 +03:00
|
|
|
println!("{}", packet);
|
2016-03-31 16:06:13 +03:00
|
|
|
return match serde_json::from_str(&packet) {
|
2015-08-06 23:00:26 +03:00
|
|
|
Ok(json) => Ok(Some(json)),
|
|
|
|
Err(err) => match err {
|
2016-03-31 16:06:13 +03:00
|
|
|
serde_json::Error::Io(ioerr) => {
|
|
|
|
return Err(ioerr.description().to_owned())
|
|
|
|
},
|
|
|
|
serde_json::Error::Syntax(_, l, c) => {
|
|
|
|
return Err(format!("syntax at {}:{}", l, c))
|
|
|
|
},
|
|
|
|
serde_json::Error::FromUtf8(e) => {
|
|
|
|
return Err(e.description().to_owned())
|
|
|
|
},
|
2015-08-06 23:00:26 +03:00
|
|
|
},
|
|
|
|
};
|
2014-11-13 00:42:35 +03:00
|
|
|
},
|
2015-03-25 18:03:51 +03:00
|
|
|
c => buffer.push(c),
|
2014-11-13 00:42:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-19 17:15:03 +04:00
|
|
|
}
|