Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Various SocketConnection improvements
* Better debugging output
* Ability to log privacy-scrubbed messages
* .sendln method
* Factor out !start-threads from .new
* Various minor code cleanups
  • Loading branch information
Geoffrey Broadwell committed Jan 16, 2014
1 parent 1543ec9 commit 30de528
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions lib/Net/IRC/SocketConnection.pm
Expand Up @@ -9,55 +9,62 @@ class Net::IRC::SocketConnection {
has Channel $.to-socket;
has Channel $.from-socket;

multi method new(:$host!, :$log = $*LOG, *%socket-options) {
$log.info("Making socket connection to $host");
my $socket = IO::Socket::INET.new(:$host, |%socket-options);
self.new($socket, $log);
}

multi method new(IO::Socket $socket, Net::IRC::Logger $log = $*LOG) {
$log.debug('Building Net::IRC::SocketConnection');
my $to-socket = Channel.new;
my $from-socket = Channel.new;
my $self = self.bless(:$log, :$socket, :$to-socket, :$from-socket);

$log.info("Starting channel -> socket thread ...");
$self!start-threads;
$self;
}

method !start-threads() {
$.log.info('Starting channel -> socket thread');
start {
loop {
winner $to-socket {
more * { $log.debug("Writing $_.elems() bytes to socket");
$socket.write($_) }
done * { $log.debug("Exiting channel -> socket thread");
winner $.to-socket {
more * { $.log.debug("»»» $_.value()");
$.socket.write($_.key) }
done * { $.log.info('Exiting channel -> socket thread');
last }
}
}
}

$log.info("Starting socket -> channel thread ...");
$.log.info('Starting socket -> channel thread');
start {
loop {
my $chunk = $socket.recv(:bin);
my $chunk = $.socket.recv(:bin);
if $chunk.elems {
$log.debug("Received $chunk.elems() bytes from socket");
$from-socket.send($chunk);
$.log.debug("<-- $chunk.elems() bytes");
$.from-socket.send($chunk);
}
else {
$log.debug("Exiting socket -> channel thread");
$from-socket.close;
$.log.info('Exiting socket -> channel thread');
$.from-socket.close;
last;
}
}
}

$self;
}

multi method new(:$host!, :$log = $*LOG, *%socket-options) {
$log.info("Making socket connection to $host ...");
my $socket = IO::Socket::INET.new(:$host, |%socket-options);
self.new($socket, $log);
multi method send(Blob $data) {
$.to-socket.send: $data => "$data.elems() bytes";
}

multi method send(Blob $data) {
$.to-socket.send($data);
multi method send(Str $text, :$scrubbed = $text) {
$.to-socket.send: $text.encode('utf8') => $scrubbed;
}

multi method send(Str $message) {
$.to-socket.send($message.encode('utf8'));
multi method sendln(Str $text, :$scrubbed = $text) {
$.to-socket.send: "$text\c13\c10".encode('utf8') => $scrubbed;
}

method close(:$to, :$from) {
Expand Down

0 comments on commit 30de528

Please sign in to comment.