Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: A Game Using TCP Sockets

by oiskuu (Hermit)
on Oct 14, 2013 at 02:46 UTC ( #1058118=note: print w/replies, xml ) Need Help??

in reply to A Game Using TCP Sockets

Ahem, I took another look at the game, just ignore my previous rambling.
Discovered the following though:

} elsif ($get =~ /MESSAGE ([^ ]+) (.*)\n$/) {
This pattern can never match since the line is chomped?

Why is Tk::repeat necessary, what happens if it's omitted?
Update: ah, I see it now. Why don't you modify get_line to return all lines available if wantarray?
Process could then do ... for my $get (get_line($server)) {} ...

Just a note: sysread() can return any number of bytes, length being the limit.
Give "ip link set eth0 mtu 60" on the server, for some short-packet fun.

Loop the sysread(), or recurse into get_line.

Update. in the git, does it not read as follows:

sub get_line { my ($fh) = @_; warn "Getting from $fh\n"; my $line = read_from_buffer($fh); return $line if defined $line; if (sysread $fh, my $read, BUFF_SIZE) { warn "sysread $read"; $buff{$fh} .= $read; } $line = read_from_buffer($fh); warn "Got $line\n"; warn do { use Data::Dumper; Dumper $buff{$fh} }; return $line; }

I would simply write (untested):

sub get_line { my ($fh) = @_; warn "Getting from $fh\n"; my $line = read_from_buffer($fh); return $line if defined $line; sysread($fh, $_, BUFF_SIZE, length) for $buff{$fh}; return get_line($fh); }

Replies are listed 'Best First'.
Re^2: A Game Using TCP Sockets
by choroba (Bishop) on Oct 14, 2013 at 20:09 UTC
    Have you seen the code? sysread is looped. readline was not working correctly - after reading a line, the following lines might end in a buffer, from which you never retrieve them, as the handle is no longer readable.

    Update: Maybe I did not understand your point. Please, show some code: how should I loop sysread? What should be the condition of the loop, to avoid blocking?

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058118]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2018-06-22 20:07 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.