Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow IO::Socket.recv() to take a parameter specifying the number of bytes to receive #1380

Closed
p6rt opened this issue Oct 25, 2009 · 4 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Oct 25, 2009

Migrated from rt.perl.org#70017 (status was 'resolved')

Searchable as RT70017$

@p6rt
Copy link
Author

p6rt commented Oct 25, 2009

From @carbin

The current incarnation of recv() continually calls Parrot's recv()
function until it returns nothing. However, Parrot's recv() will never
return empty unless the server disconnects. Instead it will hang
until, either it receives something (in which case the loop starts
again) or forever -- whichever comes first. This means that the
current recv will only return once the sever disconnects, which makes
it fine for getting data from webservers but makes it impossible to
deal with persistent connections, eg. IRC servers.

This patch gets around this by making recv() take a parameter which
specifies the number of bytes to receive, once the required number of
bytes are available it will return, storing any other data in a buffer
for later retrieval. I have made the parameter optional (defaulting to
Inf) so that any current usage of recv() will behave exactly like it
did before.

--
Carlin

@p6rt
Copy link
Author

p6rt commented Oct 25, 2009

From @carbin

0001-Change-IO-Socket.recv-to-accept-a-parameter-speci.patch
From 34f5c66ba8ea9e9f345c8847d08e58dfb3ca7d7c Mon Sep 17 00:00:00 2001
From: Carlin Bingham <carlin@theintersect.org>
Date: Mon, 26 Oct 2009 11:40:40 +1300
Subject: [PATCH] Change IO::Socket.recv() to accept a parameter specifying the number of bytes which will be received

---
 src/setting/IO/Socket.pm |   22 ++++++++++++++--------
 1 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/setting/IO/Socket.pm b/src/setting/IO/Socket.pm
index 3b175f2..2373b4b 100644
--- a/src/setting/IO/Socket.pm
+++ b/src/setting/IO/Socket.pm
@@ -2,16 +2,22 @@ use v6;
 
 role IO::Socket {
     has $!PIO;
+    has $!buffer = '';
 
-    method recv () {
+    method recv (Int $bufsize = Inf) {
         fail('Socket not available') unless $!PIO;
-        my $received = $!PIO.recv();
-        my $len = $received.chars;
-        my $buf;
-        while $len > 0 {
-            $buf = $!PIO.recv();
-            $received ~= $buf;
-            $len = $buf.chars;
+        my $received;
+        while $bufsize > $!buffer.bytes {
+            $received = $!PIO.recv();
+            last unless $received.chars;
+            $!buffer ~= $received;
+        }
+        if $bufsize == Inf {
+            $received = $!buffer;
+            $!buffer = '';
+        } else {
+            $received = $!buffer.substr(0, $bufsize);
+            $!buffer .= substr($bufsize);
         }
         return $received;
     }
-- 
1.6.0.4

@p6rt
Copy link
Author

p6rt commented Oct 26, 2009

From @moritz

Applied as 22ded10cd3f8e9774ca17d5bb8e42bed7efb7938, tests unfudged.
Thanks you very much for your patch!

Cheers,
Moritz

@p6rt
Copy link
Author

p6rt commented Oct 26, 2009

@moritz - Status changed from 'new' to 'resolved'

@p6rt p6rt closed this as completed Oct 26, 2009
@p6rt p6rt added the patch label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant