-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[subworker] WIP on subworker rust API
- Loading branch information
Showing
5 changed files
with
398 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use serde_cbor; | ||
|
||
// Create the Error, ErrorKind, ResultExt, and Result types | ||
error_chain!{ | ||
types { | ||
Error, ErrorKind, ResultExt; | ||
} | ||
foreign_links { | ||
Io(::std::io::Error); | ||
CBOR(serde_cbor::error::Error); | ||
Utf8Err(::std::str::Utf8Error); | ||
} | ||
} | ||
|
||
// Explicit alias just to make some IDEs happier | ||
pub type Result<T> = ::std::result::Result<T, Error>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::os::unix::net::UnixStream; | ||
use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian}; | ||
use super::{Result, WorkerToSubworkerMessage, SubworkerToWorkerMessage, MAX_MSG_SIZE, MSG_PROTOCOL}; | ||
use std::io::{Read, Write}; | ||
use serde_cbor; | ||
|
||
pub(crate) trait SocketExt { | ||
fn write_frame(&mut self, &[u8]) -> Result<()>; | ||
fn read_frame(&mut self) -> Result<Vec<u8>>; | ||
fn write_msg(&mut self, &SubworkerToWorkerMessage) -> Result<()>; | ||
fn read_msg(&mut self) -> Result<WorkerToSubworkerMessage>; | ||
} | ||
|
||
impl SocketExt for UnixStream { | ||
fn write_msg(&mut self, m: &SubworkerToWorkerMessage) -> Result<()> { | ||
let data = serde_cbor::to_vec(m)?; | ||
self.write_frame(&data) | ||
} | ||
|
||
fn read_msg(&mut self) -> Result<WorkerToSubworkerMessage> { | ||
let data = self.read_frame()?; | ||
let msg = serde_cbor::from_slice::<WorkerToSubworkerMessage>(&data)?; | ||
Ok(msg) | ||
} | ||
|
||
fn write_frame(&mut self, data: &[u8]) -> Result<()> { | ||
if data.len() > MAX_MSG_SIZE { | ||
bail!("write_frame: message too long ({} bytes of {} allowed)", data.len(), MAX_MSG_SIZE); | ||
} | ||
self.write_u32::<LittleEndian>(data.len() as u32)?; | ||
self.write_all(data)?; | ||
Ok(()) | ||
} | ||
|
||
fn read_frame(&mut self) -> Result<Vec<u8>> { | ||
let len = self.read_u32::<LittleEndian>()? as usize; | ||
if len > MAX_MSG_SIZE { | ||
bail!("read_frame: message too long ({} bytes of {} allowed)", len, MAX_MSG_SIZE); | ||
} | ||
let mut data = vec![0; len]; | ||
self.read_exact(&mut data)?; | ||
Ok(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.