1 unit lcorewsaasyncselect;
\r
6 uses wcore,lcore,bsearchtree,sysutils,windows,winsock,pgtypes,messages,classes;
\r
8 twineventcore=class(teventcore)
\r
10 procedure processmessages; override;
\r
11 procedure messageloop; override;
\r
12 procedure exitmessageloop;override;
\r
13 procedure setfdreverse(fd : integer;reverseto : tlasio); override;
\r
14 procedure rmasterset(fd : integer;islistensocket : boolean); override;
\r
15 procedure rmasterclr(fd: integer); override;
\r
16 procedure wmasterset(fd : integer); override;
\r
17 procedure wmasterclr(fd: integer); override;
\r
20 wm_dotasks=wm_user+1;
\r
22 twintimerwrapperinterface=class(ttimerwrapperinterface)
\r
24 function createwrappedtimer : tobject;override;
\r
25 // procedure setinitialevent(wrappedtimer : tobject;newvalue : boolean);override;
\r
26 procedure setontimer(wrappedtimer : tobject;newvalue:tnotifyevent);override;
\r
27 procedure setenabled(wrappedtimer : tobject;newvalue : boolean);override;
\r
28 procedure setinterval(wrappedtimer : tobject;newvalue : integer);override;
\r
31 procedure twineventcore.processmessages;
\r
33 wcore.processmessages;//pass off to wcore
\r
35 procedure twineventcore.messageloop;
\r
37 wcore.messageloop; //pass off to wcore
\r
39 procedure twineventcore.exitmessageloop;
\r
41 wcore.exitmessageloop;
\r
44 fdreverse : thashtable;
\r
45 fdwatches : thashtable;
\r
47 procedure twineventcore.setfdreverse(fd : integer;reverseto : tlasio);
\r
49 if findtree(@fdreverse,inttostr(fd)) <> nil then deltree(@fdreverse,inttostr(fd));
\r
50 if reverseto <> nil then addtree(@fdreverse,inttostr(fd),reverseto);
\r
55 procedure dowsaasyncselect(fd:integer; leventadd: integer; leventremove : integer);
\r
57 leventold : integer;
\r
58 leventnew : integer;
\r
59 wsaaresult : integer;
\r
61 leventold := taddrint(findtree(@fdwatches,inttostr(fd)));
\r
62 leventnew := leventold or leventadd;
\r
63 leventnew := leventnew and not leventremove;
\r
64 if leventold <> leventnew then begin
\r
65 if leventold <> 0 then deltree(@fdwatches,inttostr(fd));
\r
66 if leventnew <> 0 then addtree(@fdwatches,inttostr(fd),pointer(leventnew));
\r
68 wsaaresult := wsaasyncselect(fd,hwndlcore,wm_user,leventnew);
\r
73 //to allow detection of errors:
\r
74 //if we are asked to monitor for read or accept we also monitor for close
\r
75 //if we are asked to monitor for write we also monitor for connect
\r
78 procedure twineventcore.rmasterset(fd : integer;islistensocket : boolean);
\r
80 if islistensocket then begin
\r
81 //writeln('setting accept watch for socket number ',fd);
\r
82 dowsaasyncselect(fd,FD_ACCEPT or FD_CLOSE,0);
\r
84 //writeln('setting read watch for socket number',fd);
\r
85 dowsaasyncselect(fd,FD_READ or FD_CLOSE,0);
\r
88 procedure twineventcore.rmasterclr(fd: integer);
\r
90 //writeln('clearing read of accept watch for socket number ',fd);
\r
91 dowsaasyncselect(fd,0,FD_ACCEPT or FD_READ or FD_CLOSE);
\r
93 procedure twineventcore.wmasterset(fd : integer);
\r
95 dowsaasyncselect(fd,FD_WRITE or FD_CONNECT,0);
\r
98 procedure twineventcore.wmasterclr(fd: integer);
\r
100 dowsaasyncselect(fd,0,FD_WRITE or FD_CONNECT);
\r
104 tasksoutstanding : boolean;
\r
106 function MyWindowProc(
\r
110 alParam : LPARAM): Integer; stdcall;
\r
115 readtrigger : boolean;
\r
116 writetrigger : boolean;
\r
119 //writeln('got a message');
\r
120 Result := 0; // This means we handled the message
\r
121 if (ahwnd=hwndlcore) and (aumsg=wm_user) then begin
\r
122 //writeln('it appears to be a response to our wsaasyncselect');
\r
124 event := alparam and $FFFF;
\r
125 error := alparam shr 16;
\r
126 //writeln('socket=',socket,' event=',event,' error=',error);
\r
127 readtrigger := false;
\r
128 writetrigger := false;
\r
129 lasio := findtree(@fdreverse,inttostr(socket));
\r
130 if assigned(lasio) then begin
\r
131 if (error <> 0) or ((event and FD_CLOSE) <> 0) then begin
\r
132 if lasio.state = wsconnecting then begin
\r
133 lasio.onsessionconnected(lasio,error);
\r
135 lasio.internalclose(error);
\r
137 if (event and (FD_READ or FD_ACCEPT)) <> 0 then readtrigger := true;
\r
138 if (event and (FD_WRITE)) <> 0 then writetrigger := true;
\r
140 if readtrigger or writetrigger then lasio.handlefdtrigger(readtrigger,writetrigger);
\r
142 dowsaasyncselect(socket,0,0); //reset watches
\r
144 end else if (ahwnd=hwndlcore) and (aumsg=wm_dotasks) then begin
\r
145 //writeln('processing tasks');
\r
146 tasksoutstanding := false;
\r
149 //writeln('passing unknown message to defwindowproc');
\r
150 //not passing unknown messages on to defwindowproc will cause window
\r
151 //creation to fail! --plugwash
\r
152 Result := DefWindowProc(ahWnd, auMsg, awParam, alParam)
\r
157 procedure winaddtask(ahandler:ttaskevent;aobj:tobject;awparam,alparam:longint);
\r
159 if not tasksoutstanding then PostMessage(hwndlcore,wm_dotasks,0,0);
\r
162 twcoretimer = wcore.tltimer;
\r
164 function twintimerwrapperinterface.createwrappedtimer : tobject;
\r
166 result := twcoretimer.create(nil);
\r
168 procedure twintimerwrapperinterface.setontimer(wrappedtimer : tobject;newvalue:tnotifyevent);
\r
170 twcoretimer(wrappedtimer).ontimer := newvalue;
\r
172 procedure twintimerwrapperinterface.setenabled(wrappedtimer : tobject;newvalue : boolean);
\r
174 twcoretimer(wrappedtimer).enabled := newvalue;
\r
178 procedure twintimerwrapperinterface.setinterval(wrappedtimer : tobject;newvalue : integer);
\r
180 twcoretimer(wrappedtimer).interval := newvalue;
\r
184 MyWindowClass : TWndClass = (style : 0;
\r
185 lpfnWndProc : @MyWindowProc;
\r
192 lpszMenuName : nil;
\r
193 lpszClassName : 'lcoreClass');
\r
194 GInitData: TWSAData;
\r
197 eventcore := twineventcore.create;
\r
198 if Windows.RegisterClass(MyWindowClass) = 0 then halt;
\r
199 //writeln('about to create lcore handle, hinstance=',hinstance);
\r
200 hwndlcore := CreateWindowEx(WS_EX_TOOLWINDOW,
\r
201 MyWindowClass.lpszClassName,
\r
202 '', { Window name }
\r
203 WS_POPUP, { Window Style }
\r
205 0, 0, { Width, Height }
\r
208 HInstance, { hInstance }
\r
209 nil); { CreateParam }
\r
210 //writeln('lcore hwnd is ',hwndlcore);
\r
211 //writeln('last error is ',GetLastError);
\r
212 onaddtask := winaddtask;
\r
213 timerwrapperinterface := twintimerwrapperinterface.create(nil);
\r
215 WSAStartup($200, GInitData);
\r