]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/perl/lib/Thrift/FramedTransport.pm
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / perl / lib / Thrift / FramedTransport.pm
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 use 5.10.0;
21 use strict;
22 use warnings;
23
24 use Thrift;
25 use Thrift::Transport;
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 use base('Thrift::Transport');
35 use version 0.77; our $VERSION = version->declare("$Thrift::VERSION");
36
37 sub new
38 {
39 my $classname = shift;
40 my $transport = shift;
41 my $read = shift || 1;
42 my $write = shift || 1;
43
44 my $self = {
45 transport => $transport,
46 read => $read,
47 write => $write,
48 wBuf => '',
49 rBuf => '',
50 };
51
52 return bless($self,$classname);
53 }
54
55 sub isOpen
56 {
57 my $self = shift;
58 return $self->{transport}->isOpen();
59 }
60
61 sub open
62 {
63 my $self = shift;
64
65 $self->{transport}->open();
66 }
67
68 sub close
69 {
70 my $self = shift;
71
72 if (defined $self->{transport}) {
73 $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 my $self = shift;
87 my $len = shift;
88
89 if (!$self->{read}) {
90 return $self->{transport}->read($len);
91 }
92
93 if (length($self->{rBuf}) == 0) {
94 $self->_readFrame();
95 }
96
97
98 # Just return full buff
99 if ($len > length($self->{rBuf})) {
100 my $out = $self->{rBuf};
101 $self->{rBuf} = '';
102 return $out;
103 }
104
105 # Return substr
106 my $out = substr($self->{rBuf}, 0, $len);
107 $self->{rBuf} = substr($self->{rBuf}, $len);
108 return $out;
109 }
110
111 #
112 # Reads a chunk of data into the internal read buffer.
113 # (private)
114 sub _readFrame
115 {
116 my $self = shift;
117 my $buf = $self->{transport}->readAll(4);
118 my @val = unpack('N', $buf);
119 my $sz = $val[0];
120
121 $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 my $self = shift;
133 my $buf = shift;
134 my $len = shift;
135
136 unless($self->{write}) {
137 return $self->{transport}->write($buf, $len);
138 }
139
140 if ( defined $len && $len < length($buf)) {
141 $buf = substr($buf, 0, $len);
142 }
143
144 $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 my $self = shift;
154
155 unless ($self->{write}) {
156 return $self->{transport}->flush();
157 }
158
159 my $out = pack('N', length($self->{wBuf}));
160 $out .= $self->{wBuf};
161 $self->{transport}->write($out);
162 $self->{transport}->flush();
163 $self->{wBuf} = '';
164
165 }
166
167 #
168 # FramedTransport factory creates framed transport objects from transports
169 #
170 package Thrift::FramedTransportFactory;
171 use version 0.77; our $VERSION = version->declare("$Thrift::VERSION");
172
173 sub new {
174 my $classname = shift;
175 my $self = {};
176
177 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 my $self = shift;
188 my $trans = shift;
189
190 my $buffered = Thrift::FramedTransport->new($trans);
191 return $buffered;
192 }
193
194 1;