[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: asynchronous channel write
  [Thread Prev] | [Thread Next]
 
 
- Subject: Re: asynchronous channel write
 - From: Torsten Kuehnel <tdkuehnel@xxxxxxxxxxxxxxxxxxxxx>
 - Reply-to: libssh@xxxxxxxxxx
 - Date: Mon, 13 Jan 2020 22:54:21 +0100
 - To: libssh@xxxxxxxxxx
 
On Mon, 13 Jan 2020 21:34:37 +0100
g4-lisz@xxxxxxxxxxxx wrote:
> Hi Torsten,
> 
> the "trick" is called ssh_set_blocking(session, 0);
> 
> But be careful, this affects everything in the session.
> 
> My write loop looks lile this:
> 
>     blocking = ssh_is_blocking(session);
>     ssh_set_blocking(session, 0);
> 
>     if (len > 0) {
>         if (ssh_channel_is_open(channel)) {
>             wr = 0;
>             do {
>                 i = ssh_channel_write(channel, buf, len);
>                 if (i < 0) {
>                     log("Error writing on the direct-tcpip channel: %d", i);
>                     len = wr;
>                     break;
>                 }
>                 wr += i;
>                 log("channel_write (%d from %d)", wr, len);
>             } while (i > 0 && wr < len);
>         }
>         else {
>             log("Can't write on closed channel!");
>         }
>     }
> 
>     ssh_set_blocking(session, blocking);
> 
Hi Till !
Thanks for the hint giving ssh_set_blocking a try :)
I had a quick look at the source code of channel_write_common
(src/channel.c:1376 0.9.3 version of libssh) what does the actual
write. It ends with
...
out:
  return (int)(origlen - len);
...
which may indicate that actually partly writes may occur under a non
blocking context. I will definitely give it a try, thanks alot !
Looks to me your loop does not cope with partial writes correctly as
you pass the initial (channel, buf, len) parameters unmodified in
subsequent calls to ssh_channel_write, shall your innermost do {...}
loop iterate more than once.
Shouldn't you increment your buf pointer by the bytes already sent, or
does libssh keep track of the write progress and does this for you ?
anyway, thank you !
best
> Greetings,
> Till
> 
> On 13.01.20 17:48, Torsten Kuehnel wrote:
> > Working on the reference implementation of the ncot library i am
> > confronted with the follwoing use case:
> >
> > Data packets are to be received and send from/to several simultaneous
> > open ssh-like encrypted channels to instances of the same program
> > running on other hosts over the network.
> >
> > The one client-server connection -> one thread paradigm does not fit
> > so well. Packets have to be validated, act upon and processed in
> > different ways, and need to be resend to other connections - this is
> > the real part of the implementation.
> >
> > So my approach currently is the following one:
> >
> > A mainloop with ssh_event_dopoll manages to call my channel callbacks
> > i have established during initialization of the several sessions. But
> > when it comes to write my packets over the channels, libssh only offers
> > a blocking ssh_channel_write call what is not what i want.
> >
> > When the poll returns with write possible, i usually try to write as
> > much as i can and send tells me how much succeded. How can i achieve
> > this behaviour using libssh ?
> >
> > thanks for any insights in advance, keep up the good work !
> >
> > -- Torsten Kuehnel
> > <tdkuehnel@xxxxxxxxxxxxxxxxxxxxx>
> >
-- 
Torsten Kuehnel <tdkuehnel@xxxxxxxxxxxxxxxxxxxxx>
| Re: asynchronous channel write | g4-lisz@xxxxxxxxxxxx | 
| asynchronous channel write | Torsten Kuehnel <tdkuehnel@xxxxxxxxxxxxxxxxxxxxx> | 
| Re: asynchronous channel write | g4-lisz@xxxxxxxxxxxx |