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
|
|
|
|
2015-02-17 17:30:50 +03:00
|
|
|
use rustc_serialize::{json, Encodable};
|
|
|
|
use rustc_serialize::json::Json;
|
2015-02-12 03:24:45 +03:00
|
|
|
use std::old_io::{IoError, OtherIoError, EndOfFile, TcpStream, IoResult};
|
2014-11-13 00:42:35 +03:00
|
|
|
use std::num;
|
2014-09-19 17:15:03 +04:00
|
|
|
|
2014-11-13 00:42:35 +03:00
|
|
|
pub trait JsonPacketStream {
|
2015-01-28 04:15:50 +03:00
|
|
|
fn write_json_packet<'a, T: Encodable>(&mut self, obj: &T);
|
2015-01-08 18:00:57 +03:00
|
|
|
fn read_json_packet(&mut self) -> IoResult<Json>;
|
2014-09-19 17:15:03 +04:00
|
|
|
}
|
|
|
|
|
2014-11-13 00:42:35 +03:00
|
|
|
impl JsonPacketStream for TcpStream {
|
2015-01-28 04:15:50 +03:00
|
|
|
fn write_json_packet<'a, T: Encodable>(&mut self, obj: &T) {
|
2015-02-12 03:24:45 +03:00
|
|
|
let s = json::encode(obj).unwrap().replace("__type__", "type");
|
2015-01-04 00:39:46 +03:00
|
|
|
println!("<- {}", s);
|
2015-03-21 21:57:46 +03:00
|
|
|
self.write_str(&s.len().to_string()).unwrap();
|
2014-09-19 17:15:03 +04:00
|
|
|
self.write_u8(':' as u8).unwrap();
|
2015-03-21 21:57:46 +03:00
|
|
|
self.write_str(&s).unwrap();
|
2014-09-19 17:15:03 +04:00
|
|
|
}
|
2014-11-13 00:42:35 +03:00
|
|
|
|
2015-01-08 18:00:57 +03:00
|
|
|
fn read_json_packet<'a>(&mut self) -> IoResult<Json> {
|
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 {
|
|
|
|
let colon = ':' as u8;
|
|
|
|
match self.read_byte() {
|
|
|
|
Ok(c) if c != colon => buffer.push(c as u8),
|
|
|
|
Ok(_) => {
|
|
|
|
let packet_len_str = String::from_utf8(buffer).unwrap();
|
2015-03-21 21:57:46 +03:00
|
|
|
let packet_len = num::from_str_radix(&packet_len_str, 10).unwrap();
|
2014-11-13 00:42:35 +03:00
|
|
|
let packet_buf = self.read_exact(packet_len).unwrap();
|
|
|
|
let packet = String::from_utf8(packet_buf).unwrap();
|
2015-01-04 00:39:46 +03:00
|
|
|
println!("{}", packet);
|
2015-03-21 21:57:46 +03:00
|
|
|
return Ok(Json::from_str(&packet).unwrap())
|
2014-11-13 00:42:35 +03:00
|
|
|
},
|
|
|
|
Err(ref e) if e.kind == EndOfFile =>
|
|
|
|
return Err(IoError { kind: EndOfFile, desc: "EOF", detail: None }),
|
|
|
|
_ => return Err(IoError { kind: OtherIoError, desc: "connection error", detail: None })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-19 17:15:03 +04:00
|
|
|
}
|