Merge pull request #946 from ethcore/numbers-serde-bin

IPC serialization for custom parameters
This commit is contained in:
Arkadiy Paronyan
2016-04-13 14:10:12 +02:00
7 changed files with 397 additions and 34 deletions

View File

@@ -15,7 +15,7 @@ ethcore-devtools = { path = "../../devtools" }
semver = "0.2.0"
nanomsg = { git = "https://github.com/ethcore/nanomsg.rs.git" }
ethcore-ipc-nano = { path = "../nano" }
ethcore-util = { path = "../../util" }
[build-dependencies]
syntex = "*"

View File

@@ -101,4 +101,24 @@ mod tests {
assert!(result.is_ok());
}
#[test]
fn can_use_custom_params() {
let mut socket = TestSocket::new();
socket.read_buffer = vec![1];
let service_client = ServiceClient::init(socket);
let result = service_client.push_custom(CustomData { a: 3, b: 11});
assert_eq!(vec![
// message num..
0, 18,
// payload length
0, 0, 0, 0, 0, 0, 0, 16,
// structure raw bytes (bigendians :( )
3, 0, 0, 0, 0, 0, 0, 0,
11, 0, 0, 0, 0, 0, 0, 0],
service_client.socket().borrow().write_buffer.clone());
assert_eq!(true, result);
}
}

View File

@@ -21,6 +21,7 @@ extern crate ethcore_devtools as devtools;
extern crate semver;
extern crate nanomsg;
extern crate ethcore_ipc_nano as nanoipc;
extern crate ethcore_util as util;
pub mod service;
mod examples;

View File

@@ -16,13 +16,42 @@
use std::sync::RwLock;
use std::ops::*;
use std::convert::*;
use ipc::IpcConfig;
use util::bytes::{FromRawBytes, BytesConvertable, FromBytesError};
pub struct Service {
pub commits: RwLock<usize>,
pub rollbacks: RwLock<usize>,
}
pub struct CustomData {
pub a: usize,
pub b: usize,
}
impl FromRawBytes for CustomData {
fn from_bytes(bytes: &[u8]) -> Result<CustomData, FromBytesError> {
Ok(CustomData {
a: bytes[0] as usize * 256 + bytes[1] as usize,
b: bytes[2] as usize * 256 + bytes[3] as usize
})
}
}
impl BytesConvertable for CustomData {
fn bytes(&self) -> &[u8] {
let ip: *const CustomData = self;
let ptr: *const u8 = ip as *const _;
unsafe {
::std::slice::from_raw_parts(
ptr,
::std::mem::size_of::<CustomData>()
)
}
}
}
#[derive(Ipc)]
impl Service {
fn commit(&self, f: u32) -> u32 {
@@ -36,6 +65,15 @@ impl Service {
*lock = *lock + a_0 as usize - b as usize;
(a_0 - b) as i32
}
pub fn push_custom(&self, data: CustomData) -> bool {
let mut clock = self.commits.write().unwrap();
let mut rlock = self.commits.write().unwrap();
*clock = data.a;
*rlock = data.b;
true
}
}
impl Service {