I tried to look into it further but you code is truncated and won't compile.
Oops! I had missed that, thanks.
Below is the cycle subroutine with the truncated parts, for anyone kind enough to try to figure this out:
sub cycle {
my $self = shift;
my (@new_readable) = $self->{readable_handles}->can_read(.1);
foreach my $socket (@new_readable) {
if ($socket == $self->{main_socket}){
#new connection
my $new_socket = $socket->accept();
#Make it non-blocking
#fcntl($new_socket, F_SETFL(), O_NONBLOCK());
$self->{players}->{$new_socket} = {
read => 0,
message => ''
};
$self->{readable_handles}->add($new_socket);
print "Added new connection\n";
#added to list of connections
#may not have data yet
} else {
#check for connection closure
if (not eof($socket)){
my $message = '';
my $read_count;
#Read from socket
#First, get size
#1) Does this always work?
#2) How can we get rid of the hardcoded sizeof(int)?
$read_count = sysread($socket, $message, 4);
if($read_count < 4) {
print "Nope, can't guarantee getting message_lengt
+h\n";
} else {
#store length of incoming
#Note that we'll have to use pack/unpack to make i
+nt
$self->{players}->{$socket}->{read} = unpack("l",$
+message);
$self->{players}->{$socket}->{message}="";
print "Got incoming length: $message\n";
$read_count = sysread($socket, $message, $self->{p
+layers}->{$socket}->{read}); #was missing end of this line
if($read_count < $self->{players}->{$socket}->{rea
+d}){
print "Only got $read_count of $self->{players
+}->{$socket}->{read}\n"; #was missing end of this line
} else {
print "Got message: $message\n";
$self->{players}->{$socket}->{read}=0;
}
}
} else {
#closed socket, remove from list
$self->{readable_handles}->remove($socket);
print "Another closed socket\n";
} #end checking for socket closure
} #end checking for main/non-main handle
} #end foreach
}
|