ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/fbintf/client/FBEvents.pas
Revision: 421
Committed: Sat Oct 21 14:22:28 2023 UTC (6 months, 1 week ago) by tony
Content type: text/x-pascal
File size: 13035 byte(s)
Log Message:
Release 2.6.3 Merged

File Contents

# Content
1 (*
2 * Firebird Interface (fbintf). The fbintf components provide a set of
3 * Pascal language bindings for the Firebird API. Although predominantly
4 * a new development they include source code taken from IBX and may be
5 * considered a derived product. This software thus also includes the copyright
6 * notice and license conditions from IBX.
7 *
8 * Except for those parts dervied from IBX, contents of this file are subject
9 * to the Initial Developer's Public License Version 1.0 (the "License"); you
10 * may not use this file except in compliance with the License. You may obtain a
11 * copy of the License here:
12 *
13 * http://www.firebirdsql.org/index.php?op=doc&id=idpl
14 *
15 * Software distributed under the License is distributed on an "AS
16 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 * implied. See the License for the specific language governing rights
18 * and limitations under the License.
19 *
20 * The Initial Developer of the Original Code is Tony Whyman.
21 *
22 * The Original Code is (C) 2016 Tony Whyman, MWA Software
23 * (http://www.mwasoftware.co.uk).
24 *
25 * All Rights Reserved.
26 *
27 * Contributor(s): ______________________________________.
28 *
29 *)
30 {************************************************************************}
31 { }
32 { Borland Delphi Visual Component Library }
33 { InterBase Express core components }
34 { }
35 { Copyright (c) 1998-2000 Inprise Corporation }
36 { }
37 { InterBase Express is based in part on the product }
38 { Free IB Components, written by Gregory H. Deatz for }
39 { Hoagland, Longo, Moran, Dunst & Doukas Company. }
40 { Free IB Components is used under license. }
41 { }
42 { The contents of this file are subject to the InterBase }
43 { Public License Version 1.0 (the "License"); you may not }
44 { use this file except in compliance with the License. You }
45 { may obtain a copy of the License at http://www.Inprise.com/IPL.html }
46 { Software distributed under the License is distributed on }
47 { an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either }
48 { express or implied. See the License for the specific language }
49 { governing rights and limitations under the License. }
50 { The Original Code was created by InterBase Software Corporation }
51 { and its successors. }
52 { Portions created by Inprise Corporation are Copyright (C) Inprise }
53 { Corporation. All Rights Reserved. }
54 { Contributor(s): Jeff Overcash }
55 { }
56 { IBX For Lazarus (Firebird Express) }
57 { Contributor: Tony Whyman, MWA Software http://www.mwasoftware.co.uk }
58 { Portions created by MWA Software are copyright McCallum Whyman }
59 { Associates Ltd 2011 - 2015 }
60 { }
61 {************************************************************************}
62 unit FBEvents;
63 {$IFDEF MSWINDOWS}
64 {$DEFINE WINDOWS}
65 {$ENDIF}
66
67 {$IFDEF FPC}
68 {$mode delphi}
69 {$interfaces COM}
70 {$ENDIF}
71
72 interface
73
74 uses
75 Classes, SysUtils, IB, FBClientAPI, syncobjs, FBActivityMonitor;
76
77 type
78
79 { TFBEvents }
80
81 {Firebird Event and Result buffer syntax is:
82
83 record
84 version: byte;
85 event: array of packed record
86 strlen: byte;
87 strchars: array of AnsiChar; //no of chars given by strlen
88 EventCounts: long;
89 end;
90 end;
91
92 }
93
94 TFBEvents = class(TActivityReporter)
95 private
96 FConnectionCodePage: TSystemCodePage;
97 FEvents: TStringList;
98 FAttachment: IAttachment;
99 FEventCounts: TEventCounts;
100 FFirebirdClientAPI: TFBClientAPI;
101 protected
102 FEventBuffer: PByte;
103 FEventBufferLen: integer;
104 FResultBuffer: PByte;
105 FEventHandler: TEventHandler;
106 FCriticalSection: TCriticalSection;
107 FInWaitState: boolean;
108 procedure CreateEventBlock;
109 procedure CancelEvents(Force: boolean = false); virtual;
110 procedure EventSignaled;
111 function GetIEvents: IEvents; virtual; abstract;
112 function ProcessEventCounts: boolean;
113 public
114 const EPB_version1 = 1;
115 public
116 constructor Create(DBAttachment: IAttachment; aMonitor: IActivityMonitor; Events: TStrings);
117 destructor Destroy; override;
118 property ConnectionCodePage: TSystemCodePage read FConnectionCodePage;
119
120 {IEvents}
121 procedure GetEvents(EventNames: TStrings);
122 procedure SetEvents(EventNames: TStrings); overload;
123 procedure SetEvents(Event: string); overload;
124 procedure Cancel;
125 function ExtractEventCounts: TEventCounts;
126 function GetAttachment: IAttachment;
127 procedure AsyncWaitForEvent(EventHandler: TEventHandler); virtual; abstract;
128 procedure WaitForEvent; virtual; abstract;
129 end;
130
131
132 implementation
133
134 uses FBMessages, IBExternals;
135
136 const
137 MaxEvents = 15;
138
139 { TFBEvents }
140
141 (* Original Firebird 'C' code
142
143 SLONG API_ROUTINE_VARARG isc_event_block(UCHAR** event_buffer,
144 UCHAR** result_buffer,
145 USHORT count, ...)
146 {
147 /**************************************
148 *
149 * i s c _ e v e n t _ b l o c k
150 *
151 **************************************
152 *
153 * Functional description
154 * Create an initialized event parameter block from a
155 * variable number of input arguments.
156 * Return the size of the block.
157 *
158 * Return 0 if any error occurs.
159 *
160 **************************************/
161 va_list ptr;
162
163 va_start(ptr, count);
164
165 // calculate length of event parameter block, setting initial length to include version
166 // and counts for each argument
167
168 SLONG length = 1;
169 USHORT i = count;
170 while (i--)
171 {
172 const char* q = va_arg(ptr, SCHAR * );
173 length += static_cast<SLONG>(strlen(q)) + 5;
174 }
175 va_end(ptr);
176
177 UCHAR* p = *event_buffer = (UCHAR * ) gds__alloc((SLONG) length);
178 // FREE: apparently never freed
179 if (!*event_buffer) // NOMEM:
180 return 0;
181 if ((*result_buffer = (UCHAR * ) gds__alloc((SLONG) length)) == NULL)
182 {
183 // NOMEM:
184 // FREE: apparently never freed
185 gds__free(*event_buffer);
186 *event_buffer = NULL;
187 return 0;
188 }
189
190 // initialize the block with event names and counts
191
192 *p++ = EPB_version1;
193
194 va_start(ptr, count);
195
196 i = count;
197 while (i--)
198 {
199 const char* q = va_arg(ptr, SCHAR * );
200
201 // Strip the blanks from the ends
202 const char* end = q + strlen(q);
203 while (--end >= q && *end == ' ')
204 ;
205 *p++ = end - q + 1;
206 while (q <= end)
207 *p++ = *q++;
208 *p++ = 0;
209 *p++ = 0;
210 *p++ = 0;
211 *p++ = 0;
212 }
213 va_end(ptr);
214
215 return static_cast<SLONG>(p - *event_buffer);
216 }
217 *)
218
219 {CreateEventBlock effectively replaces isc_event_block}
220
221 procedure TFBEvents.CreateEventBlock;
222 var i: integer;
223 P: PByte;
224 var s: Ansistring;
225 begin
226 {calculate length of event parameter block, setting initial length to include version
227 and counts for each argument}
228
229 if FEventBuffer <> nil then
230 begin
231 FreeMem( FEventBuffer);
232 FEventBuffer := nil;
233 end;
234 if FResultBuffer <> nil then
235 begin
236 FreeMem( FResultBuffer);
237 FResultBuffer := nil;
238 end;
239
240 FEventBufferLen := 1;
241 for i := 0 to FEvents.Count - 1 do
242 begin
243 s := FEvents[i];
244 FEventBufferLen := FEventBufferLen + length(s) + 1 + sizeof(Long);
245 end;
246
247 with FFirebirdClientAPI do
248 begin
249 IBAlloc(FEventBuffer,0,FEventBufferLen);
250 if FEventBuffer = nil then Exit;
251 FillChar(FEventBuffer^,FEventBufferLen,0);
252 IBAlloc(FResultBuffer,0,FEventBufferLen);
253 if FResultBuffer = nil then
254 begin
255 FreeMem(FEventBuffer);
256 FEventBuffer := nil;
257 Exit;
258 end;
259 FillChar(FResultBuffer^,FEventBufferLen,0);
260
261 P := FEventBuffer;
262 P^ := EPB_version1;
263 Inc(P);
264 SetLength(FEventCounts,FEvents.Count);
265
266 for i := 0 to FEvents.Count - 1 do
267 begin
268 s := FEvents[i];
269 P^ := Length(s);
270 Inc(P);
271 Move(s[1],P^,Length(s));
272 Inc(P,Length(s)+sizeof(Long));
273 FEventCounts[i].EventName := s;
274 end;
275 end;
276
277 { for i := 0 to FEventBufferLen - 1 do
278 write(Format('%x ', [FEventBuffer[i]]));
279 writeln; }
280 end;
281
282 procedure TFBEvents.CancelEvents(Force: boolean);
283 begin
284 FEventHandler := nil;
285 end;
286
287 procedure TFBEvents.EventSignaled;
288 var Handler: TEventHandler;
289 begin
290 Handler := nil;
291 FCriticalSection.Enter;
292 try
293 if not FInWaitState then Exit;
294 if ProcessEventCounts and assigned(FEventHandler) then
295 begin
296 Handler := FEventHandler;
297 FEventHandler := nil;
298 FInWaitState := false;
299 end;
300 finally
301 FCriticalSection.Leave;
302 end;
303 if assigned(Handler) then
304 Handler(GetIEvents);
305 end;
306
307 (*
308 Original Firebird 'C' code for isc_event_counts
309
310 void API_ROUTINE isc_event_counts(ULONG* result_vector,
311 SSHORT buffer_length,
312 UCHAR* event_buffer,
313 const UCHAR* result_buffer)
314 {
315 /**************************************
316 *
317 * g d s _ $ e v e n t _ c o u n t s
318 *
319 **************************************
320 *
321 * Functional description
322 * Get the delta between two events in an event
323 * parameter block. Used to update gds_events
324 * for GPRE support of events.
325 *
326 **************************************/
327 ULONG* vec = result_vector;
328 const UCHAR* p = event_buffer;
329 const UCHAR* q = result_buffer;
330 USHORT length = buffer_length;
331 const UCHAR* const end = p + length;
332
333 // analyze the event blocks, getting the delta for each event
334
335 p++;
336 q++;
337 while (p < end)
338 {
339 // skip over the event name
340
341 const USHORT i = (USHORT)* p++;
342 p += i;
343 q += i + 1;
344
345 // get the change in count
346
347 const ULONG initial_count = gds__vax_integer(p, sizeof(SLONG));
348 p += sizeof(SLONG);
349 const ULONG new_count = gds__vax_integer(q, sizeof(SLONG));
350 q += sizeof(SLONG);
351 *vec++ = new_count - initial_count;
352 }
353
354 // copy over the result to the initial block to prepare
355 // for the next call to gds__event_wait
356
357 memcpy(event_buffer, result_buffer, length);
358 }
359 *)
360
361 {ProcessEventCounts effectively replaces isc_event_counts}
362
363 function TFBEvents.ProcessEventCounts: boolean;
364
365 var i: integer;
366 P, Q: PByte;
367 initial_count: Long;
368 new_count: Long;
369 len: byte;
370 begin
371 Result := false;
372 P := FEventBuffer;
373 Q := FResultBuffer;
374 Inc(P); {skip past version byte}
375 Inc(Q);
376 for i := 0 to Length(FEventCounts) - 1 do
377 with FFirebirdClientAPI do
378 begin
379 {skip over the event name}
380 len := P^;
381 P := P + len + 1;
382 Q := Q + len + 1; {event name length in P^}
383 initial_count := DecodeInteger(P,sizeof(Long));
384 Inc(P,sizeof(Long));
385 new_count := DecodeInteger(Q,sizeof(Long));
386 Inc(Q,sizeof(Long));
387 FEventCounts[i].Count := new_count - initial_count;
388 if FEventCounts[i].Count > 0 then
389 Result := true;
390 // writeln('Event Count[',i,'] = ',FEventCounts[i].Count);
391 end;
392 Move(FResultBuffer^,FEventBuffer^,FEventBufferLen);
393 end;
394
395 constructor TFBEvents.Create(DBAttachment: IAttachment;
396 aMonitor: IActivityMonitor; Events: TStrings);
397 begin
398 inherited Create(aMonitor);
399 FAttachment := DBAttachment;
400 FFirebirdClientAPI := DBAttachment.getFirebirdAPI as TFBClientAPI;
401 FConnectionCodePage := DBAttachment.GetCodePage;
402 if Events.Count > MaxEvents then
403 IBError(ibxeMaximumEvents, [nil]);
404
405 FCriticalSection := TCriticalSection.Create;
406 FEvents := TStringList.Create;
407 FEvents.Assign(Events);
408 CreateEventBlock;
409 end;
410
411 destructor TFBEvents.Destroy;
412 begin
413 if assigned(FCriticalSection) then FCriticalSection.Free;
414 if assigned(FEvents) then FEvents.Free;
415 with FFirebirdClientAPI do
416 begin
417 if FEventBuffer <> nil then
418 FreeMem( FEventBuffer);
419 if FResultBuffer <> nil then
420 FreeMem( FResultBuffer);
421 end;
422 inherited Destroy;
423 end;
424
425 procedure TFBEvents.GetEvents(EventNames: TStrings);
426 begin
427 EventNames.Assign(FEvents)
428 end;
429
430 procedure TFBEvents.SetEvents(EventNames: TStrings);
431 var i: integer;
432 begin
433 {$ifdef Unix}
434 if (EventNames.Count > 0) and not IsMultiThread then
435 IBError(ibxeMultiThreadRequired,['Firebird Events Handling']);
436 {$endif}
437 if EventNames.Text <> FEvents.Text then
438 begin
439 Cancel;
440 for i := 0 to EventNames.Count - 1 do
441 FEvents[i] := Trim(EventNames[i]);
442 CreateEventBlock;
443 end;
444 end;
445
446 procedure TFBEvents.SetEvents(Event: string);
447 var S: TStringList;
448 begin
449 S := TStringList.Create;
450 try
451 S.Add(Event);
452 SetEvents(S);
453 finally
454 S.Free;
455 end;
456 end;
457
458 procedure TFBEvents.Cancel;
459 begin
460 if assigned(FEventHandler) then
461 CancelEvents;
462 end;
463
464 function TFBEvents.ExtractEventCounts: TEventCounts;
465 begin
466 Result := FEventCounts;
467 end;
468
469 function TFBEvents.GetAttachment: IAttachment;
470 begin
471 Result := FAttachment;
472 end;
473
474 end.
475

Properties

Name Value
svn:eol-style native