ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/branches/udr/client/FBEvents.pas
Revision: 382
Committed: Sat Jan 15 15:23:11 2022 UTC (2 years, 10 months ago) by tony
Content type: text/x-pascal
File size: 12867 byte(s)
Log Message:
Removed illegal class to FreeAndNil in FBEvets

File Contents

# User Rev Content
1 tony 45 (*
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 tony 381 {$IFDEF MSWINDOWS}
64     {$DEFINE WINDOWS}
65 tony 56 {$ENDIF}
66 tony 45
67     {$IFDEF FPC}
68 tony 56 {$mode delphi}
69 tony 45 {$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 tony 371 {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 tony 45 TFBEvents = class(TActivityReporter)
95     private
96     FEvents: TStringList;
97     FAttachment: IAttachment;
98 tony 47 FEventCounts: TEventCounts;
99 tony 263 FFirebirdClientAPI: TFBClientAPI;
100 tony 45 protected
101 tony 56 FEventBuffer: PByte;
102 tony 45 FEventBufferLen: integer;
103 tony 56 FResultBuffer: PByte;
104 tony 45 FEventHandler: TEventHandler;
105     FCriticalSection: TCriticalSection;
106     FInWaitState: boolean;
107     procedure CreateEventBlock;
108     procedure CancelEvents(Force: boolean = false); virtual;
109     procedure EventSignaled;
110     function GetIEvents: IEvents; virtual; abstract;
111 tony 381 function ProcessEventCounts: boolean;
112 tony 45 public
113 tony 371 const EPB_version1 = 1;
114     public
115 tony 45 constructor Create(DBAttachment: IAttachment; aMonitor: IActivityMonitor; Events: TStrings);
116     destructor Destroy; override;
117    
118     {IEvents}
119     procedure GetEvents(EventNames: TStrings);
120     procedure SetEvents(EventNames: TStrings); overload;
121 tony 381 procedure SetEvents(Event: string); overload;
122 tony 45 procedure Cancel;
123     function ExtractEventCounts: TEventCounts;
124     function GetAttachment: IAttachment;
125 tony 47 procedure AsyncWaitForEvent(EventHandler: TEventHandler); virtual; abstract;
126 tony 381 procedure WaitForEvent; virtual; abstract;
127 tony 45 end;
128    
129    
130     implementation
131    
132     uses FBMessages, IBExternals;
133    
134     const
135     MaxEvents = 15;
136    
137     { TFBEvents }
138    
139 tony 371 (* Original Firebird 'C' code
140    
141     SLONG API_ROUTINE_VARARG isc_event_block(UCHAR** event_buffer,
142     UCHAR** result_buffer,
143     USHORT count, ...)
144     {
145     /**************************************
146     *
147     * i s c _ e v e n t _ b l o c k
148     *
149     **************************************
150     *
151     * Functional description
152     * Create an initialized event parameter block from a
153     * variable number of input arguments.
154     * Return the size of the block.
155     *
156     * Return 0 if any error occurs.
157     *
158     **************************************/
159     va_list ptr;
160    
161     va_start(ptr, count);
162    
163     // calculate length of event parameter block, setting initial length to include version
164     // and counts for each argument
165    
166     SLONG length = 1;
167     USHORT i = count;
168     while (i--)
169     {
170     const char* q = va_arg(ptr, SCHAR * );
171     length += static_cast<SLONG>(strlen(q)) + 5;
172     }
173     va_end(ptr);
174    
175     UCHAR* p = *event_buffer = (UCHAR * ) gds__alloc((SLONG) length);
176     // FREE: apparently never freed
177     if (!*event_buffer) // NOMEM:
178     return 0;
179     if ((*result_buffer = (UCHAR * ) gds__alloc((SLONG) length)) == NULL)
180     {
181     // NOMEM:
182     // FREE: apparently never freed
183     gds__free(*event_buffer);
184     *event_buffer = NULL;
185     return 0;
186     }
187    
188     // initialize the block with event names and counts
189    
190     *p++ = EPB_version1;
191    
192     va_start(ptr, count);
193    
194     i = count;
195     while (i--)
196     {
197     const char* q = va_arg(ptr, SCHAR * );
198    
199     // Strip the blanks from the ends
200     const char* end = q + strlen(q);
201     while (--end >= q && *end == ' ')
202     ;
203     *p++ = end - q + 1;
204     while (q <= end)
205     *p++ = *q++;
206     *p++ = 0;
207     *p++ = 0;
208     *p++ = 0;
209     *p++ = 0;
210     }
211     va_end(ptr);
212    
213     return static_cast<SLONG>(p - *event_buffer);
214     }
215     *)
216    
217     {CreateEventBlock effectively replaces isc_event_block}
218    
219 tony 45 procedure TFBEvents.CreateEventBlock;
220 tony 371 var i: integer;
221     P: PByte;
222 tony 381 var s: Ansistring;
223 tony 45 begin
224 tony 371 {calculate length of event parameter block, setting initial length to include version
225     and counts for each argument}
226    
227 tony 382 if FEventBuffer <> nil then
228     begin
229     FreeMem( FEventBuffer);
230     FEventBuffer := nil;
231     end;
232     if FResultBuffer <> nil then
233     begin
234     FreeMem( FResultBuffer);
235     FResultBuffer := nil;
236     end;
237 tony 381
238 tony 371 FEventBufferLen := 1;
239     for i := 0 to FEvents.Count - 1 do
240 tony 381 begin
241     s := FEvents[i];
242     FEventBufferLen := FEventBufferLen + length(s) + 1 + sizeof(Long);
243     end;
244 tony 371
245 tony 263 with FFirebirdClientAPI do
246 tony 45 begin
247 tony 371 IBAlloc(FEventBuffer,0,FEventBufferLen);
248     if FEventBuffer = nil then Exit;
249     FillChar(FEventBuffer^,FEventBufferLen,0);
250     IBAlloc(FResultBuffer,0,FEventBufferLen);
251     if FResultBuffer = nil then
252     begin
253 tony 382 FreeMem(FEventBuffer);
254     FEventBuffer := nil;
255 tony 371 Exit;
256     end;
257 tony 375 FillChar(FResultBuffer^,FEventBufferLen,0);
258 tony 45
259 tony 371 P := FEventBuffer;
260     P^ := EPB_version1;
261     Inc(P);
262     SetLength(FEventCounts,FEvents.Count);
263 tony 45
264 tony 371 for i := 0 to FEvents.Count - 1 do
265     begin
266 tony 381 s := FEvents[i];
267     P^ := Length(s);
268 tony 371 Inc(P);
269 tony 381 Move(s[1],P^,Length(s));
270     Inc(P,Length(s)+sizeof(Long));
271     FEventCounts[i].EventName := s;
272 tony 45 end;
273     end;
274 tony 371
275     { for i := 0 to FEventBufferLen - 1 do
276     write(Format('%x ', [FEventBuffer[i]]));
277 tony 381 writeln; }
278 tony 45 end;
279    
280     procedure TFBEvents.CancelEvents(Force: boolean);
281     begin
282     FEventHandler := nil;
283     end;
284    
285     procedure TFBEvents.EventSignaled;
286     var Handler: TEventHandler;
287     begin
288 tony 47 Handler := nil;
289 tony 45 FCriticalSection.Enter;
290     try
291     if not FInWaitState then Exit;
292 tony 381 if ProcessEventCounts and assigned(FEventHandler) then
293 tony 45 begin
294     Handler := FEventHandler;
295     FEventHandler := nil;
296 tony 381 FInWaitState := false;
297 tony 45 end;
298     finally
299 tony 47 FCriticalSection.Leave;
300 tony 45 end;
301 tony 47 if assigned(Handler) then
302     Handler(GetIEvents);
303 tony 45 end;
304    
305 tony 371 (*
306     Original Firebird 'C' code for isc_event_counts
307    
308     void API_ROUTINE isc_event_counts(ULONG* result_vector,
309     SSHORT buffer_length,
310     UCHAR* event_buffer,
311     const UCHAR* result_buffer)
312     {
313     /**************************************
314     *
315     * g d s _ $ e v e n t _ c o u n t s
316     *
317     **************************************
318     *
319     * Functional description
320     * Get the delta between two events in an event
321     * parameter block. Used to update gds_events
322     * for GPRE support of events.
323     *
324     **************************************/
325     ULONG* vec = result_vector;
326     const UCHAR* p = event_buffer;
327     const UCHAR* q = result_buffer;
328     USHORT length = buffer_length;
329     const UCHAR* const end = p + length;
330    
331     // analyze the event blocks, getting the delta for each event
332    
333     p++;
334     q++;
335     while (p < end)
336     {
337     // skip over the event name
338    
339     const USHORT i = (USHORT)* p++;
340     p += i;
341     q += i + 1;
342    
343     // get the change in count
344    
345     const ULONG initial_count = gds__vax_integer(p, sizeof(SLONG));
346     p += sizeof(SLONG);
347     const ULONG new_count = gds__vax_integer(q, sizeof(SLONG));
348     q += sizeof(SLONG);
349     *vec++ = new_count - initial_count;
350     }
351    
352     // copy over the result to the initial block to prepare
353     // for the next call to gds__event_wait
354    
355     memcpy(event_buffer, result_buffer, length);
356     }
357     *)
358    
359     {ProcessEventCounts effectively replaces isc_event_counts}
360    
361 tony 381 function TFBEvents.ProcessEventCounts: boolean;
362 tony 371
363     var i: integer;
364     P, Q: PByte;
365     initial_count: Long;
366     new_count: Long;
367     len: byte;
368 tony 47 begin
369 tony 381 Result := false;
370 tony 371 P := FEventBuffer;
371     Q := FResultBuffer;
372     Inc(P); {skip past version byte}
373     Inc(Q);
374     for i := 0 to Length(FEventCounts) - 1 do
375 tony 263 with FFirebirdClientAPI do
376 tony 47 begin
377 tony 371 {skip over the event name}
378     len := P^;
379     P := P + len + 1;
380     Q := Q + len + 1; {event name length in P^}
381     initial_count := DecodeInteger(P,sizeof(Long));
382     Inc(P,sizeof(Long));
383     new_count := DecodeInteger(Q,sizeof(Long));
384     Inc(Q,sizeof(Long));
385     FEventCounts[i].Count := new_count - initial_count;
386 tony 381 if FEventCounts[i].Count > 0 then
387     Result := true;
388     // writeln('Event Count[',i,'] = ',FEventCounts[i].Count);
389 tony 47 end;
390 tony 371 Move(FResultBuffer^,FEventBuffer^,FEventBufferLen);
391 tony 47 end;
392    
393 tony 45 constructor TFBEvents.Create(DBAttachment: IAttachment;
394     aMonitor: IActivityMonitor; Events: TStrings);
395     begin
396     inherited Create(aMonitor);
397     FAttachment := DBAttachment;
398 tony 263 FFirebirdClientAPI := DBAttachment.getFirebirdAPI as TFBClientAPI;
399 tony 45 if Events.Count > MaxEvents then
400     IBError(ibxeMaximumEvents, [nil]);
401    
402     FCriticalSection := TCriticalSection.Create;
403     FEvents := TStringList.Create;
404     FEvents.Assign(Events);
405     CreateEventBlock;
406     end;
407    
408     destructor TFBEvents.Destroy;
409     begin
410     if assigned(FCriticalSection) then FCriticalSection.Free;
411     if assigned(FEvents) then FEvents.Free;
412 tony 263 with FFirebirdClientAPI do
413 tony 45 begin
414     if FEventBuffer <> nil then
415 tony 371 FreeMem( FEventBuffer);
416 tony 45 if FResultBuffer <> nil then
417 tony 371 FreeMem( FResultBuffer);
418 tony 45 end;
419     inherited Destroy;
420     end;
421    
422     procedure TFBEvents.GetEvents(EventNames: TStrings);
423     begin
424     EventNames.Assign(FEvents)
425     end;
426    
427     procedure TFBEvents.SetEvents(EventNames: TStrings);
428 tony 371 var i: integer;
429 tony 45 begin
430 tony 263 {$ifdef Unix}
431 tony 217 if (EventNames.Count > 0) and not IsMultiThread then
432 tony 221 IBError(ibxeMultiThreadRequired,['Firebird Events Handling']);
433 tony 263 {$endif}
434 tony 45 if EventNames.Text <> FEvents.Text then
435     begin
436     Cancel;
437 tony 371 for i := 0 to EventNames.Count - 1 do
438     FEvents[i] := Trim(EventNames[i]);
439 tony 45 CreateEventBlock;
440     end;
441     end;
442    
443 tony 381 procedure TFBEvents.SetEvents(Event: string);
444 tony 45 var S: TStringList;
445     begin
446     S := TStringList.Create;
447     try
448     S.Add(Event);
449     SetEvents(S);
450     finally
451     S.Free;
452     end;
453     end;
454    
455     procedure TFBEvents.Cancel;
456     begin
457     if assigned(FEventHandler) then
458     CancelEvents;
459     end;
460    
461     function TFBEvents.ExtractEventCounts: TEventCounts;
462     begin
463 tony 47 Result := FEventCounts;
464 tony 45 end;
465    
466     function TFBEvents.GetAttachment: IAttachment;
467     begin
468     Result := FAttachment;
469     end;
470    
471     end.
472    

Properties

Name Value
svn:eol-style native