Skip to content

Commit ecae88d

Browse files
authored
Merge pull request #4 from rsocket/develop
Develop
2 parents 777d554 + 5c79d97 commit ecae88d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2077
-1793
lines changed

Cargo.toml

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rsocket_rust"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
authors = ["Jeffsky <[email protected]>"]
55
edition = "2018"
66
license = "Apache-2.0"
@@ -12,13 +12,14 @@ description = "rsocket-rust is an implementation of the RSocket protocol in Rust
1212
[dependencies]
1313
matches = "0.1.8"
1414
log = "0.4.8"
15-
bytes = "0.5.2"
15+
bytes = "0.5.3"
1616
futures = "0.3.1"
1717
lazy_static = "1.4.0"
18+
url = "2.1.0"
1819
# reactor_rs = {git = "https://github.com/jjeffcaii/reactor-rust", branch = "develop"}
1920

2021
[dependencies.tokio]
21-
version = "0.2.2"
22+
version = "0.2.6"
2223
default-features = false
2324
features = ["full"]
2425

@@ -36,6 +37,6 @@ rand = "0.7.2"
3637
name = "echo"
3738
path = "examples/echo/main.rs"
3839

39-
# [[example]]
40-
# name = "proxy"
41-
# path = "examples/proxy/main.rs"
40+
[[example]]
41+
name = "proxy"
42+
path = "examples/proxy/main.rs"

README.md

+16-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
[![GitHub Release](https://img.shields.io/github/release-pre/rsocket/rsocket-rust.svg)](https://github.com/rsocket/rsocket-rust/releases)
77

88
> rsocket-rust is an implementation of the RSocket protocol in Rust(1.39+).
9-
It's an **alpha** version and still under active development. **Do not use it in a production environment!**
9+
It's an **alpha** version and still under active development.
10+
**Do not use it in a production environment!**
1011

1112
## Example
1213

@@ -26,13 +27,15 @@ use std::error::Error;
2627
#[tokio::main]
2728
async fn main() -> Result<(), Box<dyn Error>> {
2829
env_logger::builder().init();
29-
let addr = env::args().nth(1).unwrap_or("127.0.0.1:7878".to_string());
30+
let addr = env::args().nth(1).unwrap_or("tcp://127.0.0.1:7878".to_string());
3031

3132
RSocketFactory::receive()
32-
.transport(URI::Tcp(addr))
33+
.transport(&addr)
3334
.acceptor(|setup, _socket| {
3435
info!("accept setup: {:?}", setup);
35-
Box::new(EchoRSocket)
36+
Ok(Box::new(EchoRSocket))
37+
// Or you can reject setup
38+
// Err(From::from("SETUP_NOT_ALLOW"))
3639
})
3740
.serve()
3841
.await
@@ -51,7 +54,7 @@ use rsocket_rust::prelude::*;
5154
async fn test() {
5255
let cli = RSocketFactory::connect()
5356
.acceptor(|| Box::new(EchoRSocket))
54-
.transport(URI::Tcp("127.0.0.1:7878".to_string()))
57+
.transport("tcp://127.0.0.1:7878")
5558
.setup(Payload::from("READY!"))
5659
.mime_type("text/plain", "text/plain")
5760
.start()
@@ -81,6 +84,14 @@ async fn test() {
8184
- [x] REQUEST_RESPONSE
8285
- [x] REQUEST_STREAM
8386
- [x] REQUEST_CHANNEL
87+
- More Operations
88+
- [ ] Error
89+
- [ ] Cancel
90+
- [ ] Fragmentation
91+
- [ ] Resume
92+
- QoS
93+
- [ ] RequestN
94+
- [ ] Lease
8495
- Transport
8596
- [x] TCP
8697
- [ ] Websocket

examples/echo/main.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ use std::env;
77
use std::error::Error;
88

99
#[tokio::main]
10-
async fn main() -> Result<(), Box<dyn Error>> {
11-
env_logger::builder().init();
12-
let addr = env::args().nth(1).unwrap_or("127.0.0.1:7878".to_string());
13-
10+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
11+
env_logger::builder().format_timestamp_millis().init();
12+
let addr = env::args()
13+
.nth(1)
14+
.unwrap_or("tcp://127.0.0.1:7878".to_string());
1415
RSocketFactory::receive()
15-
.transport(URI::Tcp(addr))
16+
.transport(&addr)
1617
.acceptor(|setup, _socket| {
1718
info!("accept setup: {:?}", setup);
18-
Box::new(EchoRSocket)
19+
Ok(Box::new(EchoRSocket))
20+
// Or you can reject setup
21+
// Err(From::from("SETUP_NOT_ALLOW"))
1922
})
23+
.on_start(|| info!("+++++++ echo server started! +++++++"))
2024
.serve()
2125
.await
2226
}

examples/proxy/main.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#[macro_use]
2+
extern crate log;
3+
extern crate env_logger;
4+
extern crate futures;
5+
extern crate rsocket_rust;
6+
extern crate tokio;
7+
8+
use futures::executor::block_on;
9+
use rsocket_rust::prelude::*;
10+
use std::error::Error;
11+
12+
#[tokio::main]
13+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
14+
env_logger::builder().format_timestamp_millis().init();
15+
16+
RSocketFactory::receive()
17+
.acceptor(|setup, _sending_socket| {
18+
info!("incoming socket: setup={:?}", setup);
19+
Ok(Box::new(block_on(async move {
20+
RSocketFactory::connect()
21+
.acceptor(|| Box::new(EchoRSocket))
22+
.setup(Payload::from("I'm Rust!"))
23+
.transport("tcp://127.0.0.1:7878")
24+
.start()
25+
.await
26+
.unwrap()
27+
})))
28+
})
29+
.transport("tcp://127.0.0.1:7979")
30+
.serve()
31+
.await
32+
}

rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
edition = "2018"

src/errors.rs

+53-33
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,77 @@ use std::error::Error;
22
use std::fmt;
33
use std::io;
44

5+
pub const ERR_INVALID_SETUP: u32 = 0x0000_0001;
6+
pub const ERR_UNSUPPORTED_SETUP: u32 = 0x0000_0002;
7+
pub const ERR_REJECT_SETUP: u32 = 0x0000_0003;
8+
pub const ERR_REJECT_RESUME: u32 = 0x0000_0004;
9+
pub const ERR_CONN_FAILED: u32 = 0x0000_0101;
10+
pub const ERR_CONN_CLOSED: u32 = 0x0000_0102;
11+
pub const ERR_APPLICATION: u32 = 0x0000_0201;
12+
pub const ERR_REJECTED: u32 = 0x0000_0202;
13+
pub const ERR_CANCELED: u32 = 0x0000_0203;
14+
pub const ERR_INVALID: u32 = 0x0000_0204;
15+
516
#[derive(Debug)]
617
pub enum ErrorKind {
7-
Internal(u32, &'static str),
8-
WithDescription(String),
9-
IO(io::Error),
10-
Cancelled(),
11-
Send(),
18+
Internal(u32, String),
19+
WithDescription(String),
20+
IO(io::Error),
21+
Cancelled(),
1222
}
1323

1424
#[derive(Debug)]
1525
pub struct RSocketError {
16-
kind: ErrorKind,
26+
kind: ErrorKind,
1727
}
1828

19-
impl Error for RSocketError {
20-
fn description(&self) -> &str {
21-
"this is a rsocket error"
22-
}
23-
24-
fn cause(&self) -> Option<&dyn Error> {
25-
match &self.kind {
26-
ErrorKind::IO(e) => Some(e),
27-
_ => None,
28-
}
29-
}
30-
}
29+
impl Error for RSocketError {}
3130

3231
impl fmt::Display for RSocketError {
33-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
34-
println!(">>>>>>>>>>> {:?}", self.kind);
35-
unimplemented!()
36-
}
32+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33+
match &self.kind {
34+
ErrorKind::Internal(c, s) => write!(f, "ERROR({}): {}", translate(c), s),
35+
ErrorKind::WithDescription(s) => write!(f, "{}", s),
36+
ErrorKind::IO(e) => write!(f, "{}", e),
37+
ErrorKind::Cancelled() => write!(f, "ERROR(CANCELLED)"),
38+
}
39+
}
3740
}
3841

3942
impl From<ErrorKind> for RSocketError {
40-
fn from(kind: ErrorKind) -> RSocketError {
41-
RSocketError { kind }
42-
}
43+
fn from(kind: ErrorKind) -> RSocketError {
44+
RSocketError { kind }
45+
}
4346
}
4447
impl From<String> for RSocketError {
45-
fn from(e: String) -> RSocketError {
46-
RSocketError {
47-
kind: ErrorKind::WithDescription(e),
48+
fn from(e: String) -> RSocketError {
49+
RSocketError {
50+
kind: ErrorKind::WithDescription(e),
51+
}
4852
}
49-
}
5053
}
5154

5255
impl From<&'static str> for RSocketError {
53-
fn from(e: &'static str) -> RSocketError {
54-
RSocketError {
55-
kind: ErrorKind::WithDescription(String::from(e)),
56+
fn from(e: &'static str) -> RSocketError {
57+
RSocketError {
58+
kind: ErrorKind::WithDescription(String::from(e)),
59+
}
60+
}
61+
}
62+
63+
#[inline]
64+
fn translate(code: &u32) -> &str {
65+
match *code {
66+
ERR_APPLICATION => "APPLICATION",
67+
ERR_INVALID_SETUP => "INVALID_SETUP",
68+
ERR_UNSUPPORTED_SETUP => "UNSUPPORTED_SETUP",
69+
ERR_REJECT_SETUP => "REJECT_SETUP",
70+
ERR_REJECT_RESUME => "REJECT_RESUME",
71+
ERR_CONN_FAILED => "CONN_FAILED",
72+
ERR_CONN_CLOSED => "CONN_CLOSED",
73+
ERR_REJECTED => "REJECTED",
74+
ERR_CANCELED => "CANCELED",
75+
ERR_INVALID => "INVALID",
76+
_ => "UNKNOWN",
5677
}
57-
}
5878
}

src/extension.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ impl RoutingMetadataBuilder {
2626
pub fn push_str(self, tag: &str) -> Self {
2727
self.push(String::from(tag))
2828
}
29-
3029
pub fn push(mut self, tag: String) -> Self {
3130
if tag.len() > MAX_ROUTING_TAG_LEN {
3231
panic!("exceeded maximum routing tag length!");

src/frame/cancel.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ use super::{Body, Frame};
44
pub struct Cancel {}
55

66
pub struct CancelBuilder {
7-
stream_id: u32,
8-
flag: u16,
7+
stream_id: u32,
8+
flag: u16,
99
}
1010

1111
impl CancelBuilder {
12-
pub fn build(self) -> Frame {
13-
Frame::new(self.stream_id, Body::Cancel(), self.flag)
14-
}
12+
pub fn build(self) -> Frame {
13+
Frame::new(self.stream_id, Body::Cancel(), self.flag)
14+
}
1515
}
1616

1717
impl Cancel {
18-
pub fn builder(stream_id: u32, flag: u16) -> CancelBuilder {
19-
CancelBuilder { stream_id, flag }
20-
}
18+
pub fn builder(stream_id: u32, flag: u16) -> CancelBuilder {
19+
CancelBuilder { stream_id, flag }
20+
}
2121
}

0 commit comments

Comments
 (0)