Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 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 strict;
21
use warnings;
22
 
23
use Thrift;
24
use Thrift::Transport;
25
 
26
#
27
# Framed transport. Writes and reads data in chunks that are stamped with
28
# their length.
29
#
30
# @package thrift.transport
31
#
32
package Thrift::FramedTransport;
33
 
34
use base('Thrift::Transport');
35
 
36
sub new
37
{
38
    my $classname = shift;
39
    my $transport = shift;
40
    my $read      = shift || 1;
41
    my $write     = shift || 1;
42
 
43
    my $self      = {
44
        transport => $transport,
45
        read      => $read,
46
        write     => $write,
47
        wBuf      => '',
48
        rBuf      => '',
49
    };
50
 
51
    return bless($self,$classname);
52
}
53
 
54
sub isOpen
55
{
56
    my $self = shift;
57
    return $self->{transport}->isOpen();
58
}
59
 
60
sub open
61
{
62
    my $self = shift;
63
 
64
    $self->{transport}->open();
65
}
66
 
67
sub close
68
{
69
    my $self = shift;
70
 
71
    $self->{transport}->close();
72
}
73
 
74
#
75
# Reads from the buffer. When more data is required reads another entire
76
# chunk and serves future reads out of that.
77
#
78
# @param int $len How much data
79
#
80
sub read
81
{
82
 
83
    my $self = shift;
84
    my $len  = shift;
85
 
86
    if (!$self->{read}) {
87
        return $self->{transport}->read($len);
88
    }
89
 
90
    if (length($self->{rBuf}) == 0) {
91
        $self->_readFrame();
92
    }
93
 
94
 
95
    # Just return full buff
96
    if ($len > length($self->{rBuf})) {
97
        my $out = $self->{rBuf};
98
        $self->{rBuf} = '';
99
        return $out;
100
    }
101
 
102
    # Return substr
103
    my $out = substr($self->{rBuf}, 0, $len);
104
    $self->{rBuf} = substr($self->{rBuf}, $len);
105
    return $out;
106
}
107
 
108
#
109
# Reads a chunk of data into the internal read buffer.
110
# (private)
111
sub _readFrame
112
{
113
    my $self = shift;
114
    my $buf  = $self->{transport}->readAll(4);
115
    my @val  = unpack('N', $buf);
116
    my $sz   = $val[0];
117
 
118
    $self->{rBuf} = $self->{transport}->readAll($sz);
119
}
120
 
121
#
122
# Writes some data to the pending output buffer.
123
#
124
# @param string $buf The data
125
# @param int    $len Limit of bytes to write
126
#
127
sub write
128
{
129
    my $self = shift;
130
    my $buf  = shift;
131
    my $len  = shift;
132
 
133
    unless($self->{write}) {
134
        return $self->{transport}->write($buf, $len);
135
    }
136
 
137
    if ( defined $len && $len < length($buf)) {
138
        $buf = substr($buf, 0, $len);
139
    }
140
 
141
    $self->{wBuf} .= $buf;
142
  }
143
 
144
#
145
# Writes the output buffer to the stream in the format of a 4-byte length
146
# followed by the actual data.
147
#
148
sub flush
149
{
150
    my $self = shift;
151
 
152
    unless ($self->{write}) {
153
        return $self->{transport}->flush();
154
    }
155
 
156
    my $out = pack('N', length($self->{wBuf}));
157
    $out .= $self->{wBuf};
158
    $self->{transport}->write($out);
159
    $self->{transport}->flush();
160
    $self->{wBuf} = '';
161
 
162
}
163
 
164
1;