Debian Codegraph @

lib/Danga/Client.pm

raw
1 # $Id: Client.pm,v 1.8 2005/02/14 22:06:38 msergeant Exp $
2
3 package Danga::Client;
4 use base 'Danga::TimeoutSocket';
5 use fields qw(
6 line
7 pause_count
8 read_bytes
9 data_bytes
10 callback
11 get_chunks
12 reader_object
13 );
14 use Time::HiRes ();
15
16 use bytes;
17
18 # 30 seconds max timeout!
19 sub max_idle_time { 30 }
20 sub max_connect_time { 1200 }
21
22 sub new {
23 my Danga::Client $self = shift;
24 $self = fields::new($self) unless ref $self;
25 $self->SUPER::new(@_);
26
27 $self->reset_for_next_message;
28 return $self;
29 }
30
31 sub reset_for_next_message {
32 my Danga::Client $self = shift;
33 $self->{line} = '';
34 $self->{pause_count} = 0;
35 $self->{read_bytes} = 0;
36 $self->{callback} = undef;
37 $self->{reader_object} = undef;
38 $self->{data_bytes} = '';
39 $self->{get_chunks} = 0;
40 return $self;
41 }
42
43 sub get_bytes {
44 my Danga::Client $self = shift;
45 my ($bytes, $callback) = @_;
46 if ($self->{callback}) {
47 die "get_bytes/get_chunks currently in progress!";
48 }
49 $self->{read_bytes} = $bytes;
50 $self->{data_bytes} = $self->{line};
51 $self->{read_bytes} -= length($self->{data_bytes});
52 $self->{line} = '';
53 if ($self->{read_bytes} <= 0) {
54 if ($self->{read_bytes} < 0) {
55 $self->{line} = substr(
56 $self->{data_bytes},
57 $self->{read_bytes}, # negative offset
58 0 - $self->{read_bytes}, # to end of str
59 ""
60 ); # truncate that substr
61 }
62 $callback->($self->{data_bytes});
63 return;
64 }
65 $self->{callback} = $callback;
66 }
67
68 sub process_chunk {
69 my Danga::Client $self = shift;
70 my $callback = shift;
71
72 my $last_crlf = rindex($self->{line}, "\r\n");
73
74 if ($last_crlf != -1) {
75 if ($last_crlf + 2 == length($self->{line})) {
76 my $data = $self->{line};
77 $self->{line} = '';
78 $callback->($data);
79 }
80 else {
81 my $data = substr($self->{line}, 0, $last_crlf + 2);
82 $self->{line} = substr($self->{line}, $last_crlf + 2);
83 $callback->($data);
84 }
85 }
86 }
87
88 sub get_chunks {
89 my Danga::Client $self = shift;
90 my ($bytes, $callback) = @_;
91 if ($self->{callback}) {
92 die "get_bytes/get_chunks currently in progress!";
93 }
94 $self->{read_bytes} = $bytes;
95 $self->process_chunk($callback) if length($self->{line});
96 $self->{callback} = $callback;
97 $self->{get_chunks} = 1;
98 }
99
100 sub end_get_chunks {
101 my Danga::Client $self = shift;
102 my $remaining = shift;
103 $self->{callback} = undef;
104 $self->{get_chunks} = 0;
105 if (defined($remaining)) {
106 $self->process_read_buf(\$remaining);
107 }
108 }
109
110 sub set_reader_object {
111 my Danga::Client $self = shift;
112 $self->{reader_object} = shift;
113 }
114
115 sub event_read {
116 my Danga::Client $self = shift;
117 if (my $obj = $self->{reader_object}) {
118 $self->{reader_object} = undef;
119 $obj->event_read($self);
120 }
121 elsif ($self->{callback}) {
122 $self->{alive_time} = time;
123 if ($self->{get_chunks}) {
124 my $bref = $self->read($self->{read_bytes});
125 return $self->close($!) unless defined $bref;
126 $self->{line} .= $$bref;
127 $self->process_chunk($self->{callback}) if length($self->{line});
128 return;
129 }
130 if ($self->{read_bytes} > 0) {
131 my $bref = $self->read($self->{read_bytes});
132 return $self->close($!) unless defined $bref;
133 $self->{read_bytes} -= length($$bref);
134 $self->{data_bytes} .= $$bref;
135 }
136 if ($self->{read_bytes} <= 0) {
137
138 # print "Erk, read too much!\n" if $self->{read_bytes} < 0;
139 my $cb = $self->{callback};
140 $self->{callback} = undef;
141 $cb->($self->{data_bytes});
142 }
143 }
144 else {
145 my $bref = $self->read(8192);
146 return $self->close($!) unless defined $bref;
147 $self->process_read_buf($bref);
148 }
149 }
150
151 sub process_read_buf {
152 my Danga::Client $self = shift;
153 my $bref = shift;
154 $self->{line} .= $$bref;
155 return if $self->{pause_count} || $self->{closed};
156
157 if ($self->{line} =~ s/^(.*?\n)//) {
158 my $line = $1;
159 $self->{alive_time} = time;
160 my $resp = $self->process_line($line);
161 if ($::DEBUG > 1 and $resp) {
162 print "$$:" . ($self + 0) . "S: $_\n" for split(/\n/, $resp);
163 }
164 $self->write($resp) if $resp;
165
166 # $self->watch_read(0) if $self->{pause_count};
167 return if $self->{pause_count} || $self->{closed};
168
169 # read more in a timer, to give other clients a look in
170 $self->AddTimer(
171 0,
172 sub {
173 if (length($self->{line}) && !$self->paused) {
174 $self->process_read_buf(\"")
175 ; # " for bad syntax highlighters
176 }
177 }
178 );
179 }
180 }
181
182 sub has_data {
183 my Danga::Client $self = shift;
184 return length($self->{line}) ? 1 : 0;
185 }
186
187 sub clear_data {
188 my Danga::Client $self = shift;
189 $self->{line} = '';
190 }
191
192 sub paused {
193 my Danga::Client $self = shift;
194 return 1 if $self->{pause_count};
195 return 1 if $self->{closed};
196 return 0;
197 }
198
199 sub pause_read {
200 my Danga::Client $self = shift;
201 $self->{pause_count}++;
202
203 # $self->watch_read(0);
204 }
205
206 sub continue_read {
207 my Danga::Client $self = shift;
208 $self->{pause_count}--;
209 if ($self->{pause_count} <= 0) {
210 $self->{pause_count} = 0;
211 $self->AddTimer(
212 0,
213 sub {
214 if (length($self->{line}) && !$self->paused) {
215 $self->process_read_buf(\"")
216 ; # " for bad syntax highlighters
217 }
218 }
219 );
220 }
221 }
222
223 sub process_line {
224 my Danga::Client $self = shift;
225 return '';
226 }
227
228 sub close {
229 my Danga::Client $self = shift;
230 print "closing @_\n" if $::DEBUG;
231 $self->SUPER::close(@_);
232 }
233
234 sub event_err { my Danga::Client $self = shift; $self->close("Error") }
235
236 sub event_hup {
237 my Danga::Client $self = shift;
238 $self->close("Disconnect (HUP)");
239 }
240
241 1;