Skip to content

Commit be4d475

Browse files
authored
Update to v0.10 (#5)
* Update to v0.10 with CappedBuffer * Fix README.md
1 parent bd1f693 commit be4d475

File tree

12 files changed

+287
-79
lines changed

12 files changed

+287
-79
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
<a name="v0.7.9"></a>
2+
### v0.8.0 (2018-10-15)
3+
4+
#### Features
5+
* Update rand to 0.6
6+
* Upgrade native-tls to 0.2
7+
* Add a maximal size for fragments exposed via the `max_fragment_size` setting
8+
9+
#### Bug fixes
10+
* Don't try to parse response when the socket not ready
11+
112
<a name="v0.7.9"></a>
213
### v0.7.9 (2018-10-15)
314

Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ license = "MIT"
1313
name = "parity-ws"
1414
readme = "README.md"
1515
repository = "https://github.com/paritytech/ws-rs"
16-
version = "0.8.0"
16+
version = "0.10.0"
1717

1818
[dependencies]
1919
byteorder = "1.2.1"
@@ -22,10 +22,10 @@ httparse = "1.2.4"
2222
log = "0.4.1"
2323
mio = "0.6.14"
2424
mio-extras = "2.0"
25-
rand = "0.4.2"
26-
sha1 = "0.6.0"
25+
rand = "0.7"
26+
sha-1 = "0.8.0"
2727
slab = "0.4"
28-
url = "1.7.0"
28+
url = "2.0.0"
2929

3030
[dependencies.libc]
3131
optional = true
@@ -41,11 +41,11 @@ version = "0.10"
4141

4242
[dependencies.native-tls]
4343
optional = true
44-
version = "0.1.5"
44+
version = "0.2"
4545

4646
[dev-dependencies]
4747
clap = "2.31.2"
48-
env_logger = "0.5.6"
48+
env_logger = "0.6"
4949
term = "0.5.1"
5050
time = "0.1.39"
5151

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ listen("127.0.0.1:3012", |out| {
1010
}
1111
})
1212
```
13+
1314
# This fork
1415

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

1718
Introduction
1819
------------

src/capped_buffer.rs

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
use bytes::BufMut;
2+
use std::ops::Deref;
3+
use std::io;
4+
5+
/// Safe wrapper around Vec<u8> with custom `bytes::BufMut` and `std::io::Write`
6+
/// implementations that ensure the buffer never exceeds maximum capacity.
7+
pub struct CappedBuffer {
8+
buf: Vec<u8>,
9+
max: usize,
10+
}
11+
12+
impl CappedBuffer {
13+
/// Create a new `CappedBuffer` with initial `capacity`, and a limit
14+
/// capacity set to `max`.
15+
pub fn new(mut capacity: usize, max: usize) -> Self {
16+
if capacity > max {
17+
capacity = max;
18+
}
19+
20+
Self {
21+
buf: Vec::with_capacity(capacity),
22+
max,
23+
}
24+
}
25+
26+
/// Remaining amount of bytes that can be written to the buffer
27+
/// before reaching max capacity
28+
#[inline]
29+
pub fn remaining(&self) -> usize {
30+
self.max - self.buf.len()
31+
}
32+
33+
/// Shift the content of the buffer to the left by `shift`,
34+
/// effectively forgetting the shifted out bytes.
35+
/// New length of the buffer will be adjusted accordingly.
36+
pub fn shift(&mut self, shift: usize) {
37+
if shift >= self.buf.len() {
38+
self.buf.clear();
39+
return;
40+
}
41+
42+
let src = self.buf[shift..].as_ptr();
43+
let dst = self.buf.as_mut_ptr();
44+
let new_len = self.buf.len() - shift;
45+
46+
// This is a simple, potentially overlapping memcpy within
47+
// the buffer, shifting `new_len` bytes at offset `shift` (`src`)
48+
// to the beginning of the buffer (`dst`)
49+
unsafe {
50+
std::ptr::copy(src, dst, new_len);
51+
self.buf.set_len(new_len);
52+
}
53+
}
54+
}
55+
56+
impl AsRef<[u8]> for CappedBuffer {
57+
fn as_ref(&self) -> &[u8] {
58+
&self.buf
59+
}
60+
}
61+
62+
impl AsMut<[u8]> for CappedBuffer {
63+
fn as_mut(&mut self) -> &mut [u8] {
64+
&mut self.buf
65+
}
66+
}
67+
68+
impl Deref for CappedBuffer {
69+
type Target = Vec<u8>;
70+
71+
fn deref(&self) -> &Vec<u8> {
72+
&self.buf
73+
}
74+
}
75+
76+
impl io::Write for CappedBuffer {
77+
fn write(&mut self, mut buf: &[u8]) -> io::Result<usize> {
78+
if buf.len() > self.remaining() {
79+
buf = &buf[..self.remaining()];
80+
}
81+
self.buf.extend_from_slice(buf);
82+
Ok(buf.len())
83+
}
84+
85+
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
86+
if buf.len() <= self.remaining() {
87+
self.buf.extend_from_slice(buf);
88+
Ok(())
89+
} else {
90+
Err(io::Error::new(io::ErrorKind::InvalidInput, "Exceeded maximum buffer capacity"))
91+
}
92+
}
93+
94+
fn flush(&mut self) -> io::Result<()> {
95+
self.buf.flush()
96+
}
97+
}
98+
99+
impl BufMut for CappedBuffer {
100+
fn remaining_mut(&self) -> usize {
101+
self.remaining()
102+
}
103+
104+
unsafe fn advance_mut(&mut self, cnt: usize) {
105+
assert!(cnt <= self.remaining(), "Exceeded buffer capacity");
106+
107+
self.buf.advance_mut(cnt);
108+
}
109+
110+
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
111+
let remaining = self.remaining();
112+
113+
// `self.buf.bytes_mut` does an implicit allocation
114+
if remaining == 0 {
115+
return &mut [];
116+
}
117+
118+
let mut bytes = self.buf.bytes_mut();
119+
120+
if bytes.len() > remaining {
121+
bytes = &mut bytes[..remaining];
122+
}
123+
124+
bytes
125+
}
126+
}
127+
128+
#[cfg(test)]
129+
mod test {
130+
use std::io::Write;
131+
use super::*;
132+
133+
#[test]
134+
fn shift() {
135+
let mut buffer = CappedBuffer::new(10, 20);
136+
137+
buffer.write_all(b"Hello World").unwrap();
138+
buffer.shift(6);
139+
140+
assert_eq!(&*buffer, b"World");
141+
assert_eq!(buffer.remaining(), 15);
142+
}
143+
144+
#[test]
145+
fn shift_zero() {
146+
let mut buffer = CappedBuffer::new(10, 20);
147+
148+
buffer.write_all(b"Hello World").unwrap();
149+
buffer.shift(0);
150+
151+
assert_eq!(&*buffer, b"Hello World");
152+
assert_eq!(buffer.remaining(), 9);
153+
}
154+
155+
#[test]
156+
fn shift_all() {
157+
let mut buffer = CappedBuffer::new(10, 20);
158+
159+
buffer.write_all(b"Hello World").unwrap();
160+
buffer.shift(11);
161+
162+
assert_eq!(&*buffer, b"");
163+
assert_eq!(buffer.remaining(), 20);
164+
}
165+
166+
#[test]
167+
fn shift_capacity() {
168+
let mut buffer = CappedBuffer::new(10, 20);
169+
170+
buffer.write_all(b"Hello World").unwrap();
171+
buffer.shift(20);
172+
173+
assert_eq!(&*buffer, b"");
174+
assert_eq!(buffer.remaining(), 20);
175+
}
176+
177+
#[test]
178+
fn shift_over_capacity() {
179+
let mut buffer = CappedBuffer::new(10, 20);
180+
181+
buffer.write_all(b"Hello World").unwrap();
182+
buffer.shift(50);
183+
184+
assert_eq!(&*buffer, b"");
185+
assert_eq!(buffer.remaining(), 20);
186+
}
187+
}

src/communication.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use message;
1111
use protocol::CloseCode;
1212
use result::{Error, Result};
1313
use std::cmp::PartialEq;
14+
use std::hash::{Hash, Hasher};
1415
use std::fmt;
1516

1617
#[derive(Debug, Clone)]
@@ -69,6 +70,16 @@ impl PartialEq for Sender {
6970
}
7071
}
7172

73+
impl Eq for Sender { }
74+
75+
impl Hash for Sender {
76+
fn hash<H: Hasher>(&self, state: &mut H) {
77+
self.connection_id.hash(state);
78+
self.token.hash(state);
79+
}
80+
}
81+
82+
7283
impl Sender {
7384
#[doc(hidden)]
7485
#[inline]

0 commit comments

Comments
 (0)