]>
Dogcows Code - chaz/chatty/blob - extra/cometd/Meteor/Connection.pm
2 ###############################################################################
4 # An HTTP server for the 2.0 web
5 # Copyright (c) 2006 contributing authors
10 # Common super-class for controller and subscriber
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
::Connection
;
33 ###############################################################################
35 ###############################################################################
41 our $MAX_READ_SIZE=8192;
42 our $CONNECTION_WRITE_TIMEOUT=120;
46 ###############################################################################
48 ###############################################################################
49 sub addAllHandleBits
{
56 my @cons=@Connections;
57 map {$_->addHandleBits($rVecRef,$wVecRef,$eVecRef) if(defined($_)) } @cons;
60 sub checkAllHandleBits
{
67 my @cons=@Connections;
68 map {$_->checkHandleBits($rVec,$wVec,$eVec) if(defined($_)) } @cons;
75 sub closeAllConnections
{
76 my @cons=@Connections;
78 map { $_->close(); } @cons;
81 ###############################################################################
83 ###############################################################################
86 # Create a new empty instance
97 # new instance from new server connection
99 my $self=shift-
>new();
101 $::Statistics-
>{'total_requests'}++;
104 my $socket=$server->conSocket();
106 $self->{'socket'}=$socket;
107 $self->{'socketFN'}=$socket->fileno();
109 $socket->setNonBlocking();
111 $self->{'writeBuffer'}='';
112 $self->{'readBuffer'}='';
113 $self->{'bytesWritten'}=0;
114 $self->{'ip'}=$socket->{'connection'}->{'remoteIP'};
116 push(@Connections,$self);
118 &::syslog
('debug',"New %s for %s",ref($self),$socket->{'connection'}->{'remoteIP'});
123 ###############################################################################
125 ###############################################################################
129 $self->{'writeBuffer'}.=shift;
130 $self->{'writeBufferTimestamp'}=time unless(exists($self->{'writeBufferTimestamp'}));
140 my $fno=$self->{'socketFN'};
142 if($self->{'writeBuffer'} ne '')
144 if(exists($self->{'writeBufferTimestamp'}) && $self->{'writeBufferTimestamp'}+$CONNECTION_WRITE_TIMEOUT<time)
146 &::syslog
('debug',"%s for %s: write timed out",ref($self),$self->{'socket'}->{'connection'}->{'remoteIP'});
148 $self->{'writeBuffer'}='';
152 vec($$wVecRef,$fno,1)=1;
155 vec($$rVecRef,$fno,1)=1;
156 vec($$eVecRef,$fno,1)=1;
159 sub checkHandleBits
{
166 my $fno=$self->{'socketFN'};
168 if(vec($eVec,$fno,1))
171 # Something went wrong!
173 $self->exceptionReceived();
178 if(vec($rVec,$fno,1))
181 # Data available for read
183 my $socket=$self->{'socket'};
186 my $bytesRead=sysread($socket->{'handle'},$buffer,$MAX_READ_SIZE);
187 if(defined($bytesRead) && $bytesRead>0)
189 $::Statistics-
>{'total_inbound_bytes'}+=$bytesRead;
190 $self->{'readBuffer'}.=$buffer;
191 while($self->{'readBuffer'}=~s/^([^\r\n]*)\r?\n//)
193 $self->processLine($1);
196 elsif(defined($bytesRead) && $bytesRead==0)
199 $self->{'remoteClosed'}=1;
200 $self->close(1, 'remoteClosed');
208 &::syslog
('notice',"Connection closed: $!");
209 $self->{'remoteClosed'}=1;
210 $self->close(1, 'remoteClosed');
217 if(vec($wVec,$fno,1) && $self->{'writeBuffer'} ne '')
222 my $socket=$self->{'socket'};
224 my $bytesWritten=syswrite($socket->{'handle'},$self->{'writeBuffer'});
226 if(defined($bytesWritten) && $bytesWritten>0)
228 $::Statistics-
>{'total_outbound_bytes'}+=$bytesWritten;
229 $self->{'bytesWritten'}+=$bytesWritten;
230 $self->{'writeBuffer'}=substr($self->{'writeBuffer'},$bytesWritten);
231 if(length($self->{'writeBuffer'})==0)
233 delete($self->{'writeBufferTimestamp'});
234 $self->close(1) if(exists($self->{'autoClose'}));
238 $self->{'writeBufferTimestamp'}=time;
245 &::syslog
('notice',"Connection closed: $!");
246 $self->{'remoteClosed'}=1;
247 $self->close(1, 'remoteClosed');
255 sub exceptionReceived
{
258 $self->{'writeBuffer'}='';
266 #&::syslog('debug',"Close called for %s for %s when write buffer empty",ref($self),$self->{'socket'}->{'connection'}->{'remoteIP'});
268 unless($self->{'remoteClosed'})
270 if(!exists($self->{'autoClose'}) && length($self->{'writeBuffer'})>0)
272 $self->{'autoClose'}=1;
274 &::syslog
('debug',"Will close %s for %s when write buffer empty",ref($self),$self->{'socket'}->{'connection'}->{'remoteIP'});
281 $self->{'socket'}->close();
285 # Remove connection from list of connections
288 my $numcon = scalar(@Connections);
289 for(my $i=0;$i<$numcon;$i++)
291 if($Connections[$i]==$self)
300 splice(@Connections,$idx,1);
303 &::syslog
('debug',"Closed %s for %s",ref($self),$self->{'socket'}->{'connection'}->{'remoteIP'});
312 ############################################################################EOF
This page took 0.053194 seconds and 4 git commands to generate.