]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/perl/lib/Thrift/Socket.pm
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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
25 use Thrift
::Exception
;
26 use Thrift
::Transport
;
31 package Thrift
::Socket
;
32 use base
qw( Thrift::Transport );
33 use version
0.77; our $VERSION = version-
>declare("$Thrift::VERSION");
36 # Construction and usage
39 # my $socket = Thrift::Socket->new(\%opts);
43 # host => host to connect to
44 # port => port to connect to
45 # sendTimeout => timeout used for send and for connect
46 # recvTimeout => timeout used for recv
51 my $classname = shift;
64 if (defined $opts and ref $opts eq ref {}) {
66 # argument is a hash of options so override the defaults
67 $self->{$_} = $opts->{$_} for keys %$opts;
71 # older style constructor takes 3 arguments, none of which are required
72 $self->{host
} = $opts || 'localhost';
73 $self->{port
} = shift || 9090;
77 return bless($self,$classname);
86 $self->{sendTimeout
} = $timeout;
94 $self->{recvTimeout
} = $timeout;
99 # Tests whether this is open
101 # @return bool true if the socket is open
107 if( defined $self->{handle
} ){
108 return ($self->{handle
}->handles())[0]->connected;
115 # Connects the socket.
121 my $sock = $self->__open() || do {
122 my $error = ref($self).': Could not connect to '.$self->{host
}.':'.$self->{port
}.' ('.$!.')';
123 die Thrift
::TTransportException-
>new($error, Thrift
::TTransportException
::NOT_OPEN
);
126 $self->{handle
} = IO
::Select-
>new( $sock );
135 if( defined $self->{handle
} ) {
141 # Uses stream get contents to do the reading
143 # @param int $len How many bytes
144 # @return string Binary data
152 return unless defined $self->{handle
};
157 my $sock = $self->__wait();
158 my $buf = $self->__recv($sock, $len);
160 if (!defined $buf || $buf eq '') {
162 die Thrift
::TTransportException-
>new(ref($self).': Could not read '.$len.' bytes from '.
163 $self->{host
}.':'.$self->{port
}, Thrift
::TTransportException
::END_OF_FILE
);
166 elsif ((my $sz = length($buf)) < $len) {
179 # Read from the socket
181 # @param int $len How many bytes
182 # @return string Binary data
189 return unless defined $self->{handle
};
191 my $sock = $self->__wait();
192 my $buf = $self->__recv($sock, $len);
194 if (!defined $buf || $buf eq '') {
196 die Thrift
::TTransportException-
>new(ref($self).': Could not read '.$len.' bytes from '.
197 $self->{host
}.':'.$self->{port
}, Thrift
::TTransportException
::END_OF_FILE
);
206 # Write to the socket.
208 # @param string $buf The data to write
215 return unless defined $self->{handle
};
217 while (length($buf) > 0) {
219 my @sockets = $self->{handle
}->can_write( $self->{sendTimeout
} / 1000 );
222 die Thrift
::TTransportException-
>new(ref($self).': timed out writing to bytes from '.
223 $self->{host
}.':'.$self->{port
}, Thrift
::TTransportException
::TIMED_OUT
);
226 my $sent = $self->__send($sockets[0], $buf);
228 if (!defined $sent || $sent == 0 ) {
230 die Thrift
::TTransportException-
>new(ref($self).': Could not write '.length($buf).' bytes '.
231 $self->{host
}.':'.$self->{host
}, Thrift
::TTransportException
::END_OF_FILE
);
235 $buf = substr($buf, $sent);
240 # Flush output to the socket.
246 return unless defined $self->{handle
};
248 my $ret = ($self->{handle
}->handles())[0]->flush;
252 ### Overridable methods
256 # Open a connection to a server.
261 return IO
::Socket
::INET-
>new(PeerAddr
=> $self->{host
},
262 PeerPort
=> $self->{port
},
264 Timeout
=> $self->{sendTimeout
} / 1000);
268 # Close the connection
273 CORE
::close(($self->{handle
}->handles())[0]);
279 # @param[in] $sock the socket
280 # @param[in] $len the length to read
281 # @returns the data buffer that was read
289 $sock->recv($buf, $len);
296 # @param[in] $sock the socket
297 # @param[in] $buf the data buffer
298 # @returns the number of bytes written
305 return $sock->send($buf);
309 # Wait for data to be readable
311 # @returns a socket that can be read
316 my @sockets = $self->{handle
}->can_read( $self->{recvTimeout
} / 1000 );
319 die Thrift
::TTransportException-
>new(ref($self).': timed out reading from '.
320 $self->{host
}.':'.$self->{port
}, Thrift
::TTransportException
::TIMED_OUT
);