Skip to content
This repository was archived by the owner on Oct 28, 2025. It is now read-only.

Commit 9fe9ec7

Browse files
author
Кирилл Безуглый
committed
feat: make IPROTO header encode/decode as methods, add encode_from_parts
1 parent 49d4749 commit 9fe9ec7

File tree

6 files changed

+66
-52
lines changed

6 files changed

+66
-52
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- `ffi::tarantool::box_schema_version` and `ffi::tarantool::box_session_id` functions
1111
- `network::protocol::SyncIndex::get` method
1212
- `network::protocol::codec::{LegacyCall, Nop, Prepare, Begin, Commit, Rollback}` variants
13+
- `network::protocol::codec::Header::encode_from_parts` function
1314

1415
### Changed
1516

@@ -25,6 +26,8 @@
2526
- tlua::LuaTable::get_or_create_metatable is deprecated now in favor of tlua::LuaTable::metatable.
2627

2728
### Breaking changes
29+
- Replace `network::protocol::codec::{encode_header, decode_header}` functions
30+
with `network::protocol::codec::Header::{encode, decode}` methods.
2831

2932
### Added (picodata)
3033

tarantool/src/net_box/inner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl ConnInner {
306306
cur.set_position(0);
307307
}
308308

309-
let header = protocol::decode_header(&mut cur)?;
309+
let header = protocol::Header::decode(&mut cur)?;
310310
if header.iproto_type == protocol::IProtoType::Error as u32 {
311311
let error = protocol::decode_error(&mut cur, &header)?;
312312
return Err(Error::Remote(error));

tarantool/src/net_box/recv_queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl RecvQueue {
169169
let header = {
170170
let mut buffer = self.buffer.borrow_mut();
171171
buffer.set_position(start as _);
172-
protocol::decode_header(buffer.by_ref())?
172+
protocol::Header::decode(buffer.by_ref())?
173173
};
174174

175175
let sync = header.sync;

tarantool/src/network/protocol/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub trait Request {
1616

1717
#[inline(always)]
1818
fn encode_header(&self, out: &mut impl Write, sync: SyncIndex) -> Result<(), Error> {
19-
codec::encode_header(out, sync, Self::TYPE)
19+
codec::Header::encode_from_parts(out, sync, Self::TYPE)
2020
}
2121

2222
fn encode_body(&self, out: &mut impl Write) -> Result<(), Error>;

tarantool/src/network/protocol/codec.rs

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,6 @@ pub enum IProtoType {
8484
Error = 1 << 15,
8585
}
8686

87-
pub fn encode_header(
88-
stream: &mut impl Write,
89-
sync: SyncIndex,
90-
request_type: IProtoType,
91-
) -> Result<(), Error> {
92-
rmp::encode::write_map_len(stream, 2)?;
93-
rmp::encode::write_pfix(stream, REQUEST_TYPE)?;
94-
rmp::encode::write_pfix(stream, request_type as u8)?;
95-
rmp::encode::write_pfix(stream, SYNC)?;
96-
rmp::encode::write_uint(stream, sync.0)?;
97-
Ok(())
98-
}
99-
10087
pub fn chap_sha1_auth_data(password: &str, salt: &[u8]) -> Vec<u8> {
10188
// prepare 'chap-sha1' scramble:
10289
// salt = base64_decode(encoded_salt);
@@ -389,48 +376,72 @@ pub struct Header {
389376
pub schema_version: u64,
390377
}
391378

392-
pub struct Response<T> {
393-
pub header: Header,
394-
pub payload: T,
395-
}
396-
397-
pub fn decode_header(stream: &mut (impl Read + Seek)) -> Result<Header, Error> {
398-
let mut sync: Option<u64> = None;
399-
let mut iproto_type: Option<u32> = None;
400-
let mut error_code: u32 = 0;
401-
let mut schema_version: Option<u64> = None;
379+
impl Header {
380+
pub fn encode(&self, stream: &mut impl Write) -> Result<(), Error> {
381+
rmp::encode::write_map_len(stream, 2)?;
382+
rmp::encode::write_pfix(stream, REQUEST_TYPE)?;
383+
rmp::encode::write_pfix(stream, self.iproto_type as u8)?;
384+
rmp::encode::write_pfix(stream, SYNC)?;
385+
rmp::encode::write_uint(stream, self.sync.0)?;
386+
Ok(())
387+
}
402388

403-
let map_len = rmp::decode::read_map_len(stream)?;
404-
for _ in 0..map_len {
405-
let key = rmp::decode::read_pfix(stream)?;
406-
match key {
407-
REQUEST_TYPE => {
408-
let r#type: u32 = rmp::decode::read_int(stream)?;
389+
pub fn encode_from_parts(
390+
stream: &mut impl Write,
391+
sync: SyncIndex,
392+
request_type: IProtoType,
393+
) -> Result<(), Error> {
394+
rmp::encode::write_map_len(stream, 2)?;
395+
rmp::encode::write_pfix(stream, REQUEST_TYPE)?;
396+
rmp::encode::write_pfix(stream, request_type as u8)?;
397+
rmp::encode::write_pfix(stream, SYNC)?;
398+
rmp::encode::write_uint(stream, sync.0)?;
399+
Ok(())
400+
}
409401

410-
const IPROTO_TYPE_ERROR: u32 = IProtoType::Error as _;
411-
if (r#type & IPROTO_TYPE_ERROR) != 0 {
412-
iproto_type = Some(IPROTO_TYPE_ERROR);
413-
error_code = r#type & !IPROTO_TYPE_ERROR;
414-
} else {
415-
iproto_type = Some(r#type);
402+
pub fn decode(stream: &mut (impl Read + Seek)) -> Result<Header, Error> {
403+
let mut sync: Option<u64> = None;
404+
let mut iproto_type: Option<u32> = None;
405+
let mut error_code: u32 = 0;
406+
let mut schema_version: Option<u64> = None;
407+
408+
let map_len = rmp::decode::read_map_len(stream)?;
409+
for _ in 0..map_len {
410+
let key = rmp::decode::read_pfix(stream)?;
411+
match key {
412+
REQUEST_TYPE => {
413+
let r#type: u32 = rmp::decode::read_int(stream)?;
414+
415+
const IPROTO_TYPE_ERROR: u32 = IProtoType::Error as _;
416+
if (r#type & IPROTO_TYPE_ERROR) != 0 {
417+
iproto_type = Some(IPROTO_TYPE_ERROR);
418+
error_code = r#type & !IPROTO_TYPE_ERROR;
419+
} else {
420+
iproto_type = Some(r#type);
421+
}
416422
}
423+
SYNC => sync = Some(rmp::decode::read_int(stream)?),
424+
SCHEMA_VERSION => schema_version = Some(rmp::decode::read_int(stream)?),
425+
_ => msgpack::skip_value(stream)?,
417426
}
418-
SYNC => sync = Some(rmp::decode::read_int(stream)?),
419-
SCHEMA_VERSION => schema_version = Some(rmp::decode::read_int(stream)?),
420-
_ => msgpack::skip_value(stream)?,
421427
}
422-
}
423428

424-
if sync.is_none() || iproto_type.is_none() || schema_version.is_none() {
425-
return Err(io::Error::from(io::ErrorKind::InvalidData).into());
429+
if sync.is_none() || iproto_type.is_none() || schema_version.is_none() {
430+
return Err(io::Error::from(io::ErrorKind::InvalidData).into());
431+
}
432+
433+
Ok(Header {
434+
sync: SyncIndex(sync.unwrap()),
435+
iproto_type: iproto_type.unwrap(),
436+
error_code,
437+
schema_version: schema_version.unwrap(),
438+
})
426439
}
440+
}
427441

428-
Ok(Header {
429-
sync: SyncIndex(sync.unwrap()),
430-
iproto_type: iproto_type.unwrap(),
431-
error_code,
432-
schema_version: schema_version.unwrap(),
433-
})
442+
pub struct Response<T> {
443+
pub header: Header,
444+
pub payload: T,
434445
}
435446

436447
////////////////////////////////////////////////////////////////////////////////

tarantool/src/network/protocol/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl Protocol {
253253
None
254254
}
255255
State::Auth => {
256-
let header = codec::decode_header(message)?;
256+
let header = codec::Header::decode(message)?;
257257
if header.iproto_type == IProtoType::Error as u32 {
258258
let error = codec::decode_error(message, &header)?;
259259
return Err(error::Error::Remote(error));
@@ -262,7 +262,7 @@ impl Protocol {
262262
None
263263
}
264264
State::Ready => {
265-
let header = codec::decode_header(message)?;
265+
let header = codec::Header::decode(message)?;
266266
let response;
267267
if header.iproto_type == IProtoType::Error as u32 {
268268
response = Err(codec::decode_error(message, &header)?);

0 commit comments

Comments
 (0)