File Coverage

blib/lib/Thrift/FramedTransport.pm
Criterion Covered Total %
statement 23 77 29.8
branch 0 14 0.0
condition 0 7 0.0
subroutine 8 18 44.4
pod 0 7 0.0
total 31 123 25.2


line stmt bran cond sub pod time code
1             #
2             # Licensed to the Apache Software Foundation (ASF) under one
3             # or more contributor license agreements. See the NOTICE file
4             # distributed with this work for additional information
5             # regarding copyright ownership. The ASF licenses this file
6             # to you under the Apache License, Version 2.0 (the
7             # "License"); you may not use this file except in compliance
8             # with the License. You may obtain a copy of the License at
9             #
10             # http://www.apache.org/licenses/LICENSE-2.0
11             #
12             # Unless required by applicable law or agreed to in writing,
13             # software distributed under the License is distributed on an
14             # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15             # KIND, either express or implied. See the License for the
16             # specific language governing permissions and limitations
17             # under the License.
18             #
19              
20 1     1   559 use 5.10.0;
  1         4  
21 1     1   6 use strict;
  1         3  
  1         27  
22 1     1   5 use warnings;
  1         3  
  1         32  
23              
24 1     1   5 use Thrift;
  1         2  
  1         41  
25 1     1   381 use Thrift::Transport;
  1         3  
  1         38  
26              
27             #
28             # Framed transport. Writes and reads data in chunks that are stamped with
29             # their length.
30             #
31             # @package thrift.transport
32             #
33             package Thrift::FramedTransport;
34 1     1   7 use base('Thrift::Transport');
  1         1  
  1         115  
35 1     1   7 use version 0.77; our $VERSION = version->declare("$Thrift::VERSION");
  1         13  
  1         6  
36              
37             sub new
38             {
39 0     0 0   my $classname = shift;
40 0           my $transport = shift;
41 0   0       my $read = shift || 1;
42 0   0       my $write = shift || 1;
43              
44 0           my $self = {
45             transport => $transport,
46             read => $read,
47             write => $write,
48             wBuf => '',
49             rBuf => '',
50             };
51              
52 0           return bless($self,$classname);
53             }
54              
55             sub isOpen
56             {
57 0     0 0   my $self = shift;
58 0           return $self->{transport}->isOpen();
59             }
60              
61             sub open
62             {
63 0     0 0   my $self = shift;
64              
65 0           $self->{transport}->open();
66             }
67              
68             sub close
69             {
70 0     0 0   my $self = shift;
71              
72 0 0         if (defined $self->{transport}) {
73 0           $self->{transport}->close();
74             }
75             }
76              
77             #
78             # Reads from the buffer. When more data is required reads another entire
79             # chunk and serves future reads out of that.
80             #
81             # @param int $len How much data
82             #
83             sub read
84             {
85              
86 0     0 0   my $self = shift;
87 0           my $len = shift;
88              
89 0 0         if (!$self->{read}) {
90 0           return $self->{transport}->read($len);
91             }
92              
93 0 0         if (length($self->{rBuf}) == 0) {
94 0           $self->_readFrame();
95             }
96              
97              
98             # Just return full buff
99 0 0         if ($len > length($self->{rBuf})) {
100 0           my $out = $self->{rBuf};
101 0           $self->{rBuf} = '';
102 0           return $out;
103             }
104              
105             # Return substr
106 0           my $out = substr($self->{rBuf}, 0, $len);
107 0           $self->{rBuf} = substr($self->{rBuf}, $len);
108 0           return $out;
109             }
110              
111             #
112             # Reads a chunk of data into the internal read buffer.
113             # (private)
114             sub _readFrame
115             {
116 0     0     my $self = shift;
117 0           my $buf = $self->{transport}->readAll(4);
118 0           my @val = unpack('N', $buf);
119 0           my $sz = $val[0];
120              
121 0           $self->{rBuf} = $self->{transport}->readAll($sz);
122             }
123              
124             #
125             # Writes some data to the pending output buffer.
126             #
127             # @param string $buf The data
128             # @param int $len Limit of bytes to write
129             #
130             sub write
131             {
132 0     0 0   my $self = shift;
133 0           my $buf = shift;
134 0           my $len = shift;
135              
136 0 0         unless($self->{write}) {
137 0           return $self->{transport}->write($buf, $len);
138             }
139              
140 0 0 0       if ( defined $len && $len < length($buf)) {
141 0           $buf = substr($buf, 0, $len);
142             }
143              
144 0           $self->{wBuf} .= $buf;
145             }
146              
147             #
148             # Writes the output buffer to the stream in the format of a 4-byte length
149             # followed by the actual data.
150             #
151             sub flush
152             {
153 0     0 0   my $self = shift;
154              
155 0 0         unless ($self->{write}) {
156 0           return $self->{transport}->flush();
157             }
158              
159 0           my $out = pack('N', length($self->{wBuf}));
160 0           $out .= $self->{wBuf};
161 0           $self->{transport}->write($out);
162 0           $self->{transport}->flush();
163 0           $self->{wBuf} = '';
164              
165             }
166              
167             #
168             # FramedTransport factory creates framed transport objects from transports
169             #
170             package Thrift::FramedTransportFactory;
171 1     1   661 use version 0.77; our $VERSION = version->declare("$Thrift::VERSION");
  1         42  
  1         7  
172              
173             sub new {
174 0     0     my $classname = shift;
175 0           my $self = {};
176              
177 0           return bless($self, $classname);
178             }
179              
180             #
181             # Build a framed transport from the base transport
182             #
183             # @return Thrift::FramedTransport transport
184             #
185             sub getTransport
186             {
187 0     0     my $self = shift;
188 0           my $trans = shift;
189              
190 0           my $buffered = Thrift::FramedTransport->new($trans);
191 0           return $buffered;
192             }
193              
194             1;