Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: A Game Using TCP Sockets

by oiskuu (Friar)
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. TCP.pm 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); }


Comment on Re: A Game Using TCP Sockets
Select or Download Code
Re^2: A Game Using TCP Sockets
by choroba (Canon) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058118]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2015-07-03 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (51 votes), past polls