| 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 IO::Socket::INET; | ||
| 25 | use IO::Select; | ||
| 26 | use Thrift; | ||
| 27 | use Thrift::Transport; | ||
| 28 | use Thrift::Socket; | ||
| 29 | |||
| 30 | package Thrift::ServerSocket; | ||
| 31 | use base qw( Thrift::ServerTransport ); | ||
| 32 | use version 0.77; our $VERSION = version->declare("$Thrift::VERSION"); | ||
| 33 | |||
| 34 | # | ||
| 35 | # Constructor. | ||
| 36 | # Legacy construction takes one argument, port number. | ||
| 37 | # New construction takes a hash: | ||
| 38 | # @param[in] host host interface to listen on (undef = all interfaces) | ||
| 39 | # @param[in] port port number to listen on (required) | ||
| 40 | # @param[in] queue the listen queue size (default if not specified is 128) | ||
| 41 | # @example my $serversock = Thrift::ServerSocket->new(host => undef, port => port) | ||
| 42 | # | ||
| 43 | sub new | ||
| 44 | { | ||
| 45 | my $classname = shift; | ||
| 46 | my $args = shift; | ||
| 47 | my $self; | ||
| 48 | |||
| 49 | # Support both old-style "port number" construction and newer... | ||
| 50 | if (ref($args) eq 'HASH') { | ||
| 51 | $self = $args; | ||
| 52 | } | ||
| 53 | else { | ||
| 54 | $self = { port => $args }; | ||
| 55 | } | ||
| 56 | |||
| 57 | if (not defined $self->{queue}) { | ||
| 58 | $self->{queue} = 128; | ||
| 59 | } | ||
| 60 | |||
| 61 | return bless($self, $classname); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub listen | ||
| 65 | { | ||
| 66 | my $self = shift; | ||
| 67 | |||
| 68 | my $sock = $self->__listen() || do { | ||
| 69 | my $error = ref($self) . ': Could not bind to ' . '*:' . $self->{port} . ' (' . $! . ')'; | ||
| 70 | |||
| 71 | if ($self->{debug}) { | ||
| 72 | $self->{debugHandler}->($error); | ||
| 73 | } | ||
| 74 | |||
| 75 | die Thrift::TTransportException->new($error, Thrift::TTransportException::NOT_OPEN); | ||
| 76 | }; | ||
| 77 | |||
| 78 | $self->{handle} = $sock; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub accept | ||
| 82 | { | ||
| 83 | my $self = shift; | ||
| 84 | |||
| 85 | if ( exists $self->{handle} and defined $self->{handle} ) { | ||
| 86 | my $client = $self->{handle}->accept(); | ||
| 87 | my $result = $self->__client(); | ||
| 88 | $result->{handle} = IO::Select->new($client); | ||
| 89 | return $result; | ||
| 90 | } | ||
| 91 | |||
| 92 | return undef; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub close | ||
| 96 | { | ||
| 97 | my $self = shift; | ||
| 98 | |||
| 99 | if ( exists $self->{handle} and defined $self->{handle} ) | ||
| 100 | { | ||
| 101 | $self->{handle}->close(); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | ### | ||
| 106 | ### Overridable methods | ||
| 107 | ### | ||
| 108 | |||
| 109 | sub __client | ||
| 110 | { | ||
| 111 | return Thrift::Socket->new(); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub __listen | ||
| 115 | { | ||
| 116 | my $self = shift; | ||
| 117 | return IO::Socket::INET->new(LocalAddr => $self->{host}, | ||
| 118 | LocalPort => $self->{port}, | ||
| 119 | Proto => 'tcp', | ||
| 120 | Listen => $self->{queue}, | ||
| 121 | ReuseAddr => 1); | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | 1; |