]>
Dogcows Code - chaz/chatty/blob - extra/cometd/Meteor/Socket.pm
2 ###############################################################################
4 # An HTTP server for the 2.0 web
5 # Copyright (c) 2006 contributing authors
10 # Meteor socket additions
12 ###############################################################################
14 # This program is free software; you can redistribute it and/or modify it
15 # under the terms of the GNU General Public License as published by the Free
16 # Software Foundation; either version 2 of the License, or (at your option)
19 # This program is distributed in the hope that it will be useful, but WITHOUT
20 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
24 # You should have received a copy of the GNU General Public License along
25 # with this program; if not, write to the Free Software Foundation, Inc.,
26 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 # For more information visit www.meteorserver.org
30 ###############################################################################
32 package Meteor
::Socket
;
33 ###############################################################################
35 ###############################################################################
40 use Fcntl
qw(F_GETFL F_SETFL O_NONBLOCK);
44 $Meteor::Socket
::handleNum
=0;
46 # Cache getprotobyname result as on some systems it is slow.
47 $Meteor::Socket
::TCP_PROTO_NAME
=getprotobyname('tcp');
48 $Meteor::Socket
::UDP_PROTO_NAME
=getprotobyname('udp');
51 ###############################################################################
53 ###############################################################################
75 $self->{'handle'}=shift;
78 vec($vec,CORE
::fileno($self->{'handle'}),1)=1;
79 $self->{'handleVec'}=$vec;
82 ($timeout) && ($self->{'timeout'}=$timeout);
88 my($class,$port,$queueSize,$srcIP)=@_;
90 ($port) || die("$class: port undefined!");
96 my $localAdr=INADDR_ANY
;
97 $localAdr=inet_aton
($srcIP) if(defined($srcIP) && $srcIP ne '');
100 my $sockType=AF_INET
;
101 my $proto=$Meteor::Socket
::TCP_PROTO_NAME
;
103 $self->{'port'}=$port;
104 ($local=sockaddr_in
($port,$localAdr))
105 || die("$class: sockaddr_in for port '$port' failed");
107 $self->{'handle'}=$self->nextHandle();
108 $self->{'socketType'}=$sockType;
110 socket($self->{'handle'},$sockType,SOCK_STREAM
,$proto)
111 || die("$class socket: $!");
113 setsockopt($self->{'handle'},SOL_SOCKET
,SO_REUSEADDR
,1);
115 bind($self->{'handle'},$local)
116 || die("$class bind: $!");
117 listen($self->{'handle'},$queueSize)
118 || die("$class listen: $!");
120 select((select($self->{'handle'}),$|=1)[0]);
123 vec($vec,CORE
::fileno($self->{'handle'}),1)=1;
124 $self->{'handleVec'}=$vec;
130 my($class,$port,$srcIP)=@_;
132 ($port) || die("$class: port undefined!");
134 my $self=$class->new;
136 my $localAdr=INADDR_ANY
;
137 $localAdr=inet_aton
($srcIP) if(defined($srcIP) && $srcIP ne '');
140 my $sockType=PF_INET
;
141 my $proto=$Meteor::Socket
::UDP_PROTO_NAME
;
143 $self->{'port'}=$port;
144 ($local=sockaddr_in
($port,$localAdr))
145 || die("$class: sockaddr_in for port '$port' failed");
147 $self->{'handle'}=$self->nextHandle();
148 $self->{'socketType'}=$sockType;
150 socket($self->{'handle'},$sockType,SOCK_DGRAM
,$proto)
151 || die("$class socket: $!");
153 setsockopt($self->{'handle'},SOL_SOCKET
,SO_REUSEADDR
,pack("l", 1))
154 || die("setsockopt: $!");
156 bind($self->{'handle'},$local)
157 || die("$class bind: $!");
159 select((select($self->{'handle'}),$|=1)[0]);
162 vec($vec,CORE
::fileno($self->{'handle'}),1)=1;
163 $self->{'handleVec'}=$vec;
168 ###############################################################################
170 ###############################################################################
174 if(exists($self->{'handle'}))
176 warn("$self->DESTROY caught unclosed socket")
177 unless($Meteor::Socket
::NO_WARN_ON_CLOSE
);
185 my $handle=$self->nextHandle();
188 $paddr=&saccept
($handle,$self->{'handle'}) || die($!);
190 select((select($handle),$|=1)[0]);
192 my $newSock=Meteor
::Socket-
>newWithHandle($handle,20);
193 $newSock->{'socketType'}=$self->{'socketType'};
194 if($self->{'socketType'}==AF_INET
)
196 my($port,$iaddr)=unpack_sockaddr_in
($paddr);
198 $newSock->{'connection'}->{'port'}=$port;
199 $newSock->{'connection'}->{'remoteIP'}=inet_ntoa
($iaddr);
208 my $flags=fcntl($self->{'handle'},F_GETFL
,0)
209 or die("Can't get flags for the socket: $!");
210 fcntl($self->{'handle'},F_SETFL
,$flags|O_NONBLOCK
)
211 or die("Can't set flags for the socket: $!");
217 if(exists($self->{'handle'}))
219 close($self->{'handle'});
220 delete($self->{'handle'});
224 ###############################################################################
226 ###############################################################################
230 my $name='MSHandle'.$Meteor::Socket
::handleNum
++;
231 my $pack='Meteor::Socket::';
232 my $handle=\
*{$pack.$name};
233 delete $$pack{$name};
244 $result=CORE
::select($_[0],$_[1],$_[2],$to);
247 last unless(${!}==EINTR
);
248 return 0 if($::HUP
|| $::TERM
|| $::USR1
|| $::USR2
);
264 my($dhandle,$shandle)=@_;
269 $result=CORE
::accept($dhandle,$shandle);
272 last unless(${!}==EINTR
);
273 return 0 if($::HUP
|| $::TERM
|| $::USR1
|| $::USR2
);
285 CORE
::fileno(shift-
>{'handle'});
289 ############################################################################EOF
This page took 0.057497 seconds and 4 git commands to generate.