Crates.io | tobytcp |
lib.rs | tobytcp |
version | 0.13.0 |
source | src |
created_at | 2017-08-13 22:29:11.489692 |
updated_at | 2019-06-24 01:03:09.851857 |
description | A little library for sending messages over a tcp stream |
homepage | |
repository | https://github.com/gorup/tobytcp |
max_upload_size | |
id | 27445 |
size | 16,058 |
tobytcp
TobyTcp is a protocol that allows for the use of a raw tcp
stream for communicating messages, bi-directionally. It frames the messages with a length prefix to deliniate messages. This library provides methods to encode/decode that message prefix, but also provides async
send & receive methods.
NOTE: Maybe ready for Production use. See below Disclaimer section.
Look at the /examples
and the unit tests for compiling examples!
let prefix = protocol::tobytcp_prefix(data.len());
stream.write_all(&prefix)?;
stream.write_all(&data)?;
// OR use the send method in lib, which does almost exactly ^
send(&mut data, &mut stream).await?;
Look at the /examples
and the unit tests for compiling examples!
let mut len_buf = [0; 8];
stream.read_exact(&mut len_buf)?;
let length = protocol::tobytcp_len(len_buf);
let mut msg_buf = [0; length as usize];
stream.read_exact(&mut msg_buf)?; // Done, we have received the message into msg_buf
// OR use the receive method in lib which does almost exactly ^
let data = receive(&mut buf, &mut stream).await?;;
The TobyTcp protocol uses length prefixing for message framing.
Messages must be prefixed by eight (8) bytes, for a total of 64 bits. This 8 byte/64 bit segment of every message must contain the number of bytes present in the message being sent (NOT including the 8 bytes used for describing the size). The length prefix must be big-endian.
You can use the protocol
module to retreive the prefix, which has the length of your data.
Here is an example of an encoded messages. The message has 18
bytes of data, and in the end, 18 + 8 = 26
bytes are sent, with the first 8 bytes representing the length.
00 00 00 00 00 00 00 12 74 6f 62 79 20 69 73 20 61 20 67 6f 6f 64 20 64 6f 67
Also see the protocol
tests to see what is expected!
async
methods that are not usable with stable rust (check out areweasyncyet!), which is a large blocker for me considering this 1.0
.
async
methods behind a 'feature', but I'm unsure how to do that and no-one uses this so...32
bit machines, not sure it will work!The University of Illinois/NCSA (National Center for Supercomputing Applications) Open Source License
See LICENSE file. This a permissive open source license similar to Apache-2.0.