Browse Source

Introduce bytes crate, use it for Proto{De,En}code{,r}.

wip
Titouan Rigoudy 7 years ago
parent
commit
92c30a6c6d
4 changed files with 456 additions and 248 deletions
  1. +175
    -101
      Cargo.lock
  2. +2
    -0
      Cargo.toml
  3. +2
    -0
      src/main.rs
  4. +277
    -147
      src/proto/codec.rs

+ 175
- 101
Cargo.lock View File

@ -1,20 +1,3 @@
[root]
name = "solstice"
version = "0.1.0"
dependencies = [
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mio 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
"slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"ws 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.5.3" version = "0.5.3"
@ -28,6 +11,11 @@ name = "bitflags"
version = "0.4.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitflags"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "0.5.3" version = "0.5.3"
@ -35,7 +23,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.0.0"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
@ -43,9 +31,18 @@ name = "bytes"
version = "0.3.0" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bytes"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"iovec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "0.1.0"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
@ -110,36 +107,58 @@ name = "env_logger"
version = "0.3.5" version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "futures"
version = "0.1.10"
name = "fuchsia-zircon"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"fuchsia-zircon-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fuchsia-zircon-sys"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "futures"
version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "gcc" name = "gcc"
version = "0.3.43"
version = "0.3.54"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "httparse" name = "httparse"
version = "1.2.1"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "idna" name = "idna"
version = "0.1.0"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-bidi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-normalization 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "iovec"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -153,22 +172,22 @@ dependencies = [
[[package]] [[package]]
name = "lazycell" name = "lazycell"
version = "0.4.0"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.20"
version = "0.2.33"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "log" name = "log"
version = "0.3.6"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "matches" name = "matches"
version = "0.1.4"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
@ -176,7 +195,7 @@ name = "memchr"
version = "0.1.11" version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -185,27 +204,30 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bytes 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"miow 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "miow 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)",
"net2 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
"nix 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"slab 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "mio" name = "mio"
version = "0.6.4"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"fuchsia-zircon 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"fuchsia-zircon-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"iovec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)",
"lazycell 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"net2 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
"slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -216,30 +238,30 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)",
"net2 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "miow" name = "miow"
version = "0.2.0"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)",
"net2 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "net2" name = "net2"
version = "0.2.26"
version = "0.2.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -250,20 +272,26 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "percent-encoding"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.3.15"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"fuchsia-zircon 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.1.16"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
@ -288,16 +316,16 @@ name = "rust-crypto"
version = "0.2.36" version = "0.2.36"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "rustc-serialize" name = "rustc-serialize"
version = "0.3.22"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
@ -310,7 +338,7 @@ name = "sha1"
version = "0.1.1" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -328,13 +356,37 @@ name = "slab"
version = "0.3.0" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "slab"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "solstice"
version = "0.1.0"
dependencies = [
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"bytes 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"mio 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)",
"slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-io 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"ws 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "thread-id" name = "thread-id"
version = "2.0.0" version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -347,47 +399,61 @@ dependencies = [
[[package]] [[package]]
name = "time" name = "time"
version = "0.1.36"
version = "0.1.38"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "tokio-core" name = "tokio-core"
version = "0.1.4"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"futures 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mio 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"bytes 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
"iovec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"mio 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"slab 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-io 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tokio-io"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bytes 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "unicode-bidi" name = "unicode-bidi"
version = "0.2.5"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "unicode-normalization" name = "unicode-normalization"
version = "0.1.4"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "url" name = "url"
version = "1.4.0"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"idna 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -410,12 +476,12 @@ name = "ws"
version = "0.4.8" version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"httparse 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"httparse 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"mio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
"sha1 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "sha1 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -430,10 +496,12 @@ dependencies = [
[metadata] [metadata]
"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" "checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
"checksum bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dead7461c1127cf637931a1e50934eb6eee8bff2f74433ac7909e9afcee04a3" "checksum bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dead7461c1127cf637931a1e50934eb6eee8bff2f74433ac7909e9afcee04a3"
"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
"checksum byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855" "checksum byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855"
"checksum byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c40977b0ee6b9885c9013cd41d9feffdd22deb3bb4dc3a71d901cc7a77de18c8"
"checksum byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff81738b726f5d099632ceaffe7fb65b90212e8dce59d518729e7e8634032d3d"
"checksum bytes 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c129aff112dcc562970abb69e2508b40850dd24c274761bb50fb8a0067ba6c27" "checksum bytes 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c129aff112dcc562970abb69e2508b40850dd24c274761bb50fb8a0067ba6c27"
"checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c"
"checksum bytes 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d828f97b58cc5de3e40c421d0cf2132d6b2da4ee0e11b8632fa838f0f9333ad6"
"checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de"
"checksum encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" "checksum encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec"
"checksum encoding-index-japanese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91" "checksum encoding-index-japanese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91"
"checksum encoding-index-korean 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81" "checksum encoding-index-korean 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81"
@ -442,40 +510,46 @@ dependencies = [
"checksum encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" "checksum encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18"
"checksum encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" "checksum encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569"
"checksum env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "15abd780e45b3ea4f76b4e9a26ff4843258dd8a3eed2775a0e7368c2e7936c2f" "checksum env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "15abd780e45b3ea4f76b4e9a26ff4843258dd8a3eed2775a0e7368c2e7936c2f"
"checksum futures 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "c1913eb7083840b1bbcbf9631b7fda55eaf35fe7ead13cca034e8946f9e2bc41"
"checksum gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)" = "c07c758b972368e703a562686adb39125707cc1ef3399da8c019fc6c2498a75d"
"checksum httparse 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a6e7a63e511f9edffbab707141fbb8707d1a3098615fb2adbd5769cdfcc9b17d"
"checksum idna 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1053236e00ce4f668aeca4a769a09b3bf5a682d802abd6f3cb39374f6b162c11"
"checksum fuchsia-zircon 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f6c0581a4e363262e52b87f59ee2afe3415361c6ec35e665924eb08afe8ff159"
"checksum fuchsia-zircon-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "43f3795b4bae048dc6123a6b972cadde2e676f9ded08aef6bb77f5f157684a82"
"checksum futures 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "118b49cac82e04121117cbd3121ede3147e885627d82c4546b87c702debb90c1"
"checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb"
"checksum httparse 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "af2f2dd97457e8fb1ae7c5a420db346af389926e36f43768b96f101546b04a07"
"checksum idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "014b298351066f1512874135335d62a789ffe78a9974f94b43ed5621951eaf7d"
"checksum iovec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b6e8b9c2247fcf6c6a1151f1156932be5606c9fd6f55a2d7f9fc1cb29386b2f7"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce12306c4739d86ee97c23139f3a34ddf0387bbf181bc7929d287025a8c3ef6b"
"checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5"
"checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054"
"checksum matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "efd7622e3022e1a6eaa602c4cea8912254e5582c9c692e9167714182244801b1"
"checksum lazycell 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3b585b7a6811fb03aa10e74b278a0f00f8dd9b45dc681f148bb29fa5cb61859b"
"checksum libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "5ba3df4dcb460b9dfbd070d41c94c19209620c191b0340b929ce748a2bcd42d2"
"checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b"
"checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376"
"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
"checksum mio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a637d1ca14eacae06296a008fa7ad955347e34efcb5891cfd8ba05491a37907e" "checksum mio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a637d1ca14eacae06296a008fa7ad955347e34efcb5891cfd8ba05491a37907e"
"checksum mio 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "eecdbdd49a849336e77b453f021c89972a2cfb5b51931a0026ae0ac4602de681"
"checksum mio 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0e8411968194c7b139e9105bc4ae7db0bae232af087147e72f0616ebf5fdb9cb"
"checksum miow 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3e690c5df6b2f60acd45d56378981e827ff8295562fc8d34f573deb267a59cd1" "checksum miow 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3e690c5df6b2f60acd45d56378981e827ff8295562fc8d34f573deb267a59cd1"
"checksum miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3a78d2605eb97302c10cf944b8d96b0a2a890c52957caf92fcd1f24f69049579"
"checksum net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)" = "5edf9cb6be97212423aed9413dd4729d62b370b5e1c571750e882cebbbc1e3e2"
"checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919"
"checksum net2 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "3a80f842784ef6c9a958b68b7516bc7e35883c614004dd94959a4dca1b716c09"
"checksum nix 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bfb3ddedaa14746434a02041940495bf11325c22f6d36125d3bdd56090d50a79" "checksum nix 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bfb3ddedaa14746434a02041940495bf11325c22f6d36125d3bdd56090d50a79"
"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
"checksum redox_syscall 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "8dd35cc9a8bdec562c757e3d43c1526b5c6d2653e23e2315065bc25556550753"
"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"
"checksum rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6475140dfd8655aeb72e1fd4b7a1cc1c202be65d71669476e392fe62532b9edd"
"checksum redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "8dde11f18c108289bef24469638a04dce49da56084f2d50618b226e47eb04509"
"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957" "checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" "checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
"checksum rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "237546c689f20bb44980270c73c3b9edd0891c1be49cc1274406134a66d3957b"
"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
"checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d" "checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d"
"checksum sha1 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a307a40d5834140e4213a6952483b84e9ad53bdcab918b7335a6e305e505a53c" "checksum sha1 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a307a40d5834140e4213a6952483b84e9ad53bdcab918b7335a6e305e505a53c"
"checksum slab 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d807fd58c4181bbabed77cb3b891ba9748241a552bcc5be698faaebefc54f46e" "checksum slab 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d807fd58c4181bbabed77cb3b891ba9748241a552bcc5be698faaebefc54f46e"
"checksum slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6dbdd334bd28d328dad1c41b0ea662517883d8880d8533895ef96c8003dec9c4" "checksum slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6dbdd334bd28d328dad1c41b0ea662517883d8880d8533895ef96c8003dec9c4"
"checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23"
"checksum slab 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fdeff4cd9ecff59ec7e3744cbca73dfe5ac35c2aedb2cfba8a1c715a18912e9d"
"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" "checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
"checksum time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "211b63c112206356ef1ff9b19355f43740fc3f85960c598a93d3a3d3ba7beade"
"checksum tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3d1be481b55126f02ef88ff86748086473cb537a949fc4a8f4be403a530ae54b"
"checksum unicode-bidi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a078ebdd62c0e71a709c3d53d2af693fe09fe93fbff8344aebe289b78f9032"
"checksum unicode-normalization 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e28fa37426fceeb5cf8f41ee273faa7c82c47dc8fba5853402841e665fcd86ff"
"checksum url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f5ba8a749fb4479b043733416c244fa9d1d3af3d7c23804944651c8a448cb87e"
"checksum time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d788d3aa77bc0ef3e9621256885555368b47bd495c13dd2e7413c89f845520"
"checksum tokio-core 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "c843a027f7c1df5f81e7734a0df3f67bf329411781ebf36393ce67beef6071e3"
"checksum tokio-io 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "514aae203178929dbf03318ad7c683126672d4d96eccb77b29603d33c9e25743"
"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
"checksum unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "51ccda9ef9efa3f7ef5d91e8f9b83bbe6955f9bf86aec89d5cce2c874625920f"
"checksum url 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fa35e768d4daf1d85733418a49fb42e10d7f633e394fccab4ab7aba897053fe2"
"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" "checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"


+ 2
- 0
Cargo.toml View File

@ -5,6 +5,7 @@ authors = ["letitz"]
[dependencies] [dependencies]
byteorder = "^0.5.1" byteorder = "^0.5.1"
bytes = "^0.4"
encoding = "^0.2" encoding = "^0.2"
env_logger = "^0.3.2" env_logger = "^0.3.2"
futures = "^0.1" futures = "^0.1"
@ -14,4 +15,5 @@ rust-crypto = "^0.2.34"
rustc-serialize = "^0.3.17" rustc-serialize = "^0.3.17"
slab = "^0.2" slab = "^0.2"
tokio-core = "^0.1" tokio-core = "^0.1"
tokio-io = "^0.1"
ws = "^0.4" ws = "^0.4"

+ 2
- 0
src/main.rs View File

@ -6,6 +6,7 @@ mod room;
mod user; mod user;
extern crate byteorder; extern crate byteorder;
extern crate bytes;
extern crate core; extern crate core;
extern crate crypto; extern crate crypto;
extern crate encoding; extern crate encoding;
@ -16,6 +17,7 @@ extern crate mio;
extern crate rustc_serialize; extern crate rustc_serialize;
extern crate slab; extern crate slab;
extern crate tokio_core; extern crate tokio_core;
extern crate tokio_io;
extern crate ws; extern crate ws;
use std::sync::mpsc; use std::sync::mpsc;


+ 277
- 147
src/proto/codec.rs View File

@ -4,10 +4,16 @@ use std::io;
use std::net; use std::net;
use std::u16; use std::u16;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use bytes::{Buf, BufMut, BytesMut, LittleEndian};
use bytes::buf::IntoBuf;
use encoding::{Encoding, EncoderTrap, DecoderTrap}; use encoding::{Encoding, EncoderTrap, DecoderTrap};
use encoding::all::WINDOWS_1252; use encoding::all::WINDOWS_1252;
use tokio_core::io::EasyBuf; use tokio_core::io::EasyBuf;
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_io::codec::{Decoder, Encoder};
use tokio_io::codec::length_delimited;
use proto::server::ServerResponse;
/// Length of an encoded 32-bit integer in bytes. /// Length of an encoded 32-bit integer in bytes.
const U32_BYTE_LEN : usize = 4; const U32_BYTE_LEN : usize = 4;
@ -85,162 +91,274 @@ impl From<io::Error> for DecodeError {
} }
} }
fn unexpected_eof_error(value_type: &str) -> DecodeError {
DecodeError::from(io::Error::new(io::ErrorKind::UnexpectedEof, value_type))
}
/*=================* /*=================*
* DECODE / ENCODE * * DECODE / ENCODE *
*=================*/ *=================*/
/// This trait is implemented by types that can be decoded from messages.
/// Decoding values from messages is attempted only after an entire frame has
/// been received, so it is an error if not enough data is available.
pub trait Decode: Sized {
/// Attempts to decode an instance of `Self` from the bytes in `buf`.
fn decode(buf: &mut EasyBuf) -> Result<Self, DecodeError>;
// The protocol is pretty basic, though quirky. Base types are serialized in
// the following way:
//
// * 32-bit integers are serialized in 4 bytes, little-endian.
// * 16-bit integers are serialized as 32-bit integers with upper bytes set
// to 0.
// * Booleans are serialized as single bytes, containing either 0 or 1.
// * IPv4 addresses are serialized as 32-bit integers.
// * Strings are serialized as 32-bit-length-prefixed arrays of Windows 1252
// encoded characters.
// * Vectors are serialized as length-prefixed arrays of serialized values.
/// This trait is implemented by types that can be decoded from messages with
/// a `ProtoDecoder`.
/// Only here to enable ProtoDecoder::decode_vec.
pub trait ProtoDecode : Sized {
/// Attempts to decode an instance of `Self` using the given decoder.
fn decode(decoder: &mut ProtoDecoder) -> Result<Self, DecodeError>;
} }
/// This trait is implemented by types that can be encoded into messages.
pub trait Encode {
/// Attempts to encode `self` to the given byte buffer.
fn encode(&self, &mut Vec<u8>) -> io::Result<()>;
/// This trait is implemented by types that can be encoded into messages with
/// a `ProtoEncoder`.
/// Only here to enable `ProtoEncoder::encode_vec`.
pub trait ProtoEncode {
/// Attempts to encode `self` with the given encoder.
fn encode(&self, encoder: &mut ProtoEncoder) -> io::Result<()>;
} }
// 32-bit integers are serialized in 4 bytes, little-endian.
// A `ProtoDecoder` knows how to decode various types of values from protocol
// messages.
pub struct ProtoDecoder<'a> {
// If bytes::Buf was object-safe we would just store &'a Buf. We work
// around this limitation by storing the cursor itself.
inner: &'a mut io::Cursor<BytesMut>
}
impl Decode for u32 {
fn decode(buf: &mut EasyBuf) -> Result<Self, DecodeError> {
if buf.len() < U32_BYTE_LEN {
return Err(DecodeError::from(
io::Error::new(io::ErrorKind::UnexpectedEof, "u32")));
}
buf.drain_to(U32_BYTE_LEN).as_slice().read_u32::<LittleEndian>()
.map_err(DecodeError::from)
impl<'a> ProtoDecoder<'a> {
fn new(cursor: &'a mut io::Cursor<BytesMut>) -> ProtoDecoder<'a> {
ProtoDecoder{inner: cursor}
} }
}
impl Encode for u32 {
fn encode(&self, buf: &mut Vec<u8>) -> io::Result<()> {
buf.write_u32::<LittleEndian>(*self)
fn decode_u32(&mut self) -> Result<u32, DecodeError> {
if self.inner.remaining() < U32_BYTE_LEN {
return Err(unexpected_eof_error("u32"))
}
Ok(self.inner.get_u32::<LittleEndian>())
} }
}
// Booleans are serialized as single bytes, containing either 0 or 1.
fn decode_u16(&mut self) -> Result<u16, DecodeError> {
let n = self.decode_u32()?;
if n > u16::MAX as u32 {
return Err(DecodeError::InvalidU16Error(n))
}
Ok(n as u16)
}
impl Decode for bool {
fn decode(buf: &mut EasyBuf) -> Result<Self, DecodeError> {
if buf.len() < 1 {
return Err(DecodeError::from(
io::Error::new(io::ErrorKind::UnexpectedEof, "bool")));
fn decode_bool(&mut self) -> Result<bool, DecodeError> {
if self.inner.remaining() < 1 {
return Err(unexpected_eof_error("bool"))
} }
match buf.drain_to(1).as_slice()[0] {
match self.inner.get_u8() {
0 => Ok(false), 0 => Ok(false),
1 => Ok(true), 1 => Ok(true),
n => Err(DecodeError::InvalidBoolError(n)) n => Err(DecodeError::InvalidBoolError(n))
} }
} }
}
impl Encode for bool {
fn encode(&self, buf: &mut Vec<u8>) -> io::Result<()> {
buf.push(*self as u8);
Ok(())
fn decode_ipv4_addr(&mut self) -> Result<net::Ipv4Addr, DecodeError> {
let ip = self.decode_u32()?;
Ok(net::Ipv4Addr::from(ip))
} }
}
// 16-bit integers are serialized as 32-bit integers with upper bytes set to 0.
impl Decode for u16 {
fn decode(buf: &mut EasyBuf) -> Result<Self, DecodeError> {
let n = try!(u32::decode(buf));
if n > u16::MAX as u32 {
return Err(DecodeError::InvalidU16Error(n))
fn decode_string(&mut self) -> Result<String, DecodeError> {
let len = self.decode_u32()? as usize;
if self.inner.remaining() < len {
return Err(unexpected_eof_error("string"))
} }
Ok(n as u16)
let result = {
let bytes = &self.inner.bytes()[..len];
WINDOWS_1252.decode(bytes, DecoderTrap::Strict)
.map_err(|_| DecodeError::InvalidStringError(bytes.to_vec()))
};
self.inner.advance(len);
result
} }
}
impl Encode for u16 {
fn encode(&self, buf: &mut Vec<u8>) -> io::Result<()> {
(*self as u32).encode(buf)
fn decode_vec<T : ProtoDecode>(&mut self) -> Result<Vec<T>, DecodeError> {
let len = self.decode_u32()? as usize;
let mut vec = Vec::with_capacity(len);
for _ in 0..len {
let val = T::decode(self)?;
vec.push(val);
}
Ok(vec)
} }
} }
// IPv4 addresses are serialized just as 32-bit integers.
// A `ProtoEncoder` knows how to encode various types of values into protocol
// messages.
pub struct ProtoEncoder<'a> {
// If bytes::BufMut was object-safe we would store an &'a BufMut. We work
// around this limiation by using BytesMut directly.
inner: &'a mut BytesMut
}
impl Decode for net::Ipv4Addr {
fn decode(buf: &mut EasyBuf) -> Result<Self, DecodeError> {
let ip = try!(u32::decode(buf));
Ok(net::Ipv4Addr::from(ip))
impl<'a> ProtoEncoder<'a> {
fn new(buf: &'a mut BytesMut) -> ProtoEncoder {
ProtoEncoder{inner: buf}
} }
}
impl Encode for net::Ipv4Addr {
fn encode(&self, buf: &mut Vec<u8>) -> io::Result<()> {
let mut octets = self.octets();
octets.reverse(); // Little endian.
buf.extend(&octets);
fn encode_u32(&mut self, val: u32) -> io::Result<()> {
if self.inner.remaining_mut() < U32_BYTE_LEN {
self.inner.reserve(U32_BYTE_LEN);
}
self.inner.put_u32::<LittleEndian>(val);
Ok(()) Ok(())
} }
}
// Strings are serialized as 32-bit-length-prefixed arrays of Windows 1252
// encoded characters.
impl Decode for String {
fn decode(buf: &mut EasyBuf) -> Result<Self, DecodeError> {
let len = try!(u32::decode(buf)) as usize;
let contents = buf.drain_to(len);
match WINDOWS_1252.decode(contents.as_slice(), DecoderTrap::Strict) {
Ok(string) => Ok(string),
Err(_) =>
Err(DecodeError::InvalidStringError(
contents.as_slice().to_vec()))
fn encode_u16(&mut self, val: u16) -> io::Result<()> {
self.encode_u32(val as u32)
}
fn encode_bool(&mut self, val: bool) -> io::Result<()> {
if !self.inner.has_remaining_mut() {
self.inner.reserve(1);
} }
self.inner.put_u8(val as u8);
Ok(())
} }
}
impl Encode for str {
fn encode(&self, buf: &mut Vec<u8>) -> io::Result<()> {
fn encode_ipv4_addr(&mut self, addr: net::Ipv4Addr) -> io::Result<()> {
let mut octets = addr.octets();
octets.reverse(); // Little endian.
self.inner.extend(&octets);
Ok(())
}
fn encode_string(&mut self, val: &str) -> io::Result<()> {
// Encode the string. // Encode the string.
let bytes = match WINDOWS_1252.encode(self, EncoderTrap::Strict) {
let bytes = match WINDOWS_1252.encode(val, EncoderTrap::Strict) {
Ok(bytes) => bytes, Ok(bytes) => bytes,
Err(_) => { Err(_) => {
let copy = self.to_string();
return Err(io::Error::new(io::ErrorKind::InvalidData, copy));
return Err(io::Error::new(io::ErrorKind::InvalidData, val.to_string()));
} }
}; };
// Prefix the bytes with the length. // Prefix the bytes with the length.
(bytes.len() as u32).encode(buf)?;
buf.extend(bytes);
self.encode_u32(bytes.len() as u32)?;
self.inner.extend(bytes);
Ok(())
}
fn encode_vec<T : ProtoEncode>(&mut self, vec: &[T]) -> io::Result<()> {
self.encode_u32(vec.len() as u32)?;
for ref item in vec {
item.encode(self)?;
}
Ok(()) Ok(())
} }
} }
impl ProtoDecode for u32 {
fn decode(decoder: &mut ProtoDecoder) -> Result<Self, DecodeError> {
decoder.decode_u32()
}
}
impl ProtoEncode for u32 {
fn encode(&self, encoder: &mut ProtoEncoder) -> io::Result<()> {
encoder.encode_u32(*self)
}
}
impl ProtoDecode for bool {
fn decode(decoder: &mut ProtoDecoder) -> Result<Self, DecodeError> {
decoder.decode_bool()
}
}
impl ProtoEncode for bool {
fn encode(&self, encoder: &mut ProtoEncoder) -> io::Result<()> {
encoder.encode_bool(*self)
}
}
impl ProtoDecode for u16 {
fn decode(decoder: &mut ProtoDecoder) -> Result<Self, DecodeError> {
decoder.decode_u16()
}
}
impl ProtoEncode for u16 {
fn encode(&self, encoder: &mut ProtoEncoder) -> io::Result<()> {
encoder.encode_u16(*self)
}
}
impl ProtoDecode for net::Ipv4Addr {
fn decode(decoder: &mut ProtoDecoder) -> Result<Self, DecodeError> {
decoder.decode_ipv4_addr()
}
}
impl ProtoEncode for net::Ipv4Addr {
fn encode(&self, encoder: &mut ProtoEncoder) -> io::Result<()> {
encoder.encode_ipv4_addr(*self)
}
}
impl ProtoDecode for String {
fn decode(decoder: &mut ProtoDecoder) -> Result<Self, DecodeError> {
decoder.decode_string()
}
}
impl ProtoEncode for str {
fn encode(&self, encoder: &mut ProtoEncoder) -> io::Result<()> {
encoder.encode_string(self)
}
}
// Apparently deref coercion does not work for trait methods. // Apparently deref coercion does not work for trait methods.
impl Encode for String {
fn encode(&self, buf: &mut Vec<u8>) -> io::Result<()> {
(self as &str).encode(buf)
impl ProtoEncode for String {
fn encode(&self, encoder: &mut ProtoEncoder) -> io::Result<()> {
encoder.encode_string(self)
} }
} }
// Vectors are serialized as length-prefixed arrays of serialized values.
impl<T: ProtoDecode> ProtoDecode for Vec<T> {
fn decode(decoder: &mut ProtoDecoder) -> Result<Self, DecodeError> {
decoder.decode_vec::<T>()
}
}
impl<T: Decode> Decode for Vec<T> {
fn decode(buf: &mut EasyBuf) -> Result<Self, DecodeError> {
let len = try!(u32::decode(buf)) as usize;
let mut vec = Vec::with_capacity(len);
for _ in 0..len {
vec.push(try!(T::decode(buf)));
}
Ok(vec)
impl<T: ProtoEncode> ProtoEncode for Vec<T> {
fn encode(&self, encoder: &mut ProtoEncoder) -> io::Result<()> {
encoder.encode_vec(self)
} }
} }
impl<T: Encode> Encode for Vec<T> {
fn encode(&self, buf: &mut Vec<u8>) -> io::Result<()> {
(self.len() as u32).encode(buf)?;
for ref item in self {
item.encode(buf)?;
}
Ok(())
/*=================*
* DECODER/ENCODER *
*=================*/
fn new_length_prefixed_framed<T, B>(inner: T) -> length_delimited::Framed<T, B>
where T: AsyncRead + AsyncWrite, B: IntoBuf {
length_delimited::Builder::new()
.length_field_length(4)
.little_endian()
.new_framed(inner)
}
struct ServerResponseDecoder;
impl Decoder for ServerResponseDecoder {
type Item = ServerResponse;
type Error = DecodeError;
fn decode(&mut self, buf: &mut BytesMut)
-> Result<Option<Self::Item>, Self::Error> {
unimplemented!();
} }
} }
@ -250,13 +368,19 @@ impl<T: Encode> Encode for Vec<T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{Decode, Encode};
use super::{ProtoDecoder, ProtoEncoder, ProtoDecode, ProtoEncode};
use std::io;
use std::net; use std::net;
use std::u16; use std::u16;
use std::u32; use std::u32;
use tokio_core::io::EasyBuf;
use bytes::{Buf, BytesMut};
// Helper for succinctness in tests below.
fn new_cursor(vec: Vec<u8>) -> io::Cursor<BytesMut> {
io::Cursor::new(BytesMut::from(vec))
}
// A few integers and their corresponding byte encodings. // A few integers and their corresponding byte encodings.
const U32_ENCODINGS : [(u32, [u8; 4]); 8] = [ const U32_ENCODINGS : [(u32, [u8; 4]); 8] = [
@ -273,10 +397,11 @@ mod tests {
#[test] #[test]
fn encode_u32() { fn encode_u32() {
for &(val, ref encoded_bytes) in &U32_ENCODINGS { for &(val, ref encoded_bytes) in &U32_ENCODINGS {
let mut bytes = vec![13];
val.encode(&mut bytes).unwrap();
let mut bytes = BytesMut::from(vec![13]);
let mut expected_bytes = vec![13]; let mut expected_bytes = vec![13];
expected_bytes.extend(encoded_bytes); expected_bytes.extend(encoded_bytes);
ProtoEncoder::new(&mut bytes).encode_u32(val).unwrap();
assert_eq!(bytes, expected_bytes); assert_eq!(bytes, expected_bytes);
} }
} }
@ -284,38 +409,42 @@ mod tests {
#[test] #[test]
fn decode_u32() { fn decode_u32() {
for &(expected_val, ref bytes) in &U32_ENCODINGS { for &(expected_val, ref bytes) in &U32_ENCODINGS {
let mut buf = EasyBuf::from(bytes.to_vec());
let val = u32::decode(&mut buf).unwrap();
let mut cursor = new_cursor(bytes.to_vec());
let val = ProtoDecoder::new(&mut cursor).decode_u32().unwrap();
assert_eq!(val, expected_val); assert_eq!(val, expected_val);
assert_eq!(buf.len(), 0);
assert_eq!(cursor.remaining(), 0);
} }
} }
#[test] #[test]
fn encode_bool() { fn encode_bool() {
let mut bytes = vec![13];
false.encode(&mut bytes);
assert_eq!(bytes, [13, 0]);
let mut bytes = BytesMut::from(vec![13]);
ProtoEncoder::new(&mut bytes).encode_bool(false).unwrap();
assert_eq!(*bytes, [13, 0]);
bytes.truncate(1); bytes.truncate(1);
true.encode(&mut bytes);
assert_eq!(bytes, [13, 1]);
ProtoEncoder::new(&mut bytes).encode_bool(true).unwrap();
assert_eq!(*bytes, [13, 1]);
} }
#[test] #[test]
fn decode_bool() { fn decode_bool() {
let mut buf = EasyBuf::from(vec![0]);
let mut val = bool::decode(&mut buf).unwrap();
let mut cursor = new_cursor(vec![0]);
let mut val = ProtoDecoder::new(&mut cursor).decode_bool().unwrap();
assert!(!val); assert!(!val);
assert_eq!(buf.len(), 0);
assert_eq!(cursor.remaining(), 0);
buf = EasyBuf::from(vec![1]);
val = bool::decode(&mut buf).unwrap();
cursor = new_cursor(vec![1]);
val = ProtoDecoder::new(&mut cursor).decode_bool().unwrap();
assert!(val); assert!(val);
assert_eq!(buf.len(), 0);
assert_eq!(cursor.remaining(), 0);
}
buf = EasyBuf::from(vec![42]);
assert!(!bool::decode(&mut buf).is_ok());
#[test]
#[should_panic]
fn decode_bool_invalid() {
let mut cursor = new_cursor(vec![42]);
ProtoDecoder::new(&mut cursor).decode_bool().unwrap();
} }
#[test] #[test]
@ -324,11 +453,12 @@ mod tests {
if val > u16::MAX as u32 { if val > u16::MAX as u32 {
continue; continue;
} }
let mut bytes = vec![13];
(val as u16).encode(&mut bytes).unwrap();
let mut bytes = BytesMut::from(vec![13]);
let mut expected_bytes = vec![13]; let mut expected_bytes = vec![13];
expected_bytes.extend(encoded_bytes); expected_bytes.extend(encoded_bytes);
ProtoEncoder::new(&mut bytes).encode_u16(val as u16).unwrap();
assert_eq!(bytes, expected_bytes); assert_eq!(bytes, expected_bytes);
} }
} }
@ -336,14 +466,13 @@ mod tests {
#[test] #[test]
fn decode_u16() { fn decode_u16() {
for &(expected_val, ref bytes) in &U32_ENCODINGS { for &(expected_val, ref bytes) in &U32_ENCODINGS {
let mut cursor = new_cursor(bytes.to_vec());
if expected_val <= u16::MAX as u32 { if expected_val <= u16::MAX as u32 {
let mut buf = EasyBuf::from(bytes.to_vec());
let val = u16::decode(&mut buf).unwrap();
let val = ProtoDecoder::new(&mut cursor).decode_u16().unwrap();
assert_eq!(val, expected_val as u16); assert_eq!(val, expected_val as u16);
assert_eq!(buf.len(), 0);
assert_eq!(cursor.remaining(), 0);
} else { } else {
let mut buf = EasyBuf::from(bytes.to_vec());
assert!(!u16::decode(&mut buf).is_ok());
assert!(ProtoDecoder::new(&mut cursor).decode_u16().is_err());
} }
} }
} }
@ -351,11 +480,12 @@ mod tests {
#[test] #[test]
fn encode_ipv4() { fn encode_ipv4() {
for &(val, ref encoded_bytes) in &U32_ENCODINGS { for &(val, ref encoded_bytes) in &U32_ENCODINGS {
let mut bytes = vec![13];
net::Ipv4Addr::from(val).encode(&mut bytes).unwrap();
let mut bytes = BytesMut::from(vec![13]);
let mut expected_bytes = vec![13]; let mut expected_bytes = vec![13];
expected_bytes.extend(encoded_bytes); expected_bytes.extend(encoded_bytes);
let addr = net::Ipv4Addr::from(val);
ProtoEncoder::new(&mut bytes).encode_ipv4_addr(addr).unwrap();
assert_eq!(bytes, expected_bytes); assert_eq!(bytes, expected_bytes);
} }
} }
@ -363,10 +493,10 @@ mod tests {
#[test] #[test]
fn decode_ipv4() { fn decode_ipv4() {
for &(expected_val, ref bytes) in &U32_ENCODINGS { for &(expected_val, ref bytes) in &U32_ENCODINGS {
let mut buf = EasyBuf::from(bytes.to_vec());
let val = net::Ipv4Addr::decode(&mut buf).unwrap();
let mut cursor = new_cursor(bytes.to_vec());
let val = ProtoDecoder::new(&mut cursor).decode_ipv4_addr().unwrap();
assert_eq!(val, net::Ipv4Addr::from(expected_val)); assert_eq!(val, net::Ipv4Addr::from(expected_val));
assert_eq!(buf.len(), 0);
assert_eq!(cursor.remaining(), 0);
} }
} }
@ -381,11 +511,11 @@ mod tests {
#[test] #[test]
fn encode_string() { fn encode_string() {
for &(string, encoded_bytes) in &STRING_ENCODINGS { for &(string, encoded_bytes) in &STRING_ENCODINGS {
let mut bytes = vec![13];
string.encode(&mut bytes).unwrap();
let mut bytes = BytesMut::from(vec![13]);
let mut expected_bytes = vec![13]; let mut expected_bytes = vec![13];
expected_bytes.extend(encoded_bytes); expected_bytes.extend(encoded_bytes);
ProtoEncoder::new(&mut bytes).encode_string(string).unwrap();
assert_eq!(bytes, expected_bytes); assert_eq!(bytes, expected_bytes);
} }
} }
@ -393,17 +523,17 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
fn encode_invalid_string() { fn encode_invalid_string() {
let mut bytes = vec![];
"忠犬ハチ公".encode(&mut bytes).unwrap();
let mut bytes = BytesMut::with_capacity(100);
ProtoEncoder::new(&mut bytes).encode_string("忠犬ハチ公").unwrap();
} }
#[test] #[test]
fn decode_string() { fn decode_string() {
for &(expected_string, bytes) in &STRING_ENCODINGS { for &(expected_string, bytes) in &STRING_ENCODINGS {
let mut buf = EasyBuf::from(bytes.to_vec());
let string = String::decode(&mut buf).unwrap();
let mut cursor = new_cursor(bytes.to_vec());
let string = ProtoDecoder::new(&mut cursor).decode_string().unwrap();
assert_eq!(string, expected_string); assert_eq!(string, expected_string);
assert_eq!(buf.len(), 0);
assert_eq!(cursor.remaining(), 0);
} }
} }
@ -416,8 +546,8 @@ mod tests {
expected_bytes.extend(encoded_bytes); expected_bytes.extend(encoded_bytes);
} }
let mut bytes = vec![13];
vec.encode(&mut bytes).unwrap();
let mut bytes = BytesMut::from(vec![13]);
ProtoEncoder::new(&mut bytes).encode_vec(&vec).unwrap();
assert_eq!(bytes, expected_bytes); assert_eq!(bytes, expected_bytes);
} }
@ -431,10 +561,10 @@ mod tests {
bytes.extend(encoded_bytes); bytes.extend(encoded_bytes);
} }
let mut buf = EasyBuf::from(bytes);
let vec = Vec::<u32>::decode(&mut buf).unwrap();
let mut cursor = new_cursor(bytes);
let vec = ProtoDecoder::new(&mut cursor).decode_vec::<u32>().unwrap();
assert_eq!(vec, expected_vec); assert_eq!(vec, expected_vec);
assert_eq!(buf.len(), 0);
assert_eq!(cursor.remaining(), 0);
} }
} }

Loading…
Cancel
Save