Skip to content

Commit

Permalink
Improve server-side API
Browse files Browse the repository at this point in the history
  • Loading branch information
proton-decay committed Jun 19, 2017
1 parent 7e4453b commit 9637b43
Show file tree
Hide file tree
Showing 5 changed files with 464 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Event {
},
protocol::S2C::FramebufferUpdate { count } => {
for _ in 0..count {
let rectangle = try!(protocol::Rectangle::read_from(&mut stream));
let rectangle = try!(protocol::RectangleHeader::read_from(&mut stream));
debug!("<- {:?}", rectangle);

let dst = Rect {
Expand Down
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ extern crate octavo;
#[cfg(feature = "apple-auth")]
extern crate crypto;

use std::io::Write;
use byteorder::{BigEndian, WriteBytesExt};

mod protocol;
mod zrle;
mod security;
Expand All @@ -29,6 +32,37 @@ pub struct Rect {
pub height: u16
}

impl Rect {
/// Constructs new `Rect`.
pub fn new(left: u16, top: u16, width: u16, height: u16) -> Self {
Rect {
left: left,
top: top,
width: width,
height: height,
}
}

/// Constructs new zero-sized `Rect` placed at (0, 0).
pub fn new_empty() -> Self {
Rect {
left: 0,
top: 0,
width: 0,
height: 0,
}
}

/// Writes `Rect` to given stream.
fn write_to<W: Write>(&self, writer: &mut W) -> Result<()> {
try!(writer.write_u16::<BigEndian>(self.left));
try!(writer.write_u16::<BigEndian>(self.top));
try!(writer.write_u16::<BigEndian>(self.width));
try!(writer.write_u16::<BigEndian>(self.height));
Ok(())
}
}

#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Expand Down
44 changes: 39 additions & 5 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,40 @@ pub struct PixelFormat {
pub blue_shift: u8,
}

impl PixelFormat {
/// Creates RGB pixel format with 4 bytes per pixel and 3 bytes of depth.
pub fn new_rgb8888() -> Self {
PixelFormat {
bits_per_pixel: 32,
depth: 24,
big_endian: true,
true_colour: true,
red_max: 255,
green_max: 255,
blue_max: 255,
red_shift: 0,
green_shift: 8,
blue_shift: 16,
}
}

/// Creates BGR pixel format with 4 bytes per pixel and 3 bytes of depth.
pub fn new_bgr8888() -> Self {
PixelFormat {
bits_per_pixel: 32,
depth: 24,
big_endian: true,
true_colour: true,
red_max: 255,
green_max: 255,
blue_max: 255,
red_shift: 16,
green_shift: 8,
blue_shift: 0,
}
}
}

impl Message for PixelFormat {
fn read_from<R: Read>(reader: &mut R) -> Result<PixelFormat> {
let pixel_format = PixelFormat {
Expand Down Expand Up @@ -513,17 +547,17 @@ impl Message for C2S {
}

#[derive(Debug)]
pub struct Rectangle {
pub struct RectangleHeader {
pub x_position: u16,
pub y_position: u16,
pub width: u16,
pub height: u16,
pub encoding: Encoding,
}

impl Message for Rectangle {
fn read_from<R: Read>(reader: &mut R) -> Result<Rectangle> {
Ok(Rectangle {
impl Message for RectangleHeader {
fn read_from<R: Read>(reader: &mut R) -> Result<RectangleHeader> {
Ok(RectangleHeader {
x_position: try!(reader.read_u16::<BigEndian>()),
y_position: try!(reader.read_u16::<BigEndian>()),
width: try!(reader.read_u16::<BigEndian>()),
Expand Down Expand Up @@ -571,7 +605,7 @@ pub enum S2C {
// core spec
FramebufferUpdate {
count: u16,
/* Vec<Rectangle> has to be read out manually */
// Vec<RectangleHeader> has to be read out manually
},
SetColourMapEntries {
first_colour: u16,
Expand Down
4 changes: 2 additions & 2 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ impl Proxy {
match message {
protocol::S2C::FramebufferUpdate { count } => {
for _ in 0..count {
let rectangle = try!(protocol::Rectangle::read_from(server_stream));
let rectangle = try!(protocol::RectangleHeader::read_from(server_stream));
debug!("c<-s {:?}", rectangle);
try!(protocol::Rectangle::write_to(&rectangle, &mut buffer_stream));
try!(protocol::RectangleHeader::write_to(&rectangle, &mut buffer_stream));

match rectangle.encoding {
protocol::Encoding::Raw => {
Expand Down
Loading

0 comments on commit 9637b43

Please sign in to comment.