Skip to content

Commit

Permalink
Update to v0.10 (#5)
Browse files Browse the repository at this point in the history
* Update to v0.10 with CappedBuffer

* Fix README.md
  • Loading branch information
maciejhirsz authored Sep 18, 2020
1 parent bd1f693 commit be4d475
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 79 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<a name="v0.7.9"></a>
### v0.8.0 (2018-10-15)

#### Features
* Update rand to 0.6
* Upgrade native-tls to 0.2
* Add a maximal size for fragments exposed via the `max_fragment_size` setting

#### Bug fixes
* Don't try to parse response when the socket not ready

<a name="v0.7.9"></a>
### v0.7.9 (2018-10-15)

Expand Down
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ license = "MIT"
name = "parity-ws"
readme = "README.md"
repository = "https://github.com/paritytech/ws-rs"
version = "0.8.0"
version = "0.10.0"

[dependencies]
byteorder = "1.2.1"
Expand All @@ -22,10 +22,10 @@ httparse = "1.2.4"
log = "0.4.1"
mio = "0.6.14"
mio-extras = "2.0"
rand = "0.4.2"
sha1 = "0.6.0"
rand = "0.7"
sha-1 = "0.8.0"
slab = "0.4"
url = "1.7.0"
url = "2.0.0"

[dependencies.libc]
optional = true
Expand All @@ -41,11 +41,11 @@ version = "0.10"

[dependencies.native-tls]
optional = true
version = "0.1.5"
version = "0.2"

[dev-dependencies]
clap = "2.31.2"
env_logger = "0.5.6"
env_logger = "0.6"
term = "0.5.1"
time = "0.1.39"

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ listen("127.0.0.1:3012", |out| {
}
})
```

# This fork

Note this is (hopefuly) temporary fork of the original crate until https://github.com/housleyjk/ws-rs/pull/252 gets merged.
Note this is (hopefuly) a temporary fork of the original crate until https://github.com/housleyjk/ws-rs/pull/328 gets merged.

Introduction
------------
Expand Down
187 changes: 187 additions & 0 deletions src/capped_buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
use bytes::BufMut;
use std::ops::Deref;
use std::io;

/// Safe wrapper around Vec<u8> with custom `bytes::BufMut` and `std::io::Write`
/// implementations that ensure the buffer never exceeds maximum capacity.
pub struct CappedBuffer {
buf: Vec<u8>,
max: usize,
}

impl CappedBuffer {
/// Create a new `CappedBuffer` with initial `capacity`, and a limit
/// capacity set to `max`.
pub fn new(mut capacity: usize, max: usize) -> Self {
if capacity > max {
capacity = max;
}

Self {
buf: Vec::with_capacity(capacity),
max,
}
}

/// Remaining amount of bytes that can be written to the buffer
/// before reaching max capacity
#[inline]
pub fn remaining(&self) -> usize {
self.max - self.buf.len()
}

/// Shift the content of the buffer to the left by `shift`,
/// effectively forgetting the shifted out bytes.
/// New length of the buffer will be adjusted accordingly.
pub fn shift(&mut self, shift: usize) {
if shift >= self.buf.len() {
self.buf.clear();
return;
}

let src = self.buf[shift..].as_ptr();
let dst = self.buf.as_mut_ptr();
let new_len = self.buf.len() - shift;

// This is a simple, potentially overlapping memcpy within
// the buffer, shifting `new_len` bytes at offset `shift` (`src`)
// to the beginning of the buffer (`dst`)
unsafe {
std::ptr::copy(src, dst, new_len);
self.buf.set_len(new_len);
}
}
}

impl AsRef<[u8]> for CappedBuffer {
fn as_ref(&self) -> &[u8] {
&self.buf
}
}

impl AsMut<[u8]> for CappedBuffer {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.buf
}
}

impl Deref for CappedBuffer {
type Target = Vec<u8>;

fn deref(&self) -> &Vec<u8> {
&self.buf
}
}

impl io::Write for CappedBuffer {
fn write(&mut self, mut buf: &[u8]) -> io::Result<usize> {
if buf.len() > self.remaining() {
buf = &buf[..self.remaining()];
}
self.buf.extend_from_slice(buf);
Ok(buf.len())
}

fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
if buf.len() <= self.remaining() {
self.buf.extend_from_slice(buf);
Ok(())
} else {
Err(io::Error::new(io::ErrorKind::InvalidInput, "Exceeded maximum buffer capacity"))
}
}

fn flush(&mut self) -> io::Result<()> {
self.buf.flush()
}
}

impl BufMut for CappedBuffer {
fn remaining_mut(&self) -> usize {
self.remaining()
}

unsafe fn advance_mut(&mut self, cnt: usize) {
assert!(cnt <= self.remaining(), "Exceeded buffer capacity");

self.buf.advance_mut(cnt);
}

unsafe fn bytes_mut(&mut self) -> &mut [u8] {
let remaining = self.remaining();

// `self.buf.bytes_mut` does an implicit allocation
if remaining == 0 {
return &mut [];
}

let mut bytes = self.buf.bytes_mut();

if bytes.len() > remaining {
bytes = &mut bytes[..remaining];
}

bytes
}
}

#[cfg(test)]
mod test {
use std::io::Write;
use super::*;

#[test]
fn shift() {
let mut buffer = CappedBuffer::new(10, 20);

buffer.write_all(b"Hello World").unwrap();
buffer.shift(6);

assert_eq!(&*buffer, b"World");
assert_eq!(buffer.remaining(), 15);
}

#[test]
fn shift_zero() {
let mut buffer = CappedBuffer::new(10, 20);

buffer.write_all(b"Hello World").unwrap();
buffer.shift(0);

assert_eq!(&*buffer, b"Hello World");
assert_eq!(buffer.remaining(), 9);
}

#[test]
fn shift_all() {
let mut buffer = CappedBuffer::new(10, 20);

buffer.write_all(b"Hello World").unwrap();
buffer.shift(11);

assert_eq!(&*buffer, b"");
assert_eq!(buffer.remaining(), 20);
}

#[test]
fn shift_capacity() {
let mut buffer = CappedBuffer::new(10, 20);

buffer.write_all(b"Hello World").unwrap();
buffer.shift(20);

assert_eq!(&*buffer, b"");
assert_eq!(buffer.remaining(), 20);
}

#[test]
fn shift_over_capacity() {
let mut buffer = CappedBuffer::new(10, 20);

buffer.write_all(b"Hello World").unwrap();
buffer.shift(50);

assert_eq!(&*buffer, b"");
assert_eq!(buffer.remaining(), 20);
}
}
11 changes: 11 additions & 0 deletions src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use message;
use protocol::CloseCode;
use result::{Error, Result};
use std::cmp::PartialEq;
use std::hash::{Hash, Hasher};
use std::fmt;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -69,6 +70,16 @@ impl PartialEq for Sender {
}
}

impl Eq for Sender { }

impl Hash for Sender {
fn hash<H: Hasher>(&self, state: &mut H) {
self.connection_id.hash(state);
self.token.hash(state);
}
}


impl Sender {
#[doc(hidden)]
#[inline]
Expand Down
Loading

0 comments on commit be4d475

Please sign in to comment.