2 * Firebird ADO.NET Data provider for .NET and Mono
4 * The contents of this file are subject to the Initial
5 * Developer's Public License Version 1.0 (the "License");
6 * you may not use this file except in compliance with the
7 * License. You may obtain a copy of the License at
8 * http://www.firebirdsql.org/index.php?op=doc&id=idpl
10 * Software distributed under the License is distributed on
11 * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
12 * express or implied. See the License for the specific
13 * language governing rights and limitations under the License.
15 * Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16 * All Rights Reserved.
20 using System.Collections;
22 using System.Threading;
24 using FirebirdSql.Data.Common;
26 namespace FirebirdSql.Data.Gds
28 internal class GdsEventManager
32 private GdsConnection connection;
33 private Thread thread;
34 private Hashtable events;
41 public Hashtable EventList
43 get { return this.events; }
50 public GdsEventManager(int handle, string ipAddress, int portNumber)
52 this.events = new Hashtable();
53 this.events = Hashtable.Synchronized(this.events);
56 // Initialize the connection
57 if (this.connection == null)
59 this.connection = new GdsConnection();
60 this.connection.Connect(ipAddress, portNumber);
68 public void QueueEvents(RemoteEvent remoteEvent)
72 lock (this.events.SyncRoot)
74 if (!this.events.ContainsKey(remoteEvent.LocalId))
76 this.events.Add(remoteEvent.LocalId, remoteEvent);
81 if (this.thread == null ||
82 (this.thread.ThreadState != ThreadState.Running && this.thread.ThreadState != ThreadState.Background))
84 if (this.thread == null)
87 this.thread = new Thread(new ThreadStart(ThreadHandler));
89 this.thread.IsBackground = true;
94 public void CancelEvents(RemoteEvent remoteEvent)
96 lock (this.events.SyncRoot)
98 this.events.Remove(remoteEvent.LocalId);
106 if (this.connection != null)
108 this.connection.Disconnect();
111 if (this.thread != null)
123 #region Private Methods
125 private void ThreadHandler()
130 byte[] buffer = null;
133 while (this.events.Count > 0)
137 operation = this.connection.NextOperation();
141 case IscCodes.op_response:
142 this.connection.ReadGenericResponse();
145 case IscCodes.op_exit:
146 case IscCodes.op_disconnect:
147 this.connection.Disconnect();
150 case IscCodes.op_event:
151 dbHandle = this.connection.Receive.ReadInt32();
152 buffer = this.connection.Receive.ReadBuffer();
153 ast = this.connection.Receive.ReadBytes(8);
154 eventId = this.connection.Receive.ReadInt32();
156 if (this.events.ContainsKey(eventId))
158 RemoteEvent currentEvent = (RemoteEvent)this.events[eventId];
160 lock (this.events.SyncRoot)
162 // Remove event from the list
163 this.events.Remove(eventId);
165 // Notify new event counts
166 currentEvent.EventCounts(buffer);
168 if (this.events.Count == 0)
177 catch (ThreadAbortException)