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
24 baseunix,unix,unixutil,
\r
29 exitloopflag : boolean ; {if set by app, exit mainloop}
\r
31 function getfdsrmaster : fdset; {$ifdef useinline}inline;{$endif}
\r
32 function getfdswmaster : fdset; {$ifdef useinline}inline;{$endif}
\r
37 classes,pgtypes,bfifo,
\r
42 {$include unixstuff.inc}
\r
43 {$include ltimevalstuff.inc}
\r
45 fdreverse:array[0..absoloutemaxs] of tlasio;
\r
47 tselecteventcore=class(teventcore)
\r
49 procedure processmessages; override;
\r
50 procedure messageloop; override;
\r
51 procedure exitmessageloop;override;
\r
52 procedure setfdreverse(fd : integer;reverseto : tlasio); override;
\r
53 procedure rmasterset(fd : integer;islistensocket : boolean); override;
\r
54 procedure rmasterclr(fd: integer); override;
\r
55 procedure wmasterset(fd : integer); override;
\r
56 procedure wmasterclr(fd: integer); override;
\r
59 procedure processtimers;inline;
\r
61 tv ,tvnow : ttimeval ;
\r
62 currenttimer : tltimer ;
\r
63 temptimer : tltimer ;
\r
66 gettimeofday(tvnow);
\r
67 currenttimer := firsttimer;
\r
68 while assigned(currenttimer) do begin
\r
69 //writeln(currenttimer.enabled);
\r
70 if tv_compare(tvnow,ttimeval(currenttimer.nextts)) and currenttimer.enabled then begin
\r
71 //if assigned(currenttimer.ontimer) then begin
\r
72 // if currenttimer.enabled then if currenttimer.initialevent or currenttimer.initialdone then currenttimer.ontimer(currenttimer);
\r
73 // currenttimer.initialdone := true;
\r
75 if assigned(currenttimer.ontimer) then currenttimer.ontimer(currenttimer);
\r
76 currenttimer.nextts := timeval(tvnow);
\r
77 tv_add(ttimeval(currenttimer.nextts),currenttimer.interval);
\r
79 temptimer := currenttimer;
\r
80 currenttimer := currenttimer.nexttimer;
\r
81 if temptimer.released then temptimer.free;
\r
85 procedure processasios(var fdsr,fdsw:fdset);//inline;
\r
87 currentsocket : tlasio ;
\r
88 tempsocket : tlasio ;
\r
89 socketcount : integer ; // for debugging perposes :)
\r
92 { inc(lcoretestcount);}
\r
94 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\r
95 //if (not assigned(firstasin)) and (not assigned(firsttimer)) and (not assigned(firstsignal)) then exit;
\r
98 {------- test optimised loop}
\r
100 for dw := (maxs shr 5) downto 0 do if (fdsr[dw] or fdsw[dw]) <> 0 then begin
\r
101 for bt := 0 to 31 do if (fdsr[dw] or fdsw[dw]) and (1 shl bt) <> 0 then begin
\r
103 currentsocket := fdreverse[dw shl 5 or bt];
\r
104 {if not assigned(currentsocket) then raise exception.create('currentsocket not assigned');
\r
105 if currentsocket.fdhandlein < 0 then raise exception.create('currentsocket.fdhandlein out of range');}
\r
106 {i've seen the out of range case actually happening, so it can happen. test: just close the fd - beware}
\r
107 if not assigned(currentsocket) then begin
\r
108 fdclose(dw shl 5 or bt);
\r
111 if currentsocket.fdhandlein < 0 then begin
\r
112 fdclose(dw shl 5 or bt);
\r
116 currentsocket.handlefdtrigger(fd_isset(currentsocket.fdhandlein,fdsr),fd_isset(currentsocket.fdhandleout,fdsw));
\r
118 on E: exception do begin
\r
119 currentsocket.HandleBackGroundException(e);
\r
123 if mustrefreshfds then begin
\r
124 if select(maxs+1,@fdsr,@fdsw,nil,0) <= 0 then begin
\r
132 if asinreleaseflag then begin
\r
133 asinreleaseflag := false;
\r
134 currentsocket := firstasin;
\r
135 while assigned(currentsocket) do begin
\r
136 tempsocket := currentsocket;
\r
137 currentsocket := currentsocket.nextasin;
\r
138 if tempsocket.released then begin
\r
145 - sockets which are released may not be freed because theyre never processed by the loop
\r
146 made new code for handling this, using asinreleaseflag
\r
148 - when/why does the mustrefreshfds select apply, sheck if i did it correctly?
\r
150 - what happens if calling handlefdtrigger for a socket which does not have an event
\r
152 {------- original loop}
\r
155 currentsocket := firstasin;
\r
157 while assigned(currentsocket) do begin
\r
158 if mustrefreshfds then begin
\r
159 if select(maxs,@fdsr,@fdsw,nil,0) <= 0 then begin
\r
165 if fd_isset(currentsocket.fdhandlein,fdsr) or fd_isset(currentsocket.fdhandleout,fdsw) then begin
\r
166 currentsocket.handlefdtrigger(fd_isset(currentsocket.fdhandlein,fdsr),fd_isset(currentsocket.fdhandleout,fdsw));
\r
169 on E: exception do begin
\r
170 currentsocket.HandleBackGroundException(e);
\r
173 tempsocket := currentsocket;
\r
174 currentsocket := currentsocket.nextasin;
\r
176 if tempsocket.released then begin
\r
180 { debugout('socketcount='+inttostr(socketcount));}
\r
183 procedure tselecteventcore.processmessages;
\r
185 fdsr , fdsw : fdset ;
\r
186 selectresult : longint ;
\r
188 mustrefreshfds := false;
\r
192 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
193 while (selectresult>0) or assigned(firsttask) or assigned(currenttask) do begin;
\r
197 if selectresult > 0 then begin
\r
198 processasios(fdsr,fdsw);
\r
200 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
203 mustrefreshfds := true;
\r
208 FDSR , FDSW : fdset;
\r
211 fdsrmaster , fdswmaster : fdset ;
\r
213 function getfdsrmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
215 result := fdsrmaster;
\r
217 function getfdswmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
219 result := fdswmaster;
\r
223 Function doSelect(timeOut:PTimeVal):longint;//inline;
\r
225 localtimeval : ttimeval;
\r
226 maxslocal : integer;
\r
229 //zeromemory(@sset,sizeof(sset));
\r
231 fdsr := getfdsrmaster;
\r
232 fdsw := getfdswmaster;
\r
234 if assigned(firsttask) then begin
\r
235 localtimeval.tv_sec := 0;
\r
236 localtimeval.tv_usec := 0;
\r
237 timeout := @localtimeval;
\r
241 mustrefreshfds := false;
\r
242 { debugout('about to call select');}
\r
244 sigprocmask(SIG_UNBLOCK,@blockset,nil);
\r
246 result := select(maxslocal+1,@FDSR,@FDSW,nil,timeout);
\r
247 if result <= 0 then begin
\r
250 if result=-1 then begin
\r
251 if linuxerror = SYS_EINTR then begin
\r
252 // we received a signal it's not a problem
\r
254 raise esocketexception.create('select returned error '+inttostr(linuxerror));
\r
259 sigprocmask(SIG_BLOCK,@blockset,nil);
\r
261 { debugout('select complete');}
\r
264 procedure tselecteventcore.exitmessageloop;
\r
266 exitloopflag := true
\r
271 procedure tselecteventcore.messageloop;
\r
273 tv ,tvnow : ttimeval ;
\r
274 currenttimer : tltimer ;
\r
275 selectresult:integer;
\r
280 {currentsocket := firstasin;
\r
281 if not assigned(currentsocket) then exit; //the message loop will exit if all lsockets are destroyed
\r
284 if currentsocket.state = wsconnected then currentsocket.sendflush;
\r
285 currentsocket := currentsocket.nextasin;
\r
286 until not assigned(currentsocket);}
\r
291 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\r
292 if (not assigned(firstasin)) and (not assigned(firsttimer)) {$ifndef nosignal} and (not assigned(firstsignal)){$endif} then exit;
\r
295 currentsocket := firstasin;
\r
296 if not assigned(currentsocket) then exit; //the message loop will exit if all lsockets are destroyed
\r
299 if (not currentsocket.released) and (currentsocket.state<>wsclosed) then fd_set(currentsocket.fdhandlein,fdsr);
\r
300 if (not currentsocket.released) and (currentsocket.state=wsconnecting) then fd_set(currentsocket.fdhandleout,fdsw);
\r
301 if currentsocket is tlsocket then begin
\r
302 if (not currentsocket.released) and (currentsocket.state=wsconnected) and(tlsocket(currentsocket).sendq <> '') then fd_set(currentsocket.fdhandleout,fdsw);
\r
304 tempsocket := currentsocket;
\r
305 currentsocket := currentsocket.nextasin;
\r
306 if tempsocket.released then begin
\r
309 until not assigned(currentsocket);
\r
312 //currenttask := nil;
\r
314 //if assigned(firsttimer) then begin
\r
315 // tv.tv_sec := maxlongint;
\r
316 tv := tv_invalidtimebig;
\r
317 currenttimer := firsttimer;
\r
318 while assigned(currenttimer) do begin
\r
319 if tv_compare(tv,currenttimer.nextts) and currenttimer.enabled then tv := currenttimer.nextts;
\r
320 currenttimer := currenttimer.nexttimer;
\r
324 if tv_compare(tv,tv_invalidtimebig) then begin
\r
325 //writeln('no timers active');
\r
326 if exitloopflag then break;
\r
328 selectresult := doselect(nil);
\r
331 gettimeofday(tvnow);
\r
332 tv_substract(tv,tvnow);
\r
334 //writeln('timers active');
\r
335 if tv.tv_sec < 0 then begin
\r
337 tv.tv_usec := 0; {0.1 sec}
\r
339 if exitloopflag then break;
\r
341 selectresult := doselect(@tv);
\r
345 if selectresult > 0 then processasios(fdsr,fdsw);
\r
346 {!!!only call processasios if select has asio events -beware}
\r
348 {artificial delay to throttle the number of processasios per second possible and reduce cpu usage}
\r
353 procedure tselecteventcore.rmasterset(fd : integer;islistensocket : boolean);
\r
355 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
356 if fd > maxs then maxs := fd;
\r
357 if fd_isset(fd,fdsrmaster) then exit;
\r
358 fd_set(fd,fdsrmaster);
\r
362 procedure tselecteventcore.rmasterclr(fd: integer);
\r
364 if not fd_isset(fd,fdsrmaster) then exit;
\r
365 fd_clr(fd,fdsrmaster);
\r
370 procedure tselecteventcore.wmasterset(fd : integer);
\r
372 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
373 if fd > maxs then maxs := fd;
\r
375 if fd_isset(fd,fdswmaster) then exit;
\r
376 fd_set(fd,fdswmaster);
\r
380 procedure tselecteventcore.wmasterclr(fd: integer);
\r
382 if not fd_isset(fd,fdswmaster) then exit;
\r
383 fd_clr(fd,fdswmaster);
\r
386 procedure tselecteventcore.setfdreverse(fd : integer;reverseto : tlasio);
\r
388 fdreverse[fd] := reverseto;
\r
394 eventcore := tselecteventcore.create;
\r
397 fd_zero(fdsrmaster);
\r
398 fd_zero(fdswmaster);
\r