├── .gitignore ├── README.md ├── db ├── Cargo.lock ├── Cargo.toml └── src │ └── main.rs ├── echo-server ├── Cargo.lock ├── Cargo.toml └── src │ └── main.rs ├── http-tls ├── Cargo.lock ├── Cargo.toml └── src │ └── main.rs ├── pipelined-server ├── Cargo.lock ├── Cargo.toml └── src │ └── main.rs ├── prime-timeout ├── Cargo.lock ├── Cargo.toml └── src │ └── main.rs └── streams ├── Cargo.lock ├── Cargo.toml └── src └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | target/ 4 | 5 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 6 | # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock 7 | # Cargo.lock 8 | 9 | # These are backup files generated by rustfmt 10 | **/*.rs.bk 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learning Tokio 2 | 3 | This project will work through all of the Tokio tutorials available at 4 | https://tokio.rs/docs/getting-started/tokio/ 5 | 6 | ## Echo Server 7 | 8 | + `Codec` parses/encodes the request and response data 9 | + `Proto` handles receiving the request/submitting the response 10 | + `Service` transforms the parsed request into the not-yet-parsed response 11 | + `Proto` holds the specific `Codec` 12 | + `Server` is initialized with a `Proto` 13 | + `Server` then serves the `Service` 14 | 15 | 16 | ## db/futures 17 | 18 | + Each connection processed by separate thread in thread pool. 19 | + Processing done in a future - allows requests to be processed concurrently; 20 | returns when future is evaluated. 21 | 22 | ## Streams 23 | 24 | + Provide in-order, infinite stream processing using futures 25 | + Can create a `core.handle` and spwan a lightweight thread for each stream 26 | element. This makes work asynchronous. 27 | + Async, lightweight threads using `core.handle` good for io-intensive work 28 | + Prefer a thread pool for cpu intensive work 29 | 30 | ## Event Loops 31 | 32 | + listener (eg `TcpListener`) has `incoming` method that returns iterator over 33 | all events 34 | + `Core`'s `run` method sets up the event loop, taking a listener iterator 35 | + Within iterator, handle each event 36 | + `Handle` gives a reference to the event loop, allowing the processing of an 37 | event (asynchronously) to spawn additional tasks on the event loop 38 | + `Handle` puts all tasks on the event-loop thread: it is not thread safe (ie no 39 | `Send`) 40 | + `Remote` is `Send`. `spawn`s a *closure* that creates a future (and is `Send`) 41 | + `Remote::spawn`'s closure can be executed on a different thread 42 | 43 | ## High-level IO 44 | 45 | + TCP/UDP - only async IO available across all rust platforms 46 | + Tokio `TcpListener`, `TcpStream`, `UdpSocket` non-blocking; return futures or 47 | streams 48 | + `tokio_core::io` helper methods. Can be replaced with more specific logic; 49 | only work with "future aware"/non-blocking implementations of `Read` and `Write` 50 | + helpers return futures that yield back ownership. Futures must be 'static; 51 | take IO by value 52 | + `Io::split` takes a `Read + Write` and returns separate `Read` and `Write` for 53 | ownership ease 54 | + `Io::framed` takes `Codec` that takes stream/sink of bytes -> `Stream` and 55 | `Sink` impls 56 | + returns `Framed` which impls `Sink` and `Stream`. Can be split with 57 | `Io::split` 58 | + `Codec` provides `EasyBuf` allows easy buffering through `drain_to` 59 | + To encode, append bytes to provided `Vec` 60 | + `UdpSocket` is *not* byte stream. Impls convenience methods 61 | 62 | ## Pipelined server 63 | 64 | + Build server that is generic over a particular service 65 | + Implemented an echo server and passed into server/handler function 66 | + Spawn each connection onto the event loop and handle asynchronously 67 | 68 | ## Http/Tls Client 69 | + build up a single future that executes the entire request asynchronously using 70 | `and_then` to chain the results of futures together. 71 | + Finally run the future using `core.run` 72 | + Use existing HTTP library instead of handcrafting the request string 73 | -------------------------------------------------------------------------------- /db/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "db" 3 | version = "0.1.0" 4 | dependencies = [ 5 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 6 | "futures-cpupool 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 7 | "postgres 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)", 8 | "r2d2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", 9 | "r2d2_postgres 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", 10 | "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", 11 | "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", 12 | "tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 13 | "tokio-minihttp 0.1.0 (git+https://github.com/tokio-rs/tokio-minihttp)", 14 | "tokio-proto 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 15 | "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 16 | ] 17 | 18 | [[package]] 19 | name = "antidote" 20 | version = "1.0.0" 21 | source = "registry+https://github.com/rust-lang/crates.io-index" 22 | 23 | [[package]] 24 | name = "bufstream" 25 | version = "0.1.2" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | 28 | [[package]] 29 | name = "byteorder" 30 | version = "0.5.3" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | 33 | [[package]] 34 | name = "cfg-if" 35 | version = "0.1.0" 36 | source = "registry+https://github.com/rust-lang/crates.io-index" 37 | 38 | [[package]] 39 | name = "crossbeam" 40 | version = "0.2.10" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | 43 | [[package]] 44 | name = "fallible-iterator" 45 | version = "0.1.3" 46 | source = "registry+https://github.com/rust-lang/crates.io-index" 47 | 48 | [[package]] 49 | name = "futures" 50 | version = "0.1.9" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | dependencies = [ 53 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 54 | ] 55 | 56 | [[package]] 57 | name = "futures-cpupool" 58 | version = "0.1.2" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | dependencies = [ 61 | "crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", 62 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 63 | "num_cpus 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 64 | ] 65 | 66 | [[package]] 67 | name = "hex" 68 | version = "0.2.0" 69 | source = "registry+https://github.com/rust-lang/crates.io-index" 70 | 71 | [[package]] 72 | name = "httparse" 73 | version = "1.2.1" 74 | source = "registry+https://github.com/rust-lang/crates.io-index" 75 | 76 | [[package]] 77 | name = "kernel32-sys" 78 | version = "0.2.2" 79 | source = "registry+https://github.com/rust-lang/crates.io-index" 80 | dependencies = [ 81 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 82 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 83 | ] 84 | 85 | [[package]] 86 | name = "lazycell" 87 | version = "0.4.0" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | 90 | [[package]] 91 | name = "libc" 92 | version = "0.2.20" 93 | source = "registry+https://github.com/rust-lang/crates.io-index" 94 | 95 | [[package]] 96 | name = "log" 97 | version = "0.3.6" 98 | source = "registry+https://github.com/rust-lang/crates.io-index" 99 | 100 | [[package]] 101 | name = "md5" 102 | version = "0.2.1" 103 | source = "registry+https://github.com/rust-lang/crates.io-index" 104 | 105 | [[package]] 106 | name = "memchr" 107 | version = "0.1.11" 108 | source = "registry+https://github.com/rust-lang/crates.io-index" 109 | dependencies = [ 110 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 111 | ] 112 | 113 | [[package]] 114 | name = "mio" 115 | version = "0.6.4" 116 | source = "registry+https://github.com/rust-lang/crates.io-index" 117 | dependencies = [ 118 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 119 | "lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 120 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 121 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 122 | "miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 123 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 124 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 125 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 126 | ] 127 | 128 | [[package]] 129 | name = "miow" 130 | version = "0.2.0" 131 | source = "registry+https://github.com/rust-lang/crates.io-index" 132 | dependencies = [ 133 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 134 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 135 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 136 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 137 | ] 138 | 139 | [[package]] 140 | name = "net2" 141 | version = "0.2.26" 142 | source = "registry+https://github.com/rust-lang/crates.io-index" 143 | dependencies = [ 144 | "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 145 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 146 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 147 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 148 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 149 | ] 150 | 151 | [[package]] 152 | name = "num_cpus" 153 | version = "1.2.1" 154 | source = "registry+https://github.com/rust-lang/crates.io-index" 155 | dependencies = [ 156 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 157 | ] 158 | 159 | [[package]] 160 | name = "phf" 161 | version = "0.7.20" 162 | source = "registry+https://github.com/rust-lang/crates.io-index" 163 | dependencies = [ 164 | "phf_shared 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)", 165 | ] 166 | 167 | [[package]] 168 | name = "phf_shared" 169 | version = "0.7.20" 170 | source = "registry+https://github.com/rust-lang/crates.io-index" 171 | 172 | [[package]] 173 | name = "postgres" 174 | version = "0.13.5" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | dependencies = [ 177 | "bufstream 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 178 | "fallible-iterator 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", 179 | "hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 180 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 181 | "postgres-protocol 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 182 | "postgres-shared 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 183 | ] 184 | 185 | [[package]] 186 | name = "postgres-protocol" 187 | version = "0.2.0" 188 | source = "registry+https://github.com/rust-lang/crates.io-index" 189 | dependencies = [ 190 | "byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", 191 | "fallible-iterator 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", 192 | "hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 193 | "md5 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 194 | "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", 195 | ] 196 | 197 | [[package]] 198 | name = "postgres-shared" 199 | version = "0.1.0" 200 | source = "registry+https://github.com/rust-lang/crates.io-index" 201 | dependencies = [ 202 | "fallible-iterator 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", 203 | "hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 204 | "phf 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)", 205 | "postgres-protocol 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 206 | ] 207 | 208 | [[package]] 209 | name = "r2d2" 210 | version = "0.7.1" 211 | source = "registry+https://github.com/rust-lang/crates.io-index" 212 | dependencies = [ 213 | "antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 214 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 215 | ] 216 | 217 | [[package]] 218 | name = "r2d2_postgres" 219 | version = "0.11.1" 220 | source = "registry+https://github.com/rust-lang/crates.io-index" 221 | dependencies = [ 222 | "postgres 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)", 223 | "r2d2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", 224 | ] 225 | 226 | [[package]] 227 | name = "rand" 228 | version = "0.3.15" 229 | source = "registry+https://github.com/rust-lang/crates.io-index" 230 | dependencies = [ 231 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 232 | ] 233 | 234 | [[package]] 235 | name = "redox_syscall" 236 | version = "0.1.16" 237 | source = "registry+https://github.com/rust-lang/crates.io-index" 238 | 239 | [[package]] 240 | name = "rustc-serialize" 241 | version = "0.3.22" 242 | source = "registry+https://github.com/rust-lang/crates.io-index" 243 | 244 | [[package]] 245 | name = "scoped-tls" 246 | version = "0.1.0" 247 | source = "registry+https://github.com/rust-lang/crates.io-index" 248 | 249 | [[package]] 250 | name = "slab" 251 | version = "0.3.0" 252 | source = "registry+https://github.com/rust-lang/crates.io-index" 253 | 254 | [[package]] 255 | name = "smallvec" 256 | version = "0.2.1" 257 | source = "registry+https://github.com/rust-lang/crates.io-index" 258 | 259 | [[package]] 260 | name = "take" 261 | version = "0.1.0" 262 | source = "registry+https://github.com/rust-lang/crates.io-index" 263 | 264 | [[package]] 265 | name = "time" 266 | version = "0.1.36" 267 | source = "registry+https://github.com/rust-lang/crates.io-index" 268 | dependencies = [ 269 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 270 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 271 | "redox_syscall 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", 272 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 273 | ] 274 | 275 | [[package]] 276 | name = "tokio-core" 277 | version = "0.1.4" 278 | source = "registry+https://github.com/rust-lang/crates.io-index" 279 | dependencies = [ 280 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 281 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 282 | "mio 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", 283 | "scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 284 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 285 | ] 286 | 287 | [[package]] 288 | name = "tokio-minihttp" 289 | version = "0.1.0" 290 | source = "git+https://github.com/tokio-rs/tokio-minihttp#753a94c138921f1962fcc8a0c38a4edcf9c0c2a6" 291 | dependencies = [ 292 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 293 | "httparse 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 294 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 295 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 296 | "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", 297 | "tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 298 | "tokio-proto 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 299 | "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 300 | ] 301 | 302 | [[package]] 303 | name = "tokio-proto" 304 | version = "0.1.0" 305 | source = "registry+https://github.com/rust-lang/crates.io-index" 306 | dependencies = [ 307 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 308 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 309 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 310 | "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", 311 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 312 | "smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 313 | "take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 314 | "tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 315 | "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 316 | ] 317 | 318 | [[package]] 319 | name = "tokio-service" 320 | version = "0.1.0" 321 | source = "registry+https://github.com/rust-lang/crates.io-index" 322 | dependencies = [ 323 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 324 | ] 325 | 326 | [[package]] 327 | name = "winapi" 328 | version = "0.2.8" 329 | source = "registry+https://github.com/rust-lang/crates.io-index" 330 | 331 | [[package]] 332 | name = "winapi-build" 333 | version = "0.1.1" 334 | source = "registry+https://github.com/rust-lang/crates.io-index" 335 | 336 | [[package]] 337 | name = "ws2_32-sys" 338 | version = "0.2.1" 339 | source = "registry+https://github.com/rust-lang/crates.io-index" 340 | dependencies = [ 341 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 342 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 343 | ] 344 | 345 | [metadata] 346 | "checksum antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "34fde25430d87a9388dadbe6e34d7f72a462c8b43ac8d309b42b0a8505d7e2a5" 347 | "checksum bufstream 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7b48dbe2ff0e98fa2f03377d204a9637d3c9816cd431bfe05a8abbd0ea11d074" 348 | "checksum byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855" 349 | "checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" 350 | "checksum crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0c5ea215664ca264da8a9d9c3be80d2eaf30923c259d03e870388eb927508f97" 351 | "checksum fallible-iterator 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5d48ab1bc11a086628e8cc0cc2c2dc200b884ac05c4b48fb71d6036b6999ff1d" 352 | "checksum futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "7463cac13a9c96d3fb5a0d2a653c5cbd6e79997ba153cf407117659aa97afe9b" 353 | "checksum futures-cpupool 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bb982bb25cd8fa5da6a8eb3a460354c984ff1113da82bcb4f0b0862b5795db82" 354 | "checksum hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d6a22814455d41612f41161581c2883c0c6a1c41852729b17d5ed88f01e153aa" 355 | "checksum httparse 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a6e7a63e511f9edffbab707141fbb8707d1a3098615fb2adbd5769cdfcc9b17d" 356 | "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" 357 | "checksum lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce12306c4739d86ee97c23139f3a34ddf0387bbf181bc7929d287025a8c3ef6b" 358 | "checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5" 359 | "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" 360 | "checksum md5 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7df230903ccdffd6b3b4ec21624498ea64c912ce50297846907f0b8e1bb249dd" 361 | "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" 362 | "checksum mio 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "eecdbdd49a849336e77b453f021c89972a2cfb5b51931a0026ae0ac4602de681" 363 | "checksum miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3a78d2605eb97302c10cf944b8d96b0a2a890c52957caf92fcd1f24f69049579" 364 | "checksum net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)" = "5edf9cb6be97212423aed9413dd4729d62b370b5e1c571750e882cebbbc1e3e2" 365 | "checksum num_cpus 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a225d1e2717567599c24f88e49f00856c6e825a12125181ee42c4257e3688d39" 366 | "checksum phf 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)" = "0c6afb2057bb5f846a7b75703f90bc1cef4970c35209f712925db7768e999202" 367 | "checksum phf_shared 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)" = "286385a0e50d4147bce15b2c19f0cf84c395b0e061aaf840898a7bf664c2cfb7" 368 | "checksum postgres 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)" = "585ca978431cddac0aa926246f18fe30a47401eabbe9bbda573dc60389c10ea1" 369 | "checksum postgres-protocol 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "283e27d237a5772ef00c9e3f97e632f9a565ff514761af3e88e129576af7077c" 370 | "checksum postgres-shared 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f09b8819c2586032ed23bfbe95f6edfbebdc18bf9d0fe02c1f785f659958fbb" 371 | "checksum r2d2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ecfed1b03be2e66624ec87cef173dad54253f25405bd3c918b321e4dda3ad32" 372 | "checksum r2d2_postgres 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d9ea3d4773725bf025184675ad9c7328609e8e4ddd27557dd0b67fff29bf2c2f" 373 | "checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" 374 | "checksum redox_syscall 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "8dd35cc9a8bdec562c757e3d43c1526b5c6d2653e23e2315065bc25556550753" 375 | "checksum rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "237546c689f20bb44980270c73c3b9edd0891c1be49cc1274406134a66d3957b" 376 | "checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d" 377 | "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" 378 | "checksum smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c8cbcd6df1e117c2210e13ab5109635ad68a929fcbb8964dc965b76cb5ee013" 379 | "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" 380 | "checksum time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "211b63c112206356ef1ff9b19355f43740fc3f85960c598a93d3a3d3ba7beade" 381 | "checksum tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3d1be481b55126f02ef88ff86748086473cb537a949fc4a8f4be403a530ae54b" 382 | "checksum tokio-minihttp 0.1.0 (git+https://github.com/tokio-rs/tokio-minihttp)" = "" 383 | "checksum tokio-proto 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c0d6031f94d78d7b4d509d4a7c5e1cdf524a17e7b08d1c188a83cf720e69808" 384 | "checksum tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "24da22d077e0f15f55162bdbdc661228c1581892f52074fb242678d015b45162" 385 | "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" 386 | "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" 387 | "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" 388 | -------------------------------------------------------------------------------- /db/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "db" 3 | version = "0.1.0" 4 | authors = ["stuart"] 5 | 6 | [dependencies] 7 | futures = "0.1" 8 | futures-cpupool = "0.1" 9 | tokio-core = "0.1" 10 | tokio-service = "0.1" 11 | tokio-proto = "0.1" 12 | tokio-minihttp = { git = "https://github.com/tokio-rs/tokio-minihttp" } 13 | postgres = "0.13" 14 | r2d2 = "0.7" 15 | r2d2_postgres = "0.11" 16 | rustc-serialize = "0.3" 17 | rand = "0.3" 18 | -------------------------------------------------------------------------------- /db/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate futures; 2 | extern crate tokio_proto; 3 | extern crate tokio_service; 4 | 5 | extern crate tokio_minihttp; 6 | extern crate r2d2; 7 | extern crate r2d2_postgres; 8 | 9 | extern crate futures_cpupool; 10 | extern crate rand; 11 | extern crate rustc_serialize; 12 | 13 | use std::io; 14 | use rand::Rng; 15 | use tokio_proto::TcpServer; 16 | use tokio_service::Service; 17 | use futures::{ Future, BoxFuture }; 18 | use futures_cpupool::CpuPool; 19 | use r2d2_postgres::{ PostgresConnectionManager, TlsMode }; 20 | use tokio_minihttp::{ Request, Response }; 21 | 22 | struct Server { 23 | thread_pool: CpuPool, 24 | db_pool: r2d2::Pool 25 | } 26 | 27 | #[derive(RustcEncodable)] 28 | struct Message { 29 | id: i32, 30 | random_number: i32 31 | } 32 | 33 | impl Service for Server { 34 | type Request = Request; 35 | type Response = Response; 36 | type Error = io::Error; 37 | type Future = BoxFuture; 38 | 39 | fn call(&self, req: Request) -> Self::Future { 40 | assert_eq!(req.path(), "/db"); 41 | 42 | let random_id = rand::thread_rng().gen_range(0, 10_000); 43 | let db = self.db_pool.clone(); 44 | let msg = self.thread_pool.spawn_fn(move || { 45 | let conn = db.get().map_err(|e| { 46 | io::Error::new(io::ErrorKind::Other, 47 | format!("timeout: {}", e)) 48 | })?; 49 | let stmt = conn.prepare_cached("select * from world where id = ?")?; 50 | let rows = stmt.query(&[&random_id])?; 51 | let row = rows.get(0); 52 | 53 | Ok(Message { 54 | id: row.get("id"), 55 | random_number: row.get("randomNumber") 56 | }) 57 | }); 58 | 59 | msg.map(|msg| { 60 | let json = rustc_serialize::json::encode(&msg).unwrap(); 61 | let mut response = Response::new(); 62 | response.header("Content-Type", "application/json"); 63 | response.body(&json); 64 | response 65 | }).boxed() 66 | } 67 | } 68 | 69 | fn main() { 70 | let addr: std::net::SocketAddr = "127.0.0.1:3000".parse().unwrap(); 71 | let thread_pool = CpuPool::new(10); 72 | let db_url = "postgres://postgres@localhost"; 73 | let db_config = r2d2::Config::default(); 74 | let db_manager = PostgresConnectionManager::new(db_url, TlsMode::None).unwrap(); 75 | let db_pool = r2d2::Pool::new(db_config, db_manager).unwrap(); 76 | 77 | TcpServer::new(tokio_minihttp::Http, addr).serve(move || { 78 | Ok(Server { 79 | thread_pool: thread_pool.clone(), 80 | db_pool: db_pool.clone() 81 | }) 82 | }); 83 | } 84 | -------------------------------------------------------------------------------- /echo-server/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "echo-server" 3 | version = "0.1.0" 4 | dependencies = [ 5 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 6 | "tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 7 | "tokio-proto 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 8 | "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 9 | ] 10 | 11 | [[package]] 12 | name = "cfg-if" 13 | version = "0.1.0" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | 16 | [[package]] 17 | name = "futures" 18 | version = "0.1.9" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | dependencies = [ 21 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 22 | ] 23 | 24 | [[package]] 25 | name = "kernel32-sys" 26 | version = "0.2.2" 27 | source = "registry+https://github.com/rust-lang/crates.io-index" 28 | dependencies = [ 29 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 30 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 31 | ] 32 | 33 | [[package]] 34 | name = "lazycell" 35 | version = "0.4.0" 36 | source = "registry+https://github.com/rust-lang/crates.io-index" 37 | 38 | [[package]] 39 | name = "libc" 40 | version = "0.2.20" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | 43 | [[package]] 44 | name = "log" 45 | version = "0.3.6" 46 | source = "registry+https://github.com/rust-lang/crates.io-index" 47 | 48 | [[package]] 49 | name = "mio" 50 | version = "0.6.3" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | dependencies = [ 53 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 54 | "lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 55 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 56 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 57 | "miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 58 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 59 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 60 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 61 | ] 62 | 63 | [[package]] 64 | name = "miow" 65 | version = "0.2.0" 66 | source = "registry+https://github.com/rust-lang/crates.io-index" 67 | dependencies = [ 68 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 69 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 70 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 71 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 72 | ] 73 | 74 | [[package]] 75 | name = "net2" 76 | version = "0.2.26" 77 | source = "registry+https://github.com/rust-lang/crates.io-index" 78 | dependencies = [ 79 | "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 80 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 81 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 82 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 83 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 84 | ] 85 | 86 | [[package]] 87 | name = "rand" 88 | version = "0.3.15" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | dependencies = [ 91 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 92 | ] 93 | 94 | [[package]] 95 | name = "scoped-tls" 96 | version = "0.1.0" 97 | source = "registry+https://github.com/rust-lang/crates.io-index" 98 | 99 | [[package]] 100 | name = "slab" 101 | version = "0.3.0" 102 | source = "registry+https://github.com/rust-lang/crates.io-index" 103 | 104 | [[package]] 105 | name = "smallvec" 106 | version = "0.2.1" 107 | source = "registry+https://github.com/rust-lang/crates.io-index" 108 | 109 | [[package]] 110 | name = "take" 111 | version = "0.1.0" 112 | source = "registry+https://github.com/rust-lang/crates.io-index" 113 | 114 | [[package]] 115 | name = "tokio-core" 116 | version = "0.1.4" 117 | source = "registry+https://github.com/rust-lang/crates.io-index" 118 | dependencies = [ 119 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 120 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 121 | "mio 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", 122 | "scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 123 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 124 | ] 125 | 126 | [[package]] 127 | name = "tokio-proto" 128 | version = "0.1.0" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | dependencies = [ 131 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 132 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 133 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 134 | "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", 135 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 136 | "smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 137 | "take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 138 | "tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 139 | "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 140 | ] 141 | 142 | [[package]] 143 | name = "tokio-service" 144 | version = "0.1.0" 145 | source = "registry+https://github.com/rust-lang/crates.io-index" 146 | dependencies = [ 147 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 148 | ] 149 | 150 | [[package]] 151 | name = "winapi" 152 | version = "0.2.8" 153 | source = "registry+https://github.com/rust-lang/crates.io-index" 154 | 155 | [[package]] 156 | name = "winapi-build" 157 | version = "0.1.1" 158 | source = "registry+https://github.com/rust-lang/crates.io-index" 159 | 160 | [[package]] 161 | name = "ws2_32-sys" 162 | version = "0.2.1" 163 | source = "registry+https://github.com/rust-lang/crates.io-index" 164 | dependencies = [ 165 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 166 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 167 | ] 168 | 169 | -------------------------------------------------------------------------------- /echo-server/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "echo-server" 3 | version = "0.1.0" 4 | authors = ["stuart"] 5 | 6 | [dependencies] 7 | futures = "0.1" 8 | tokio-core = "0.1" 9 | tokio-service = "0.1" 10 | tokio-proto = "0.1" 11 | -------------------------------------------------------------------------------- /echo-server/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate futures; 2 | extern crate tokio_core; 3 | extern crate tokio_proto; 4 | extern crate tokio_service; 5 | 6 | use std::io; 7 | use std::str; 8 | use tokio_core::io::{ Codec, EasyBuf, Io, Framed }; 9 | use tokio_proto::TcpServer; 10 | use tokio_proto::pipeline::ServerProto; 11 | use tokio_service::Service; 12 | use futures::{ future, Future, BoxFuture }; 13 | 14 | pub struct LineCodec; 15 | 16 | impl Codec for LineCodec { 17 | type In = String; 18 | type Out = String; 19 | 20 | fn decode(&mut self, buf: &mut EasyBuf) -> io::Result> { 21 | if let Some(i) = buf.as_slice().iter().position(|&b| b == b'\n') { 22 | let line = buf.drain_to(i); 23 | buf.drain_to(1); 24 | 25 | match str::from_utf8(line.as_slice()) { 26 | Ok(s) => Ok(Some(s.to_string())), 27 | Err(_) => Err(io::Error::new(io::ErrorKind::Other, "invalid UTF-8")) 28 | } 29 | } else { 30 | Ok(None) 31 | } 32 | } 33 | 34 | fn encode(&mut self, msg: String, buf: &mut Vec) -> io::Result<()> { 35 | buf.extend(msg.as_bytes()); 36 | buf.push(b'\n'); 37 | Ok(()) 38 | } 39 | } 40 | 41 | pub struct LineProto; 42 | 43 | impl ServerProto for LineProto { 44 | type Request = String; 45 | type Response = String; 46 | type Transport = Framed; 47 | type BindTransport = Result; 48 | 49 | fn bind_transport(&self, io: T) -> Self::BindTransport { 50 | Ok(io.framed(LineCodec)) 51 | } 52 | } 53 | 54 | pub struct Echo; 55 | 56 | impl Service for Echo { 57 | type Request = String; 58 | type Response = String; 59 | type Error = io::Error; 60 | type Future = BoxFuture; 61 | 62 | fn call(&self, req: Self::Request) -> Self::Future { 63 | future::ok(req).boxed() 64 | } 65 | } 66 | 67 | pub struct EchoRev; 68 | 69 | impl Service for EchoRev { 70 | type Request = String; 71 | type Response = String; 72 | type Error = io::Error; 73 | type Future = BoxFuture; 74 | 75 | fn call(&self, req: Self::Request) -> Self::Future { 76 | let rev: String = req.chars() 77 | .rev() 78 | .collect(); 79 | future::ok(rev).boxed() 80 | } 81 | } 82 | 83 | fn main() { 84 | let addr = "0.0.0.0:12345".parse().unwrap(); 85 | let server = TcpServer::new(LineProto, addr); 86 | 87 | // server.serve(|| Ok(Echo)); 88 | server.serve(|| Ok(EchoRev)); 89 | } 90 | -------------------------------------------------------------------------------- /http-tls/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "http-tls" 3 | version = "0.1.0" 4 | dependencies = [ 5 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 6 | "native-tls 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 7 | "tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 8 | "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 9 | "tokio-tls 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 10 | ] 11 | 12 | [[package]] 13 | name = "advapi32-sys" 14 | version = "0.2.0" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | dependencies = [ 17 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 18 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 19 | ] 20 | 21 | [[package]] 22 | name = "bitflags" 23 | version = "0.7.0" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | 26 | [[package]] 27 | name = "cfg-if" 28 | version = "0.1.0" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | 31 | [[package]] 32 | name = "core-foundation" 33 | version = "0.2.3" 34 | source = "registry+https://github.com/rust-lang/crates.io-index" 35 | dependencies = [ 36 | "core-foundation-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 37 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 38 | ] 39 | 40 | [[package]] 41 | name = "core-foundation-sys" 42 | version = "0.2.3" 43 | source = "registry+https://github.com/rust-lang/crates.io-index" 44 | dependencies = [ 45 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 46 | ] 47 | 48 | [[package]] 49 | name = "crypt32-sys" 50 | version = "0.2.0" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | dependencies = [ 53 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 54 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 55 | ] 56 | 57 | [[package]] 58 | name = "error-chain" 59 | version = "0.7.2" 60 | source = "registry+https://github.com/rust-lang/crates.io-index" 61 | 62 | [[package]] 63 | name = "futures" 64 | version = "0.1.9" 65 | source = "registry+https://github.com/rust-lang/crates.io-index" 66 | dependencies = [ 67 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 68 | ] 69 | 70 | [[package]] 71 | name = "gdi32-sys" 72 | version = "0.2.0" 73 | source = "registry+https://github.com/rust-lang/crates.io-index" 74 | dependencies = [ 75 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 76 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 77 | ] 78 | 79 | [[package]] 80 | name = "kernel32-sys" 81 | version = "0.2.2" 82 | source = "registry+https://github.com/rust-lang/crates.io-index" 83 | dependencies = [ 84 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 85 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 86 | ] 87 | 88 | [[package]] 89 | name = "lazy_static" 90 | version = "0.2.2" 91 | source = "registry+https://github.com/rust-lang/crates.io-index" 92 | 93 | [[package]] 94 | name = "lazycell" 95 | version = "0.4.0" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | 98 | [[package]] 99 | name = "libc" 100 | version = "0.2.20" 101 | source = "registry+https://github.com/rust-lang/crates.io-index" 102 | 103 | [[package]] 104 | name = "log" 105 | version = "0.3.6" 106 | source = "registry+https://github.com/rust-lang/crates.io-index" 107 | 108 | [[package]] 109 | name = "metadeps" 110 | version = "1.1.1" 111 | source = "registry+https://github.com/rust-lang/crates.io-index" 112 | dependencies = [ 113 | "error-chain 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", 114 | "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", 115 | "toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 116 | ] 117 | 118 | [[package]] 119 | name = "mio" 120 | version = "0.6.4" 121 | source = "registry+https://github.com/rust-lang/crates.io-index" 122 | dependencies = [ 123 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 124 | "lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 125 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 126 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 127 | "miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 128 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 129 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 130 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 131 | ] 132 | 133 | [[package]] 134 | name = "miow" 135 | version = "0.2.0" 136 | source = "registry+https://github.com/rust-lang/crates.io-index" 137 | dependencies = [ 138 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 139 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 140 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 141 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 142 | ] 143 | 144 | [[package]] 145 | name = "native-tls" 146 | version = "0.1.1" 147 | source = "registry+https://github.com/rust-lang/crates.io-index" 148 | dependencies = [ 149 | "openssl 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)", 150 | "schannel 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 151 | "security-framework 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 152 | "security-framework-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 153 | "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", 154 | ] 155 | 156 | [[package]] 157 | name = "net2" 158 | version = "0.2.26" 159 | source = "registry+https://github.com/rust-lang/crates.io-index" 160 | dependencies = [ 161 | "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 162 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 163 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 164 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 165 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 166 | ] 167 | 168 | [[package]] 169 | name = "openssl" 170 | version = "0.9.6" 171 | source = "registry+https://github.com/rust-lang/crates.io-index" 172 | dependencies = [ 173 | "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", 174 | "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 175 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 176 | "openssl-sys 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)", 177 | ] 178 | 179 | [[package]] 180 | name = "openssl-sys" 181 | version = "0.9.6" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | dependencies = [ 184 | "gdi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 185 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 186 | "metadeps 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 187 | "user32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 188 | ] 189 | 190 | [[package]] 191 | name = "pkg-config" 192 | version = "0.3.9" 193 | source = "registry+https://github.com/rust-lang/crates.io-index" 194 | 195 | [[package]] 196 | name = "rand" 197 | version = "0.3.15" 198 | source = "registry+https://github.com/rust-lang/crates.io-index" 199 | dependencies = [ 200 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 201 | ] 202 | 203 | [[package]] 204 | name = "schannel" 205 | version = "0.1.2" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | dependencies = [ 208 | "advapi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 209 | "crypt32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 210 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 211 | "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 212 | "secur32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 213 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 214 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 215 | ] 216 | 217 | [[package]] 218 | name = "scoped-tls" 219 | version = "0.1.0" 220 | source = "registry+https://github.com/rust-lang/crates.io-index" 221 | 222 | [[package]] 223 | name = "secur32-sys" 224 | version = "0.2.0" 225 | source = "registry+https://github.com/rust-lang/crates.io-index" 226 | dependencies = [ 227 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 228 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 229 | ] 230 | 231 | [[package]] 232 | name = "security-framework" 233 | version = "0.1.10" 234 | source = "registry+https://github.com/rust-lang/crates.io-index" 235 | dependencies = [ 236 | "core-foundation 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 237 | "core-foundation-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 238 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 239 | "security-framework-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 240 | ] 241 | 242 | [[package]] 243 | name = "security-framework-sys" 244 | version = "0.1.10" 245 | source = "registry+https://github.com/rust-lang/crates.io-index" 246 | dependencies = [ 247 | "core-foundation-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 248 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 249 | ] 250 | 251 | [[package]] 252 | name = "slab" 253 | version = "0.3.0" 254 | source = "registry+https://github.com/rust-lang/crates.io-index" 255 | 256 | [[package]] 257 | name = "tempdir" 258 | version = "0.3.5" 259 | source = "registry+https://github.com/rust-lang/crates.io-index" 260 | dependencies = [ 261 | "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", 262 | ] 263 | 264 | [[package]] 265 | name = "tokio-core" 266 | version = "0.1.4" 267 | source = "registry+https://github.com/rust-lang/crates.io-index" 268 | dependencies = [ 269 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 270 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 271 | "mio 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", 272 | "scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 273 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 274 | ] 275 | 276 | [[package]] 277 | name = "tokio-service" 278 | version = "0.1.0" 279 | source = "registry+https://github.com/rust-lang/crates.io-index" 280 | dependencies = [ 281 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 282 | ] 283 | 284 | [[package]] 285 | name = "tokio-tls" 286 | version = "0.1.1" 287 | source = "registry+https://github.com/rust-lang/crates.io-index" 288 | dependencies = [ 289 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 290 | "native-tls 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 291 | "tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 292 | ] 293 | 294 | [[package]] 295 | name = "toml" 296 | version = "0.2.1" 297 | source = "registry+https://github.com/rust-lang/crates.io-index" 298 | 299 | [[package]] 300 | name = "user32-sys" 301 | version = "0.2.0" 302 | source = "registry+https://github.com/rust-lang/crates.io-index" 303 | dependencies = [ 304 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 305 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 306 | ] 307 | 308 | [[package]] 309 | name = "winapi" 310 | version = "0.2.8" 311 | source = "registry+https://github.com/rust-lang/crates.io-index" 312 | 313 | [[package]] 314 | name = "winapi-build" 315 | version = "0.1.1" 316 | source = "registry+https://github.com/rust-lang/crates.io-index" 317 | 318 | [[package]] 319 | name = "ws2_32-sys" 320 | version = "0.2.1" 321 | source = "registry+https://github.com/rust-lang/crates.io-index" 322 | dependencies = [ 323 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 324 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 325 | ] 326 | 327 | [metadata] 328 | "checksum advapi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e06588080cb19d0acb6739808aafa5f26bfb2ca015b2b6370028b44cf7cb8a9a" 329 | "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" 330 | "checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" 331 | "checksum core-foundation 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "25bfd746d203017f7d5cbd31ee5d8e17f94b6521c7af77ece6c9e4b2d4b16c67" 332 | "checksum core-foundation-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "065a5d7ffdcbc8fa145d6f0746f3555025b9097a9e9cda59f7467abae670c78d" 333 | "checksum crypt32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e34988f7e069e0b2f3bfc064295161e489b2d4e04a2e4248fb94360cdf00b4ec" 334 | "checksum error-chain 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "318cb3c71ee4cdea69fdc9e15c173b245ed6063e1709029e8fd32525a881120f" 335 | "checksum futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "7463cac13a9c96d3fb5a0d2a653c5cbd6e79997ba153cf407117659aa97afe9b" 336 | "checksum gdi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0912515a8ff24ba900422ecda800b52f4016a56251922d397c576bf92c690518" 337 | "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" 338 | "checksum lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6abe0ee2e758cd6bc8a2cd56726359007748fbf4128da998b65d0b70f881e19b" 339 | "checksum lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce12306c4739d86ee97c23139f3a34ddf0387bbf181bc7929d287025a8c3ef6b" 340 | "checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5" 341 | "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" 342 | "checksum metadeps 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "829fffe7ea1d747e23f64be972991bc516b2f1ac2ae4a3b33d8bea150c410151" 343 | "checksum mio 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "eecdbdd49a849336e77b453f021c89972a2cfb5b51931a0026ae0ac4602de681" 344 | "checksum miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3a78d2605eb97302c10cf944b8d96b0a2a890c52957caf92fcd1f24f69049579" 345 | "checksum native-tls 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b805ee0e8fa268f67a4e5c7f4f80adb8af1fc4428ea0ce5b0ecab1430ef17ec0" 346 | "checksum net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)" = "5edf9cb6be97212423aed9413dd4729d62b370b5e1c571750e882cebbbc1e3e2" 347 | "checksum openssl 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0c00da69323449142e00a5410f0e022b39e8bbb7dc569cee8fc6af279279483c" 348 | "checksum openssl-sys 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b1482f9a06f56c906007e17ea14d73d102210b5d27bc948bf5e175f493f3f7c3" 349 | "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" 350 | "checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" 351 | "checksum schannel 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0168331892e26bcd763535c1edd4b850708d0288b0e73942c116bbbf8e903c7f" 352 | "checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d" 353 | "checksum secur32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3f412dfa83308d893101dd59c10d6fda8283465976c28c287c5c855bf8d216bc" 354 | "checksum security-framework 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d7c1ff1c71e4e4474b46ded6687f0c28c721de2f5a05577e7f533d36330e4e3a" 355 | "checksum security-framework-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "5103c988054803538fe4d85333abf4c633f069510ab687dc71a50572104216d0" 356 | "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" 357 | "checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6" 358 | "checksum tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3d1be481b55126f02ef88ff86748086473cb537a949fc4a8f4be403a530ae54b" 359 | "checksum tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "24da22d077e0f15f55162bdbdc661228c1581892f52074fb242678d015b45162" 360 | "checksum tokio-tls 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a85d8a0e53d372cd25ee2e498d23d4496a318f5a1b9b3f959bfcdfac4f094d2" 361 | "checksum toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "736b60249cb25337bc196faa43ee12c705e426f3d55c214d73a4e7be06f92cb4" 362 | "checksum user32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4ef4711d107b21b410a3a974b1204d9accc8b10dad75d8324b5d755de1617d47" 363 | "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" 364 | "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" 365 | "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" 366 | -------------------------------------------------------------------------------- /http-tls/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "http-tls" 3 | version = "0.1.0" 4 | authors = ["stuart"] 5 | 6 | [dependencies] 7 | futures = "0.1" 8 | tokio-core = "0.1" 9 | tokio-service = "0.1" 10 | native-tls = "0.1" 11 | tokio-tls = "0.1" 12 | -------------------------------------------------------------------------------- /http-tls/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate futures; 2 | extern crate native_tls; 3 | extern crate tokio_core; 4 | extern crate tokio_tls; 5 | 6 | use std::io; 7 | use std::net::ToSocketAddrs; 8 | 9 | use futures::Future; 10 | use native_tls::TlsConnector; 11 | use tokio_core::net::TcpStream; 12 | use tokio_core::reactor::Core; 13 | use tokio_tls::TlsConnectorExt; 14 | 15 | fn main() { 16 | let mut core = Core::new().unwrap(); 17 | let handle = core.handle(); 18 | let addr = "www.rust-lang.org:443".to_socket_addrs().unwrap().next().unwrap(); 19 | 20 | let cx = TlsConnector::builder().unwrap().build().unwrap(); 21 | let socket = TcpStream::connect(&addr, &handle); 22 | 23 | let tls_handshake = socket.and_then(|socket| { 24 | let tls = cx.connect_async("www.rust-lang.org", socket); 25 | tls.map_err(|e| io::Error::new(io::ErrorKind::Other, e)) 26 | }); 27 | 28 | let request = tls_handshake.and_then(|socket| { 29 | tokio_core::io::write_all(socket, "\ 30 | GET / HTTP/1.0\r\n\ 31 | Host: www.rust-lang.org\r\n\ 32 | \r\n\ 33 | ".as_bytes()) 34 | }); 35 | 36 | let response = request.and_then(|(socket, _request)| { 37 | tokio_core::io::read_to_end(socket, Vec::new()) 38 | }); 39 | 40 | let (_socket, data) = core.run(response).unwrap(); 41 | println!("{}", String::from_utf8_lossy(&data)); 42 | } 43 | -------------------------------------------------------------------------------- /pipelined-server/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "pipelined-server" 3 | version = "0.1.0" 4 | dependencies = [ 5 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 6 | "tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 7 | "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 8 | ] 9 | 10 | [[package]] 11 | name = "cfg-if" 12 | version = "0.1.0" 13 | source = "registry+https://github.com/rust-lang/crates.io-index" 14 | 15 | [[package]] 16 | name = "futures" 17 | version = "0.1.9" 18 | source = "registry+https://github.com/rust-lang/crates.io-index" 19 | dependencies = [ 20 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 21 | ] 22 | 23 | [[package]] 24 | name = "kernel32-sys" 25 | version = "0.2.2" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | dependencies = [ 28 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 29 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 30 | ] 31 | 32 | [[package]] 33 | name = "lazycell" 34 | version = "0.4.0" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | 37 | [[package]] 38 | name = "libc" 39 | version = "0.2.20" 40 | source = "registry+https://github.com/rust-lang/crates.io-index" 41 | 42 | [[package]] 43 | name = "log" 44 | version = "0.3.6" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | 47 | [[package]] 48 | name = "mio" 49 | version = "0.6.4" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | dependencies = [ 52 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 53 | "lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 54 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 55 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 56 | "miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 57 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 58 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 59 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 60 | ] 61 | 62 | [[package]] 63 | name = "miow" 64 | version = "0.2.0" 65 | source = "registry+https://github.com/rust-lang/crates.io-index" 66 | dependencies = [ 67 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 68 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 69 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 70 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 71 | ] 72 | 73 | [[package]] 74 | name = "net2" 75 | version = "0.2.26" 76 | source = "registry+https://github.com/rust-lang/crates.io-index" 77 | dependencies = [ 78 | "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 79 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 80 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 81 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 82 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 83 | ] 84 | 85 | [[package]] 86 | name = "scoped-tls" 87 | version = "0.1.0" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | 90 | [[package]] 91 | name = "slab" 92 | version = "0.3.0" 93 | source = "registry+https://github.com/rust-lang/crates.io-index" 94 | 95 | [[package]] 96 | name = "tokio-core" 97 | version = "0.1.4" 98 | source = "registry+https://github.com/rust-lang/crates.io-index" 99 | dependencies = [ 100 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 101 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 102 | "mio 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", 103 | "scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 104 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 105 | ] 106 | 107 | [[package]] 108 | name = "tokio-service" 109 | version = "0.1.0" 110 | source = "registry+https://github.com/rust-lang/crates.io-index" 111 | dependencies = [ 112 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 113 | ] 114 | 115 | [[package]] 116 | name = "winapi" 117 | version = "0.2.8" 118 | source = "registry+https://github.com/rust-lang/crates.io-index" 119 | 120 | [[package]] 121 | name = "winapi-build" 122 | version = "0.1.1" 123 | source = "registry+https://github.com/rust-lang/crates.io-index" 124 | 125 | [[package]] 126 | name = "ws2_32-sys" 127 | version = "0.2.1" 128 | source = "registry+https://github.com/rust-lang/crates.io-index" 129 | dependencies = [ 130 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 131 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 132 | ] 133 | 134 | [metadata] 135 | "checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" 136 | "checksum futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "7463cac13a9c96d3fb5a0d2a653c5cbd6e79997ba153cf407117659aa97afe9b" 137 | "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" 138 | "checksum lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce12306c4739d86ee97c23139f3a34ddf0387bbf181bc7929d287025a8c3ef6b" 139 | "checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5" 140 | "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" 141 | "checksum mio 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "eecdbdd49a849336e77b453f021c89972a2cfb5b51931a0026ae0ac4602de681" 142 | "checksum miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3a78d2605eb97302c10cf944b8d96b0a2a890c52957caf92fcd1f24f69049579" 143 | "checksum net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)" = "5edf9cb6be97212423aed9413dd4729d62b370b5e1c571750e882cebbbc1e3e2" 144 | "checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d" 145 | "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" 146 | "checksum tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3d1be481b55126f02ef88ff86748086473cb537a949fc4a8f4be403a530ae54b" 147 | "checksum tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "24da22d077e0f15f55162bdbdc661228c1581892f52074fb242678d015b45162" 148 | "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" 149 | "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" 150 | "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" 151 | -------------------------------------------------------------------------------- /pipelined-server/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pipelined-server" 3 | version = "0.1.0" 4 | authors = ["stuart"] 5 | 6 | [dependencies] 7 | futures = "0.1" 8 | tokio-core = "0.1" 9 | tokio-service = "0.1" 10 | -------------------------------------------------------------------------------- /pipelined-server/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate futures; 2 | extern crate tokio_core; 3 | extern crate tokio_service; 4 | 5 | use std::io; 6 | use std::str; 7 | use tokio_core::io::{ Codec, EasyBuf, Io }; 8 | use tokio_core::reactor::Core; 9 | use tokio_core::net::TcpListener; 10 | use tokio_service::{ Service, NewService }; 11 | use futures::{ future, Future, Stream, Sink, BoxFuture }; 12 | 13 | pub struct LineCodec; 14 | 15 | impl Codec for LineCodec { 16 | type In = String; 17 | type Out = String; 18 | 19 | fn decode(&mut self, buf: &mut EasyBuf) -> io::Result> { 20 | if let Some(i) = buf.as_slice().iter().position(|&b| b == b'\n') { 21 | let line = buf.drain_to(i); 22 | buf.drain_to(1); 23 | 24 | match str::from_utf8(line.as_slice()) { 25 | Ok(s) => Ok(Some(s.to_string())), 26 | Err(_) => Err(io::Error::new(io::ErrorKind::Other, "invalid UTF-8")) 27 | } 28 | } else { 29 | Ok(None) 30 | } 31 | } 32 | 33 | fn encode(&mut self, msg: String, buf: &mut Vec) -> io::Result<()> { 34 | buf.extend(msg.as_bytes()); 35 | buf.push(b'\n'); 36 | Ok(()) 37 | } 38 | } 39 | 40 | struct EchoService; 41 | 42 | impl Service for EchoService { 43 | type Request = String; 44 | type Response = String; 45 | type Error = io::Error; 46 | type Future = BoxFuture; 47 | fn call(&self, input: String) -> Self::Future { 48 | future::ok(input).boxed() 49 | } 50 | } 51 | 52 | fn serve(s: S) -> io::Result<()> 53 | where S: NewService + 'static 56 | { 57 | let mut core = Core::new()?; 58 | let handle = core.handle(); 59 | 60 | let address = "0.0.0.0:12345".parse().unwrap(); 61 | let listener = TcpListener::bind(&address, &handle)?; 62 | 63 | let connections = listener.incoming(); 64 | let server = connections.for_each(move |(socket, _peer_addr)| { 65 | let (writer, reader) = socket.framed(LineCodec).split(); 66 | let service = s.new_service()?; 67 | 68 | let responses = reader.and_then(move |req| service.call(req)); 69 | let server = writer.send_all(responses).then(|_| Ok(())); 70 | handle.spawn(server); 71 | 72 | Ok(()) 73 | }); 74 | 75 | core.run(server) 76 | } 77 | 78 | fn main() { 79 | if let Err(e) = serve(|| Ok(EchoService)) { 80 | println!("Server failed with {}", e); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /prime-timeout/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "prime-timeout" 3 | version = "0.1.0" 4 | dependencies = [ 5 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 6 | "futures-cpupool 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 7 | "tokio-timer 0.1.0 (git+https://github.com/tokio-rs/tokio-timer)", 8 | ] 9 | 10 | [[package]] 11 | name = "crossbeam" 12 | version = "0.2.10" 13 | source = "registry+https://github.com/rust-lang/crates.io-index" 14 | 15 | [[package]] 16 | name = "futures" 17 | version = "0.1.9" 18 | source = "registry+https://github.com/rust-lang/crates.io-index" 19 | dependencies = [ 20 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 21 | ] 22 | 23 | [[package]] 24 | name = "futures-cpupool" 25 | version = "0.1.2" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | dependencies = [ 28 | "crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", 29 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 30 | "num_cpus 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 31 | ] 32 | 33 | [[package]] 34 | name = "libc" 35 | version = "0.2.20" 36 | source = "registry+https://github.com/rust-lang/crates.io-index" 37 | 38 | [[package]] 39 | name = "log" 40 | version = "0.3.6" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | 43 | [[package]] 44 | name = "num_cpus" 45 | version = "1.2.1" 46 | source = "registry+https://github.com/rust-lang/crates.io-index" 47 | dependencies = [ 48 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 49 | ] 50 | 51 | [[package]] 52 | name = "slab" 53 | version = "0.3.0" 54 | source = "registry+https://github.com/rust-lang/crates.io-index" 55 | 56 | [[package]] 57 | name = "tokio-timer" 58 | version = "0.1.0" 59 | source = "git+https://github.com/tokio-rs/tokio-timer#d803e55b64ab1cb89d43b6802c6fd4494a570115" 60 | dependencies = [ 61 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 62 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 63 | ] 64 | 65 | [metadata] 66 | "checksum crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0c5ea215664ca264da8a9d9c3be80d2eaf30923c259d03e870388eb927508f97" 67 | "checksum futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "7463cac13a9c96d3fb5a0d2a653c5cbd6e79997ba153cf407117659aa97afe9b" 68 | "checksum futures-cpupool 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bb982bb25cd8fa5da6a8eb3a460354c984ff1113da82bcb4f0b0862b5795db82" 69 | "checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5" 70 | "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" 71 | "checksum num_cpus 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a225d1e2717567599c24f88e49f00856c6e825a12125181ee42c4257e3688d39" 72 | "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" 73 | "checksum tokio-timer 0.1.0 (git+https://github.com/tokio-rs/tokio-timer)" = "" 74 | -------------------------------------------------------------------------------- /prime-timeout/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "prime-timeout" 3 | version = "0.1.0" 4 | authors = ["stuart"] 5 | 6 | [dependencies] 7 | futures = "0.1" 8 | futures-cpupool = "0.1" 9 | tokio-timer = { git = "https://github.com/tokio-rs/tokio-timer" } 10 | -------------------------------------------------------------------------------- /prime-timeout/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate futures; 2 | extern crate futures_cpupool; 3 | extern crate tokio_timer; 4 | 5 | use std::time::Duration; 6 | use futures::Future; 7 | use futures_cpupool::CpuPool; 8 | use tokio_timer::Timer; 9 | 10 | const BIG_PRIME: u64 = 15485867; 11 | 12 | fn is_prime(num: u64) -> bool { 13 | !(2..num).any(|i| num % i == 0) 14 | } 15 | 16 | fn main() { 17 | let pool = CpuPool::new_num_cpus(); 18 | let timer = Timer::default(); 19 | 20 | let timeout = timer.sleep(Duration::from_millis(750)).then(|_| Err(())); 21 | 22 | let prime_future = pool.spawn_fn(|| { 23 | let prime = is_prime(BIG_PRIME); 24 | 25 | let res: Result = Ok(prime); 26 | res 27 | }); 28 | 29 | let winner = timeout.select(prime_future).map(|(win, _)| win); 30 | 31 | match winner.wait() { 32 | Ok(primeness) => println!("Prime? {}", primeness), 33 | Err(_) => println!("Timed out") 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /streams/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "streams" 3 | version = "0.1.0" 4 | dependencies = [ 5 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 6 | "tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 7 | ] 8 | 9 | [[package]] 10 | name = "cfg-if" 11 | version = "0.1.0" 12 | source = "registry+https://github.com/rust-lang/crates.io-index" 13 | 14 | [[package]] 15 | name = "futures" 16 | version = "0.1.9" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | dependencies = [ 19 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 20 | ] 21 | 22 | [[package]] 23 | name = "kernel32-sys" 24 | version = "0.2.2" 25 | source = "registry+https://github.com/rust-lang/crates.io-index" 26 | dependencies = [ 27 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 28 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 29 | ] 30 | 31 | [[package]] 32 | name = "lazycell" 33 | version = "0.4.0" 34 | source = "registry+https://github.com/rust-lang/crates.io-index" 35 | 36 | [[package]] 37 | name = "libc" 38 | version = "0.2.20" 39 | source = "registry+https://github.com/rust-lang/crates.io-index" 40 | 41 | [[package]] 42 | name = "log" 43 | version = "0.3.6" 44 | source = "registry+https://github.com/rust-lang/crates.io-index" 45 | 46 | [[package]] 47 | name = "mio" 48 | version = "0.6.4" 49 | source = "registry+https://github.com/rust-lang/crates.io-index" 50 | dependencies = [ 51 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 52 | "lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 53 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 54 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 55 | "miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 56 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 57 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 58 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 59 | ] 60 | 61 | [[package]] 62 | name = "miow" 63 | version = "0.2.0" 64 | source = "registry+https://github.com/rust-lang/crates.io-index" 65 | dependencies = [ 66 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 67 | "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", 68 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 69 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 70 | ] 71 | 72 | [[package]] 73 | name = "net2" 74 | version = "0.2.26" 75 | source = "registry+https://github.com/rust-lang/crates.io-index" 76 | dependencies = [ 77 | "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 78 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 79 | "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", 80 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 81 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 82 | ] 83 | 84 | [[package]] 85 | name = "scoped-tls" 86 | version = "0.1.0" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | 89 | [[package]] 90 | name = "slab" 91 | version = "0.3.0" 92 | source = "registry+https://github.com/rust-lang/crates.io-index" 93 | 94 | [[package]] 95 | name = "tokio-core" 96 | version = "0.1.4" 97 | source = "registry+https://github.com/rust-lang/crates.io-index" 98 | dependencies = [ 99 | "futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 100 | "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 101 | "mio 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", 102 | "scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 103 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 104 | ] 105 | 106 | [[package]] 107 | name = "winapi" 108 | version = "0.2.8" 109 | source = "registry+https://github.com/rust-lang/crates.io-index" 110 | 111 | [[package]] 112 | name = "winapi-build" 113 | version = "0.1.1" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | 116 | [[package]] 117 | name = "ws2_32-sys" 118 | version = "0.2.1" 119 | source = "registry+https://github.com/rust-lang/crates.io-index" 120 | dependencies = [ 121 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 122 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 123 | ] 124 | 125 | [metadata] 126 | "checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" 127 | "checksum futures 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "7463cac13a9c96d3fb5a0d2a653c5cbd6e79997ba153cf407117659aa97afe9b" 128 | "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" 129 | "checksum lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce12306c4739d86ee97c23139f3a34ddf0387bbf181bc7929d287025a8c3ef6b" 130 | "checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5" 131 | "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" 132 | "checksum mio 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "eecdbdd49a849336e77b453f021c89972a2cfb5b51931a0026ae0ac4602de681" 133 | "checksum miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3a78d2605eb97302c10cf944b8d96b0a2a890c52957caf92fcd1f24f69049579" 134 | "checksum net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)" = "5edf9cb6be97212423aed9413dd4729d62b370b5e1c571750e882cebbbc1e3e2" 135 | "checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d" 136 | "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" 137 | "checksum tokio-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3d1be481b55126f02ef88ff86748086473cb537a949fc4a8f4be403a530ae54b" 138 | "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" 139 | "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" 140 | "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" 141 | -------------------------------------------------------------------------------- /streams/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "streams" 3 | version = "0.1.0" 4 | authors = ["stuart"] 5 | 6 | [dependencies] 7 | futures = "0.1" 8 | tokio-core = "0.1" 9 | -------------------------------------------------------------------------------- /streams/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate futures; 2 | extern crate tokio_core; 3 | 4 | use futures::Future; 5 | use futures::stream::Stream; 6 | use tokio_core::reactor::Core; 7 | use tokio_core::net::TcpListener; 8 | 9 | fn main() { 10 | let mut core = Core::new().unwrap(); 11 | let address = "127.0.0.1:3000".parse().unwrap(); 12 | let listener = TcpListener::bind(&address, &core.handle()).unwrap(); 13 | 14 | let handle = core.handle(); 15 | let connections = listener.incoming(); 16 | let welcomes = connections.and_then(|(socket, _)| { 17 | let serve_one = tokio_core::io::write_all(socket, b"Hello world!\n").then(|_| Ok(())); 18 | handle.spawn(serve_one); 19 | Ok(()) 20 | }); 21 | let server = welcomes.for_each(|_| { 22 | Ok(()) 23 | }); 24 | 25 | core.run(server).unwrap(); 26 | } 27 | --------------------------------------------------------------------------------