www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples | github

Archive Index This month's Index

Subject: Connection loss detection with keepalive

Connection loss detection with keepalive

From: Anton Obukhov <anton_at_ubnt.com>
Date: Wed, 10 Oct 2012 18:57:32 -0700

Hello libssh2 authors,

I'm working on an application, which needs to stream data from a remote
machine in real time. Libssh2 is just great for handling sftp and exec
sessions, but when it comes to unreliable networks conditions, it doesn't
really have any handles to control its behavior.

I scooped through this mailing list and the rest of googles, and found a
couple of patches that were attempting to implement a reliable connection
loss detection. (Namely, a patch attached to this ticket:
http://trac.libssh2.org/ticket/183). However, even that was not enough for

The two things missing in that patch are:
1. It doesn't handle a situation when the outgoing queue is full, and the
libssh2_keepalive_send operation silently returns;
2. Even if want_reply set to 1, there is no checking for server responses
to heartbeat, as we don't really read anything (normally, heartbeats are
important in those scenarios, when there is a pause in data exchange and
nothing else is being sent or received).

What I had to implement for my application is a dedicated channel, which is
used just make the core of the library update incoming packets queue, and
also reset outstanding heartbeats counters. Something like this:

LIBSSH2_CHANNEL *channelKeepAlive = createChannel(session, sock);
while (1) {
    char buffer[1024];
    libssh2_keepalive_send(session, &seconds_to_next);
    libssh2_channel_read_ex(channelKeepAlive, 0, buffer, sizeof(buffer));
    ... //do other tasks

I'd like to understand if a patch that makes keepalive act as a connection
loss detector would be interesting for this community, as a part of the
library. If yes - what is the preferred API for dedicated channel

Please note that all said above was tested only in non-blocking socket
conditions, I guess something might be different for a blocking scenario.


libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2012-10-11

the libssh2 team