@@ -62,26 +62,30 @@ pub const TYPE_METADATA_PUSH: u16 = 0x0C;
6262pub const TYPE_RESUME : u16 = 0x0D ;
6363pub const TYPE_RESUME_OK : u16 = 0x0E ;
6464
65- pub const ERR_INVALID_SETUP : u32 = 0x00000001 ;
66- pub const ERR_UNSUPPORTED_SETUP : u32 = 0x00000002 ;
67- pub const ERR_REJECT_SETUP : u32 = 0x00000003 ;
68- pub const ERR_REJECT_RESUME : u32 = 0x00000004 ;
69- pub const ERR_CONN_FAILED : u32 = 0x00000101 ;
70- pub const ERR_CONN_CLOSED : u32 = 0x00000102 ;
71- pub const ERR_APPLICATION : u32 = 0x00000201 ;
72- pub const ERR_REJECTED : u32 = 0x00000202 ;
73- pub const ERR_CANCELED : u32 = 0x00000203 ;
74- pub const ERR_INVALID : u32 = 0x00000204 ;
75-
76- pub const REQUEST_MAX : u32 = 2147483647 ;
77- const LEN_HEADER : u32 = 6 ;
65+ pub const ERR_INVALID_SETUP : u32 = 0x0000_0001 ;
66+ pub const ERR_UNSUPPORTED_SETUP : u32 = 0x0000_0002 ;
67+ pub const ERR_REJECT_SETUP : u32 = 0x0000_0003 ;
68+ pub const ERR_REJECT_RESUME : u32 = 0x0000_0004 ;
69+ pub const ERR_CONN_FAILED : u32 = 0x0000_0101 ;
70+ pub const ERR_CONN_CLOSED : u32 = 0x0000_0102 ;
71+ pub const ERR_APPLICATION : u32 = 0x0000_0201 ;
72+ pub const ERR_REJECTED : u32 = 0x0000_0202 ;
73+ pub const ERR_CANCELED : u32 = 0x0000_0203 ;
74+ pub const ERR_INVALID : u32 = 0x0000_0204 ;
75+
76+ pub const REQUEST_MAX : u32 = 0x7FFF_FFFF ; // 2147483647
77+
78+ const LEN_HEADER : usize = 6 ;
7879
7980pub trait Writeable {
8081 fn write_to ( & self , bf : & mut BytesMut ) ;
81- fn len ( & self ) -> u32 ;
82+ fn len ( & self ) -> usize ;
83+ fn is_empty ( & self ) -> bool {
84+ self . len ( ) == 0
85+ }
8286}
8387
84- #[ derive( Debug ) ]
88+ #[ derive( Debug , PartialEq ) ]
8589pub enum Body {
8690 Setup ( Setup ) ,
8791 Lease ( Lease ) ,
@@ -99,7 +103,7 @@ pub enum Body {
99103 ResumeOK ( ResumeOK ) ,
100104}
101105
102- #[ derive( Debug ) ]
106+ #[ derive( Debug , PartialEq ) ]
103107pub struct Frame {
104108 stream_id : u32 ,
105109 body : Body ,
@@ -128,7 +132,7 @@ impl Writeable for Frame {
128132 }
129133 }
130134
131- fn len ( & self ) -> u32 {
135+ fn len ( & self ) -> usize {
132136 // header len
133137 LEN_HEADER
134138 + match & self . body {
@@ -167,20 +171,20 @@ impl Frame {
167171 b. advance ( 2 ) ;
168172 let ( flag, kind) = ( n & 0x03FF , ( n & 0xFC00 ) >> 10 ) ;
169173 let body = match kind {
170- TYPE_SETUP => Setup :: decode ( flag, b) . map ( |it| Body :: Setup ( it ) ) ,
171- TYPE_REQUEST_RESPONSE => RequestResponse :: decode ( flag, b) . map ( |it| Body :: RequestResponse ( it ) ) ,
172- TYPE_REQUEST_STREAM => RequestStream :: decode ( flag, b) . map ( |it| Body :: RequestStream ( it ) ) ,
173- TYPE_REQUEST_CHANNEL => RequestChannel :: decode ( flag, b) . map ( |it| Body :: RequestChannel ( it ) ) ,
174- TYPE_REQUEST_FNF => RequestFNF :: decode ( flag, b) . map ( |it| Body :: RequestFNF ( it ) ) ,
175- TYPE_REQUEST_N => RequestN :: decode ( flag, b) . map ( |it| Body :: RequestN ( it ) ) ,
176- TYPE_METADATA_PUSH => MetadataPush :: decode ( flag, b) . map ( |it| Body :: MetadataPush ( it ) ) ,
177- TYPE_KEEPALIVE => Keepalive :: decode ( flag, b) . map ( |it| Body :: Keepalive ( it ) ) ,
178- TYPE_PAYLOAD => Payload :: decode ( flag, b) . map ( |it| Body :: Payload ( it ) ) ,
179- TYPE_LEASE => Lease :: decode ( flag, b) . map ( |it| Body :: Lease ( it ) ) ,
174+ TYPE_SETUP => Setup :: decode ( flag, b) . map ( Body :: Setup ) ,
175+ TYPE_REQUEST_RESPONSE => RequestResponse :: decode ( flag, b) . map ( Body :: RequestResponse ) ,
176+ TYPE_REQUEST_STREAM => RequestStream :: decode ( flag, b) . map ( Body :: RequestStream ) ,
177+ TYPE_REQUEST_CHANNEL => RequestChannel :: decode ( flag, b) . map ( Body :: RequestChannel ) ,
178+ TYPE_REQUEST_FNF => RequestFNF :: decode ( flag, b) . map ( Body :: RequestFNF ) ,
179+ TYPE_REQUEST_N => RequestN :: decode ( flag, b) . map ( Body :: RequestN ) ,
180+ TYPE_METADATA_PUSH => MetadataPush :: decode ( flag, b) . map ( Body :: MetadataPush ) ,
181+ TYPE_KEEPALIVE => Keepalive :: decode ( flag, b) . map ( Body :: Keepalive ) ,
182+ TYPE_PAYLOAD => Payload :: decode ( flag, b) . map ( Body :: Payload ) ,
183+ TYPE_LEASE => Lease :: decode ( flag, b) . map ( Body :: Lease ) ,
180184 TYPE_CANCEL => Ok ( Body :: Cancel ( ) ) ,
181- TYPE_ERROR => Error :: decode ( flag, b) . map ( |it| Body :: Error ( it ) ) ,
182- TYPE_RESUME_OK => ResumeOK :: decode ( flag, b) . map ( |it| Body :: ResumeOK ( it ) ) ,
183- TYPE_RESUME => Resume :: decode ( flag, b) . map ( |it| Body :: Resume ( it ) ) ,
185+ TYPE_ERROR => Error :: decode ( flag, b) . map ( Body :: Error ) ,
186+ TYPE_RESUME_OK => ResumeOK :: decode ( flag, b) . map ( Body :: ResumeOK ) ,
187+ TYPE_RESUME => Resume :: decode ( flag, b) . map ( Body :: Resume ) ,
184188 _ => Err ( RSocketError :: from ( "illegal frame type" ) ) ,
185189 } ;
186190 body. map ( |it| Frame :: new ( sid, it, flag) )
@@ -212,7 +216,7 @@ impl Frame {
212216}
213217
214218fn to_frame_type ( body : & Body ) -> u16 {
215- return match body {
219+ match body {
216220 Body :: Setup ( _) => TYPE_SETUP ,
217221 Body :: Lease ( _) => TYPE_LEASE ,
218222 Body :: Keepalive ( _) => TYPE_KEEPALIVE ,
@@ -227,5 +231,5 @@ fn to_frame_type(body: &Body) -> u16 {
227231 Body :: MetadataPush ( _) => TYPE_METADATA_PUSH ,
228232 Body :: Resume ( _) => TYPE_RESUME ,
229233 Body :: ResumeOK ( _) => TYPE_RESUME_OK ,
230- } ;
234+ }
231235}
0 commit comments