Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 1373 lines (1073 sloc) 45.037 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
package POE::Component::Server::TCP;

use strict;

use vars qw($VERSION);
$VERSION = '1.020'; # NOTE - Should be #.### (three decimal places)

use Carp qw(carp croak);
use Socket qw(INADDR_ANY inet_ntoa inet_aton AF_INET AF_UNIX PF_UNIX);
use Errno qw(ECONNABORTED ECONNRESET);

# Explicit use to import the parameter constants.
use POE::Session;
use POE::Driver::SysRW;
use POE::Filter::Line;
use POE::Wheel::ReadWrite;
use POE::Wheel::SocketFactory;

sub DEBUG () { 0 }

# Create the server. This is just a handy way to encapsulate
# POE::Session->create(). Because the states are so small, it uses
# real inline coderefs.

sub new {
  my $type = shift;

  # Helper so we don't have to type it all day. $mi is a name I call
  # myself.
  my $mi = $type . '->new()';

  # If they give us lemons, tell them to make their own damn
  # lemonade.
  croak "$mi requires an even number of parameters" if (@_ & 1);
  my %param = @_;

  # Extract parameters.
  my $alias = delete $param{Alias};
  my $address = delete $param{Address};
  my $hname = delete $param{Hostname};
  my $port = delete $param{Port};
  my $domain = delete($param{Domain}) || AF_INET;
  my $concurrency = delete $param{Concurrency};

  $port = 0 unless defined $port;

  foreach (
    qw(
Acceptor Error ClientInput ClientConnected ClientDisconnected
ClientError ClientFlushed
ClientLow ClientHigh
)
  ) {
    croak "$_ must be a coderef"
      if defined($param{$_}) and ref($param{$_}) ne 'CODE';
  }

  my $high_mark_level = delete $param{HighMark};
  my $low_mark_level = delete $param{LowMark};
  my $high_event = delete $param{ClientHigh};
  my $low_event = delete $param{ClientLow};

  my $mark_param_count = (
    grep { defined $_ }
    ($high_mark_level, $low_mark_level, $high_event, $low_event)
  );
  if ($mark_param_count and $mark_param_count < 4) {
    croak "If you use the Mark settings, you must define all four";
  }

  $high_event = sub { } unless defined $high_event;
  $low_event = sub { } unless defined $low_event;

  my $accept_callback = delete $param{Acceptor};
  my $error_callback = delete $param{Error};

  my $client_input = delete $param{ClientInput};

  # Acceptor and ClientInput are mutually exclusive.
  croak "$mi needs either an Acceptor or a ClientInput but not both"
    unless defined($accept_callback) xor defined($client_input);

  # Make sure ClientXyz are accompanied by ClientInput.
  unless (defined($client_input)) {
    foreach (grep /^Client/, keys %param) {
      croak "$_ not permitted without ClientInput";
    }
  }

  my $client_connected = delete $param{ClientConnected};
  my $client_disconnected = delete $param{ClientDisconnected};
  my $client_error = delete $param{ClientError};
  my $client_filter = delete $param{ClientFilter};
  my $client_infilter = delete $param{ClientInputFilter};
  my $client_outfilter = delete $param{ClientOutputFilter};
  my $client_flushed = delete $param{ClientFlushed};
  my $session_type = delete $param{SessionType};
  my $session_params = delete $param{SessionParams};
  my $server_started = delete $param{Started};
  my $listener_args = delete $param{ListenerArgs};

  $listener_args = [] unless defined $listener_args;
  croak "ListenerArgs must be an array reference"
    unless ref($listener_args) eq 'ARRAY';

  if (exists $param{Args}) {
    if (exists $param{ClientArgs}) {
      carp "Args is deprecated, and ignored since ClientArgs is present";
      delete $param{Args};
    }
    else {
      carp "Args is deprecated but allowed for now. Please use ClientArgs";
    }
  }

  my $client_args = delete($param{ClientArgs}) || delete($param{Args});

  # Defaults.

  $concurrency = -1 unless defined $concurrency;
  my $accept_session_id;

  if (!defined $address && defined $hname) {
    $address = inet_aton($hname);
  }
  $address = INADDR_ANY unless defined $address;

  $error_callback = \&_default_server_error unless defined $error_callback;
  $server_started = sub {} unless ref($server_started) eq 'CODE';

  $session_type = 'POE::Session' unless defined $session_type;
  if (defined($session_params) && ref($session_params)) {
    if (ref($session_params) ne 'ARRAY') {
      croak "SessionParams must be an array reference";
    }
  } else {
    $session_params = [ ];
  }

  if (defined $client_input) {
    $client_error = \&_default_client_error unless defined $client_error;
    $client_connected = sub {} unless defined $client_connected;
    $client_disconnected = sub {} unless defined $client_disconnected;
    $client_flushed = sub {} unless defined $client_flushed;
    $client_args = [] unless defined $client_args;

    # Extra states.

    my $inline_states = delete $param{InlineStates};
    $inline_states = {} unless defined $inline_states;

    my $package_states = delete $param{PackageStates};
    $package_states = [] unless defined $package_states;

    my $object_states = delete $param{ObjectStates};
    $object_states = [] unless defined $object_states;

    my $shutdown_on_error = 1;
    if (exists $param{ClientShutdownOnError}) {
      $shutdown_on_error = delete $param{ClientShutdownOnError};
    }

    croak "InlineStates must be a hash reference"
      unless ref($inline_states) eq 'HASH';

    croak "PackageStates must be a list or array reference"
      unless ref($package_states) eq 'ARRAY';

    croak "ObjectsStates must be a list or array reference"
      unless ref($object_states) eq 'ARRAY';

    croak "ClientArgs must be an array reference"
      unless ref($client_args) eq 'ARRAY';

    # Sanity check, thanks to crab@irc for making this mistake, ha!
    # TODO we could move this to POE::Session and make it a
    # "sanity checking" sub somehow...
    if (POE::Kernel::ASSERT_USAGE) {
      my %forbidden_handlers = (
        _child => 1,
        _start => 1,
        _stop => 1,
        shutdown => 1,
        tcp_server_got_error => 1,
        tcp_server_got_flush => 1,
        tcp_server_got_high => 1,
        tcp_server_got_input => 1,
        tcp_server_got_low => 1,
      );

      if (
        my @forbidden_inline_handlers = (
          grep { exists $inline_states->{$_} }
          keys %forbidden_handlers
        )
      ) {
        croak "These InlineStates aren't allowed: @forbidden_inline_handlers";
      }

      my %handlers = (
        PackageStates => $package_states,
        ObjectStates => $object_states,
      );

      while (my ($name, $states) = each(%handlers)) {
        my %states_hash = @$states;
        my @forbidden_handlers;
        while (my ($package, $handlers) = each %states_hash) {
          croak "Undefined $name member for $package" unless (
            defined $handlers
          );

          if (ref($handlers) eq 'HASH') {
            push(
              @forbidden_handlers,
              grep { exists $handlers->{$_} }
              keys %forbidden_handlers
            );
          }
          elsif (ref($handlers) eq 'ARRAY') {
            push(
              @forbidden_handlers,
              grep { exists $forbidden_handlers{$_} }
              @$handlers
            );
          }
          else {
            croak "Unknown $name member type for $package";
          }
        }

        croak "These $name aren't allowed: @forbidden_handlers" if (
          @forbidden_handlers
        );
      }
    }

    # Revise the acceptor callback so it spawns a session.

    unless (defined $accept_callback) {
      $accept_callback = sub {
        my ($socket, $remote_addr, $remote_port) = @_[ARG0, ARG1, ARG2];


        $session_type->create(
          @$session_params,
          inline_states => {
            _start => sub {
              my ( $kernel, $session, $heap ) = @_[KERNEL, SESSION, HEAP];

              $heap->{shutdown} = 0;
              $heap->{shutdown_on_error} = $shutdown_on_error;

              # Unofficial UNIX support, suggested by Damir Dzeko.
              # Real UNIX socket support should go into a separate
              # module, but if that module only differs by four
              # lines of code it would be bad to maintain two
              # modules for the price of one. One solution would be
              # to pull most of this into a base class and derive
              # TCP and UNIX versions from that.
              if (
                $domain == AF_UNIX or $domain == PF_UNIX
              ) {
                $heap->{remote_ip} = "LOCAL";
              }
              elsif (length($remote_addr) == 4) {
                $heap->{remote_ip} = inet_ntoa($remote_addr);
              }
              else {
                $heap->{remote_ip} =
                  Socket6::inet_ntop($domain, $remote_addr);
              }

              $heap->{remote_port} = $remote_port;

              $heap->{client} = POE::Wheel::ReadWrite->new(
                Handle => $_[ARG0],
                Driver => POE::Driver::SysRW->new(),
                _get_filters(
                  $client_filter,
                  $client_infilter,
                  $client_outfilter
                ),
                InputEvent => 'tcp_server_got_input',
                ErrorEvent => 'tcp_server_got_error',
                FlushedEvent => 'tcp_server_got_flush',

                (
                  $mark_param_count
                  ? (
                    HighMark => $high_mark_level,
                    HighEvent => 'tcp_server_got_high',
                    LowMark => $low_mark_level,
                    LowEvent => 'tcp_server_got_low',
                  )
                  : ()
                ),
              );

              # Expand the Args constructor array, and place a copy
              # into @_[ARG0..]. There are only 2 parameters.
              splice(@_, ARG0, 2, @{$_[ARG1]});

              $client_connected->(@_);
            },
            tcp_server_got_high => $high_event,
            tcp_server_got_low => $low_event,

            # To quiet ASSERT_STATES.
            _child => sub { },

            tcp_server_got_input => sub {
              return if $_[HEAP]->{shutdown};
              $client_input->(@_);
              undef;
            },
            tcp_server_got_error => sub {
              DEBUG and warn(
                "$$: $alias child Error ARG0=$_[ARG0] ARG1=$_[ARG1]"
              );
              unless ($_[ARG0] eq 'accept' and $_[ARG1] == ECONNABORTED) {
                $client_error->(@_);
                if ($_[HEAP]->{shutdown_on_error}) {
                  $_[HEAP]->{got_an_error} = 1;
                  $_[KERNEL]->yield("shutdown");
                }
              }
            },
            tcp_server_got_flush => sub {
              my $heap = $_[HEAP];
              DEBUG and warn "$$: $alias child Flush";
              $client_flushed->(@_);
              if ($heap->{shutdown}) {
                DEBUG and warn "$$: $alias child Flush, callback";
                $client_disconnected->(@_);
                delete $heap->{client};
              }
            },
            shutdown => sub {
              DEBUG and warn "$$: $alias child Shutdown";
              my $heap = $_[HEAP];
              $heap->{shutdown} = 1;
              if (defined $heap->{client}) {
                if (
                  $heap->{got_an_error} or
                  not $heap->{client}->get_driver_out_octets()
                ) {
                  DEBUG and warn "$$: $alias child Shutdown, callback";
                  $client_disconnected->(@_);
                  delete $heap->{client};
                }
              }
            },
            _stop => sub {
              ## concurrency on close
              DEBUG and warn(
                "$$: $alias _stop accept_session = $accept_session_id"
              );
              if( defined $accept_session_id ) {
                $_[KERNEL]->call( $accept_session_id, 'disconnected' );
              }
              else {
                # This means that the Server::TCP was shutdown before
                # this connection closed. So it doesn't really matter that
                # we can't decrement the connection counter.
                DEBUG and warn(
                  "$$: $_[HEAP]->{alias} Disconnected from a connection ",
                  "without POE::Component::Server::TCP parent"
                );
              }
              return;
            },

            # User supplied states.
            %$inline_states
          },

          # More user supplied states.
          package_states => $package_states,
          object_states => $object_states,

          # XXX - If you change the number of args here, also change
          # the splice elsewhere.
          args => [ $socket, $client_args ],
        );
      };
    }
  };

  # Complain about strange things we're given.
  foreach (sort keys %param) {
    carp "$mi doesn't recognize \"$_\" as a parameter";
  }

  ## verify concurrency on accept
  my $orig_accept_callback = $accept_callback;
  $accept_callback = sub {
    $_[HEAP]->{connections}++;
    DEBUG and warn(
      "$$: $_[HEAP]->{alias} Connection opened ",
      "($_[HEAP]->{connections} open)"
    );
    if( $_[HEAP]->{concurrency} != -1 and $_[HEAP]->{listener} ) {
      if( $_[HEAP]->{connections} >= $_[HEAP]->{concurrency} ) {
        DEBUG and warn(
          "$$: $_[HEAP]->{alias} Concurrent connection limit reached, ",
          "pausing accept"
        );
        $_[HEAP]->{listener}->pause_accept()
      }
    }
    $orig_accept_callback->(@_);
  };

  # Create the session, at long last.
  # This is done inline so that closures can customize it.
  # We save the accept session's ID to avoid self reference.

  $accept_session_id = $session_type->create(
    @$session_params,
    inline_states => {
      _start => sub {
        if (defined $alias) {
          $_[HEAP]->{alias} = $alias;
          $_[KERNEL]->alias_set( $alias );
        }

        $_[HEAP]->{concurrency} = $concurrency;
        $_[HEAP]->{connections} = 0;

        $_[HEAP]->{listener} = POE::Wheel::SocketFactory->new(
          ( ($domain == AF_UNIX or $domain == PF_UNIX)
            ? ()
            : ( BindPort => $port )
          ),
          BindAddress => $address,
          SocketDomain => $domain,
          Reuse => 'yes',
          SuccessEvent => 'tcp_server_got_connection',
          FailureEvent => 'tcp_server_got_error',
        );
        $server_started->(@_);
      },
      # Catch an error.
      tcp_server_got_error => $error_callback,

      # We accepted a connection. Do something with it.
      tcp_server_got_connection => $accept_callback,

      # conncurrency on close.
      disconnected => sub {
        $_[HEAP]->{connections}--;
        DEBUG and warn(
          "$$: $_[HEAP]->{alias} Connection closed ",
          "($_[HEAP]->{connections} open)"
        );
        if ($_[HEAP]->{connections} < 0) {
          warn(
            "Excessive 'disconnected' event ",
            "from $_[CALLER_FILE] at line $_[CALLER_LINE]\n"
          );
          $_[HEAP]->{connections} = 0;
        }
        if( $_[HEAP]->{concurrency} != -1 and $_[HEAP]->{listener} ) {
          if( $_[HEAP]->{connections} == ($_[HEAP]->{concurrency}-1) ) {
            DEBUG and warn(
              "$$: $_[HEAP]->{alias} Concurrent connection limit ",
              "reestablished, resuming accept"
            );
            $_[HEAP]->{listener}->resume_accept();
          }
        }
      },

      set_concurrency => sub {
        $_[HEAP]->{concurrency} = $_[ARG0];
        DEBUG and warn(
          "$$: $_[HEAP]->{alias} Concurrent connection ",
          "limit = $_[HEAP]->{concurrency}"
        );
        if( $_[HEAP]->{concurrency} != -1 and $_[HEAP]->{listener} ) {
          if( $_[HEAP]->{connections} >= $_[HEAP]->{concurrency} ) {
            DEBUG and warn(
              "$$: $_[HEAP]->{alias} Concurrent connection limit ",
              "reached, pausing accept"
            );
            $_[HEAP]->{listener}->pause_accept()
          }
          else {
            DEBUG and warn(
              "$$: $_[HEAP]->{alias} Concurrent connection limit ",
              "reestablished, resuming accept"
            );
            $_[HEAP]->{listener}->resume_accept();
          }
        }
      },

      # Shut down.
      shutdown => sub {
        delete $_[HEAP]->{listener};
        $_[KERNEL]->alias_remove( $_[HEAP]->{alias} )
          if defined $_[HEAP]->{alias};
      },

      # Dummy states to prevent warnings.
      _stop => sub {
        DEBUG and warn "$$: $_[HEAP]->{alias} _stop";
        undef($accept_session_id);
        return 0;
      },
      _child => sub { },
    },

    args => $listener_args,
  )->ID;

  # Return the session ID.
  return $accept_session_id;
}

sub _get_filters {
    my ($client_filter, $client_infilter, $client_outfilter) = @_;
    if (defined $client_infilter or defined $client_outfilter) {
      return (
        "InputFilter" => _load_filter($client_infilter),
        "OutputFilter" => _load_filter($client_outfilter)
      );
      if (defined $client_filter) {
        carp(
          "ClientFilter ignored with ClientInputFilter or ClientOutputFilter"
        );
      }
    }
    elsif (defined $client_filter) {
     return ( "Filter" => _load_filter($client_filter) );
    }
    else {
      return ( Filter => POE::Filter::Line->new(), );
    }

}

# Get something: either arrayref, ref, or string
# Return filter
sub _load_filter {
    my $filter = shift;
    if (ref ($filter) eq 'ARRAY') {
        my @args = @$filter;
        $filter = shift @args;
        if ( _test_filter($filter) ){
            return $filter->new(@args);
        } else {
            return POE::Filter::Line->new(@args);
        }
    }
    elsif (ref $filter) {
        return $filter->clone();
    }
    else {
        if ( _test_filter($filter) ) {
            return $filter->new();
        } else {
            return POE::Filter::Line->new();
        }
    }
}

# Test if a Filter can be loaded, return success or failure
sub _test_filter {
    my $filter = shift;
    my $eval = eval {
        (my $mod = $filter) =~ s!::!/!g;
        require "$mod.pm";
        1;
    };
    if (!$eval and $@) {
        carp(
          "Failed to load [$filter]\n" .
          "Reason $@\nUsing defualt POE::Filter::Line "
        );
        return 0;
    }
    return 1;
}

# The default server error handler logs to STDERR and shuts down the
# server.

sub _default_server_error {
  warn("$$: ".
    'Server ', $_[SESSION]->ID,
    " got $_[ARG0] error $_[ARG1] ($_[ARG2])\n"
  );
  delete $_[HEAP]->{listener};
}

# The default client error handler logs to STDERR

sub _default_client_error {
  my ($syscall, $errno, $error) = @_[ARG0..ARG2];
  unless ($syscall eq "read" and ($errno == 0 or $errno == ECONNRESET)) {
    $error = "(no error)" unless $errno;
    warn("$$: ".
      'Client session ', $_[SESSION]->ID,
      " got $syscall error $errno ($error)\n"
    );
  }
}

1;

__END__

=head1 NAME

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

=head1 SYNOPSIS

#!perl

use warnings;
use strict;

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

POE::Component::Server::TCP->new(
Port => 12345,
ClientConnected => sub {
print "got a connection from $_[HEAP]{remote_ip}\n";
$_[HEAP]{client}->put("Smile from the server!");
},
ClientInput => sub {
my $client_input = $_[ARG0];
$client_input =~ tr[a-zA-Z][n-za-mN-ZA-M];
$_[HEAP]{client}->put($client_input);
},
);

POE::Kernel->run;
exit;

=head1 DESCRIPTION

POE::Component::Server::TCP implements a generic multi-Session server.
Simple services may be put together in a few lines of code. For
example, a server that echoes input back to the client:

use POE qw(Component::Server::TCP);
POE::Component::Server::TCP->new(
Port => 12345,
ClientInput => sub { $_[HEAP]{client}->put($_[ARG0]) },
);
POE::Kernel->run();

=head2 Accepting Connections Yourself

POE::Component::Server::TCP has a default mode where it accepts new
connections and creates the sessions to handle them. Programs can do
this themselves by providing their own C<Acceptor> callbacks. See
L</Acceptor> for details.

=head2 Master Listener Session

At creation time, POE::Component::Server::TCP starts one POE::Session
to listen for new connections. The component's C<Alias> refers to
this master session.

If C<Acceptor> is specified, then it's up to that callback to deal
with newly accepted sockets. Its parameters are that of
POE::Wheel::SocketFactory's C<SuccessEvent>.

Otherwise, the default C<Acceptor> callback will start a new session
to handle each connection. These child sessions do not have their own
aliases, but their C<ClientConnected> and C<ClientDisconnected>
callbacks may be used to register and unregister the sessions with a
shared namespace, such as a hash keyed on session IDs, or an object
that manages such a hash.

my %client_namespace;

sub handle_client_connected {
my $client_session_id = $_[SESSION]->ID;
$client_namespace{$client_session_id} = \%anything;
}

sub handle_client_disconnected {
my $client_session_id = $_[SESSION]->ID;
$client_namespace{$client_session_id} = \%anything;
}

The component's C<Started> callback is invoked at the end of the
master session's startup routine. The @_[ARG0..$#_] parameters are
set to a copy of the values in the server's C<ListenerArgs>
constructor parameter. The other parameters are standard for
POE::Session's _start handlers.

The component's C<Error> callback is invoked when the server has a
problem listening for connections. C<Error> may also be called if the
component's default acceptor has trouble accepting a connection.
C<Error> receives the usual ones for POE::Wheel::SocketFactory and
POE::Wheel::ReadWrite L<ErrorEvent>.

=head2 Default Child Connection Sessions

If C<Acceptor> isn't specified, POE::Component::Server::TCP's default
handler will start a new session for each new client connection. As
mentioned above, these child sessions have no aliases of their own,
but they may set aliases or register themselves another way during
their C<ClientConnected> and C<ClientDisconnected> callbacks.

It can't be stressed enough that the following callbacks are executed
within the context of dynamic child sessions---one per client
connection---and not in the master listening session. This has been a
major point of confusion. We welcome suggestions for making this
clearer.

TODO - Document some of the implications of having each connection
handled by a separate session.

The component's C<ClientInput> callback defines how child sessions
will handle input from their clients. Its parameters are that of
POE::Wheel::ReadWrite's C<InputEvent>.

As mentioned
C<ClientConnected> is called at the end of the child session's
C<_start> routine. The C<ClientConneted> callback receives the same
parameters as the client session's _start does. The arrayref passed
to the constructor's C<Args> parameter is flattened and included in
C<ClientConnected>'s parameters as @_[ARG0..$#_].

sub handle_client_connected {
my @constructor_args = @_[ARG0..$#_];
...
}

C<ClientDisconnected> is called when the client has disconnected,
either because the remote socket endpoint has closed or the local
endpoint has been closed by the server. This doesn't mean the
client's session has ended, but the session most likely will very
shortly. C<ClientDisconnected> is called from a couple disparate
places within the component, so its parameters are neither consistent
nor generally useful.

C<ClientError> is called when an error has occurred on the socket.
Its parameters are those of POE::Wheel::ReadWrite's C<ErrorEvent>.

C<ClientFlushed> is called when all pending output has been flushed to
the client socket. Its parameters come from POE::Wheel::ReadWrite's
C<ErrorEvent>.

=head2 Performance Considerations

This ease of use comes at a price: POE::Component::Server::TCP often
performs significantly slower than a comparable server written with
POE::Wheel::SocketFactory and POE::Wheel::ReadWrite.

If performance is your primary goal, POE::Kernel's select_read() and
select_write() perform about the same as IO::Select, but your code
will be portable across every event loop POE supports.

=head2 Special Needs Considerations

POE::Component::Server::TCP is written to be easy for the most common
use cases. Programs with more special needs should consider using
POE::Wheel::SocketFactory and POE::Wheel::ReadWrite instead. These
are lower-level modules, and using them requires more effort. They
are more flexible and customizable, however.

=head1 PUBLIC METHODS

=head2 new

new() starts a server based on POE::Component::Server::TCP and returns
a session ID for the master listening session. All error handling is
done within the server, via the C<Error> and C<ClientError> callbacks.

The server may be shut down by posting a "shutdown" event to the
master session, either by its ID or the name given to it by the
C<Alias> parameter.

POE::Component::Server::TCP does a lot of work in its constructor.
The design goal is to push as much overhead into one-time construction
so that ongoing runtime has less overhead. Because of this, the
server's constructor can take quite a daunting number of parameters.

POE::Component::Server::TCP always returns a POE::Session ID for the
session that will be listening for new connections.

Many of the constructor parameters have been previously described.
They are covered briefly again below.

=head3 Server Sesson Configuration

These constructor parameters affect POE::Component::Server::TCP's main
listening session.

TODO - Document the shutdown procedure somewhere.

=head4 Acceptor

C<Acceptor> defines a CODE reference that POE::Wheel::SocketFactory's
C<SuccessEvent> will trigger to handle new connections. Therefore the
parameters passed to C<Acceptor> are identical to those given to
C<SuccessEvent>.

C<Acceptor> is optional; the default handler will create a new session
for each connection. All the "Client" constructor parameters are used
to customize this session. In other words, C<CleintInput> and such
B<are not used when C<Acceptor> is set>.

The default C<Acceptor> adds significant convenience and flexibility
to POE::Component::Server::TCP, but it's not always a good fit for
every application. In some cases, a custom C<Acceptor> or even
rolling one's own server with POE::Wheel::SocketFactory and
POE::Wheel::ReadWrite may be better and/or faster.

Acceptor => sub {
my ($socket, $remote_address, $remote_port) = @_[ARG0..ARG2];
# Set up something to interact with the client.
}

=head4 Address

C<Address> defines a single interface address the server will bind to.
It defaults to INADDR_ANY or INADDR6_ANY, when using IPv4 or IPv6,
respectively. It is often used with C<Port>.

The value in C<Address> is passed to POE::Wheel::SocketFactory's
C<BindAddress> parameter, so it may be in whatever form that module
supports. At the time of this writing, that may be a dotted IPv4
quad, an IPv6 address, a host name, or a packed Internet address. See
also L</Hostname>.

TODO - Example, using the lines below.

Address => '127.0.0.1' # Localhost IPv4
Address => "::1" # Localhost IPv6

=head4 Alias

C<Alias> is an optional name that will be given to the server's master
listening session. Events sent to this name will not be delivered to
individual connections.

The server's C<Alias> may be important if it's necessary to shut a
server down.

sub sigusr1_handler {
$_[KERNEL]->post(chargen_server => 'shutdown');
$_[KERNEL]->sig_handled();
}

=head4 Concurrency

C<Concurrency> controls how many connections may be active at the same
time. It defaults to -1, which allows POE::Component::Server::TCP to
accept concurrent connections until the process runs out of resources.

Setting C<Concurrency> to 0 prevents the server from accepting new
connections. This may be useful if a server must perform lengthy
initialization before allowing connections. When the initialization
finishes, it can yield(set_concurrency => -1) to enable connections.
Likewise, a running server may yield(set_concurrency => 0) or any
other number to dynamically tune its concurrency. See L<EVENTS> for
more about the set_concurrency event.

Note: For C<Concurrency> to work with a custom C<Acceptor>, the
server's listening session must receive a C<disconnected> event
whenever clients disconnect. Otherwise the listener cannot mediate
between its connections.

Example:

Acceptor => sub {
# ....
POE::Session->create(
# ....
inline_states => {
_start => sub {
# ....
# remember who our parent is
$_[HEAP]->{server_tcp} = $_[SENDER]->ID;
# ....
},
got_client_disconnect => sub {
# ....
$_[KERNEL]->post( $_[HEAP]->{server_tcp} => 'disconnected' );
# ....
}
}
);
}


=head4 Domain

C<Domain> sets the address or protocol family within which to operate.
The C<Domain> may be any value that POE::Wheel::SocketFactory
supports. AF_INET (Internet address space) is used by default.

Use AF_INET6 for IPv6 support. This constant is exported by Socket6,
which must be loaded B<before> POE::Component::Server::TCP.

=head4 Error

C<Error> is the callback that will be invoked when the server socket
reports an error. The Error callback will be used to handle
POE::Wheel::SocketFactory's FailureEvent, so it will receive the same
parameters as discussed there.

A default error handler will be provided if Error is omitted. The
default handler will log the error to STDERR and shut down the server.
Active connections will be permitted to to complete their
transactions.

Error => sub {
my ($syscall_name, $err_num, $err_str) = @_[ARG0..ARG2];
# Handle the error.
}

=head4 Hostname

C<Hostname> is the optional non-packed name of the interface the TCP
server will bind to. The hostname will always be resolved via
inet_aton() and so can either be a dotted quad or a name. Name
resolution is a one-time startup action; there are no ongoing runtime
penalties for using it.

C<Hostname> guarantees name resolution, where C<Address> does not.
It's therefore preferred to use C<Hostname> in cases where resolution
must always be done.

=head4 InlineStates

C<InlineStates> is optional. If specified, it must hold a hashref of
named callbacks. Its syntax is that of POE:Session->create()'s
inline_states parameter.

Remember: These InlineStates handlers will be added to the client
sessions, not to the main listening session. A yield() in the listener
will not reach these handlers.

If POE::Kernel::ASSERT_USAGE is enabled, the constructor will croak() if it
detects a state that it uses internally. For example, please use the "Started"
callback if you want to specify your own "_start" event.

=head4 ObjectStates

If C<ObjectStates> is specified, it must holde an arrayref of objects
and the events they will handle. The arrayref must follow the syntax
for POE::Session->create()'s object_states parameter.

Remember: These ObjectStates handlers will be added to the client
sessions, not to the main listening session. A yield() in the listener
will not reach these handlers.

If POE::Kernel::ASSERT_USAGE is enabled, the constructor will croak() if it
detects a state that it uses internally. For example, please use the "Started"
callback if you want to specify your own "_start" event.

=head4 PackageStates

When the optional C<PackageStates> is set, it must hold an arrayref of
package names and the events they will handle The arrayref must
follow the syntax for POE::Session->create()'s package_states
parameter.

Remember: These PackageStates handlers will be added to the client
sessions, not to the main listening session. A yield() in the listener
will not reach these handlers.

If POE::Kernel::ASSERT_USAGE is enabled, the constructor will croak() if it
detects a state that it uses internally. For example, please use the "Started"
callback if you want to specify your own "_start" event.

=head4 Port

C<Port> contains the port the listening socket will be bound to. It
defaults to 0, which usually lets the operating system pick a
port at random.

Port => 30023

It is often used with C<Address>.

=head4 Started

C<Started> sets an optional callback that will be invoked within the
main server session's context. It notifies the server that it has
fully started. The callback's parameters are the usual for a
session's _start handler.

=head4 ListenerArgs

C<ListenerArgs> is passed to the listener session as the C<args> parameter. In
other words, it must be an arrayref, and the values are are passed into the
C<Started> handler as ARG0, ARG1, etc.

=head3 Connection Session Configuration

These constructor parameters affect the individual sessions that
interact with established connections.

=head4 ClientArgs

C<ClientArgs> is optional. When specified, it holds an ARRAYREF that
will be expanded one level and passed to the C<ClientConnected>
callback in @_[ARG0..$#_].

=head4 ClientConnected

Each new client connection is handled by a new POE::Session instance.
C<ClientConnected> is a callback that notifies the application when a
client's session is started and ready for operation. Banners are
often sent to the remote client from this callback.

The @_[ARG0..$#_] parameters to C<ClientConnected> are a copy of the
values in the C<ClientArgs> constructor parameter's array referece.
The other @_ members are standard for a POE::Session _start handler.

C<ClientConnected> is called once per session startup. It will never
be called twice for the same connection.

ClientConnected => sub {
$_[HEAP]{client}->put("Hello, client!");
# Other client initialization here.
},

=head4 ClientDisconnected

C<ClientDisconnected> is a callback that will be invoked when the
client disconnects or has been disconnected by the server. It's
useful for cleaning up global client information, such as chat room
structures. C<ClientDisconnected> callbacks receive the usual POE
parameters, but nothing special is included.

ClientDisconnected => sub {
warn "Client disconnected"; # log it
}

=head4 ClientError

The C<ClientError> callback is invoked when a client socket reports an
error. C<ClientError> is called with POE's usual parameters, plus the
common error parameters: $_[ARG0] describes what was happening at the
time of failure. $_[ARG1] and $_[ARG2] contain the numeric and string
versions of $!, respectively.

C<ClientError> is optional. If omitted, POE::Component::Server::TCP
will provide a default callback that logs most errors to STDERR.

If C<ClientShutdownOnError> is set, the connection will be shut down
after C<ClientError> returns. If C<ClientDisconnected> is specified,
it will be called as the client session is cleaned up.

C<ClientError> is triggered by POE::Wheel::ReadWrite's ErrorEvent, so
it follows that event's form. Please see the ErrorEvent documentation
in POE::Wheel::ReadWrite for more details.

ClientError => sub {
my ($syscall_name, $error_num, $error_str) = @_[ARG0..ARG2];
# Handle the client error here.
}

=head4 ClientFilter

C<ClientFilter> specifies the POE::Filter object or class that will
parse input from each client and serialize output before it's sent to
each client.

C<ClientFilter> may be a SCALAR, in which case it should name the
POE::Filter class to use. Each new connection will be given a freshly
instantiated filter of that class. No constructor parameters will be
passed.

ClientFilter => "POE::Filter::Stream",

Some filters require constructor parameters. These may be specified
by an ARRAYREF. The first element is the POE::Filter class name, and
subsequent elements are passed to the class' constructor.

ClientFilter => [ "POE::Filter::Line", Literal => "\n" ],

C<ClientFilter> may also be given an archetypical POE::Filter OBJECT.
In this case, each new client session will receive a clone() of the
given object.

ClientFilter => POE::Filter::Line->new(Literal => "\n"),

C<ClientFilter> is optional. The component will use
"POE::Filter::Line" if it is omitted.

Filter modules are not automatically loaded. Be sure that the program
loads the class before using it.

=head4 ClientFlushed

C<ClientFlushed> exposes POE::Wheel::ReadWrite's C<FlushedEvent> as a
callback. It is called whenever the client's output buffer has been
fully flushed to the client socket. At this point it's safe to shut
down the socket without losing data.

C<ClientFlushed> is useful for streaming servers, where a "flushed"
event signals the need to send more data.

ClientFlushed => sub {
my $data_source = $_[HEAP]{file_handle};
my $read_count = sysread($data_source, my $buffer = "", 65536);
if ($read_count) {
$_[HEAP]{client}->put($buffer);
}
else {
$_[KERNEL]->yield("shutdown");
}
},

POE::Component::Server::TCP's default C<Acceptor> ensures that data is
flushed before finishing a client shutdown.

=head4 ClientInput

C<ClientInput> defines a per-connection callback to handle client
input. This callback receives its parameters directly from
POE::Wheel::ReadWrite's C<InputEvent>. ARG0 contains the input
recored, the format of which is defined by C<ClientFilter> or
C<ClientInputFilter>. ARG1 has the wheel's unique ID, and so on.
Please see POE:Wheel::ReadWrite for an in-depth description of
C<InputEvent>.

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

ClientInput => sub {
my $input = $_[ARG0];
$_[HEAP]{wheel}->put("You said: $input");
},

=head4 ClientInputFilter

C<ClientInputFilter> is used with C<ClientOutputFilter> to specify
different protocols for input and output. Both must be used together.
Both follow the same usage as L</ClientFilter>.

ClientInputFilter => [ "POE::Filter::Line", Literal => "\n" ],
ClientOutputFilter => 'POE::Filter::Stream',

=head4 ClientOutputFilter

C<ClientOutputFilter> is used with C<ClientInputFilter> to specify
different protocols for input and output. Both must be used together.
Both follow the same usage as L</ClientFilter>.

ClientInputFilter => POE::Filter::Line->new(Literal => "\n"),
ClientOutputFilter => 'POE::Filter::Stream',

=head4 ClientShutdownOnError

C<ClientShutdownOnError> tells the component whether client
connections should be shut down automatically if an error is detected.
It defaults to "true". Setting it to false (0, undef, "") turns off
this feature.

The application is responsible for dealing with client errors if this
feature is disabled. Not doing so may cause the component to emit a
constant stream of errors, eventually bogging down the application
with dead connections that spin out of control.

Yes, this is terrible. You have been warned.

=head4 SessionParams

C<SessionParams> specifies additional parameters that will be passed
to the C<SessionType> constructor at creation time. It must be an
array reference.

SessionParams => [ options => { debug => 1, trace => 1 } ],

Note: POE::Component::Server::TCP supplies its own POE::Session
constructor parameters. Conflicts between them and C<SessionParams>
may cause the component to behave erratically. To avoid such
problems, please limit SessionParams to the C<options> hash. See
L<POE::Session> for an known options.

We may enable other options later. Please let us know if you need
something.

=head4 SessionType

C<SessionType> specifies the POE::Session subclass that will be
created for each new client connection. "POE::Session" is the
default.

SessionType => "POE::Session::MultiDispatch"

=head1 EVENTS

It's possible to manipulate a TCP server component by sending it
messages.

=head2 Main Server Commands

These events must be sent to the main server, usually by the alias set
in its L<Alias> parameter.

=head3 disconnected

The "disconnected" event informs the TCP server that a connection was
closed. It is needed when using L<Concurrency> with an L<Acceptor>
callback. The custom Acceptor must provide its own disconnect
notification so that the server's connection counting logic works.

Otherwise Concurrency clients will be accepted, and then no more. The
server will never know when clients have disconnected.

=head3 set_concurrency

"set_concurrency" set the number of simultaneous connections the
server will be willing to accept. See L<Concurrency> for more
details. "set_concurrency" must have one parameter: the new maximum
connection count.

$kernel->call("my_server_alias", "set_concurrency", $max_count);

=head3 shutdown

The "shutdown" event starts a graceful server shutdown. No new
connections will be accepted. Existing connections will be allowed to
finish. The server will be destroyed after the last connection ends.

=head2 Per-Connection Commands

These commands affect each client connection session.

=head3 shutdown

Sending "shutdown" to an individual client session instructs the
server to gracefully shut down that connection. No new input will be
received, and any buffered output will be sent before the session
ends.

Client sessions usually yield("shutdown") when they wish to disconnect
the client.

ClientInput => sub {
if ($_[ARG0] eq "quit") {
$_[HEAP]{client}->put("B'bye!");
$_[KERNEL]->yield("shutdown");
return;
}

# Handle other input here.
},

=head1 Reserved HEAP Members

Unlike most POE modules, POE::Component::Server::TCP stores data in
the client sessions' HEAPs. These values are provided as conveniences
for application developers.

=head2 HEAP Members for Master Listening Sessions

The master listening session holds different data than client
connections.

=head3 alias

$_[HEAP]{alias} contains the server's Alias.

=head3 concurrency

$_[HEAP]{concurrency} remembers the server's C<Concurrency> parameter.

=head3 connections

$_[HEAP]{connections} is used to track the current number of
concurrent client connections. It's incremented whenever a new
connection is accepted, and it's decremented whenever a client
disconnects.

=head3 listener

$_[HEAP]{listener} contains the POE::Wheel::SocketFactory object used
to listen for connections and accept them.

=head2 HEAP Members for Connection Sessions

These data members exist within the individual connections' sessions.

=head3 client

$_[HEAP]{client} contains a POE::Wheel::ReadWrite object used to
interact with the client. All POE::Wheel::ReadWrite methods work.

=head3 got_an_error

$_[HEAP]{got_an_error} remembers whether the client connection has
already encountered an error. It is part of the shutdown-on-error
procedure.

=head3 remote_ip

$_[HEAP]{remote_ip} contains the remote client's nuermic address in
human-readable form.

=head3 remote_port

$_[HEAP]{remote_port} contains the remote client's numeric socket port
in human-readable form.

=head3 remote_addr

$_[HEAP]{remote_addr} contains the remote client's packed socket
address in computer-readabe form.

=head3 shutdown

$_[HEAP]{shutdown} is true if the client is in the process of shutting
down. The component uses it to ignore client input during shutdown,
and to close the connection after pending output has been flushed.

=head3 shutdown_on_error

$_[HEAP]{shutdown_on_error} remembers whether the client connection
should automatically shut down if an error occurs.

=head1 SEE ALSO

The SEE ALSO section in L<POE> contains a table of contents covering
the entire POE distribution.

L<POE::Component::Client::TCP> is the client-side counterpart to this
module.

This component uses and exposes features from L<POE::Filter>,
L<POE::Wheel::SocketFactory>, and L<POE::Wheel::ReadWrite>.

=head1 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 at once. This may
be a limitation in SocketFactory, but it's not by design.

This component needs better error handling.

Some use cases require different session classes for the listener and
the connection handlers. This isn't currently supported. Please send
patches. :)

TODO - Document that Reuse is set implicitly.

=head1 AUTHORS & COPYRIGHTS

POE::Component::Server::TCP is Copyright 2000-2009 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 E<lt>kudra@domaintje.comE<gt>.

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

=cut

# rocco // vim: ts=2 sw=2 expandtab
# TODO - Edit.
Something went wrong with that request. Please try again.