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
46 absoloutemaxs_select = (sizeof(fdset)*8)-1;
\r
49 fdreverse:array[0..absoloutemaxs_select] of tlasio;
\r
51 tselecteventcore=class(teventcore)
\r
53 procedure processmessages; override;
\r
54 procedure messageloop; override;
\r
55 procedure exitmessageloop;override;
\r
56 procedure setfdreverse(fd : integer;reverseto : tlasio); override;
\r
57 procedure rmasterset(fd : integer;islistensocket : boolean); override;
\r
58 procedure rmasterclr(fd: integer); override;
\r
59 procedure wmasterset(fd : integer); override;
\r
60 procedure wmasterclr(fd: integer); override;
\r
63 procedure processtimers;inline;
\r
65 tv ,tvnow : ttimeval ;
\r
66 currenttimer : tltimer ;
\r
67 temptimer : tltimer ;
\r
70 gettimeofday(tvnow);
\r
71 currenttimer := firsttimer;
\r
72 while assigned(currenttimer) do begin
\r
73 //writeln(currenttimer.enabled);
\r
74 if tv_compare(tvnow,ttimeval(currenttimer.nextts)) and currenttimer.enabled then begin
\r
75 //if assigned(currenttimer.ontimer) then begin
\r
76 // if currenttimer.enabled then if currenttimer.initialevent or currenttimer.initialdone then currenttimer.ontimer(currenttimer);
\r
77 // currenttimer.initialdone := true;
\r
79 if assigned(currenttimer.ontimer) then currenttimer.ontimer(currenttimer);
\r
80 currenttimer.nextts := timeval(tvnow);
\r
81 tv_add(ttimeval(currenttimer.nextts),currenttimer.interval);
\r
83 temptimer := currenttimer;
\r
84 currenttimer := currenttimer.nexttimer;
\r
85 if temptimer.released then temptimer.free;
\r
89 procedure processasios(var fdsr,fdsw:fdset);//inline;
\r
91 currentsocket : tlasio ;
\r
92 tempsocket : tlasio ;
\r
93 socketcount : integer ; // for debugging perposes :)
\r
96 { inc(lcoretestcount);}
\r
98 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\r
99 //if (not assigned(firstasin)) and (not assigned(firsttimer)) and (not assigned(firstsignal)) then exit;
\r
102 {------- test optimised loop}
\r
104 for dw := (maxs shr 5) downto 0 do if (fdsr[dw] or fdsw[dw]) <> 0 then begin
\r
105 for bt := 0 to 31 do if (fdsr[dw] or fdsw[dw]) and (1 shl bt) <> 0 then begin
\r
107 currentsocket := fdreverse[dw shl 5 or bt];
\r
108 {if not assigned(currentsocket) then raise exception.create('currentsocket not assigned');
\r
109 if currentsocket.fdhandlein < 0 then raise exception.create('currentsocket.fdhandlein out of range');}
\r
110 {i've seen the out of range case actually happening, so it can happen. test: just close the fd - beware}
\r
111 if not assigned(currentsocket) then begin
\r
112 fdclose(dw shl 5 or bt);
\r
115 if currentsocket.fdhandlein < 0 then begin
\r
116 fdclose(dw shl 5 or bt);
\r
120 currentsocket.handlefdtrigger(fd_isset(currentsocket.fdhandlein,fdsr),fd_isset(currentsocket.fdhandleout,fdsw));
\r
122 on E: exception do begin
\r
123 currentsocket.HandleBackGroundException(e);
\r
127 if mustrefreshfds then begin
\r
128 if select(maxs+1,@fdsr,@fdsw,nil,0) <= 0 then begin
\r
136 if asinreleaseflag then begin
\r
137 asinreleaseflag := false;
\r
138 currentsocket := firstasin;
\r
139 while assigned(currentsocket) do begin
\r
140 tempsocket := currentsocket;
\r
141 currentsocket := currentsocket.nextasin;
\r
142 if tempsocket.released then begin
\r
149 - sockets which are released may not be freed because theyre never processed by the loop
\r
150 made new code for handling this, using asinreleaseflag
\r
152 - when/why does the mustrefreshfds select apply, sheck if i did it correctly?
\r
154 - what happens if calling handlefdtrigger for a socket which does not have an event
\r
156 {------- original loop}
\r
159 currentsocket := firstasin;
\r
161 while assigned(currentsocket) do begin
\r
162 if mustrefreshfds then begin
\r
163 if select(maxs,@fdsr,@fdsw,nil,0) <= 0 then begin
\r
169 if fd_isset(currentsocket.fdhandlein,fdsr) or fd_isset(currentsocket.fdhandleout,fdsw) then begin
\r
170 currentsocket.handlefdtrigger(fd_isset(currentsocket.fdhandlein,fdsr),fd_isset(currentsocket.fdhandleout,fdsw));
\r
173 on E: exception do begin
\r
174 currentsocket.HandleBackGroundException(e);
\r
177 tempsocket := currentsocket;
\r
178 currentsocket := currentsocket.nextasin;
\r
180 if tempsocket.released then begin
\r
184 { debugout('socketcount='+inttostr(socketcount));}
\r
187 procedure tselecteventcore.processmessages;
\r
189 fdsr , fdsw : fdset ;
\r
190 selectresult : longint ;
\r
192 mustrefreshfds := false;
\r
196 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
197 while (selectresult>0) or assigned(firsttask) or assigned(currenttask) do begin;
\r
201 if selectresult > 0 then begin
\r
202 processasios(fdsr,fdsw);
\r
204 selectresult := select(maxs+1,@fdsr,@fdsw,nil,0);
\r
207 mustrefreshfds := true;
\r
212 FDSR , FDSW : fdset;
\r
215 fdsrmaster , fdswmaster : fdset ;
\r
217 function getfdsrmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
219 result := fdsrmaster;
\r
221 function getfdswmaster : fdset; {$ifdef fpc}inline;{$endif}
\r
223 result := fdswmaster;
\r
227 Function doSelect(timeOut:PTimeVal):longint;//inline;
\r
229 localtimeval : ttimeval;
\r
230 maxslocal : integer;
\r
233 //zeromemory(@sset,sizeof(sset));
\r
235 fdsr := getfdsrmaster;
\r
236 fdsw := getfdswmaster;
\r
238 if assigned(firsttask) then begin
\r
239 localtimeval.tv_sec := 0;
\r
240 localtimeval.tv_usec := 0;
\r
241 timeout := @localtimeval;
\r
245 mustrefreshfds := false;
\r
246 { debugout('about to call select');}
\r
248 sigprocmask(SIG_UNBLOCK,@blockset,nil);
\r
250 result := select(maxslocal+1,@FDSR,@FDSW,nil,timeout);
\r
251 if result <= 0 then begin
\r
254 if result=-1 then begin
\r
255 if linuxerror = SYS_EINTR then begin
\r
256 // we received a signal it's not a problem
\r
258 raise esocketexception.create('select returned error '+inttostr(linuxerror));
\r
263 sigprocmask(SIG_BLOCK,@blockset,nil);
\r
265 { debugout('select complete');}
\r
268 procedure tselecteventcore.exitmessageloop;
\r
270 exitloopflag := true
\r
275 procedure tselecteventcore.messageloop;
\r
277 tv ,tvnow : ttimeval ;
\r
278 currenttimer : tltimer ;
\r
279 selectresult:integer;
\r
284 {currentsocket := firstasin;
\r
285 if not assigned(currentsocket) then exit; //the message loop will exit if all lsockets are destroyed
\r
288 if currentsocket.state = wsconnected then currentsocket.sendflush;
\r
289 currentsocket := currentsocket.nextasin;
\r
290 until not assigned(currentsocket);}
\r
295 //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed
\r
296 if (not assigned(firstasin)) and (not assigned(firsttimer)) {$ifndef nosignal} and (not assigned(firstsignal)){$endif} then exit;
\r
299 currentsocket := firstasin;
\r
300 if not assigned(currentsocket) then exit; //the message loop will exit if all lsockets are destroyed
\r
303 if (not currentsocket.released) and (currentsocket.state<>wsclosed) then fd_set(currentsocket.fdhandlein,fdsr);
\r
304 if (not currentsocket.released) and (currentsocket.state=wsconnecting) then fd_set(currentsocket.fdhandleout,fdsw);
\r
305 if currentsocket is tlsocket then begin
\r
306 if (not currentsocket.released) and (currentsocket.state=wsconnected) and(tlsocket(currentsocket).sendq <> '') then fd_set(currentsocket.fdhandleout,fdsw);
\r
308 tempsocket := currentsocket;
\r
309 currentsocket := currentsocket.nextasin;
\r
310 if tempsocket.released then begin
\r
313 until not assigned(currentsocket);
\r
316 //currenttask := nil;
\r
318 //if assigned(firsttimer) then begin
\r
319 // tv.tv_sec := maxlongint;
\r
320 tv := tv_invalidtimebig;
\r
321 currenttimer := firsttimer;
\r
322 while assigned(currenttimer) do begin
\r
323 if tv_compare(tv,currenttimer.nextts) and currenttimer.enabled then tv := currenttimer.nextts;
\r
324 currenttimer := currenttimer.nexttimer;
\r
328 if tv_compare(tv,tv_invalidtimebig) then begin
\r
329 //writeln('no timers active');
\r
330 if exitloopflag then break;
\r
332 selectresult := doselect(nil);
\r
335 gettimeofday(tvnow);
\r
336 tv_substract(tv,tvnow);
\r
338 //writeln('timers active');
\r
339 if tv.tv_sec < 0 then begin
\r
341 tv.tv_usec := 0; {0.1 sec}
\r
343 if exitloopflag then break;
\r
345 selectresult := doselect(@tv);
\r
349 if selectresult > 0 then processasios(fdsr,fdsw);
\r
350 {!!!only call processasios if select has asio events -beware}
\r
352 {artificial delay to throttle the number of processasios per second possible and reduce cpu usage}
\r
357 procedure tselecteventcore.rmasterset(fd : integer;islistensocket : boolean);
\r
359 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
360 if fd > maxs then maxs := fd;
\r
361 if fd_isset(fd,fdsrmaster) then exit;
\r
362 fd_set(fd,fdsrmaster);
\r
366 procedure tselecteventcore.rmasterclr(fd: integer);
\r
368 if not fd_isset(fd,fdsrmaster) then exit;
\r
369 fd_clr(fd,fdsrmaster);
\r
374 procedure tselecteventcore.wmasterset(fd : integer);
\r
376 if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range');
\r
377 if fd > maxs then maxs := fd;
\r
379 if fd_isset(fd,fdswmaster) then exit;
\r
380 fd_set(fd,fdswmaster);
\r
384 procedure tselecteventcore.wmasterclr(fd: integer);
\r
386 if not fd_isset(fd,fdswmaster) then exit;
\r
387 fd_clr(fd,fdswmaster);
\r
390 procedure tselecteventcore.setfdreverse(fd : integer;reverseto : tlasio);
\r
392 fdreverse[fd] := reverseto;
\r
398 eventcore := tselecteventcore.create;
\r
400 absoloutemaxs := absoloutemaxs_select;
\r
403 fd_zero(fdsrmaster);
\r
404 fd_zero(fdswmaster);
\r