Matrix-Zhang/tokio_kcp

Never reaches EOF (or 0 bytes read)

Closed this issue · 3 comments

I've been playing with this crate, but I can't get my read loop to stop (when it reads 0 bytes, like in your example).

On the client-side, I'm writing 80 bytes. On the server-side, the first loop iteration reads the whole 80 bytes and then keeps looping, even if my client has been dropped (closed).

I tried various options like stream: false, but couldn't make it work. I think this might be an issue with the async implementation specifically.

Any ideas?

Well, because there is no FIN in KCP, so there is no way for server to know the client have been dropped.

tokio_kcp/src/session.rs

Lines 143 to 168 in b6a9563

if !is_client {
// If this is a server stream, close it automatically after a period of time
let last_update_time = socket.last_update_time();
let elapsed = last_update_time.elapsed();
if elapsed > session.session_expire {
if elapsed > session.session_expire * 2 {
// Force close. Client may have already gone.
trace!(
"[SESSION] force close inactive session, conv: {}, last_update: {}s ago",
socket.conv(),
elapsed.as_secs()
);
break;
}
if !is_closed {
trace!(
"[SESSION] closing inactive session, conv: {}, last_update: {}s ago",
socket.conv(),
elapsed.as_secs()
);
session.closed.store(true, Ordering::Release);
}
}
}

The server socket will expire after session_expire seconds. So you may try to set it via config.session_expire.

Ok, so I should design my representation of data with a delimiter to mark the end of a message.