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
98 //writeln('entering processasios');
\r
99 { inc(lcoretestcount);}
\r
101 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\r
102 //if (not assigned(firstasin)) and (not assigned(firsttimer)) and (not assigned(firstsignal)) then exit;
\r
105 {------- test optimised loop}
\r
107 for dw := (maxs shr 5) downto 0 do if (fdsr[dw] or fdsw[dw]) <> 0 then begin
\r
108 for bt := 0 to 31 do if (fdsr[dw] or fdsw[dw]) and (1 shl bt) <> 0 then begin
\r
110 fd := dw shl 5 or bt;
\r
111 //writeln('reversing fd ',fd);
\r
112 currentsocket := fdreverse[fd];
\r
113 {if not assigned(currentsocket) then raise exception.create('currentsocket not assigned');
\r
114 if currentsocket.fdhandlein < 0 then raise exception.create('currentsocket.fdhandlein out of range');}
\r
115 {i've seen the out of range case actually happening, so it can happen. test: just close the fd - beware}
\r
116 if not assigned(currentsocket) then begin
\r
120 if currentsocket.fdhandlein < 0 then begin
\r
125 currentsocket.handlefdtrigger(fd_isset(fd,fdsr),fd_isset(fd,fdsw));
\r
127 on E: exception do begin
\r
128 currentsocket.HandleBackGroundException(e);
\r
132 if mustrefreshfds then begin
\r
133 if select(maxs+1,@fdsr,@fdsw,nil,0) <= 0 then begin
\r
143 - sockets which are released may not be freed because theyre never processed by the loop
\r
144 made new code for handling this, using asinreleaseflag
\r
146 - when/why does the mustrefreshfds select apply, sheck if i did it correctly?
\r
148 - what happens if calling handlefdtrigger for a socket which does not have an event
\r
150 {------- original loop}
\r
153 currentsocket := firstasin;
\r
155 while assigned(currentsocket) do begin
\r
156 if mustrefreshfds then begin
\r
157 if select(maxs,@fdsr,@fdsw,nil,0) <= 0 then begin
\r
163 if fd_isset(currentsocket.fdhandlein,fdsr) or fd_isset(currentsocket.fdhandleout,fdsw) then begin
\r
164 currentsocket.handlefdtrigger(fd_isset(currentsocket.fdhandlein,fdsr),fd_isset(currentsocket.fdhandleout,fdsw));
\r
167 on E: exception do begin
\r
168 currentsocket.HandleBackGroundException(e);
\r
171 tempsocket := currentsocket;
\r
172 currentsocket := currentsocket.nextasin;
\r
174 if tempsocket.released then begin
\r
178 { debugout('socketcount='+inttostr(socketcount));}
\r
179 //writeln('leaving processasios');
\r
182 procedure tselecteventcore.processmessages;
\r
184 fdsr , fdsw : fdset ;
\r
185 selectresult : longint ;
\r
187 mustrefreshfds := false;
\r
191 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
192 while (selectresult>0) or assigned(firsttask) or assigned(currenttask) do begin;
\r
196 if selectresult > 0 then begin
\r
197 processasios(fdsr,fdsw);
\r
199 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
202 mustrefreshfds := true;
\r
207 FDSR , FDSW : fdset;
\r
210 fdsrmaster , fdswmaster : fdset ;
\r
212 function getfdsrmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
214 result := fdsrmaster;
\r
216 function getfdswmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
218 result := fdswmaster;
\r
222 Function doSelect(timeOut:PTimeVal):longint;//inline;
\r
224 localtimeval : ttimeval;
\r
225 maxslocal : integer;
\r
228 //zeromemory(@sset,sizeof(sset));
\r
230 fdsr := getfdsrmaster;
\r
231 fdsw := getfdswmaster;
\r
233 if assigned(firsttask) then begin
\r
234 localtimeval.tv_sec := 0;
\r
235 localtimeval.tv_usec := 0;
\r
236 timeout := @localtimeval;
\r
240 mustrefreshfds := false;
\r
241 { debugout('about to call select');}
\r
243 sigprocmask(SIG_UNBLOCK,@blockset,nil);
\r
245 result := select(maxslocal+1,@FDSR,@FDSW,nil,timeout);
\r
246 if result <= 0 then begin
\r
249 if result=-1 then begin
\r
250 if linuxerror = SYS_EINTR then begin
\r
251 // we received a signal it's not a problem
\r
253 raise esocketexception.create('select returned error '+inttostr(linuxerror));
\r
258 sigprocmask(SIG_BLOCK,@blockset,nil);
\r
260 { debugout('select complete');}
\r
263 procedure tselecteventcore.exitmessageloop;
\r
265 exitloopflag := true
\r
270 procedure tselecteventcore.messageloop;
\r
272 tv ,tvnow : ttimeval ;
\r
273 currenttimer : tltimer ;
\r
274 selectresult:integer;
\r
279 {currentsocket := firstasin;
\r
280 if not assigned(currentsocket) then exit; //the message loop will exit if all lsockets are destroyed
\r
283 if currentsocket.state = wsconnected then currentsocket.sendflush;
\r
284 currentsocket := currentsocket.nextasin;
\r
285 until not assigned(currentsocket);}
\r
290 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\r
292 //currenttask := nil;
\r
294 //if assigned(firsttimer) then begin
\r
295 // tv.tv_sec := maxlongint;
\r
296 tv := tv_invalidtimebig;
\r
297 currenttimer := firsttimer;
\r
298 while assigned(currenttimer) do begin
\r
299 if tv_compare(tv,currenttimer.nextts) and currenttimer.enabled then tv := currenttimer.nextts;
\r
300 currenttimer := currenttimer.nexttimer;
\r
304 if tv_compare(tv,tv_invalidtimebig) then begin
\r
305 //writeln('no timers active');
\r
306 if exitloopflag then break;
\r
308 selectresult := doselect(nil);
\r
311 gettimeofday(tvnow);
\r
312 tv_substract(tv,tvnow);
\r
314 //writeln('timers active');
\r
315 if tv.tv_sec < 0 then begin
\r
317 tv.tv_usec := 0; {0.1 sec}
\r
319 if exitloopflag then break;
\r
321 selectresult := doselect(@tv);
\r
325 if selectresult > 0 then processasios(fdsr,fdsw);
\r
326 {!!!only call processasios if select has asio events -beware}
\r
328 {artificial delay to throttle the number of processasios per second possible and reduce cpu usage}
\r
333 procedure tselecteventcore.rmasterset(fd : integer;islistensocket : boolean);
\r
335 //writeln('rmasterset called with fd ',fd);
\r
336 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
337 if fd > maxs then maxs := fd;
\r
338 if fd_isset(fd,fdsrmaster) then exit;
\r
339 fd_set(fd,fdsrmaster);
\r
343 procedure tselecteventcore.rmasterclr(fd: integer);
\r
345 //writeln('rmasterclr called with fd ',fd);
\r
346 if not fd_isset(fd,fdsrmaster) then exit;
\r
347 fd_clr(fd,fdsrmaster);
\r
352 procedure tselecteventcore.wmasterset(fd : integer);
\r
354 //writeln('wmasterset called with fd ',fd);
\r
355 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
356 if fd > maxs then maxs := fd;
\r
358 if fd_isset(fd,fdswmaster) then exit;
\r
359 fd_set(fd,fdswmaster);
\r
363 procedure tselecteventcore.wmasterclr(fd: integer);
\r
365 //writeln('wmasterclr called with fd ',fd);
\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
378 procedure lcoreinit;
\r
380 if inited then exit;
\r
382 eventcore := tselecteventcore.create;
\r
384 absoloutemaxs := absoloutemaxs_select;
\r
387 fd_zero(fdsrmaster);
\r
388 fd_zero(fdswmaster);
\r