Matrix-Zhang/tokio_kcp

feature request: support modifying datagram to allow customizable protocol header

Opened this issue · 1 comments

oyyd commented

Firstly, thanks for the great repository!

Currently, tokio_kcp can't communicate with kcp-go because: beside kcp, kcp-go also adds other features that change its protocol header, e.g. crypto, crc, fec. It should be emphasized that these features are kcp-go specific.

Though, being compatible with kcp-go might be not the goal of tokio_kcp, I believe adding the ability for users to modify the datagram to support their own headers makes it possbile. It would also extend the potential usage of tokio_kcp.

oyyd commented

For example, define a trait like below for users to impl and use them in tokio_kcp:

pub trait ProtocolExtender {
    // tokio_kcp needs this beforehand when caculate mtu
    fn header_len() -> u16;

    // When receive udp datagram, firstly call recv() to modify the data, then pass it to kcp.
    fn recv(&mut self, data: Vec<u8>) -> Vec<u8>;

    // When kcp tries to send data, firstly call send() to modify the data, then pass it to udp socket.
    fn send(&mut self, data: Vec<u8>) -> Vec<u8>;
}