2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-19 00:16:29 +08:00

runtime: remove unnecessary null bytes from session protocol.

This commit is contained in:
whitequark 2016-10-06 15:38:33 +00:00
parent 5428a866b3
commit bcdbd00e7b
2 changed files with 4 additions and 8 deletions

View File

@ -166,7 +166,7 @@ class CommGeneric:
return self._read_chunk(self._read_int32()) return self._read_chunk(self._read_int32())
def _read_string(self): def _read_string(self):
return self._read_bytes()[:-1].decode("utf-8") return self._read_bytes().decode("utf-8")
# #
# Writer interface # Writer interface
@ -214,7 +214,7 @@ class CommGeneric:
self._write_buffer.append(value) self._write_buffer.append(value)
def _write_string(self, value): def _write_string(self, value):
self._write_bytes(value.encode("utf-8") + b"\0") self._write_bytes(value.encode("utf-8"))
# #
# Exported APIs # Exported APIs

View File

@ -66,14 +66,10 @@ pub fn write_bytes(writer: &mut Write, value: &[u8]) -> io::Result<()> {
} }
pub fn read_string(reader: &mut Read) -> io::Result<String> { pub fn read_string(reader: &mut Read) -> io::Result<String> {
let mut bytes = try!(read_bytes(reader)); let bytes = try!(read_bytes(reader));
let len = bytes.len() - 1; // length without trailing \0
bytes.resize(len, 0); // FIXME: don't send \0 in the first place
String::from_utf8(bytes).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) String::from_utf8(bytes).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
} }
pub fn write_string(writer: &mut Write, value: &str) -> io::Result<()> { pub fn write_string(writer: &mut Write, value: &str) -> io::Result<()> {
try!(write_u32(writer, (value.len() + 1) as u32)); write_bytes(writer, value.as_bytes())
try!(writer.write_all(value.as_bytes()));
write_u8(writer, 0)
} }