home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / SocketFactory.pm < prev    next >
Encoding:
Perl POD Document  |  2004-05-25  |  47.9 KB  |  1,452 lines

  1. # $Id: SocketFactory.pm,v 1.77 2004/05/24 21:48:20 rcaputo Exp $
  2.  
  3. package POE::Wheel::SocketFactory;
  4. use POE::Preprocessor ( isa => "POE::Macro::UseBytes" );
  5.  
  6. use strict;
  7.  
  8. use vars qw($VERSION);
  9. $VERSION = do {my@r=(q$Revision: 1.77 $=~/\d+/g);sprintf"%d."."%04d"x$#r,@r};
  10.  
  11. use Carp;
  12. use Symbol;
  13.  
  14. use POSIX qw(fcntl_h);
  15. use Errno qw(EWOULDBLOCK EADDRNOTAVAIL EINPROGRESS EADDRINUSE);
  16. use Socket;
  17. use POE qw(Wheel);
  18.  
  19. sub CRIMSON_SCOPE_HACK ($) { 0 }
  20. sub DEBUG () { 0 }
  21.  
  22. sub MY_SOCKET_HANDLE   () {  0 }
  23. sub MY_UNIQUE_ID       () {  1 }
  24. sub MY_EVENT_SUCCESS   () {  2 }
  25. sub MY_EVENT_FAILURE   () {  3 }
  26. sub MY_SOCKET_DOMAIN   () {  4 }
  27. sub MY_STATE_ACCEPT    () {  5 }
  28. sub MY_STATE_CONNECT   () {  6 }
  29. sub MY_MINE_SUCCESS    () {  7 }
  30. sub MY_MINE_FAILURE    () {  8 }
  31. sub MY_SOCKET_PROTOCOL () {  9 }
  32. sub MY_SOCKET_TYPE     () { 10 }
  33. sub MY_STATE_ERROR     () { 11 }
  34. sub MY_SOCKET_SELECTED () { 12 }
  35.  
  36. # Fletch has subclassed SSLSocketFactory from SocketFactory.  He's
  37. # added new members after MY_SOCKET_SELECTED.  Be sure, if you extend
  38. # this, to extend add stuff BEFORE MY_SOCKET_SELECTED or let Fletch
  39. # know you've broken his module.
  40.  
  41. # Provide dummy constants for systems that don't have them.
  42. BEGIN {
  43.   if ($^O eq 'MSWin32') {
  44.  
  45.     # Constants are evaluated first so they exist when the code uses
  46.     # them.
  47.     eval( '*F_GETFL       = sub {     0 };' .
  48.           '*F_SETFL       = sub {     0 };' .
  49.  
  50.           # Garrett Goebel's patch to support non-blocking connect()
  51.           # or MSWin32 follows.  His notes on the matter:
  52.           #
  53.           # As my patch appears to turn on the overlapped attributes
  54.           # for all successive sockets... it might not be the optimal
  55.           # solution. But it works for me ;)
  56.           #
  57.           # A better Win32 approach would probably be to:
  58.           # o  create a dummy socket
  59.           # o  cache the value of SO_OPENTYPE
  60.           # o  set the overlapped io attribute
  61.           # o  close dummy socket
  62.           #
  63.           # o  create our sock
  64.           #
  65.           # o  create a dummy socket
  66.           # o  restore previous value of SO_OPENTYPE
  67.           # o  close dummy socket
  68.           #
  69.           # This way we'd only be turning on the overlap attribute for
  70.           # the socket we created... and not all subsequent sockets.
  71.  
  72.           '*SO_OPENTYPE = sub () { 0x7008 };' .
  73.           '*SO_SYNCHRONOUS_ALERT    = sub () { 0x10 };' .
  74.           '*SO_SYNCHRONOUS_NONALERT = sub () { 0x20 };'
  75.         );
  76.     die if $@;
  77.  
  78.     # Turn on socket overlapped IO attribute per MSKB: Q181611.  This
  79.     # concludes Garrett's patch.
  80.  
  81.     eval( 'socket(POE, AF_INET, SOCK_STREAM, getprotobyname("tcp"))' .
  82.           'or die "socket failed: $!";' .
  83.           'my $opt = unpack("I", getsockopt(POE, SOL_SOCKET, SO_OPENTYPE));' .
  84.           '$opt &= ~(SO_SYNCHRONOUS_ALERT|SO_SYNCHRONOUS_NONALERT);' .
  85.           'setsockopt(POE, SOL_SOCKET, SO_OPENTYPE, $opt);' .
  86.           'close POE;'
  87.  
  88.           # End of Garrett's patch.
  89.         );
  90.     die if $@;
  91.   }
  92.  
  93.   unless (exists $INC{"Socket6.pm"}) {
  94.     eval "*Socket6::AF_INET6 = sub () { ~0 }";
  95.     eval "*Socket6::PF_INET6 = sub () { ~0 }";
  96.   }
  97. }
  98.  
  99. #------------------------------------------------------------------------------
  100. # These tables customize the socketfactory.  Many protocols share the
  101. # same operations, it seems, and this is a way to add new ones with a
  102. # minimum of additional code.
  103.  
  104. sub DOM_UNIX  () { 'unix'  }  # UNIX domain socket
  105. sub DOM_INET  () { 'inet'  }  # INET domain socket
  106. sub DOM_INET6 () { 'inet6' }  # INET v6 domain socket
  107.  
  108. # AF_XYZ and PF_XYZ may be different.
  109. my %map_family_to_domain =
  110.   ( AF_UNIX,  DOM_UNIX,  PF_UNIX,  DOM_UNIX,
  111.     AF_INET,  DOM_INET,  PF_INET,  DOM_INET,
  112.     &Socket6::AF_INET6, DOM_INET6,
  113.     &Socket6::PF_INET6, DOM_INET6,
  114.   );
  115.  
  116. sub SVROP_LISTENS () { 'listens' }  # connect/listen sockets
  117. sub SVROP_NOTHING () { 'nothing' }  # connectionless sockets
  118.  
  119. # Map family/protocol pairs to connection or connectionless
  120. # operations.
  121. my %supported_protocol =
  122.   ( DOM_UNIX,  { none => SVROP_LISTENS },
  123.     DOM_INET,  { tcp  => SVROP_LISTENS,
  124.                  udp  => SVROP_NOTHING,
  125.                },
  126.     DOM_INET6, { tcp  => SVROP_LISTENS,
  127.                  udp  => SVROP_NOTHING,
  128.                },
  129.   );
  130.  
  131. # Sane default socket types for each supported protocol.  -><- Maybe
  132. # this structure can be combined with %supported_protocol?
  133. my %default_socket_type =
  134.   ( DOM_UNIX,  { none => SOCK_STREAM },
  135.     DOM_INET,  { tcp  => SOCK_STREAM,
  136.                  udp  => SOCK_DGRAM,
  137.                },
  138.     DOM_INET6, { tcp  => SOCK_STREAM,
  139.                  udp  => SOCK_DGRAM,
  140.                },
  141.   );
  142.  
  143. #------------------------------------------------------------------------------
  144. # Perform system-dependent translations on Unix addresses, if
  145. # necessary.
  146.  
  147. sub condition_unix_address {
  148.   my ($address) = @_;
  149.  
  150.   # OS/2 would like sockets to use backwhacks, and please place them
  151.   # in the virtual \socket\ directory.  Thank you.
  152.   if ($^O eq 'os2') {
  153.     $address =~ tr[\\][/];
  154.     if ($address !~ m{^/socket/}) {
  155.       $address =~ s{^/?}{/socket/};
  156.     }
  157.     $address =~ tr[/][\\];
  158.   }
  159.  
  160.   $address;
  161. }
  162.  
  163. #------------------------------------------------------------------------------
  164. # Define the select handler that will accept connections.
  165.  
  166. sub _define_accept_state {
  167.   my $self = shift;
  168.  
  169.   # We do these stupid closure tricks to avoid putting $self in it
  170.   # directly.  If you include $self in one of the state() closures,
  171.   # the component will fail to shut down properly: there will be a
  172.   # circular definition in the closure holding $self alive.
  173.  
  174.   my $domain = $map_family_to_domain{ $self->[MY_SOCKET_DOMAIN] };
  175.   $domain = '(undef)' unless defined $domain;
  176.   my $event_success = \$self->[MY_EVENT_SUCCESS];
  177.   my $event_failure = \$self->[MY_EVENT_FAILURE];
  178.   my $unique_id     =  $self->[MY_UNIQUE_ID];
  179.  
  180.   $poe_kernel->state
  181.     ( $self->[MY_STATE_ACCEPT] = ref($self) . "($unique_id) -> select accept",
  182.       sub {
  183.         # prevents SEGV
  184.         0 && CRIMSON_SCOPE_HACK('<');
  185.  
  186.         # subroutine starts here
  187.         my ($k, $me, $handle) = @_[KERNEL, SESSION, ARG0];
  188.  
  189.         my $new_socket = gensym;
  190.         my $peer = accept($new_socket, $handle);
  191.  
  192.         if ($peer) {
  193.           my ($peer_addr, $peer_port);
  194.           if ( $domain eq DOM_UNIX ) {
  195.             $peer_addr = $peer_port = undef;
  196.           }
  197.           elsif ( $domain eq DOM_INET ) {
  198.             ($peer_port, $peer_addr) = unpack_sockaddr_in($peer);
  199.           }
  200.           elsif ( $domain eq DOM_INET6 ) {
  201.             $peer = getpeername($new_socket);
  202.             ($peer_port, $peer_addr) = Socket6::unpack_sockaddr_in6($peer);
  203.           }
  204.           else {
  205.             die "sanity failure: socket domain == $domain";
  206.           }
  207.           $k->call( $me, $$event_success,
  208.                     $new_socket, $peer_addr, $peer_port,
  209.                     $unique_id
  210.                   );
  211.         }
  212.         elsif ($! != EWOULDBLOCK) {
  213.           $$event_failure &&
  214.             $k->call( $me, $$event_failure,
  215.                       'accept', ($!+0), $!, $unique_id
  216.                     );
  217.         }
  218.       }
  219.     );
  220.  
  221.   $self->[MY_SOCKET_SELECTED] = 'yes';
  222.   $poe_kernel->select_read( $self->[MY_SOCKET_HANDLE],
  223.                             $self->[MY_STATE_ACCEPT]
  224.                           );
  225. }
  226.  
  227. #------------------------------------------------------------------------------
  228. # Define the select handler that will finalize an established
  229. # connection.
  230.  
  231. sub _define_connect_state {
  232.   my $self = shift;
  233.  
  234.   # We do these stupid closure tricks to avoid putting $self in it
  235.   # directly.  If you include $self in one of the state() closures,
  236.   # the component will fail to shut down properly: there will be a
  237.   # circular definition in the closure holding $self alive.
  238.  
  239.   my $domain = $map_family_to_domain{ $self->[MY_SOCKET_DOMAIN] };
  240.   $domain = '(undef)' unless defined $domain;
  241.   my $event_success   = \$self->[MY_EVENT_SUCCESS];
  242.   my $event_failure   = \$self->[MY_EVENT_FAILURE];
  243.   my $unique_id       =  $self->[MY_UNIQUE_ID];
  244.   my $socket_selected = \$self->[MY_SOCKET_SELECTED];
  245.  
  246.   my $socket_handle   = \$self->[MY_SOCKET_HANDLE];
  247.   my $state_accept    = \$self->[MY_STATE_ACCEPT];
  248.   my $state_connect   = \$self->[MY_STATE_CONNECT];
  249.   my $mine_success    = \$self->[MY_MINE_SUCCESS];
  250.   my $mine_failure    = \$self->[MY_MINE_FAILURE];
  251.  
  252.   $poe_kernel->state
  253.     ( $self->[MY_STATE_CONNECT] = ( ref($self) .
  254.                                     "($unique_id) -> select connect"
  255.                                   ),
  256.       sub {
  257.         # This prevents SEGV in older versions of Perl.
  258.         0 && CRIMSON_SCOPE_HACK('<');
  259.  
  260.         # Grab some values and stop watching the socket.
  261.         my ($k, $me, $handle) = @_[KERNEL, SESSION, ARG0];
  262.  
  263.     _shutdown(
  264.       $socket_selected, $socket_handle,
  265.       $state_accept, $state_connect,
  266.       $mine_success, $event_success,
  267.       $mine_failure, $event_failure,
  268.     );
  269.  
  270.         # Throw a failure if the connection failed.
  271.         $! = unpack('i', getsockopt($handle, SOL_SOCKET, SO_ERROR));
  272.         if ($!) {
  273.           (defined $$event_failure) and
  274.             $k->call( $me, $$event_failure,
  275.                       'connect', ($!+0), $!, $unique_id
  276.                     );
  277.           return;
  278.         }
  279.  
  280.         # Get the remote address, or throw an error if that fails.
  281.         my $peer = getpeername($handle);
  282.         if ($!) {
  283.           (defined $$event_failure) and
  284.             $k->call( $me, $$event_failure,
  285.                       'getpeername', ($!+0), $!, $unique_id
  286.                     );
  287.           return;
  288.         }
  289.  
  290.         # Parse the remote address according to the socket's domain.
  291.         my ($peer_addr, $peer_port);
  292.  
  293.         # UNIX sockets have some trouble with peer addresses.
  294.         if ($domain eq DOM_UNIX) {
  295.           if (defined $peer) {
  296.             eval {
  297.               $peer_addr = unpack_sockaddr_un($peer);
  298.             };
  299.             undef $peer_addr if length $@;
  300.           }
  301.         }
  302.  
  303.         # INET socket stacks tend not to.
  304.         elsif ($domain eq DOM_INET) {
  305.           if (defined $peer) {
  306.             eval {
  307.               ($peer_port, $peer_addr) = unpack_sockaddr_in($peer);
  308.             };
  309.             if (length $@) {
  310.               $peer_port = $peer_addr = undef;
  311.             }
  312.           }
  313.         }
  314.  
  315.         # INET6 socket stacks tend not to.
  316.         elsif ($domain eq DOM_INET6) {
  317.           if (defined $peer) {
  318.             eval {
  319.               ($peer_port, $peer_addr) = Socket6::unpack_sockaddr_in6($peer);
  320.             };
  321.             if (length $@) {
  322.               $peer_port = $peer_addr = undef;
  323.             }
  324.           }
  325.         }
  326.  
  327.         # What are we doing here?
  328.         else {
  329.           die "sanity failure: socket domain == $domain";
  330.         }
  331.  
  332.         # Tell the session it went okay.  Also let go of the socket.
  333.         $k->call( $me, $$event_success,
  334.                   $handle, $peer_addr, $peer_port, $unique_id
  335.                 );
  336.       }
  337.     );
  338.  
  339.   # Cygwin expects an error state registered to expedite.  This code
  340.   # is nearly identical the stuff above.
  341.   if ($^O eq "cygwin") {
  342.     $poe_kernel->state
  343.       ( $self->[MY_STATE_ERROR] = ( ref($self) .
  344.                                     "($unique_id) -> connect error"
  345.                                   ),
  346.         sub {
  347.           # This prevents SEGV in older versions of Perl.
  348.           0 && CRIMSON_SCOPE_HACK('<');
  349.  
  350.           # Grab some values and stop watching the socket.
  351.           my ($k, $me, $handle) = @_[KERNEL, SESSION, ARG0];
  352.  
  353.       _shutdown(
  354.         $socket_selected, $socket_handle,
  355.         $state_accept, $state_connect,
  356.         $mine_success, $event_success,
  357.         $mine_failure, $event_failure,
  358.       );
  359.  
  360.           # Throw a failure if the connection failed.
  361.           $! = unpack('i', getsockopt($handle, SOL_SOCKET, SO_ERROR));
  362.           if ($!) {
  363.             (defined $$event_failure) and
  364.               $k->call( $me, $$event_failure,
  365.                         'connect', ($!+0), $!, $unique_id
  366.                       );
  367.             return;
  368.           }
  369.         }
  370.       );
  371.     $poe_kernel->select_expedite( $self->[MY_SOCKET_HANDLE],
  372.                                   $self->[MY_STATE_ERROR]
  373.                                 );
  374.   }
  375.  
  376.   $self->[MY_SOCKET_SELECTED] = 'yes';
  377.   $poe_kernel->select_write( $self->[MY_SOCKET_HANDLE],
  378.                              $self->[MY_STATE_CONNECT]
  379.                            );
  380. }
  381.  
  382. #------------------------------------------------------------------------------
  383.  
  384. sub event {
  385.   my $self = shift;
  386.   push(@_, undef) if (scalar(@_) & 1);
  387.  
  388.   while (@_) {
  389.     my ($name, $event) = splice(@_, 0, 2);
  390.  
  391.     if ($name eq 'SuccessEvent') {
  392.       if (defined $event) {
  393.         if (ref($event)) {
  394.           carp "reference for SuccessEvent will be treated as an event name"
  395.         }
  396.         $self->[MY_EVENT_SUCCESS] = $event;
  397.         undef $self->[MY_MINE_SUCCESS];
  398.       }
  399.       else {
  400.         carp "SuccessEvent requires an event name.  ignoring undef";
  401.       }
  402.     }
  403.     elsif ($name eq 'FailureEvent') {
  404.       if (defined $event) {
  405.         if (ref($event)) {
  406.           carp "reference for FailureEvent will be treated as an event name";
  407.         }
  408.         $self->[MY_EVENT_FAILURE] = $event;
  409.         undef $self->[MY_MINE_FAILURE];
  410.       }
  411.       else {
  412.         carp "FailureEvent requires an event name.  ignoring undef";
  413.       }
  414.     }
  415.     else {
  416.       carp "ignoring unknown SocketFactory parameter '$name'";
  417.     }
  418.   }
  419.  
  420.   $self->[MY_SOCKET_SELECTED] = 'yes';
  421.   if (defined $self->[MY_STATE_ACCEPT]) {
  422.     $poe_kernel->select_read($self->[MY_SOCKET_HANDLE],
  423.                              $self->[MY_STATE_ACCEPT]
  424.                             );
  425.   }
  426.   elsif (defined $self->[MY_STATE_CONNECT]) {
  427.     $poe_kernel->select_write( $self->[MY_SOCKET_HANDLE],
  428.                                $self->[MY_STATE_CONNECT]
  429.                              );
  430.     if ($^O eq "cygwin") {
  431.       $poe_kernel->select_expedite( $self->[MY_SOCKET_HANDLE],
  432.                                     $self->[MY_STATE_ERROR]
  433.                                   );
  434.     }
  435.   }
  436.   else {
  437.     die "POE developer error - no state defined";
  438.   }
  439. }
  440.  
  441. #------------------------------------------------------------------------------
  442.  
  443. sub getsockname {
  444.   my $self = shift;
  445.   return undef unless defined $self->[MY_SOCKET_HANDLE];
  446.   return getsockname($self->[MY_SOCKET_HANDLE]);
  447. }
  448.  
  449. sub ID {
  450.   return $_[0]->[MY_UNIQUE_ID];
  451. }
  452.  
  453. #------------------------------------------------------------------------------
  454.  
  455. sub new {
  456.   my $type = shift;
  457.  
  458.   # Don't take responsibility for a bad parameter count.
  459.   croak "$type requires an even number of parameters" if @_ & 1;
  460.  
  461.   my %params = @_;
  462.  
  463.   # The calling convention experienced a hard deprecation.
  464.   croak "wheels no longer require a kernel reference as their first parameter"
  465.     if (@_ && (ref($_[0]) eq 'POE::Kernel'));
  466.  
  467.   # Ensure some of the basic things are present.
  468.   croak "$type requires a working Kernel" unless (defined $poe_kernel);
  469.   croak 'SuccessEvent required' unless (defined $params{SuccessEvent});
  470.   croak 'FailureEvent required' unless (defined $params{FailureEvent});
  471.   my $event_success = $params{SuccessEvent};
  472.   my $event_failure = $params{FailureEvent};
  473.  
  474.   # Create the SocketServer.  Cache a copy of the socket handle.
  475.   my $socket_handle = gensym();
  476.   my $self = bless
  477.     ( [ $socket_handle,                   # MY_SOCKET_HANDLE
  478.         &POE::Wheel::allocate_wheel_id(), # MY_UNIQUE_ID
  479.         $event_success,                   # MY_EVENT_SUCCESS
  480.         $event_failure,                   # MY_EVENT_FAILURE
  481.         undef,                            # MY_SOCKET_DOMAIN
  482.         undef,                            # MY_STATE_ACCEPT
  483.         undef,                            # MY_STATE_CONNECT
  484.         undef,                            # MY_MINE_SUCCESS
  485.         undef,                            # MY_MINE_FAILURE
  486.         undef,                            # MY_SOCKET_PROTOCOL
  487.         undef,                            # MY_SOCKET_TYPE
  488.         undef,                            # MY_STATE_ERROR
  489.         undef,                            # MY_SOCKET_SELECTED
  490.       ],
  491.       $type
  492.     );
  493.  
  494.   # Default to Internet sockets.
  495.   my $domain = delete $params{SocketDomain};
  496.   $domain = AF_INET unless defined $domain;
  497.   $self->[MY_SOCKET_DOMAIN] = $domain;
  498.  
  499.   # Abstract the socket domain into something we don't have to keep
  500.   # testing duplicates of.
  501.   my $abstract_domain = $map_family_to_domain{$self->[MY_SOCKET_DOMAIN]};
  502.   unless (defined $abstract_domain) {
  503.     $poe_kernel->yield( $event_failure,
  504.                         'domain', 0, '', $self->[MY_UNIQUE_ID]
  505.                       );
  506.     return $self;
  507.   }
  508.  
  509.   #---------------#
  510.   # Create Socket #
  511.   #---------------#
  512.  
  513.   # Declare the protocol name out here; it'll be needed by
  514.   # getservbyname later.
  515.   my $protocol_name;
  516.  
  517.   # Unix sockets don't use protocols; warn the programmer, and force
  518.   # PF_UNSPEC.
  519.   if ($abstract_domain eq DOM_UNIX) {
  520.     carp 'SocketProtocol ignored for Unix socket'
  521.       if defined $params{SocketProtocol};
  522.     $self->[MY_SOCKET_PROTOCOL] = PF_UNSPEC;
  523.     $protocol_name = 'none';
  524.   }
  525.  
  526.   # Internet sockets use protocols.  Default the INET protocol to tcp,
  527.   # and try to resolve it.
  528.   elsif ( $abstract_domain eq DOM_INET or
  529.           $abstract_domain eq DOM_INET6
  530.         ) {
  531.     my $socket_protocol =
  532.       (defined $params{SocketProtocol}) ? $params{SocketProtocol} : 'tcp';
  533.  
  534.     if ($socket_protocol !~ /^\d+$/) {
  535.       unless ($socket_protocol = getprotobyname($socket_protocol)) {
  536.         $poe_kernel->yield( $event_failure,
  537.                             'getprotobyname', $!+0, $!, $self->[MY_UNIQUE_ID]
  538.                           );
  539.         return $self;
  540.       }
  541.     }
  542.  
  543.     # Get the protocol's name regardless of what was provided.  If the
  544.     # protocol isn't supported, croak now instead of making the
  545.     # programmer wonder why things fail later.
  546.     $protocol_name = lc(getprotobynumber($socket_protocol));
  547.     unless ($protocol_name) {
  548.       $poe_kernel->yield( $event_failure,
  549.                           'getprotobynumber', $!+0, $!, $self->[MY_UNIQUE_ID]
  550.                         );
  551.       return $self;
  552.     }
  553.  
  554.     unless (defined $supported_protocol{$abstract_domain}->{$protocol_name}) {
  555.       croak "SocketFactory does not support Internet $protocol_name sockets";
  556.     }
  557.  
  558.     $self->[MY_SOCKET_PROTOCOL] = $socket_protocol;
  559.   }
  560.   else {
  561.     die "Mail this error to the author of POE: Internal consistency error";
  562.   }
  563.  
  564.   # If no SocketType, default it to something appropriate.
  565.   if (defined $params{SocketType}) {
  566.     $self->[MY_SOCKET_TYPE] = $params{SocketType};
  567.   }
  568.   else {
  569.     unless (defined $default_socket_type{$abstract_domain}->{$protocol_name}) {
  570.       croak "SocketFactory does not support $abstract_domain $protocol_name";
  571.     }
  572.     $self->[MY_SOCKET_TYPE] =
  573.       $default_socket_type{$abstract_domain}->{$protocol_name};
  574.   }
  575.  
  576.   # Create the socket.
  577.   unless (socket( $socket_handle, $self->[MY_SOCKET_DOMAIN],
  578.                   $self->[MY_SOCKET_TYPE], $self->[MY_SOCKET_PROTOCOL]
  579.                 )
  580.   ) {
  581.     $poe_kernel->yield( $event_failure,
  582.                         'socket', $!+0, $!, $self->[MY_UNIQUE_ID]
  583.                       );
  584.     return $self;
  585.   }
  586.  
  587.   DEBUG && warn "socket";
  588.  
  589.   #------------------#
  590.   # Configure Socket #
  591.   #------------------#
  592.  
  593.   # Make the socket binary.  It's wrapped in eval{} because tied
  594.   # filehandle classes may actually die in their binmode methods.
  595.   eval { binmode($socket_handle) };
  596.  
  597.   # Don't block on socket operations, because the socket will be
  598.   # driven by a select loop.
  599.  
  600.   # RCC 2002-12-19: Replace the complex blocking checks and methods
  601.   # with IO::Handle's blocking(0) method.  This is theoretically more
  602.   # portable and less maintenance than rolling our own.  If things
  603.   # work out, we'll remove the commented out code.
  604.  
  605.   # RCC 2003-01-20: Unfortunately, blocking() isn't available in perl
  606.   # 5.005_03, and people still use that.  We'll use blocking() for
  607.   # Perl 5.8.0 and beyond, since that's the first version of
  608.   # ActivePerl that has a problem.
  609.  
  610.   if ($] >= 5.008) {
  611.     $socket_handle->blocking(0);
  612.   }
  613.   else {
  614.     # Do it the Win32 way.  XXX This is incomplete.
  615.     if ($^O eq 'MSWin32') {
  616.       my $set_it = "1";
  617.  
  618.       # 126 is FIONBIO (some docs say 0x7F << 16)
  619.       ioctl( $socket_handle,
  620.              0x80000000 | (4 << 16) | (ord('f') << 8) | 126,
  621.              $set_it
  622.            )
  623.         or do {
  624.           $poe_kernel->yield( $event_failure,
  625.                               'ioctl', $!+0, $!, $self->[MY_UNIQUE_ID]
  626.                             );
  627.           return $self;
  628.         };
  629.     }
  630.  
  631.     # Do it the way everyone else does.
  632.     else {
  633.       my $flags = fcntl($socket_handle, F_GETFL, 0)
  634.         or do {
  635.           $poe_kernel->yield( $event_failure,
  636.                               'fcntl', $!+0, $!, $self->[MY_UNIQUE_ID]
  637.                             );
  638.           return $self;
  639.         };
  640.       $flags = fcntl($socket_handle, F_SETFL, $flags | O_NONBLOCK)
  641.         or do {
  642.           $poe_kernel->yield( $event_failure,
  643.                               'fcntl', $!+0, $!, $self->[MY_UNIQUE_ID]
  644.                             );
  645.           return $self;
  646.         };
  647.     }
  648.   }
  649.  
  650.   # Make the socket reusable, if requested.
  651.   if ( (defined $params{Reuse})
  652.        and ( (lc($params{Reuse}) eq 'yes')
  653.              or (lc($params{Reuse}) eq 'on')
  654.              or ( ($params{Reuse} =~ /\d+/)
  655.                   and $params{Reuse}
  656.                 )
  657.            )
  658.      )
  659.   {
  660.     setsockopt($socket_handle, SOL_SOCKET, SO_REUSEADDR, 1)
  661.       or do {
  662.         $poe_kernel->yield( $event_failure,
  663.                             'setsockopt', $!+0, $!, $self->[MY_UNIQUE_ID]
  664.                           );
  665.         return $self;
  666.       };
  667.   }
  668.  
  669.   #-------------#
  670.   # Bind Socket #
  671.   #-------------#
  672.  
  673.   my $bind_address;
  674.  
  675.   # Check SocketFactory /Bind.*/ parameters in an Internet socket
  676.   # context, and translate them into parameters that bind()
  677.   # understands.
  678.   if ($abstract_domain eq DOM_INET) {
  679.     # Don't bind if the creator doesn't specify a related parameter.
  680.     if ((defined $params{BindAddress}) or (defined $params{BindPort})) {
  681.  
  682.       # Set the bind address, or default to INADDR_ANY.
  683.       $bind_address = ( (defined $params{BindAddress})
  684.                         ? $params{BindAddress}
  685.                         : INADDR_ANY
  686.                       );
  687.  
  688.       {% use_bytes %}
  689.  
  690.       # Resolve the bind address if it's not already packed.
  691.       unless (length($bind_address) == 4) {
  692.         $bind_address = inet_aton($bind_address);
  693.       }
  694.  
  695.       unless (defined $bind_address) {
  696.         $! = EADDRNOTAVAIL;
  697.         $poe_kernel->yield( $event_failure,
  698.                             "inet_aton", $!+0, $!, $self->[MY_UNIQUE_ID]
  699.                           );
  700.         return $self;
  701.       }
  702.  
  703.       # Set the bind port, or default to 0 (any) if none specified.
  704.       # Resolve it to a number, if at all possible.
  705.       my $bind_port = (defined $params{BindPort}) ? $params{BindPort} : 0;
  706.       if ($bind_port =~ /[^0-9]/) {
  707.         $bind_port = getservbyname($bind_port, $protocol_name);
  708.         unless (defined $bind_port) {
  709.           $! = EADDRNOTAVAIL;
  710.           $poe_kernel->yield( $event_failure,
  711.                               'getservbyname', $!+0, $!, $self->[MY_UNIQUE_ID]
  712.                             );
  713.           return $self;
  714.         }
  715.       }
  716.  
  717.       $bind_address = pack_sockaddr_in($bind_port, $bind_address);
  718.       unless (defined $bind_address) {
  719.         $poe_kernel->yield( $event_failure,
  720.                             "pack_sockaddr_in", $!+0, $!, $self->[MY_UNIQUE_ID]
  721.                           );
  722.         return $self;
  723.       }
  724.     }
  725.   }
  726.  
  727.   # Check SocketFactory /Bind.*/ parameters in an Internet socket
  728.   # context, and translate them into parameters that bind()
  729.   # understands.
  730.   elsif ($abstract_domain eq DOM_INET6) {
  731.  
  732.     # Don't bind if the creator doesn't specify a related parameter.
  733.     if ((defined $params{BindAddress}) or (defined $params{BindPort})) {
  734.  
  735.       # Set the bind address, or default to INADDR_ANY.
  736.       $bind_address = (
  737.         (defined $params{BindAddress})
  738.         ? $params{BindAddress}
  739.         : Socket6::in6addr_any()
  740.       );
  741.  
  742.       # Set the bind port, or default to 0 (any) if none specified.
  743.       # Resolve it to a number, if at all possible.
  744.       my $bind_port = (defined $params{BindPort}) ? $params{BindPort} : 0;
  745.       if ($bind_port =~ /[^0-9]/) {
  746.         $bind_port = getservbyname($bind_port, $protocol_name);
  747.         unless (defined $bind_port) {
  748.           $! = EADDRNOTAVAIL;
  749.           $poe_kernel->yield( $event_failure,
  750.                               'getservbyname', $!+0, $!, $self->[MY_UNIQUE_ID]
  751.                             );
  752.           return $self;
  753.         }
  754.       }
  755.  
  756.       {% use_bytes %}
  757.  
  758.       # Resolve the bind address.
  759.       my @info = Socket6::getaddrinfo(
  760.         $bind_address, $bind_port,
  761.         $self->[MY_SOCKET_DOMAIN], $self->[MY_SOCKET_TYPE],
  762.       );
  763.  
  764. # Deprecated Socket6 interfaces.  Solaris, for one, does not use them.
  765. # TODO - Remove this if nothing needs it.
  766. #      $bind_address =
  767. #        Socket6::gethostbyname2($bind_address, $self->[MY_SOCKET_DOMAIN]);
  768.  
  769.       if (@info < 5) {  # unless defined $bind_address
  770.         $! = EADDRNOTAVAIL;
  771.         $poe_kernel->yield( $event_failure,
  772.                             "getaddrinfo", $!+0, $!, $self->[MY_UNIQUE_ID]
  773.                           );
  774.         return $self;
  775.       }
  776.  
  777.       $bind_address = $info[3];
  778.  
  779. # Deprecated Socket6 interfaces.  Solaris, for one, does not use them.
  780. # TODO - Remove this if nothing needs it.
  781. #      $bind_address = Socket6::pack_sockaddr_in6($bind_port, $bind_address);
  782. #      warn unpack "H*", $bind_address;
  783. #      unless (defined $bind_address) {
  784. #        $poe_kernel->yield( $event_failure,
  785. #                            "pack_sockaddr_in6", $!+0, $!,
  786. #                            $self->[MY_UNIQUE_ID]
  787. #                          );
  788. #        return $self;
  789. #      }
  790.     }
  791.   }
  792.  
  793.   # Check SocketFactory /Bind.*/ parameters in a Unix context, and
  794.   # translate them into parameters bind() understands.
  795.   elsif ($abstract_domain eq DOM_UNIX) {
  796.     carp 'BindPort ignored for Unix socket' if defined $params{BindPort};
  797.  
  798.     if (defined $params{BindAddress}) {
  799.       # Is this necessary, or will bind() return EADDRINUSE?
  800.       if (defined $params{RemotePort}) {
  801.         $! = EADDRINUSE;
  802.         $poe_kernel->yield( $event_failure,
  803.                             'bind', $!+0, $!, $self->[MY_UNIQUE_ID]
  804.                           );
  805.         return $self;
  806.       }
  807.  
  808.       $bind_address = &condition_unix_address($params{BindAddress});
  809.       $bind_address = pack_sockaddr_un($bind_address);
  810.       unless ($bind_address) {
  811.         $poe_kernel->yield( $event_failure,
  812.                             'pack_sockaddr_un', $!+0, $!, $self->[MY_UNIQUE_ID]
  813.                           );
  814.         return $self;
  815.       }
  816.     }
  817.   }
  818.  
  819.   # This is an internal consistency error, and it should be hard
  820.   # trapped right away.
  821.   else {
  822.     die "Mail this error to the author of POE: Internal consistency error";
  823.   }
  824.  
  825.   # Perform the actual bind, if there's a bind address to bind to.
  826.   if (defined $bind_address) {
  827.     unless (bind($socket_handle, $bind_address)) {
  828.       $poe_kernel->yield( $event_failure,
  829.                           'bind', $!+0, $!, $self->[MY_UNIQUE_ID]
  830.                         );
  831.       return $self;
  832.     }
  833.  
  834.     DEBUG && warn "bind";
  835.   }
  836.  
  837.   #---------#
  838.   # Connect #
  839.   #---------#
  840.  
  841.   my $connect_address;
  842.  
  843.   if (defined $params{RemoteAddress}) {
  844.  
  845.     # Check SocketFactory /Remote.*/ parameters in an Internet socket
  846.     # context, and translate them into parameters that connect()
  847.     # understands.
  848.     if ($abstract_domain eq DOM_INET or
  849.         $abstract_domain eq DOM_INET6
  850.        ) {
  851.       # connecting if RemoteAddress
  852.       croak 'RemotePort required' unless (defined $params{RemotePort});
  853.       carp 'ListenQueue ignored' if (defined $params{ListenQueue});
  854.  
  855.       my $remote_port = $params{RemotePort};
  856.       if ($remote_port =~ /[^0-9]/) {
  857.         unless ($remote_port = getservbyname($remote_port, $protocol_name)) {
  858.           $! = EADDRNOTAVAIL;
  859.           $poe_kernel->yield( $event_failure,
  860.                               'getservbyname', $!+0, $!, $self->[MY_UNIQUE_ID]
  861.                             );
  862.           return $self;
  863.         }
  864.       }
  865.  
  866.       my $error_tag;
  867.       if ($abstract_domain eq DOM_INET) {
  868.         $connect_address = inet_aton($params{RemoteAddress});
  869.         $error_tag = "inet_aton";
  870.       }
  871.       elsif ($abstract_domain eq DOM_INET6) {
  872.         my @info = Socket6::getaddrinfo(
  873.           $params{RemoteAddress}, $remote_port,
  874.           $self->[MY_SOCKET_DOMAIN], $self->[MY_SOCKET_TYPE],
  875.         );
  876.  
  877.         if (@info < 5) {
  878.           $connect_address = undef;
  879.         }
  880.         else {
  881.           $connect_address = $info[3];
  882.         }
  883.  
  884.         $error_tag = "getaddrinfo";
  885.  
  886. # Deprecated Socket6 interfaces.  Solaris, for one, does not use them.
  887. # TODO - Remove this if nothing needs it.
  888. #        $connect_address =
  889. #          Socket6::gethostbyname2( $params{RemoteAddress},
  890. #                                   $self->[MY_SOCKET_DOMAIN]
  891. #                                 );
  892. #        $error_tag = "gethostbyname2";
  893.       }
  894.       else {
  895.         die "unknown domain $abstract_domain";
  896.       }
  897.  
  898.       # TODO - If the gethostbyname2() code is removed, then we can
  899.       # combine the previous code with the following code, and perhaps
  900.       # remove one of these redundant $connect_address checks.  The
  901.       # 0.29 release should tell us pretty quickly whether it's
  902.       # needed.  If we reach 0.30 without incident, it's probably safe
  903.       # to remove the old gethostbyname2() code and clean this up.
  904.       unless (defined $connect_address) {
  905.         $! = EADDRNOTAVAIL;
  906.         $poe_kernel->yield( $event_failure,
  907.                             $error_tag, $!+0, $!, $self->[MY_UNIQUE_ID]
  908.                           );
  909.         return $self;
  910.       }
  911.  
  912.       if ($abstract_domain eq DOM_INET) {
  913.         $connect_address = pack_sockaddr_in($remote_port, $connect_address);
  914.         $error_tag = "pack_sockaddr_in";
  915.       }
  916.       elsif ($abstract_domain eq DOM_INET6) {
  917. # Deprecated Socket6 interfaces.  Solaris, for one, does not use them.
  918. # TODO - Remove this if nothing needs it.
  919. #        $connect_address =
  920. #          Socket6::pack_sockaddr_in6($remote_port, $connect_address);
  921.         $error_tag = "pack_sockaddr_in6";
  922.       }
  923.       else {
  924.         die "unknown domain $abstract_domain";
  925.       }
  926.  
  927.       unless ($connect_address) {
  928.         $! = EADDRNOTAVAIL;
  929.         $poe_kernel->yield( $event_failure,
  930.                             $error_tag, $!+0, $!, $self->[MY_UNIQUE_ID]
  931.                           );
  932.         return $self;
  933.       }
  934.     }
  935.  
  936.     # Check SocketFactory /Remote.*/ parameters in a Unix socket
  937.     # context, and translate them into parameters connect()
  938.     # understands.
  939.     elsif ($abstract_domain eq DOM_UNIX) {
  940.  
  941.       $connect_address = condition_unix_address($params{RemoteAddress});
  942.       $connect_address = pack_sockaddr_un($connect_address);
  943.       unless (defined $connect_address) {
  944.         $poe_kernel->yield( $event_failure,
  945.                             'pack_sockaddr_un', $!+0, $!, $self->[MY_UNIQUE_ID]
  946.                           );
  947.         return $self;
  948.       }
  949.     }
  950.  
  951.     # This is an internal consistency error, and it should be trapped
  952.     # right away.
  953.     else {
  954.       die "Mail this error to the author of POE: Internal consistency error";
  955.     }
  956.   }
  957.  
  958.   else {
  959.     carp "RemotePort ignored without RemoteAddress"
  960.       if defined $params{RemotePort};
  961.   }
  962.  
  963.   # Perform the actual connection, if a connection was requested.  If
  964.   # the connection can be established, then return the SocketFactory
  965.   # handle.
  966.   if (defined $connect_address) {
  967.     unless (connect($socket_handle, $connect_address)) {
  968.       if ($! and ($! != EINPROGRESS) and ($! != EWOULDBLOCK)) {
  969.         $poe_kernel->yield( $event_failure,
  970.                             'connect', $!+0, $!, $self->[MY_UNIQUE_ID]
  971.                           );
  972.         return $self;
  973.       }
  974.     }
  975.  
  976.     DEBUG && warn "connect";
  977.  
  978.     $self->[MY_SOCKET_HANDLE] = $socket_handle;
  979.     $self->_define_connect_state();
  980.     $self->event( SuccessEvent => $params{SuccessEvent},
  981.                   FailureEvent => $params{FailureEvent},
  982.                 );
  983.     return $self;
  984.   }
  985.  
  986.   #---------------------#
  987.   # Listen, or Whatever #
  988.   #---------------------#
  989.  
  990.   # A connection wasn't requested, so this must be a server socket.
  991.   # Do whatever it is that needs to be done for whatever type of
  992.   # server socket this is.
  993.   if (exists $supported_protocol{$abstract_domain}->{$protocol_name}) {
  994.     my $protocol_op = $supported_protocol{$abstract_domain}->{$protocol_name};
  995.  
  996.     DEBUG && warn "$abstract_domain + $protocol_name = $protocol_op";
  997.  
  998.     if ($protocol_op eq SVROP_LISTENS) {
  999.       my $listen_queue = $params{ListenQueue} || SOMAXCONN;
  1000.       # <rmah> In SocketFactory, you limit the ListenQueue parameter
  1001.       #        to SOMAXCON (or is it SOCONNMAX?)...why?
  1002.       # <rmah> ah, here's czth, he'll have more to say on this issue
  1003.       # <czth> not really.  just that SOMAXCONN can lie, notably on
  1004.       #        Solaris and reportedly on BSDs too
  1005.       # 
  1006.       # ($listen_queue > SOMAXCONN) && ($listen_queue = SOMAXCONN);
  1007.       unless (listen($socket_handle, $listen_queue)) {
  1008.         $poe_kernel->yield( $event_failure,
  1009.                             'listen', $!+0, $!, $self->[MY_UNIQUE_ID]
  1010.                           );
  1011.         return $self;
  1012.       }
  1013.  
  1014.       DEBUG && warn "listen";
  1015.  
  1016.       $self->[MY_SOCKET_HANDLE] = $socket_handle;
  1017.       $self->_define_accept_state();
  1018.       $self->event( SuccessEvent => $params{SuccessEvent},
  1019.                     FailureEvent => $params{FailureEvent},
  1020.                   );
  1021.       return $self;
  1022.     }
  1023.     else {
  1024.       carp "Ignoring ListenQueue parameter for non-listening socket"
  1025.         if defined $params{ListenQueue};
  1026.       if ($protocol_op eq SVROP_NOTHING) {
  1027.         # Do nothing.  Duh.  Fire off a success event immediately, and
  1028.         # return.
  1029.         $poe_kernel->yield( $event_success,
  1030.                             $socket_handle, undef, undef, $self->[MY_UNIQUE_ID]
  1031.                           );
  1032.         return $self;
  1033.       }
  1034.       else {
  1035.         die "Mail this error to the author of POE: Internal consistency error";
  1036.       }
  1037.     }
  1038.   }
  1039.   else {
  1040.     die "SocketFactory doesn't support $abstract_domain $protocol_name socket";
  1041.   }
  1042.  
  1043.   die "Mail this error to the author of POE: Internal consistency error";
  1044. }
  1045.  
  1046. # Pause and resume accept.
  1047. sub pause_accept {
  1048.   my $self = shift;
  1049.   if ( defined $self->[MY_SOCKET_HANDLE] and
  1050.        defined $self->[MY_STATE_ACCEPT] and
  1051.        defined $self->[MY_SOCKET_SELECTED]
  1052.      ) {
  1053.     $poe_kernel->select_pause_read($self->[MY_SOCKET_HANDLE]);
  1054.   }
  1055. }
  1056.  
  1057. sub resume_accept {
  1058.   my $self = shift;
  1059.   if ( defined $self->[MY_SOCKET_HANDLE] and
  1060.        defined $self->[MY_STATE_ACCEPT] and
  1061.        defined $self->[MY_SOCKET_SELECTED]
  1062.      ) {
  1063.     $poe_kernel->select_resume_read($self->[MY_SOCKET_HANDLE]);
  1064.   }
  1065. }
  1066.  
  1067. #------------------------------------------------------------------------------
  1068. # DESTROY and _shutdown pass things by reference because _shutdown is
  1069. # called from the state() closures above.  As a result, we can't
  1070. # mention $self explicitly, or the wheel won't shut itself down
  1071. # properly.  Rather, it will form a circular reference on $self.
  1072.  
  1073. sub DESTROY {
  1074.   my $self = shift;
  1075.   _shutdown(
  1076.     \$self->[MY_SOCKET_SELECTED],
  1077.     \$self->[MY_SOCKET_HANDLE],
  1078.     \$self->[MY_STATE_ACCEPT],
  1079.     \$self->[MY_STATE_CONNECT],
  1080.     \$self->[MY_MINE_SUCCESS],
  1081.     \$self->[MY_EVENT_SUCCESS],
  1082.     \$self->[MY_MINE_FAILURE],
  1083.     \$self->[MY_EVENT_FAILURE],
  1084.   );
  1085.   &POE::Wheel::free_wheel_id($self->[MY_UNIQUE_ID]);
  1086. }
  1087.  
  1088. sub _shutdown {
  1089.   my (
  1090.     $socket_selected, $socket_handle,
  1091.     $state_accept, $state_connect,
  1092.     $mine_success, $event_success,
  1093.     $mine_failure, $event_failure,
  1094.   ) = @_;
  1095.  
  1096.   if (defined $$socket_selected) {
  1097.     $poe_kernel->select($$socket_handle);
  1098.     $$socket_selected = undef;
  1099.   }
  1100.  
  1101.   if (defined $$state_accept) {
  1102.     $poe_kernel->state($$state_accept);
  1103.     $$state_accept = undef;
  1104.   }
  1105.  
  1106.   if (defined $$state_connect) {
  1107.     $poe_kernel->state($$state_connect);
  1108.     $$state_connect = undef;
  1109.   }
  1110.  
  1111.   if (defined $$mine_success) {
  1112.     $poe_kernel->state($$event_success);
  1113.     $$mine_success = $$event_success = undef;
  1114.   }
  1115.  
  1116.   if (defined $$mine_failure) {
  1117.     $poe_kernel->state($$event_failure);
  1118.     $$mine_failure = $$event_failure = undef;
  1119.   }
  1120. }
  1121.  
  1122. ###############################################################################
  1123. 1;
  1124.  
  1125. __END__
  1126.  
  1127. =head1 NAME
  1128.  
  1129. POE::Wheel::SocketFactory - non-blocking socket creation and management
  1130.  
  1131. =head1 SYNOPSIS
  1132.  
  1133.   use Socket; # For the constants
  1134.  
  1135.   # Listening Unix domain socket.
  1136.   $wheel = POE::Wheel::SocketFactory->new(
  1137.     SocketDomain => AF_UNIX,               # Sets the socket() domain
  1138.     BindAddress  => $unix_socket_address,  # Sets the bind() address
  1139.     SuccessEvent => $event_success,        # Event to emit upon accept()
  1140.     FailureEvent => $event_failure,        # Event to emit upon error
  1141.     # Optional parameters (and default values):
  1142.     SocketType   => SOCK_STREAM,           # Sets the socket() type
  1143.   );
  1144.  
  1145.   # Connecting Unix domain socket.
  1146.   $wheel = POE::Wheel::SocketFactory->new(
  1147.     SocketDomain  => AF_UNIX,              # Sets the socket() domain
  1148.     RemoteAddress => $unix_server_address, # Sets the connect() address
  1149.     SuccessEvent  => $event_success,       # Event to emit on connection
  1150.     FailureEvent  => $event_failure,       # Event to emit on error
  1151.     # Optional parameters (and default values):
  1152.     SocketType    => SOCK_STREAM,          # Sets the socket() type
  1153.     # Optional parameters (that have no defaults):
  1154.     BindAddress   => $unix_client_address, # Sets the bind() address
  1155.   );
  1156.  
  1157.   # Listening Internet domain socket.
  1158.   $wheel = POE::Wheel::SocketFactory->new(
  1159.     BindAddress    => $inet_address,       # Sets the bind() address
  1160.     BindPort       => $inet_port,          # Sets the bind() port
  1161.     SuccessEvent   => $event_success,      # Event to emit upon accept()
  1162.     FailureEvent   => $event_failure,      # Event to emit upon error
  1163.     # Optional parameters (and default values):
  1164.     SocketDomain   => AF_INET,             # Sets the socket() domain
  1165.     SocketType     => SOCK_STREAM,         # Sets the socket() type
  1166.     SocketProtocol => 'tcp',               # Sets the socket() protocol
  1167.     ListenQueue    => SOMAXCONN,           # The listen() queue length
  1168.     Reuse          => 'on',                # Lets the port be reused
  1169.   );
  1170.  
  1171.   # Connecting Internet domain socket.
  1172.   $wheel = POE::Wheel::SocketFactory->new(
  1173.     RemoteAddress  => $inet_address,       # Sets the connect() address
  1174.     RemotePort     => $inet_port,          # Sets the connect() port
  1175.     SuccessEvent   => $event_success,      # Event to emit on connection
  1176.     FailureEvent   => $event_failure,      # Event to emit on error
  1177.     # Optional parameters (and default values):
  1178.     SocketDomain   => AF_INET,             # Sets the socket() domain
  1179.     SocketType     => SOCK_STREAM,         # Sets the socket() type
  1180.     SocketProtocol => 'tcp',               # Sets the socket() protocol
  1181.     Reuse          => 'yes',               # Lets the port be reused
  1182.   );
  1183.  
  1184.   $wheel->event( ... );
  1185.  
  1186.   $wheel->ID();
  1187.  
  1188.   $wheel->pause_accept();
  1189.   $wheel->resume_accept();
  1190.  
  1191. =head1 DESCRIPTION
  1192.  
  1193. SocketFactory creates sockets.  It can create connectionless sockets
  1194. like UDP, or connected sockets like UNIX domain streams and TCP
  1195. sockets.
  1196.  
  1197. The SocketFactory manages connecting and listening sockets on behalf
  1198. of the session that created it.  It will watch a connecting socket and
  1199. fire a SuccessEvent or FailureEvent event when something happens.  It
  1200. will watch a listening socket and fire a SuccessEvent or FailureEvent
  1201. for every connection.
  1202.  
  1203. =head1 PUBLIC METHODS
  1204.  
  1205. =over 2
  1206.  
  1207. =item new LOTS_OF_THINGS
  1208.  
  1209. new() creates a new socket.  If necessary, it registers event handlers
  1210. to manage the socket.  new() has parameters for just about every
  1211. aspect of socket creation; thankfully they all aren't needed at once.
  1212.  
  1213. new() always returns a SocketFactory wheel reference, even if a socket
  1214. couldn't be created.
  1215.  
  1216. These parameters provide information for the SocketFactory's socket()
  1217. call.
  1218.  
  1219. =over 2
  1220.  
  1221. =item SocketDomain
  1222.  
  1223. SocketDomain supplies socket() with its DOMAIN parameter.  Supported
  1224. values are AF_UNIX, AF_INET, AF_INET6, PF_UNIX, PF_INET, and PF_INET6.
  1225. If SocketDomain is omitted, it defaults to AF_INET.
  1226.  
  1227. Note: AF_INET6 and PF_INET6 are supplied by the Socket6 module, which
  1228. is available on the CPAN.  You must have Socket6 loaded before
  1229. SocketFactory can create IPv6 sockets.
  1230.  
  1231. =item SocketType
  1232.  
  1233. SocketType supplies socket() with its TYPE parameter.  Supported
  1234. values are SOCK_STREAM and SOCK_DGRAM, although datagram sockets
  1235. haven't been tested at this time.  If SocketType is omitted, it
  1236. defaults to SOCK_STREAM.
  1237.  
  1238. =item SocketProtocol
  1239.  
  1240. SocketProtocol supplies socket() with its PROTOCOL parameter.
  1241. Protocols may be specified by number or by a name that can be found in
  1242. the system's protocol (or equivalent) database.  SocketProtocol is
  1243. ignored for UNIX domain sockets.  It defaults to 'tcp' if it's omitted
  1244. from an INET socket factory.
  1245.  
  1246. =back
  1247.  
  1248. These parameters provide information for the SocketFactory's bind()
  1249. call.
  1250.  
  1251. =over 2
  1252.  
  1253. =item BindAddress
  1254.  
  1255. BindAddress supplies the address where a socket will be bound to.  It
  1256. has different meanings and formats depending on the socket domain.
  1257.  
  1258. BindAddress may contain either a string or a packed Internet address
  1259. when it's specified for INET sockets.  The string form of BindAddress
  1260. should hold a dotted numeric address or resolvable host name.
  1261. BindAddress is optional for INET sockets, and SocketFactory will use
  1262. INADDR_ANY by default.
  1263.  
  1264. When used to bind a UNIX domain socket, BindAddress should contain a
  1265. path describing the socket's filename.  This is required for server
  1266. sockets and datagram client sockets.  BindAddress has no default value
  1267. for UNIX sockets.
  1268.  
  1269. =item BindPort
  1270.  
  1271. BindPort is only meaningful for INET domain sockets.  It contains a
  1272. port on the BindAddress interface where the socket will be bound.  It
  1273. defaults to 0 if omitted.
  1274.  
  1275. BindPort may be a port number or a name that can be looked up in the
  1276. system's services (or equivalent) database.
  1277.  
  1278. =back
  1279.  
  1280. These parameters are used for outbound sockets.
  1281.  
  1282. =over 2
  1283.  
  1284. =item RemoteAddress
  1285.  
  1286. RemoteAddress specifies the remote address to which a socket should
  1287. connect.  If present, the SocketFactory will create a connecting
  1288. socket.  Otherwise, it will make a listening socket, should the
  1289. protocol warrant it.
  1290.  
  1291. Like with the bind address, RemoteAddress may be a string containing a
  1292. dotted quad or a resolvable host name.  It may also be a packed
  1293. Internet address, or a UNIX socket path.  It will be packed, with or
  1294. without an accompanying RemotePort, as necessary for the socket
  1295. domain.
  1296.  
  1297. =item RemotePort
  1298.  
  1299. RemotePort is the port to which the socket should connect.  It is
  1300. required for connecting Internet sockets and ignored in all other
  1301. cases.
  1302.  
  1303. The remote port may be a number or a name in the /etc/services (or
  1304. equivalent) database.
  1305.  
  1306. =back
  1307.  
  1308. This parameter is used for listening sockets.
  1309.  
  1310. =over 2
  1311.  
  1312. =item ListenQueue
  1313.  
  1314. ListenQueue specifies the length of the socket's listen() queue.  It
  1315. defaults to SOMAXCONN if omitted.  SocketFactory will ensure that it
  1316. doesn't exceed SOMAXCONN.
  1317.  
  1318. =back
  1319.  
  1320. =item event EVENT_TYPE => EVENT_NAME, ...
  1321.  
  1322. event() is covered in the POE::Wheel manpage.
  1323.  
  1324. =item getsockname
  1325.  
  1326. getsockname() behaves like the built-in function of the same name.
  1327. Because the SocketFactory's underlying socket is hidden away, it's
  1328. hard to do this directly.
  1329.  
  1330. It's useful for finding which address and/or port the SocketFactory
  1331. has bound to when it's been instructed to use BindAddress =>
  1332. INADDR_ANY or BindPort => 0.
  1333.  
  1334. =item ID
  1335.  
  1336. The ID method returns a SocketFactory wheel's unique ID.  This ID will
  1337. be included in every event the wheel generates, and it can be used to
  1338. match events with the wheels which generated them.
  1339.  
  1340. =item pause_accept
  1341.  
  1342. =item resume_accept
  1343.  
  1344. Listening SocketFactory instances will accept connections for as long
  1345. as they exist.  This may not be desirable in pre-forking servers where
  1346. the main process must not handle connections.
  1347.  
  1348. pause_accept() temporarily stops a SocketFactory from accepting new
  1349. connections.  It continues to listen, however.  resume_accept() ends a
  1350. temporary pause, allowing a SocketFactory to accept new connections.
  1351.  
  1352. In a pre-forking server, the main process would pause_accept()
  1353. immediately after the SocketFactory was created.  As forked child
  1354. processes start, they call resume_accept() to begin accepting
  1355. connections.
  1356.  
  1357. =back
  1358.  
  1359. =head1 EVENTS AND PARAMETERS
  1360.  
  1361. =over 2
  1362.  
  1363. =item SuccessEvent
  1364.  
  1365. SuccessEvent defines the event that will be emitted when a socket has
  1366. been established successfully.  The SuccessEvent event is fired when
  1367. outbound sockets have connected or whenever listening sockets accept
  1368. new connections.
  1369.  
  1370. SuccessEvent must be the name of a state within the current session.
  1371.  
  1372. In all cases, C<ARG0> holds the new socket handle.  C<ARG3> holds the
  1373. wheel's unique ID.  The parameters between them differ according to
  1374. the socket's domain and whether it's listening or connecting.
  1375.  
  1376. For INET sockets, C<ARG1> and C<ARG2> hold the socket's remote address
  1377. and port, respectively.  The address is packed; use inet_ntoa() (See
  1378. L<Socket>) if a human-readable version is necessary.
  1379.  
  1380. For UNIX B<client> sockets, C<ARG1> holds the server address.  It may
  1381. be undefined on systems that have trouble retrieving a UNIX socket's
  1382. remote address.  C<ARG2> is always undefined for UNIX B<client>
  1383. sockets.
  1384.  
  1385. According to _Perl Cookbook_, the remote address returned by accept()
  1386. on UNIX sockets is undefined, so C<ARG1> and C<ARG2> are also
  1387. undefined in this case.
  1388.  
  1389. A sample SuccessEvent handler:
  1390.  
  1391.   sub server_accept {
  1392.     my $accepted_handle = $_[ARG0];
  1393.  
  1394.     my $peer_host = inet_ntoa($_[ARG1]);
  1395.     print( "Wheel $_[ARG3] accepted a connection from ",
  1396.            "$peer_host port $peer_port\n"
  1397.          );
  1398.  
  1399.     # Do something with the new connection.
  1400.     &spawn_connection_session( $accepted_handle );
  1401.   }
  1402.  
  1403. =item FailureEvent
  1404.  
  1405. FailureEvent defines the event that will be emitted when a socket
  1406. error occurs.  EAGAIN does not count as an error since the
  1407. SocketFactory knows what to do with it.
  1408.  
  1409. FailureEvent must be the name of a state within the current session.
  1410.  
  1411. The FailureEvent event comes with the standard error event parameters.
  1412.  
  1413. C<ARG0> contains the name of the operation that failed.  C<ARG1> and
  1414. C<ARG2> hold numeric and string values for C<$!>, respectively.
  1415. C<ARG3> contains the wheel's unique ID, which may be matched back to
  1416. the wheel itself via the $wheel->ID call.
  1417.  
  1418. A sample ErrorEvent handler:
  1419.  
  1420.   sub error_state {
  1421.     my ($operation, $errnum, $errstr, $wheel_id) = @_[ARG0..ARG3];
  1422.     warn "Wheel $wheel_id generated $operation error $errnum: $errstr\n";
  1423.     delete $heap->{wheels}->{$wheel_id}; # shut down that wheel
  1424.   }
  1425.  
  1426. =back
  1427.  
  1428. =head1 SEE ALSO
  1429.  
  1430. POE::Wheel, Socket6.
  1431.  
  1432. The SEE ALSO section in L<POE> contains a table of contents covering
  1433. the entire POE distribution.
  1434.  
  1435. =head1 BUGS
  1436.  
  1437. Many (if not all) of the croak/carp/warn/die statements should fire
  1438. back FailureEvent instead.
  1439.  
  1440. SocketFactory is only tested with UNIX streams and INET sockets using
  1441. the UDP and TCP protocols.  Others may or may not work, but the latest
  1442. design is data driven and should be easy to extend.  Patches are
  1443. welcome, as are test cases for new families and protocols.  Even if
  1444. test cases fail, they'll make nice reference code to test additions to
  1445. the SocketFactory class.
  1446.  
  1447. =head1 AUTHORS & COPYRIGHTS
  1448.  
  1449. Please see L<POE> for more information about authors and contributors.
  1450.  
  1451. =cut
  1452.