From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Chris Vine Newsgroups: gmane.lisp.guile.user Subject: Re: How to notice abrupt tcp connection losses in server/client? Date: Fri, 22 Jun 2018 00:09:16 +0100 Message-ID: <20180622000916.e5180d431446d15843d3f1e8@gmail.com> References: <197dbcf5-97f0-aab9-87b4-cbf61321adab@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1529622467 21787 195.159.176.226 (21 Jun 2018 23:07:47 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 21 Jun 2018 23:07:47 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Jun 22 01:07:43 2018 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fW8g3-0005aV-1w for guile-user@m.gmane.org; Fri, 22 Jun 2018 01:07:43 +0200 Original-Received: from localhost ([::1]:57960 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fW8iA-0005eY-5M for guile-user@m.gmane.org; Thu, 21 Jun 2018 19:09:54 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59864) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fW8hg-0005dg-EE for guile-user@gnu.org; Thu, 21 Jun 2018 19:09:25 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fW8hc-0000Q1-0B for guile-user@gnu.org; Thu, 21 Jun 2018 19:09:24 -0400 Original-Received: from mail-wm0-x22e.google.com ([2a00:1450:400c:c09::22e]:53852) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fW8hb-0000Pn-Oo for guile-user@gnu.org; Thu, 21 Jun 2018 19:09:19 -0400 Original-Received: by mail-wm0-x22e.google.com with SMTP id x6-v6so315310wmc.3 for ; Thu, 21 Jun 2018 16:09:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=C0l4Odmbf/ITO390PJCE3bkG2uHxtJkSkPsTOJAia9Q=; b=m0r4/sAPjaebHxhex8nE1xoDGmK8wmyoIP4byXSehVs9c3v4vMGZAfyYil+IlkaD/2 RkA/blPfnh4VZ2qs+RYffQZaYu+9BPk+dTgvfOBycvTUnkyc1AY+Jf2bsl3875D7OH2M YEcWnmYiDonNmax998eEdJoR8Dg7awO5abmRfFhJRqUJmdw8kOb9Rb7LMyq/00gPRx+K /M6npKG1nbVNoqKxN7twFNMsOFoV5a9K0up8GT9tyOtf1c05qfjmOvB84+BGjQWqxt46 93rh4MaIj4DDfW80087bkJOblfwjHPyXDNxjCP423o5P094RZx1xpCjYB7AJZECPObxN VVFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=C0l4Odmbf/ITO390PJCE3bkG2uHxtJkSkPsTOJAia9Q=; b=ZfQCh/7OSeD9jtdkd3c7EjBbqxg6FqMkYflzjW1qG2Pyh5QHZ3fqXKsFe9/A7XKhLE Vm4mLcA9zKHlvIN8aezhJ/BLllgGKSVcwhKS7gZ97eItR28lDIGs8R/+9Tvc/7qjXGqW x4GNZv9iQD4Wx835kW6NoDbeJ8nHpEPiZJ+RRXXnPrU0eYk+g1GShVakZiuepViIM/Wi RnGsNgFx+7iShc4i2t/KcmaT9QRxqjJbqLmkDGmpPXaFGOdwpaNtgVwZbF0d0rI/LE4W JXHDFNORbgMR/oPSqNyqg0loOww5NHuX8t5kSA5GMqdIxwfMeKTGSPPvYTo8sqT+6JIO fG9A== X-Gm-Message-State: APt69E3nbiC20I5IdxRVS1tPjrrH3kBx8uvSw89nUeCiXlr8NjM9VWgl 4h3kijcZgfVRWLYd8kol4p0srw== X-Google-Smtp-Source: ADUXVKJQs5IDWOp9bVaRMMj7lkYK78XMJntwv2ZoO3HDywGHEfiWfr65xRY3I8G8Vi/1rSgTTZjhpg== X-Received: by 2002:a1c:448:: with SMTP id 69-v6mr7046745wme.31.1529622558356; Thu, 21 Jun 2018 16:09:18 -0700 (PDT) Original-Received: from bother.homenet ([95.146.112.93]) by smtp.gmail.com with ESMTPSA id d15-v6sm3212928wmb.0.2018.06.21.16.09.16 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 21 Jun 2018 16:09:17 -0700 (PDT) Original-Received: from bother.homenet (localhost [127.0.0.1]) by bother.homenet (Postfix) with SMTP id 5ADC8261E64 for ; Fri, 22 Jun 2018 00:09:16 +0100 (BST) In-Reply-To: <197dbcf5-97f0-aab9-87b4-cbf61321adab@gmail.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-unknown-linux-gnu) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::22e X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:14645 Archived-At: On Fri, 22 Jun 2018 00:19:49 +0200 Zelphir Kaltstahl wrote: > On 21.06.2018 18:00, guile-user-request@gnu.org wrote: > > From: Chris Vine [snip] > > The POSIX recv() function returns 0 on end of file (connection closed) > > so I expect the scheme recv! procedure does the same. So on end-of-file > > your code appears to be producing an endless supply of empty strings. > > I actually tried to (break) the loop when the received message is > (eof-object? ...), but it might be that I used this predicate at the > wrong place (directly inside the loop when the message was already > copied from the receive-buffer, instead of when recv!, I believe), now > that I think about it … and maybe that is why (break)ing the loop did > not work. Need to investigate more. It looks as if recv! returns 0 on end-of-file, not an eof-object, so that may be your problem. That is not the case with get-line or get-bytevector-n. Note that recv! may also return less than the requested number of bytes - it does not guarantee to block until the entire request has been met or end-of-file is reached, as it is just a wrapper for POSIX/windows recv(). I think you will need to use recv! with windows, because you cannot read from sockets in windows using POSIX read(). With other OS's it would be more normal to use the R5RS/R6RS port procedures, because they are easier to use and are buffered. For asynchronous non-blocking ports, I am not sure whether recv! is safe with guile-2.2's suspendable ports or not. The get-line and get-bytevector-n procedures are. The get-bytevector-n! procedure is not. I suspect recv! may not be also. > > More generally, you will find it easier to use something like the > > get-line procedure to read text, or something like get-bytevector-n or > > get-bytevector-n! for reading binary records. recv! only really becomes > > important when the flags argument is meaningful. > Ah ok, thanks for the advice! I thought recv! was the way to go, as it > worked so nicely so far. A call to unix read() on a socket is equivalent to a call to recv() with default flags. At the end of it, the buffered guile port reading procedures generally involve a call to read(). > > Rather than starting a new thread for each connection, you will get much > > better scalability if you use something like fibers > > ( https://github.com/wingo/fibers/ ), guile-a-sync2 > > ( https://github.com/ChrisVine/guile-a-sync2/ ) or 8sync > > ( https://www.gnu.org/software/8sync/ ). > Yep =) I am aware. I already wrote a comment inside the code noting that > this new thread thing is probably not so good. Not sure I should > introduce fibers into this example code yet though. And looking at > fibers and 8sync is on my to-do list ; ) > > Chris > I will try these things and see how it goes. Thanks! The guile-a-sync/guile-a-sync2 libraries (of which I am the author) have an example of a client and server in the doc directory, which might be of interest to you. fibers is definitely worth a look also, but is linux only, and also guile-2.2 only. I think it also has some examples (not sure). Chris