3 {io and timer code by plugwash}
\r
5 { Copyright (C) 2005 Bas Steendijk and Peter Green
\r
6 For conditions of distribution and use, see copyright notice in zlib_license.txt
\r
7 which is included in the package
\r
8 ----------------------------------------------------------------------------- }
\r
19 exitloopflag : boolean ; {if set by app, exit mainloop}
\r
21 function getfdsrmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
22 function getfdswmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
32 classes,pgtypes,bfifo,
\r
37 {$include unixstuff.inc}
\r
38 {$include ltimevalstuff.inc}
\r
40 fdreverse:array[0..absoloutemaxs] of tlasio;
\r
42 tselecteventcore=class(teventcore)
\r
44 procedure processmessages; override;
\r
45 procedure messageloop; override;
\r
46 procedure exitmessageloop;override;
\r
47 procedure setfdreverse(fd : integer;reverseto : tlasio); override;
\r
48 procedure rmasterset(fd : integer;islistensocket : boolean); override;
\r
49 procedure rmasterclr(fd: integer); override;
\r
50 procedure wmasterset(fd : integer); override;
\r
51 procedure wmasterclr(fd: integer); override;
\r
54 procedure processtimers;inline;
\r
56 tv ,tvnow : ttimeval ;
\r
57 currenttimer : tltimer ;
\r
58 temptimer : tltimer ;
\r
61 gettimeofday(tvnow);
\r
62 currenttimer := firsttimer;
\r
63 while assigned(currenttimer) do begin
\r
64 //writeln(currenttimer.enabled);
\r
65 if tv_compare(tvnow,ttimeval(currenttimer.nextts)) and currenttimer.enabled then begin
\r
66 //if assigned(currenttimer.ontimer) then begin
\r
67 // if currenttimer.enabled then if currenttimer.initialevent or currenttimer.initialdone then currenttimer.ontimer(currenttimer);
\r
68 // currenttimer.initialdone := true;
\r
70 if assigned(currenttimer.ontimer) then currenttimer.ontimer(currenttimer);
\r
71 currenttimer.nextts := timeval(tvnow);
\r
72 tv_add(ttimeval(currenttimer.nextts),currenttimer.interval);
\r
74 temptimer := currenttimer;
\r
75 currenttimer := currenttimer.nexttimer;
\r
76 if temptimer.released then temptimer.free;
\r
80 procedure processasios(var fdsr,fdsw:fdset);//inline;
\r
82 currentsocket : tlasio ;
\r
83 tempsocket : tlasio ;
\r
84 socketcount : integer ; // for debugging perposes :)
\r
87 { inc(lcoretestcount);}
\r
89 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\r
90 //if (not assigned(firstasin)) and (not assigned(firsttimer)) and (not assigned(firstsignal)) then exit;
\r
93 {------- test optimised loop}
\r
95 for dw := (maxs shr 5) downto 0 do if (fdsr[dw] or fdsw[dw]) <> 0 then begin
\r
96 for bt := 0 to 31 do if (fdsr[dw] or fdsw[dw]) and (1 shl bt) <> 0 then begin
\r
98 currentsocket := fdreverse[dw shl 5 or bt];
\r
99 {if not assigned(currentsocket) then raise exception.create('currentsocket not assigned');
\r
100 if currentsocket.fdhandlein < 0 then raise exception.create('currentsocket.fdhandlein out of range');}
\r
101 {i've seen the out of range case actually happening, so it can happen. test: just close the fd - beware}
\r
102 if not assigned(currentsocket) then begin
\r
103 fdclose(dw shl 5 or bt);
\r
106 if currentsocket.fdhandlein < 0 then begin
\r
107 fdclose(dw shl 5 or bt);
\r
111 currentsocket.handlefdtrigger(fd_isset(currentsocket.fdhandlein,fdsr),fd_isset(currentsocket.fdhandleout,fdsw));
\r
113 on E: exception do begin
\r
114 currentsocket.HandleBackGroundException(e);
\r
118 if mustrefreshfds then begin
\r
119 if select(maxs+1,@fdsr,@fdsw,nil,0) <= 0 then begin
\r
127 if asinreleaseflag then begin
\r
128 asinreleaseflag := false;
\r
129 currentsocket := firstasin;
\r
130 while assigned(currentsocket) do begin
\r
131 tempsocket := currentsocket;
\r
132 currentsocket := currentsocket.nextasin;
\r
133 if tempsocket.released then begin
\r
140 - sockets which are released may not be freed because theyre never processed by the loop
\r
141 made new code for handling this, using asinreleaseflag
\r
143 - when/why does the mustrefreshfds select apply, sheck if i did it correctly?
\r
145 - what happens if calling handlefdtrigger for a socket which does not have an event
\r
147 {------- original loop}
\r
150 currentsocket := firstasin;
\r
152 while assigned(currentsocket) do begin
\r
153 if mustrefreshfds then begin
\r
154 if select(maxs,@fdsr,@fdsw,nil,0) <= 0 then begin
\r
160 if fd_isset(currentsocket.fdhandlein,fdsr) or fd_isset(currentsocket.fdhandleout,fdsw) then begin
\r
161 currentsocket.handlefdtrigger(fd_isset(currentsocket.fdhandlein,fdsr),fd_isset(currentsocket.fdhandleout,fdsw));
\r
164 on E: exception do begin
\r
165 currentsocket.HandleBackGroundException(e);
\r
168 tempsocket := currentsocket;
\r
169 currentsocket := currentsocket.nextasin;
\r
171 if tempsocket.released then begin
\r
175 { debugout('socketcount='+inttostr(socketcount));}
\r
178 procedure tselecteventcore.processmessages;
\r
180 fdsr , fdsw : fdset ;
\r
181 selectresult : longint ;
\r
183 mustrefreshfds := false;
\r
187 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
188 while (selectresult>0) or assigned(firsttask) or assigned(currenttask) do begin;
\r
192 if selectresult > 0 then begin
\r
193 processasios(fdsr,fdsw);
\r
195 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
198 mustrefreshfds := true;
\r
203 FDSR , FDSW : fdset;
\r
205 Function doSelect(timeOut:PTimeVal):longint;//inline;
\r
207 localtimeval : ttimeval;
\r
208 maxslocal : integer;
\r
211 //zeromemory(@sset,sizeof(sset));
\r
213 fdsr := getfdsrmaster;
\r
214 fdsw := getfdswmaster;
\r
216 if assigned(firsttask) then begin
\r
217 localtimeval.tv_sec := 0;
\r
218 localtimeval.tv_usec := 0;
\r
219 timeout := @localtimeval;
\r
223 mustrefreshfds := false;
\r
224 { debugout('about to call select');}
\r
226 sigprocmask(SIG_UNBLOCK,@blockset,nil);
\r
228 result := select(maxslocal+1,@FDSR,@FDSW,nil,timeout);
\r
229 if result <= 0 then begin
\r
232 if result=-1 then begin
\r
233 if linuxerror = SYS_EINTR then begin
\r
234 // we received a signal it's not a problem
\r
236 raise esocketexception.create('select returned error '+inttostr(linuxerror));
\r
241 sigprocmask(SIG_BLOCK,@blockset,nil);
\r
243 { debugout('select complete');}
\r
246 procedure tselecteventcore.exitmessageloop;
\r
248 exitloopflag := true
\r
253 procedure tselecteventcore.messageloop;
\r
255 tv ,tvnow : ttimeval ;
\r
256 currenttimer : tltimer ;
\r
257 selectresult:integer;
\r
262 {currentsocket := firstasin;
\r
263 if not assigned(currentsocket) then exit; //the message loop will exit if all lsockets are destroyed
\r
266 if currentsocket.state = wsconnected then currentsocket.sendflush;
\r
267 currentsocket := currentsocket.nextasin;
\r
268 until not assigned(currentsocket);}
\r
273 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\r
274 if (not assigned(firstasin)) and (not assigned(firsttimer)) {$ifndef nosignal} and (not assigned(firstsignal)){$endif} then exit;
\r
277 currentsocket := firstasin;
\r
278 if not assigned(currentsocket) then exit; //the message loop will exit if all lsockets are destroyed
\r
281 if (not currentsocket.released) and (currentsocket.state<>wsclosed) then fd_set(currentsocket.fdhandlein,fdsr);
\r
282 if (not currentsocket.released) and (currentsocket.state=wsconnecting) then fd_set(currentsocket.fdhandleout,fdsw);
\r
283 if currentsocket is tlsocket then begin
\r
284 if (not currentsocket.released) and (currentsocket.state=wsconnected) and(tlsocket(currentsocket).sendq <> '') then fd_set(currentsocket.fdhandleout,fdsw);
\r
286 tempsocket := currentsocket;
\r
287 currentsocket := currentsocket.nextasin;
\r
288 if tempsocket.released then begin
\r
291 until not assigned(currentsocket);
\r
294 //currenttask := nil;
\r
296 //if assigned(firsttimer) then begin
\r
297 // tv.tv_sec := maxlongint;
\r
298 tv := tv_invalidtimebig;
\r
299 currenttimer := firsttimer;
\r
300 while assigned(currenttimer) do begin
\r
301 if tv_compare(tv,currenttimer.nextts) and currenttimer.enabled then tv := currenttimer.nextts;
\r
302 currenttimer := currenttimer.nexttimer;
\r
306 if tv_compare(tv,tv_invalidtimebig) then begin
\r
307 //writeln('no timers active');
\r
308 if exitloopflag then break;
\r
310 selectresult := doselect(nil);
\r
313 gettimeofday(tvnow);
\r
314 tv_substract(tv,tvnow);
\r
316 //writeln('timers active');
\r
317 if tv.tv_sec < 0 then begin
\r
319 tv.tv_usec := 0; {0.1 sec}
\r
321 if exitloopflag then break;
\r
323 selectresult := doselect(@tv);
\r
327 if selectresult > 0 then processasios(fdsr,fdsw);
\r
328 {!!!only call processasios if select has asio events -beware}
\r
330 {artificial delay to throttle the number of processasios per second possible and reduce cpu usage}
\r
335 fdsrmaster , fdswmaster : fdset ;
\r
337 procedure tselecteventcore.rmasterset(fd : integer;islistensocket : boolean);
\r
339 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
340 if fd > maxs then maxs := fd;
\r
341 if fd_isset(fd,fdsrmaster) then exit;
\r
342 fd_set(fd,fdsrmaster);
\r
346 procedure tselecteventcore.rmasterclr(fd: integer);
\r
348 if not fd_isset(fd,fdsrmaster) then exit;
\r
349 fd_clr(fd,fdsrmaster);
\r
354 procedure tselecteventcore.wmasterset(fd : integer);
\r
356 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
357 if fd > maxs then maxs := fd;
\r
359 if fd_isset(fd,fdswmaster) then exit;
\r
360 fd_set(fd,fdswmaster);
\r
364 procedure tselecteventcore.wmasterclr(fd: integer);
\r
366 if not fd_isset(fd,fdswmaster) then exit;
\r
367 fd_clr(fd,fdswmaster);
\r
370 procedure tselecteventcore.setfdreverse(fd : integer;reverseto : tlasio);
\r
372 fdreverse[fd] := reverseto;
\r
375 function getfdsrmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
377 result := fdsrmaster;
\r
379 function getfdswmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
381 result := fdswmaster;
\r
386 eventcore := tselecteventcore.create;
\r
389 fd_zero(fdsrmaster);
\r
390 fd_zero(fdswmaster);
\r