WebSocket/Streamer.pm

filter: Statementstmt 95.0% 38 / 40 Branchbran 75.0% 6 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 83.3% 55 / 66 CC 6 SCAR 7.4
CC
6
Cov
88%
DESTROY
3.8
_load_frame_class
3.0
create_chunk
2.0
CRAP 2.1
« WebSocket/Base/DataFrame.pm ↑ Summary WebSocket/Endpoint/Client.pm »
1 package Net::WebSocket::Streamer;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Net::WebSocket::Streamer - Stream a WebSocket message easily
8  
9 =head1 SYNOPSIS
10  
11 Here’s the gist of it:
12  
13 #Use the ::Client or ::Server subclass as needed.
14 my $streamer = Net::WebSocket::Streamer::Client->new('binary');
15  
16 my $frame = $streamer->create_chunk($buf);
17  
18 my $last_frame = $streamer->create_final($buf);
19  
20 … but a more complete example might be this: streaming a file
21 of arbitrary size in 64-KiB chunks:
22  
23 my $size = -s $rfh;
24  
25 while ( read $rfh, my $buf, 65536 ) {
26 my $frame;
27  
28 if (tell($rfh) == $size) {
29 $frame = $streamer->create_final($buf);
30 }
31 else {
32 $frame = $streamer->create_chunk($buf);
33 }
34  
35 syswrite $wfh, $frame->to_bytes();
36 }
37  
38 You can, of course, create/send an empty final frame for cases where you’re
39 not sure how much data will actually be sent.
40  
41 Note that the receiving application won’t necessarily have access to the
42 individual message fragments (i.e., frames) that you send. Web browsers,
43 for example, only expose messages, not frames. You may thus be better off
44 sending full messages rather than frames.
45  
46 =head1 EXTENSION SUPPORT
47  
48 To stream custom frame types (or overridden classes), you can subclass
49 this module and define C<frame_class_*> constants, where C<*> is the
50 frame type, e.g., C<text>, C<binary>.
51  
52 =cut
53  
54 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 1 ▶use Net::WebSocket::Frame::continuation ();
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use Net::WebSocket::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 use constant {
61  
62 #These can be overridden in subclasses.
63 1 frame_class_text => 'Net::WebSocket::Frame::text',
64 frame_class_binary => 'Net::WebSocket::Frame::binary',
65  
66 FINISHED_INDICATOR => __PACKAGE__ . '::__ALREADY_SENT_FINAL',
67 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 sub new {
70 18 ▶ my ($class, $type) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
71  
72 18 my $frame_class = $class->_load_frame_class($type);
73  
74 18 return bless { class => $frame_class, pid => $$ }, $class;
75 }
76  
77 sub create_chunk {
78 35 ▶ my $self = shift;
Subroutine create_chunk: called CC=2 SCAR=6.9
Pod undocumented
79  
80 35 my $frame = $self->{'class'}->new(
81 fin => 0,
82 $self->FRAME_MASK_ARGS(),
83 payload_sr => \$_[0],
84 );
85  
86 #The first $frame we create needs to be typed (e.g., text or binary),
87 #but all subsequent ones must be continuation.
88 35 ▶ if ($self->{'class'} ne 'Net::WebSocket::Frame::continuation') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"class"} ne "Net::WebSocket::Frame::continuation") 18 17 35
89 18 $self->{'class'} = 'Net::WebSocket::Frame::continuation';
90 }
91  
92 35 return $frame;
93 }
94  
95 sub create_final {
96 18 ▶ my $self = shift;
Subroutine create_final: called CC=1 SCAR=0.0
Pod undocumented
97  
98 18 my $frame = $self->{'class'}->new(
99 $self->FRAME_MASK_ARGS(),
100 fin => 1,
101 payload_sr => \$_[0],
102 );
103  
104 18 $self->{'finished'} = 1;
105  
106 18 return $frame;
107 }
108  
109 sub _load_frame_class {
110 18 ▶ my ($class, $type) = @_;
Subroutine _load_frame_class: called CC=3 SCAR=11.1
111  
112 18 my $frame_class = $class->can("frame_class_$type");
113 18 ▶ if (!$frame_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame_class) 0 18 18
114 0 die "Unknown frame type: “$type”!";
115 }
116  
117 18 $frame_class = $frame_class->();
118 18 ▶ if (!$frame_class->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame_class->can("new")) 1 17 18
119 1 Module::Load::load($frame_class);
120 }
121  
122 18 return $frame_class;
123 }
124  
125 sub DESTROY {
126 18 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=3 SCAR=13.3
127  
128 18 ▶ if (($self->{'pid'} == $$) && !$self->{'finished'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid"} == $$ and not $self->{"finished"}) 0 18 18
Condition33%1/3
$self->{"pid"} == $$ and not $self->{"finished"}
!ll&&!rl&&r
0180
MC/DC0%0/2
$self->{"pid"} == $$ and not $self->{"finished"}
$self->{"pid"} == $$ not $self->{"finished"}
Truth table33%1/3
A and not(B)
A= $self->{"pid"} == $$B= not $self->{"finished"}
ABresult
0X0
100
111
129 0 die Net::WebSocket::X->create('UnfinishedStream', $self);
130 }
131  
132 18 return;
133 }
134  
135 1 1;
« WebSocket/Base/DataFrame.pm ↑ Summary WebSocket/Endpoint/Client.pm »