| 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::ServerSocket; | ||
| 26 | use Thrift::UnixSocket; | ||
| 27 | |||
| 28 | use IO::Socket::UNIX; | ||
| 29 | |||
| 30 | package Thrift::UnixServerSocket; | ||
| 31 | use base qw( Thrift::ServerSocket ); | ||
| 32 | use version 0.77; our $VERSION = version->declare("$Thrift::VERSION"); | ||
| 33 | |||
| 34 | # | ||
| 35 | # Constructor. | ||
| 36 | # If a single argument is given that is not a hash, that is the unix domain socket path. | ||
| 37 | # If a single argument is given that is a hash: | ||
| 38 | # @param[in] path unix domain socket file name | ||
| 39 | # @param[in] queue the listen queue size (default is not specified is supplied by ServerSocket) | ||
| 40 | # @example my $serversock = Thrift::UnixServerSocket->new($path); | ||
| 41 | # @example my $serversock = Thrift::UnixServerSocket->new(path => "somepath", queue => 64); | ||
| 42 | # | ||
| 43 | sub new | ||
| 44 | { | ||
| 45 | my $classname = shift; | ||
| 46 | my $args = shift; | ||
| 47 | my $self; | ||
| 48 | |||
| 49 | if (ref($args) eq 'HASH') { | ||
| 50 | $self = $classname->SUPER::new($args); | ||
| 51 | } else { | ||
| 52 | $self = $classname->SUPER::new(); | ||
| 53 | $self->{path} = $args; | ||
| 54 | } | ||
| 55 | |||
| 56 | return bless($self, $classname); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub __client | ||
| 60 | { | ||
| 61 | return Thrift::UnixSocket->new(); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub __listen | ||
| 65 | { | ||
| 66 | my $self = shift; | ||
| 67 | |||
| 68 | my $sock = IO::Socket::UNIX->new( | ||
| 69 | Type => IO::Socket::SOCK_STREAM, | ||
| 70 | Local => $self->{path}, | ||
| 71 | Listen => $self->{queue}) | ||
| 72 | || do { | ||
| 73 | my $error = 'UnixServerSocket: Could not bind to ' . | ||
| 74 | $self->{path} . ' (' . $! . ')'; | ||
| 75 | if ($self->{debug}) { | ||
| 76 | $self->{debugHandler}->($error); | ||
| 77 | } | ||
| 78 | die Thrift::TTransportException->new($error, Thrift::TTransportException::NOT_OPEN); | ||
| 79 | }; | ||
| 80 | |||
| 81 | return $sock; | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; |