The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

POE::Component::Server::TCP - a simplified TCP server

SYNOPSIS

  use POE qw(Component::Server::TCP);

  # First form just accepts connections.

  POE::Component::Server::TCP->new
    ( Port     => $bind_port,
      Address  => $bind_address,    # Optional.
      Acceptor => \&accept_handler,
      Error    => \&error_handler,  # Optional.
    );

  # Second form accepts and handles connections.

  POE::Component::Server::TCP->new
    ( Port     => $bind_port,
      Address  => $bind_address,    # Optional.
      Acceptor => \&accept_handler, # Optional.
      Error    => \&error_handler,  # Optional.

      ClientInput        => \&handle_client_input,      # Required.
      ClientConnected    => \&handle_client_connect,    # Optional.
      ClientDisconnected => \&handle_client_disconnect, # Optional.
      ClientError        => \&handle_client_error,      # Optional.
      ClientFlushed      => \&handle_client_flush,      # Optional.
      ClientFilter       => "POE::Filter::Xyz",         # Optional.

      # Optionally define other states for the client session.
      InlineStates  => { ... },
      PackageStates => [ ... ],
      ObjectStates  => [ ... ],
    );

  # Call signatures for handlers.

  sub accept_handler {
    my ($socket, $remote_address, $remote_port) = @_[ARG0, ARG1, ARG2];
  }

  sub error_handler {
    my ($syscall_name, $error_number, $error_string) = @_[ARG0, ARG1, ARG2];
  }

  sub handle_client_input {
    my $input_record = $_[ARG0];
  }

  sub handle_client_error {
    my ($syscall_name, $error_number, $error_string) = @_[ARG0, ARG1, ARG2];
  }

  sub handle_client_connect {
    # no special parameters
  }

  sub handle_client_disconnect {
    # no special parameters
  }

  sub handle_client_flush {
    # no special parameters
  }

  # Reserved HEAP variables:

  $heap->{listener}    = SocketFactory (only Acceptor and Error callbacks)
  $heap->{client}      = ReadWrite     (only in ClientXyz callbacks)
  $heap->{remote_ip}   = remote IP address in dotted form
  $heap->{remote_port} = remote port
  $heap->{remote_addr} = packed remote address and port
  $heap->{shutdown}    = shutdown flag (check to see if shutting down)

  # Accepted public events.

  $kernel->yield( "shutdown" )           # initiate shutdown in a connection
  $kernel->post( server => "shutdown" )  # stop listening for connections

DESCRIPTION

The TCP server component hides the steps needed to create a server using Wheel::SocketFactory. The steps aren't many, but they're still tiresome after a while.

POE::Component::Server::TCP supplies common defaults for most callbacks and handlers. The authors hope that servers can be created with as little work as possible.

Constructor parameters:

Acceptor

Acceptor is a coderef which will be called to handle accepted sockets. The coderef receives its parameters directly from SocketFactory's SuccessEvent. ARG0 is the accepted socket handle, suitable for giving to a ReadWrite wheel. ARG1 and ARG2 contain the packed remote address and numeric port, respectively. ARG3 is the SocketFactory wheel's ID.

  Acceptor => \&accept_handler

Acceptor and ClientInput are mutually exclusive. Enabling one prohibits the other.

Address

Address is the optional interface address the TCP server will bind to. It defaults to INADDR_ANY.

  Address => '127.0.0.1'

It's passed directly to SocketFactory's BindAddress parameter, so it can be in whatever form SocketFactory supports. At the time of this writing, that's a dotted quad, a host name, or a packed Internet address.

Alias

Alias is an optional name by which this server may be referenced. It's used to pass events to a TCP server from other sessions.

  Alias => 'chargen'

Later on, the 'chargen' service can be shut down with:

  $kernel->post( chargen => 'shutdown' );
ClientConnected

ClientConnected is a coderef that will be called for each new client connection. ClientConnected callbacks receive the usual POE parameters, but nothing special is included.

ClientDisconnected

ClientDisconnected is a coderef that will be called for each client disconnection. ClientDisconnected callbacks receive the usual POE parameters, but nothing special is included.

ClientError

ClientError is a coderef that will be called whenever an error occurs on a socket. It receives the usual error handler parameters: ARG0 is the name of the function that failed. ARG1 is the numeric failure code ($! in numeric context). ARG2 is the string failure code ($! in string context).

If ClientError is omitted, a default one will be provided. The default error handler logs the error to STDERR and closes the connection.

ClientFilter

ClientFilter is the name of a POE::Filter class. Objects of that class will be instantiated to interpret and serialize data for the client socket. POE::Component::Server::TCP will provide a generic Line filter by default.

If a ClientFilter class is specified, it's up to the programmer to use or define that class.

ClientInput

ClientInput is a coderef that will be called to handle client input. The callback receives its parameters directyl from ReadWrite's InputEvent. ARG0 is the input record, and ARG1 is the wheel's unique ID.

  ClientInput => \&input_handler

ClientInput and Acceptor are mutually exclusive. Enabling one prohibits the other.

Error

Error is an optional coderef which will be called to handle server socket errors. The coderef is used as POE::Wheel::SocketFactory's FailureEvent, so it accepts the same parameters. If it is omitted, a default error handler will be provided. The default handler will log the error to STDERR and shut down the server.

InlineStates

InlineStates holds a hashref of inline coderefs to handle events. The hashref is keyed on event name. For more information, see POE::Session's create() method.

ObjectStates

ObjectStates holds a list reference of objects and the events they handle. For more information, see POE::Session's create() method.

PackageStates

PackageStates holds a list reference of Perl package names and the events they handle. For more information, see POE::Session's create() method.

Port

Port is the port the listening socket will be bound to. It defaults to INADDR_ANY, which usually lets the operating system pick a port.

  Port => 30023

EVENTS

It's possible to manipulate a TCP server component from some other session. This is useful for shutting them down, and little else so far.

shutdown

Shuts down the TCP server. This entails destroying the SocketFactory that's listening for connections and removing the TCP server's alias, if one is set.

SEE ALSO

POE::Component::Client::TCP, POE::Wheel::SocketFactory, POE::Wheel::ReadWrite, POE::Filter

CAVEATS

This is not suitable for complex tasks. For example, you cannot engage in a challenge-response with the client-- you can only reply to the one message a client sends.

BUGS

This looks nothing like what Ann envisioned.

This component currently does not accept many of the options that POE::Wheel::SocketFactory does.

This component will not bind to several addresses. This may be a limitation in SocketFactory.

This component needs more complex error handling which appends for construction errors and replaces for runtime errors, instead of replacing for all.

AUTHORS & COPYRIGHTS

POE::Component::Server::TCP is Copyright 2000-2001 by Rocco Caputo. All rights are reserved. POE::Component::Server::TCP is free software, and it may be redistributed and/or modified under the same terms as Perl itself.

POE::Component::Server::TCP is based on code, used with permission, from Ann Barcomb <kudra@domaintje.com>.

POE::Component::Server::TCP is based on code, used with permission, from Jos Boumans <kane@cpan.org>.