| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # Win32::ProcFarm::Port - manages access to the TCP port for ProcFarm system | ||
| 4 | # | ||
| 5 | # Author: Toby Everett | ||
| 6 | # Revision: 2.15 | ||
| 7 | # Last Change: Namespace change | ||
| 8 | ############################################################################# | ||
| 9 | # Copyright 1999, 2000, 2001 Toby Everett. All rights reserved. | ||
| 10 | # | ||
| 11 | # This file is distributed under the Artistic License. See | ||
| 12 | # http://www.ActiveState.com/corporate/artistic_license.htm or | ||
| 13 | # the license that comes with your perl distribution. | ||
| 14 | # | ||
| 15 | # For comments, questions, bugs or general interest, feel free to | ||
| 16 | # contact Toby Everett at teverett@alascom.att.com | ||
| 17 | ############################################################################# | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | Win32::ProcFarm::Port - manages access to the TCP port for ProcFarm system | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | use Win32::ProcFarm::Port; | ||
| 26 | |||
| 27 | $port_obj = Win32::ProcFarm::Port->new(9000, 1); | ||
| 28 | |||
| 29 | print $port_obj->get_port_num; | ||
| 30 | |||
| 31 | $socket = $port_obj->get_next_connection; | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | =head2 Installation instructions | ||
| 36 | |||
| 37 | This installs with MakeMaker as part of Win32::ProcFarm. | ||
| 38 | |||
| 39 | To install via MakeMaker, it's the usual procedure - download from CPAN, | ||
| 40 | extract, type "perl Makefile.PL", "nmake" then "nmake install". Don't | ||
| 41 | do an "nmake test" because the I haven't written a test suite yet. | ||
| 42 | |||
| 43 | =head1 METHODS | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | use IO::Socket; | ||
| 48 | |||
| 49 | package Win32::ProcFarm::Port; | ||
| 50 | |||
| 51 | use strict; | ||
| 52 | use vars qw($VERSION @ISA); | ||
| 53 | |||
| 54 | $VERSION = '2.15'; | ||
| 55 | |||
| 56 | =head2 new | ||
| 57 | |||
| 58 | The C<new> method creates a new C<Win32::ProcFarm::Port> object (fancy that!). It takes two | ||
| 59 | parameters - the first is the port number to listen on, and the second is the number of listeners | ||
| 60 | to create - this will specify the number of "hold lines" for the port object. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub new { | ||
| 65 | my $class = shift; | ||
| 66 | my($port_num, $count) = @_; | ||
| 67 | |||
| 68 | my $self = { | ||
| 69 | 'port_num' => $port_num, | ||
| 70 | 'port' => undef, | ||
| 71 | 'listeners' => $count, | ||
| 72 | }; | ||
| 73 | |||
| 74 | $self->{port} = IO::Socket::INET->new(LocalPort => $self->{port_num}, Proto => 'tcp', | ||
| 75 | Listen => $count, Reuse => 1) or die "Could not connect: $!"; | ||
| 76 | |||
| 77 | bless $self, $class; | ||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | =head2 get_port_num | ||
| 82 | |||
| 83 | This returns the port number passed in the C<new> method. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub get_port_num { | ||
| 88 | my $self = shift; | ||
| 89 | return $self->{port_num}; | ||
| 90 | } | ||
| 91 | |||
| 92 | =head2 get_listeners | ||
| 93 | |||
| 94 | This returns the number of listeners created in the C<new> method. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub get_listeners { | ||
| 99 | my $self = shift; | ||
| 100 | return $self->{listeners}; | ||
| 101 | } | ||
| 102 | |||
| 103 | =head2 get_next_connection | ||
| 104 | |||
| 105 | This accepts an inbound connection and returns the socket object. If the inbound connection is | ||
| 106 | not from 127.0.0.1, the method calls C<die> as this indicates an attempt to hack the system by an | ||
| 107 | external host. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub get_next_connection { | ||
| 112 | my $self = shift; | ||
| 113 | |||
| 114 | my $socket = $self->{port}->accept(); | ||
| 115 | $socket->peerhost eq "127.0.0.1" or | ||
| 116 | die "Attempt to connect from illegal address: ".$socket->peerhost."\n"; | ||
| 117 | return $socket; | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; |