Skip to content

Commit d1e3b1c

Browse files
author
Mathieu Amiot
committed
Done!
1 parent fd017b4 commit d1e3b1c

3 files changed

Lines changed: 17 additions & 19 deletions

File tree

src/codec.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::error::NatsError;
2-
use crate::protocol::{CommandError, Op};
2+
use crate::protocol::Op;
33
use bytes::{BufMut, BytesMut};
44
use tokio_codec::{Decoder, Encoder};
55

@@ -41,14 +41,21 @@ impl Decoder for OpCodec {
4141
return Ok(None);
4242
}
4343

44-
debug!(target: "nitox", "codec buffer is {:?}", buf);
44+
debug!(target: "nitox", "next index: {}", self.next_index);
45+
46+
//debug!(target: "nitox", "codec buffer is {:?}", buf);
4547
// Let's check if we find a blank space at the beginning
4648
if let Some(command_offset) = buf[self.next_index..]
4749
.iter()
4850
.position(|b| *b == b' ' || *b == b'\t' || *b == b'\r')
4951
{
5052
let command_end = self.next_index + command_offset;
51-
debug!(target: "nitox", "codec detected command name {:?}", &buf[..command_end]);
53+
debug!(target: "nitox", "command end: {}", command_end);
54+
//debug!(target: "nitox", "codec detected command name {:?}", &buf[..command_end]);
55+
if !Op::command_exists(&buf[..command_end]) {
56+
debug!(target: "nitox", "command was incomplete");
57+
return Ok(None);
58+
}
5259

5360
if let Some(command_body_offset) = buf[command_end..].windows(2).position(|w| w == b"\r\n") {
5461
let mut end_buf_pos = command_end + command_body_offset + 2;
@@ -60,29 +67,19 @@ impl Decoder for OpCodec {
6067
end_buf_pos += new_end + 2;
6168
} else {
6269
debug!(target: "nitox", "command was incomplete");
63-
self.next_index = buf.len();
6470
return Ok(None);
6571
}
6672
}
6773

6874
debug!(target: "nitox", "codec detected command body {:?}", &buf[..end_buf_pos]);
69-
if !Op::command_exists(&buf[..command_end]) {
70-
debug!(target: "nitox", "command was incomplete");
71-
self.next_index = buf.len();
72-
return Ok(None);
73-
}
7475

76+
let cmd_buf = buf.split_to(end_buf_pos);
7577
debug!(target: "nitox", "buffer now contains {:?}", buf);
7678
self.next_index = 0;
77-
match Op::from_bytes(buf[..command_end].into(), command_end) {
78-
Err(CommandError::IncompleteCommandError) => {
79-
debug!(target: "nitox", "command was incomplete");
80-
self.next_index = buf.len();
81-
Ok(None)
82-
}
79+
80+
match Op::from_bytes(cmd_buf.freeze(), command_end) {
8381
Ok(op) => {
8482
debug!(target: "nitox", "codec parsed command {:#?}", op);
85-
let _ = buf.split_to(end_buf_pos);
8683
Ok(Some(op))
8784
}
8885
Err(e) => {
@@ -92,8 +89,7 @@ impl Decoder for OpCodec {
9289
}
9390
} else {
9491
debug!(target: "nitox", "command was incomplete");
95-
self.next_index = buf.len();
96-
return Ok(None);
92+
Ok(None)
9793
}
9894
} else {
9995
// First blank not found yet, continuing

src/protocol/client/pub_cmd.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ impl Command for PubCommand {
3535

3636
fn into_vec(self) -> Result<Bytes, CommandError> {
3737
let (rt_len, rt) = self.reply_to.map_or((0, "".into()), |rp| (rp.len() + 1, rp));
38+
// Computes the string length of the payload_len by dividing the number par ln(10)
3839
let size_len = ((self.payload.len() + 1) as f64 / std::f64::consts::LN_10).ceil() as usize;
39-
let len = 9 + self.subject.len() + rt_len + size_len;
40+
let len = 9 + self.subject.len() + rt_len + size_len + self.payload.len();
4041

4142
let mut bytes = BytesMut::with_capacity(len);
4243
bytes.put("PUB\t");

src/protocol/client/unsub_cmd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl Command for UnsubCommand {
3232
const CMD_NAME: &'static [u8] = b"UNSUB";
3333

3434
fn into_vec(self) -> Result<Bytes, CommandError> {
35+
// Computes the string length of the payload_len by dividing the number par ln(10)
3536
let (mm_len, mm) = self.max_msgs.map_or((0, 0), |mm| {
3637
(((mm + 1) as f64 / std::f64::consts::LN_10).ceil() as usize, mm)
3738
});

0 commit comments

Comments
 (0)