Keith Wang
and 1 contributors

NAME

DynGig::Util::TCPServer - A generic multithreaded TCP Server interface.

DESCRIPTION

A server listens for and accepts incoming TCP connections on a port or a Unix domain socket. And a pool of threads are created to handle the connections in parallel. The server code handling the connections is to be implemented in the inheriting class.

A more sophisticated implementation may require serialization in processing requests, e.g. logging. There must be a dedicated worker thread that serially processes requests from the server threads.

Hence _server() is the interface method to be implemented, and _worker() is an optional interface method. If _worker() is inplemented, each _server() thread needs to communicate with it via a pair of queues for two-way communication. e.g.

 sub _server
 {
     my ( $this, $socket, @queue ) = @_;
     ...
 }

 sub _worker
 {
     my ( $this, @queue ) = @_;
     ...
 }

run()

Launches server.

EXAMPLE

 ## an echo server module

 package Echo;
 
 use base DynGig::Util::TCPServer;
 use strict;
 
 use constant MAX_BUF => 2 ** 5;
 
 sub _server
 {
     my ( $this, $socket ) = @_;
     my $buffer;
 
     syswrite( $socket, $buffer ) if sysread( $socket, $buffer, MAX_BUF );
 }

 1;

 __END__


 ## echo server

 use strict;
 use Echo;

 my $server = Echo->new
 ( 
     port => 12345,
     thread => 30,
     listen => 10,
     maxconn => 300,
 );

 $server->run();

SEE ALSO

Socket, threads, Thread::Queue, and IO::Select,

NOTE

See DynGig::Util