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
|
#!/usr/bin/perl -w -T
###----------------------------------------###
### sample udp server class ###
###----------------------------------------###
package MyUDPD;
use lib qw(/home/rhandom/Net-Server/lib);
use vars qw(@ISA);
use strict;
### what type of server is this - we could
### use multi type when we add command line
### parsing to this http server to allow
### for different configurations
use Net::Server::PreFork;
@ISA = qw(Net::Server::PreFork);
### run the server
MyUDPD->run( port => '20203/udp',
# we could also do the following:
# port => '*:20203/udp',
# port => 'somehost:20203/udp',
# port => '20203/udp', port => '20204/udp',
# port => '20203/udp', port => '20203/tcp',
);
exit;
### set up some server parameters
sub configure_hook {
my $self = shift;
### change the packet len?
# $self->{server}->{udp_recv_len} ||= 2048; # default is 4096
}
### this is the main method to override
### this is where most of the work will occur
### A sample server is shown below.
sub process_request {
my $self = shift;
my $prop = $self->{server};
### if we were writing a server that did both tcp and udp,
### we would need to check $prop->{udp_true} to see
### if the current connection is udp or not
# if( $prop->{udp_true} ){
# # yup, this is udp
# }
if( $prop->{udp_data} =~ /dump/ ){
require "Data/Dumper.pm";
$prop->{client}->send( Data::Dumper::Dumper( $self ) , 0);
}else{
$prop->{client}->send( "You said \"$prop->{udp_data}\"", 0);
}
return;
}
|