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
34 procedure lcoreinit;
\r
39 classes,pgtypes,bfifo,
\r
45 {$include unixstuff.inc}
\r
48 absoloutemaxs_select = (sizeof(fdset)*8)-1;
\r
51 fdreverse:array[0..absoloutemaxs_select] of tlasio;
\r
53 tselecteventcore=class(teventcore)
\r
55 procedure processmessages; override;
\r
56 procedure messageloop; override;
\r
57 procedure exitmessageloop;override;
\r
58 procedure setfdreverse(fd : integer;reverseto : tlasio); override;
\r
59 procedure rmasterset(fd : integer;islistensocket : boolean); override;
\r
60 procedure rmasterclr(fd: integer); override;
\r
61 procedure wmasterset(fd : integer); override;
\r
62 procedure wmasterclr(fd: integer); override;
\r
65 procedure processtimers;inline;
\r
67 tv ,tvnow : ttimeval ;
\r
68 currenttimer : tltimer ;
\r
69 temptimer : tltimer ;
\r
72 gettimeofday(tvnow);
\r
73 currenttimer := firsttimer;
\r
74 while assigned(currenttimer) do begin
\r
75 //writeln(currenttimer.enabled);
\r
76 if tv_compare(tvnow,ttimeval(currenttimer.nextts)) and currenttimer.enabled then begin
\r
77 //if assigned(currenttimer.ontimer) then begin
\r
78 // if currenttimer.enabled then if currenttimer.initialevent or currenttimer.initialdone then currenttimer.ontimer(currenttimer);
\r
79 // currenttimer.initialdone := true;
\r
81 if assigned(currenttimer.ontimer) then currenttimer.ontimer(currenttimer);
\r
82 currenttimer.nextts := timeval(tvnow);
\r
83 tv_add(ttimeval(currenttimer.nextts),currenttimer.interval);
\r
85 temptimer := currenttimer;
\r
86 currenttimer := currenttimer.nexttimer;
\r
90 procedure processasios(var fdsr,fdsw:fdset);//inline;
\r
92 currentsocket : tlasio ;
\r
93 tempsocket : tlasio ;
\r
94 socketcount : integer ; // for debugging perposes :)
\r
97 { inc(lcoretestcount);}
\r
99 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\r
100 //if (not assigned(firstasin)) and (not assigned(firsttimer)) and (not assigned(firstsignal)) then exit;
\r
103 {------- test optimised loop}
\r
105 for dw := (maxs shr 5) downto 0 do if (fdsr[dw] or fdsw[dw]) <> 0 then begin
\r
106 for bt := 0 to 31 do if (fdsr[dw] or fdsw[dw]) and (1 shl bt) <> 0 then begin
\r
108 currentsocket := fdreverse[dw shl 5 or bt];
\r
109 {if not assigned(currentsocket) then raise exception.create('currentsocket not assigned');
\r
110 if currentsocket.fdhandlein < 0 then raise exception.create('currentsocket.fdhandlein out of range');}
\r
111 {i've seen the out of range case actually happening, so it can happen. test: just close the fd - beware}
\r
112 if not assigned(currentsocket) then begin
\r
113 fdclose(dw shl 5 or bt);
\r
116 if currentsocket.fdhandlein < 0 then begin
\r
117 fdclose(dw shl 5 or bt);
\r
121 currentsocket.handlefdtrigger(fd_isset(currentsocket.fdhandlein,fdsr),fd_isset(currentsocket.fdhandleout,fdsw));
\r
123 on E: exception do begin
\r
124 currentsocket.HandleBackGroundException(e);
\r
128 if mustrefreshfds then begin
\r
129 if select(maxs+1,@fdsr,@fdsw,nil,0) <= 0 then begin
\r
139 - sockets which are released may not be freed because theyre never processed by the loop
\r
140 made new code for handling this, using asinreleaseflag
\r
142 - when/why does the mustrefreshfds select apply, sheck if i did it correctly?
\r
144 - what happens if calling handlefdtrigger for a socket which does not have an event
\r
146 {------- original loop}
\r
149 currentsocket := firstasin;
\r
151 while assigned(currentsocket) do begin
\r
152 if mustrefreshfds then begin
\r
153 if select(maxs,@fdsr,@fdsw,nil,0) <= 0 then begin
\r
159 if fd_isset(currentsocket.fdhandlein,fdsr) or fd_isset(currentsocket.fdhandleout,fdsw) then begin
\r
160 currentsocket.handlefdtrigger(fd_isset(currentsocket.fdhandlein,fdsr),fd_isset(currentsocket.fdhandleout,fdsw));
\r
163 on E: exception do begin
\r
164 currentsocket.HandleBackGroundException(e);
\r
167 tempsocket := currentsocket;
\r
168 currentsocket := currentsocket.nextasin;
\r
170 if tempsocket.released then begin
\r
174 { debugout('socketcount='+inttostr(socketcount));}
\r
177 procedure tselecteventcore.processmessages;
\r
179 fdsr , fdsw : fdset ;
\r
180 selectresult : longint ;
\r
182 mustrefreshfds := false;
\r
186 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
187 while (selectresult>0) or assigned(firsttask) or assigned(currenttask) do begin;
\r
191 if selectresult > 0 then begin
\r
192 processasios(fdsr,fdsw);
\r
194 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
197 mustrefreshfds := true;
\r
202 FDSR , FDSW : fdset;
\r
205 fdsrmaster , fdswmaster : fdset ;
\r
207 function getfdsrmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
209 result := fdsrmaster;
\r
211 function getfdswmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
213 result := fdswmaster;
\r
217 Function doSelect(timeOut:PTimeVal):longint;//inline;
\r
219 localtimeval : ttimeval;
\r
220 maxslocal : integer;
\r
223 //zeromemory(@sset,sizeof(sset));
\r
225 fdsr := getfdsrmaster;
\r
226 fdsw := getfdswmaster;
\r
228 if assigned(firsttask) then begin
\r
229 localtimeval.tv_sec := 0;
\r
230 localtimeval.tv_usec := 0;
\r
231 timeout := @localtimeval;
\r
235 mustrefreshfds := false;
\r
236 { debugout('about to call select');}
\r
238 sigprocmask(SIG_UNBLOCK,@blockset,nil);
\r
240 result := select(maxslocal+1,@FDSR,@FDSW,nil,timeout);
\r
241 if result <= 0 then begin
\r
244 if result=-1 then begin
\r
245 if linuxerror = SYS_EINTR then begin
\r
246 // we received a signal it's not a problem
\r
248 raise esocketexception.create('select returned error '+inttostr(linuxerror));
\r
253 sigprocmask(SIG_BLOCK,@blockset,nil);
\r
255 { debugout('select complete');}
\r
258 procedure tselecteventcore.exitmessageloop;
\r
260 exitloopflag := true
\r
265 procedure tselecteventcore.messageloop;
\r
267 tv ,tvnow : ttimeval ;
\r
268 currenttimer : tltimer ;
\r
269 selectresult:integer;
\r
274 {currentsocket := firstasin;
\r
275 if not assigned(currentsocket) then exit; //the message loop will exit if all lsockets are destroyed
\r
278 if currentsocket.state = wsconnected then currentsocket.sendflush;
\r
279 currentsocket := currentsocket.nextasin;
\r
280 until not assigned(currentsocket);}
\r
285 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\r
287 //currenttask := nil;
\r
289 //if assigned(firsttimer) then begin
\r
290 // tv.tv_sec := maxlongint;
\r
291 tv := tv_invalidtimebig;
\r
292 currenttimer := firsttimer;
\r
293 while assigned(currenttimer) do begin
\r
294 if tv_compare(tv,currenttimer.nextts) and currenttimer.enabled then tv := currenttimer.nextts;
\r
295 currenttimer := currenttimer.nexttimer;
\r
299 if tv_compare(tv,tv_invalidtimebig) then begin
\r
300 //writeln('no timers active');
\r
301 if exitloopflag then break;
\r
303 selectresult := doselect(nil);
\r
306 gettimeofday(tvnow);
\r
307 tv_substract(tv,tvnow);
\r
309 //writeln('timers active');
\r
310 if tv.tv_sec < 0 then begin
\r
312 tv.tv_usec := 0; {0.1 sec}
\r
314 if exitloopflag then break;
\r
316 selectresult := doselect(@tv);
\r
320 if selectresult > 0 then processasios(fdsr,fdsw);
\r
321 {!!!only call processasios if select has asio events -beware}
\r
323 {artificial delay to throttle the number of processasios per second possible and reduce cpu usage}
\r
328 procedure tselecteventcore.rmasterset(fd : integer;islistensocket : boolean);
\r
330 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
331 if fd > maxs then maxs := fd;
\r
332 if fd_isset(fd,fdsrmaster) then exit;
\r
333 fd_set(fd,fdsrmaster);
\r
337 procedure tselecteventcore.rmasterclr(fd: integer);
\r
339 if not fd_isset(fd,fdsrmaster) then exit;
\r
340 fd_clr(fd,fdsrmaster);
\r
345 procedure tselecteventcore.wmasterset(fd : integer);
\r
347 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
348 if fd > maxs then maxs := fd;
\r
350 if fd_isset(fd,fdswmaster) then exit;
\r
351 fd_set(fd,fdswmaster);
\r
355 procedure tselecteventcore.wmasterclr(fd: integer);
\r
357 if not fd_isset(fd,fdswmaster) then exit;
\r
358 fd_clr(fd,fdswmaster);
\r
361 procedure tselecteventcore.setfdreverse(fd : integer;reverseto : tlasio);
\r
363 fdreverse[fd] := reverseto;
\r
369 procedure lcoreinit;
\r
371 if inited then exit;
\r
373 eventcore := tselecteventcore.create;
\r
375 absoloutemaxs := absoloutemaxs_select;
\r
378 fd_zero(fdsrmaster);
\r
379 fd_zero(fdswmaster);
\r