Skip to content

Commit

Permalink
channel basics
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 9, 2016
1 parent ee0f220 commit 4324a82
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/Mojo/Channel.pm
@@ -0,0 +1,4 @@
package Mojo::Channel;
use Mojo::Base -base;

1;
4 changes: 4 additions & 0 deletions lib/Mojo/Channel/HTTP.pm
@@ -0,0 +1,4 @@
package Mojo::Channel::HTTP;
use Mojo::Base 'Mojo::Channel';

1;
4 changes: 4 additions & 0 deletions lib/Mojo/Channel/HTTP/Client.pm
@@ -0,0 +1,4 @@
package Mojo::Channel::HTTP::Client;
use Mojo::Base 'Mojo::Channel::HTTP';

1;
4 changes: 4 additions & 0 deletions lib/Mojo/Channel/HTTP/Server.pm
@@ -0,0 +1,4 @@
package Mojo::Channel::HTTP::Server;
use Mojo::Base 'Mojo::Channel::HTTP';

1;
4 changes: 4 additions & 0 deletions lib/Mojo/Channel/WebSocket.pm
@@ -0,0 +1,4 @@
package Mojo::Channel::WebSocket;
use Mojo::Base 'Mojo::Channel';

1;
4 changes: 4 additions & 0 deletions lib/Mojo/Channel/WebSocket/Client.pm
@@ -0,0 +1,4 @@
package Mojo::Channel::WebSocket::Client;
use Mojo::Base 'Mojo::Channel::WebSocket';

1;
4 changes: 4 additions & 0 deletions lib/Mojo/Channel/WebSocket/Server.pm
@@ -0,0 +1,4 @@
package Mojo::Channel::WebSocket::Server;
use Mojo::Base 'Mojo::Channel::WebSocket';

1;
9 changes: 7 additions & 2 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -2,6 +2,8 @@ package Mojo::Server::Daemon;
use Mojo::Base 'Mojo::Server';

use Carp 'croak';
use Mojo::Channel::HTTP::Server;
use Mojo::Channel::WebSocket::Server;
use Mojo::IOLoop;
use Mojo::Transaction::WebSocket;
use Mojo::URL;
Expand Down Expand Up @@ -126,7 +128,9 @@ sub _finish {

# Successful upgrade
if ($ws->handshake($tx->next(undef))->res->code == 101) {
$c->{tx} = $ws;
warn Mojo::Util::dumper $c;
$c = $self->{connections}{$id}
= Mojo::Channel::WebSocket::Server->new(tls => $c->{tls}, tx => $ws);
weaken $self;
$ws->on(resume => sub { $self->_write($id) });
$ws->server_open;
Expand Down Expand Up @@ -174,7 +178,8 @@ sub _listen {
$options => sub {
my ($loop, $stream, $id) = @_;

my $c = $self->{connections}{$id} = {tls => $tls};
my $c = $self->{connections}{$id}
= Mojo::Channel::HTTP::Server->new(tls => $tls);
warn "-- Accept $id (@{[$stream->handle->peerhost]})\n" if DEBUG;
$stream->timeout($self->inactivity_timeout);

Expand Down
14 changes: 12 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -3,6 +3,8 @@ use Mojo::Base 'Mojo::EventEmitter';

# "Fry: Since when is the Internet about robbing people of their privacy?
# Bender: August 6, 1991."
use Mojo::Channel::HTTP::Client;
use Mojo::Channel::WebSocket::Client;
use Mojo::IOLoop;
use Mojo::Util qw(monkey_patch term_escape);
use Mojo::UserAgent::CookieJar;
Expand Down Expand Up @@ -142,7 +144,11 @@ sub _connect_proxy {
$self->_remove($id);
$id = $self->_connect($loop, 0, $old, $handle,
sub { shift->_start($loop, $old->connection($id), $cb) });
$self->{connections}{$id} = {cb => $cb, ioloop => $loop, tx => $old};
$self->{connections}{$id} = Mojo::Channel::HTTP::Client->new(
cb => $cb,
ioloop => $loop,
tx => $old
);
}
);
}
Expand Down Expand Up @@ -186,7 +192,8 @@ sub _connection {
# Connect
$id = $self->_connect($loop, 1, $tx, undef, \&_connected);
warn "-- Connect $id ($proto://$host:$port)\n" if DEBUG;
$self->{connections}{$id} = {cb => $cb, ioloop => $loop, tx => $tx};
$self->{connections}{$id}
= Mojo::Channel::HTTP::Client->new(cb => $cb, ioloop => $loop, tx => $tx);

return $id;
}
Expand Down Expand Up @@ -232,6 +239,9 @@ sub _finish {

# Upgrade connection to WebSocket
if (my $new = $self->transactor->upgrade($old)) {
$self->{connections}{$id}
= Mojo::Channel::WebSocket::Client->new(ioloop => $c->{ioloop},
tx => $new);
weaken $self;
$new->on(resume => sub { $self->_write($id) });
$c->{cb}($self, $c->{tx} = $new);
Expand Down

0 comments on commit 4324a82

Please sign in to comment.