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,sockets,
\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
96 currentfdword:fdword;
\r
99 //writeln('entering processasios');
\r
100 { inc(lcoretestcount);}
\r
102 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\r
103 //if (not assigned(firstasin)) and (not assigned(firsttimer)) and (not assigned(firstsignal)) then exit;
\r
106 {------- test optimised loop}
\r
108 for dw := (maxs shr fdwordshift) downto 0 do if (fdsr[dw] or fdsw[dw]) <> 0 then begin
\r
109 currentfdword := (fdsr[dw] or fdsw[dw]);
\r
110 for bt := fdwordmaxbit downto 0 do if currentfdword and (1 shl bt) <> 0 then begin
\r
112 fd := dw shl fdwordshift or bt;
\r
113 //writeln('reversing fd ',fd);
\r
114 currentsocket := fdreverse[fd];
\r
115 {if not assigned(currentsocket) then raise exception.create('currentsocket not assigned');
\r
116 if currentsocket.fdhandlein < 0 then raise exception.create('currentsocket.fdhandlein out of range');}
\r
117 {i've seen the out of range case actually happening, so it can happen. test: just close the fd - beware}
\r
118 if not assigned(currentsocket) then begin
\r
122 if currentsocket.fdhandlein < 0 then begin
\r
127 currentsocket.handlefdtrigger(fd_isset(fd,fdsr),fd_isset(fd,fdsw));
\r
129 on E: exception do begin
\r
130 currentsocket.HandleBackGroundException(e);
\r
134 if mustrefreshfds then begin
\r
135 if select(maxs+1,@fdsr,@fdsw,nil,0) <= 0 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
181 //writeln('leaving processasios');
\r
184 procedure tselecteventcore.processmessages;
\r
186 fdsr , fdsw : fdset ;
\r
187 selectresult : longint ;
\r
189 mustrefreshfds := false;
\r
193 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
194 while (selectresult>0) or assigned(firsttask) or assigned(currenttask) do begin;
\r
198 if selectresult > 0 then begin
\r
199 processasios(fdsr,fdsw);
\r
201 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
204 mustrefreshfds := true;
\r
209 FDSR , FDSW : fdset;
\r
212 fdsrmaster , fdswmaster : fdset ;
\r
214 function getfdsrmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
216 result := fdsrmaster;
\r
218 function getfdswmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
220 result := fdswmaster;
\r
224 Function doSelect(timeOut:PTimeVal):longint;//inline;
\r
226 localtimeval : ttimeval;
\r
227 maxslocal : integer;
\r
230 //zeromemory(@sset,sizeof(sset));
\r
232 fdsr := getfdsrmaster;
\r
233 fdsw := getfdswmaster;
\r
235 if assigned(firsttask) then begin
\r
236 localtimeval.tv_sec := 0;
\r
237 localtimeval.tv_usec := 0;
\r
238 timeout := @localtimeval;
\r
242 mustrefreshfds := false;
\r
243 { debugout('about to call select');}
\r
245 sigprocmask(SIG_UNBLOCK,@blockset,nil);
\r
247 result := select(maxslocal+1,@FDSR,@FDSW,nil,timeout);
\r
248 if result <= 0 then begin
\r
251 if result=-1 then begin
\r
252 if linuxerror = SYS_EINTR then begin
\r
253 // we received a signal it's not a problem
\r
255 raise esocketexception.create('select returned error '+inttostr(linuxerror));
\r
260 sigprocmask(SIG_BLOCK,@blockset,nil);
\r
262 { debugout('select complete');}
\r
265 procedure tselecteventcore.exitmessageloop;
\r
267 exitloopflag := true
\r
272 procedure tselecteventcore.messageloop;
\r
274 tv ,tvnow : ttimeval ;
\r
275 currenttimer : tltimer ;
\r
276 selectresult:integer;
\r
281 {currentsocket := firstasin;
\r
282 if not assigned(currentsocket) then exit; //the message loop will exit if all lsockets are destroyed
\r
285 if currentsocket.state = wsconnected then currentsocket.sendflush;
\r
286 currentsocket := currentsocket.nextasin;
\r
287 until not assigned(currentsocket);}
\r
292 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\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 procedure tselecteventcore.rmasterset(fd : integer;islistensocket : boolean);
\r
337 //writeln('rmasterset called with fd ',fd);
\r
338 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
339 if fd > maxs then maxs := fd;
\r
340 if fd_isset(fd,fdsrmaster) then exit;
\r
341 fd_set(fd,fdsrmaster);
\r
345 procedure tselecteventcore.rmasterclr(fd: integer);
\r
347 //writeln('rmasterclr called with fd ',fd);
\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 //writeln('wmasterset called with fd ',fd);
\r
357 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
358 if fd > maxs then maxs := fd;
\r
360 if fd_isset(fd,fdswmaster) then exit;
\r
361 fd_set(fd,fdswmaster);
\r
365 procedure tselecteventcore.wmasterclr(fd: integer);
\r
367 //writeln('wmasterclr called with fd ',fd);
\r
368 if not fd_isset(fd,fdswmaster) then exit;
\r
369 fd_clr(fd,fdswmaster);
\r
372 procedure tselecteventcore.setfdreverse(fd : integer;reverseto : tlasio);
\r
374 fdreverse[fd] := reverseto;
\r
380 procedure lcoreinit;
\r
382 if inited then exit;
\r
384 eventcore := tselecteventcore.create;
\r
386 absoloutemaxs := absoloutemaxs_select;
\r
389 fd_zero(fdsrmaster);
\r
390 fd_zero(fdswmaster);
\r