=head1 NAME

IO::Mux::Net::TCP - handle a TCP connection

=head1 INHERITANCE

 IO::Mux::Net::TCP
   is a IO::Mux::Handler::Read
   is a IO::Mux::Handler

 IO::Mux::Net::TCP
   is a IO::Mux::Handler::Write
   is a IO::Mux::Handler

 IO::Mux::Net::TCP is extended by
   IO::Mux::HTTP

=head1 DESCRIPTION

Handle a service or locally initiated TCP connection.

=head1 METHODS

=head2 Constructors

=over 4

=item IO::Mux::Net::TCP-E<gt>B<new>(OPTIONS)

Build a connection as client or server. You may either pass an prepared
C<socket> object or parameters to initiate one. All OPTIONS which start
with capitals are passed to the socket creation. See L<extractSocket()|IO::Mux::Handler/"Helpers">
for those additional OPTIONS.

 -Option    --Defined in     --Default
  fh          IO::Mux::Handler  <required>
  name        IO::Mux::Handler  'tcp $host:$port'
  read_size   IO::Mux::Handler::Read  32768
  socket                       <required>
  write_size  IO::Mux::Handler::Write  4096

=over 2

=item fh => FILEHANDLE

=item name => STRING

=item read_size => INTEGER

=item socket => IO::Socket::INET

Provide a socket, either as object or the parameters to instantiate it.

=item write_size => INTEGER

=back

example: 

  # long form, most flexible
  my $socket = IO::Socket::INET->new
    ( PeerAddr => 'www.example.com:80'
    , Reuse    => 1
    );
  my $client = IO::Mux::Net::TCP->new
    ( socket   => $socket
    );
  $mux->add($client);

  # short form
  my $client = IO::Mux::Net::TCP->new
    ( PeerAddr => 'www.example.com:80'
    , Reuse    => 1
    );
  $mux->add($client);

  # even shorter
  my $client = $mux->open('tcp'
    , PeerAddr => 'www.example.com:80'
    , Reuse    => 1
    );

=item IO::Mux::Net::TCP-E<gt>B<open>(MODE, WHAT, OPTIONS)
See L<IO::Mux::Handler/"Constructors">

=item IO::Mux::Net::TCP-E<gt>B<open>(MODE, WHAT, OPTIONS)
See L<IO::Mux::Handler/"Constructors">

=back

=head2 Accessors

=over 4

=item $obj-E<gt>B<fh>
See L<IO::Mux::Handler/"Accessors">

=item $obj-E<gt>B<fh>
See L<IO::Mux::Handler/"Accessors">

=item $obj-E<gt>B<fileno>
See L<IO::Mux::Handler/"Accessors">

=item $obj-E<gt>B<fileno>
See L<IO::Mux::Handler/"Accessors">

=item $obj-E<gt>B<mux>
See L<IO::Mux::Handler/"Accessors">

=item $obj-E<gt>B<mux>
See L<IO::Mux::Handler/"Accessors">

=item $obj-E<gt>B<name>
See L<IO::Mux::Handler/"Accessors">

=item $obj-E<gt>B<name>
See L<IO::Mux::Handler/"Accessors">

=item $obj-E<gt>B<readSize>([INTEGER])
See L<IO::Mux::Handler::Read/"Accessors">

=item $obj-E<gt>B<socket>

=item $obj-E<gt>B<usesSSL>
See L<IO::Mux::Handler/"Accessors">

=item $obj-E<gt>B<usesSSL>
See L<IO::Mux::Handler/"Accessors">

=item $obj-E<gt>B<writeSize>([INTEGER])
See L<IO::Mux::Handler::Write/"Accessors">

=back

=head2 User interface

=head3 Connection

=over 4

=item $obj-E<gt>B<close>([CALLBACK])
See L<IO::Mux::Handler/"Connection">

=item $obj-E<gt>B<close>([CALLBACK])
See L<IO::Mux::Handler/"Connection">

=item $obj-E<gt>B<shutdown>((0|1|2))

Shut down a socket for reading or writing or both. See the C<shutdown>
Perl documentation for further details.

If the shutdown is for reading (0 or 2), it happens immediately. However,
shutdowns for writing (1 or 2) are delayed until any pending output has
been successfully written to the socket.

example: 

  $conn->shutdown(1);

=item $obj-E<gt>B<timeout>([TIMEOUT])
See L<IO::Mux::Handler/"Connection">

=item $obj-E<gt>B<timeout>([TIMEOUT])
See L<IO::Mux::Handler/"Connection">

=back

=head3 Reading

=over 4

=item $obj-E<gt>B<readline>(CALLBACK)
See L<IO::Mux::Handler::Read/"Reading">

=item $obj-E<gt>B<slurp>(CALLBACK)
See L<IO::Mux::Handler::Read/"Reading">

=back

=head3 Writing

=over 4

=item $obj-E<gt>B<print>(STRING|SCALAR|LIST|ARRAY)
See L<IO::Mux::Handler::Write/"Writing">

=item $obj-E<gt>B<printf>(FORMAT, PARAMS)
See L<IO::Mux::Handler::Write/"Writing">

=item $obj-E<gt>B<say>(STRING|SCALAR|LIST|ARRAY)
See L<IO::Mux::Handler::Write/"Writing">

=item $obj-E<gt>B<write>(SCALAR, [MORE])
See L<IO::Mux::Handler::Write/"Writing">

=back

=head2 Multiplexer

=head3 Connection

=over 4

=item $obj-E<gt>B<mux_init>(MUX, [HANDLER])
See L<IO::Mux::Handler/"Connection">

=item $obj-E<gt>B<mux_init>(MUX, [HANDLER])
See L<IO::Mux::Handler/"Connection">

=item $obj-E<gt>B<mux_remove>
See L<IO::Mux::Handler/"Connection">

=item $obj-E<gt>B<mux_remove>
See L<IO::Mux::Handler/"Connection">

=item $obj-E<gt>B<mux_timeout>
See L<IO::Mux::Handler/"Connection">

=item $obj-E<gt>B<mux_timeout>
See L<IO::Mux::Handler/"Connection">

=back

=head3 Reading

=over 4

=item $obj-E<gt>B<mux_eof>

For sockets, this does not nessecarily mean that the descriptor has been
closed, as the other end of a socket could have used L<shutdown()|IO::Mux::Net::TCP/"Connection"> to
close just half of the socket, leaving us free to write data back down
the still open half.

example: 

In this example, we send a final reply to the other end of the socket,
and then shut it down for writing.  Since it is also shut down for reading
(implicly by the EOF condition), it will be closed once the output has
been sent, after which the mux_close callback will be called.

  sub mux_eof
  {   my ($self, $ref_input) = @_;
      print $fh "Well, goodbye then!\n";
      $self->shutdown(1);
  }

=item $obj-E<gt>B<mux_except_flagged>(FILENO)
See L<IO::Mux::Handler/"Reading">

=item $obj-E<gt>B<mux_except_flagged>(FILENO)
See L<IO::Mux::Handler/"Reading">

=item $obj-E<gt>B<mux_input>(BUFFER)
See L<IO::Mux::Handler::Read/"Reading">

=item $obj-E<gt>B<mux_read_flagged>(FILENO)
See L<IO::Mux::Handler/"Reading">

=item $obj-E<gt>B<mux_read_flagged>(FILENO)
See L<IO::Mux::Handler/"Reading">

=back

=head3 Writing

=over 4

=item $obj-E<gt>B<mux_outbuffer_empty>
See L<IO::Mux::Handler::Write/"Writing">

=item $obj-E<gt>B<mux_output_waiting>
See L<IO::Mux::Handler::Write/"Writing">

=item $obj-E<gt>B<mux_write_flagged>(FILENO)
See L<IO::Mux::Handler/"Writing">

=item $obj-E<gt>B<mux_write_flagged>(FILENO)
See L<IO::Mux::Handler/"Writing">

=back

=head3 Service

=head2 Helpers

=over 4

=item $obj-E<gt>B<extractSocket>(HASH)

=item IO::Mux::Net::TCP-E<gt>B<extractSocket>(HASH)
See L<IO::Mux::Handler/"Helpers">

=item $obj-E<gt>B<extractSocket>(HASH)

=item IO::Mux::Net::TCP-E<gt>B<extractSocket>(HASH)
See L<IO::Mux::Handler/"Helpers">

=item $obj-E<gt>B<fdset>(STATE, READ, WRITE, ERROR)
See L<IO::Mux::Handler/"Helpers">

=item $obj-E<gt>B<fdset>(STATE, READ, WRITE, ERROR)
See L<IO::Mux::Handler/"Helpers">

=item $obj-E<gt>B<show>
See L<IO::Mux::Handler/"Helpers">

=item $obj-E<gt>B<show>
See L<IO::Mux::Handler/"Helpers">

=back

=head1 SEE ALSO

This module is part of IO-Mux distribution version 0.11,
built on January 26, 2011. Website: F<http://perl.overmeer.net/>
All modules in this suite:
L</Any::Daemon>,
L</IO::Mux>, and
L</IO::Mux::HTTP>.

Please post questions or ideas to F<perl@overmeer.net>

=head1 LICENSE

Copyrights 2011 by Mark Overmeer. For other contributors see ChangeLog.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See F<http://www.perl.com/perl/misc/Artistic.html>

