changeset 95:ebdff34b9e4f

Merge
author Ivo Smits <Ivo@UCIS.nl>
date Thu, 26 Jun 2014 18:45:56 +0200
parents 9b898d8b2541 (diff) 3c1bba376dca (current diff)
children 94df2951d118
files Net/HTTP.cs Util/ArrayUtil.cs
diffstat 6 files changed, 95 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/Database.cs	Sat Jun 21 00:40:33 2014 +0200
+++ b/Database.cs	Thu Jun 26 18:45:56 2014 +0200
@@ -22,7 +22,7 @@
 			return conn;
 		}
 
-		private IDbCommand PrepareQuery(IDbConnection Connection, string Query, params object[] Parameters) {
+		private static IDbCommand PrepareQuery(IDbConnection Connection, string Query, params object[] Parameters) {
 			IDbCommand Command = Connection.CreateCommand();
 			Command.CommandType = CommandType.Text;
 			Command.CommandText = Query;
@@ -118,5 +118,74 @@
 		/*public DBReader GetReader(string QueryString, params object[] Parameters) {
 			return new DBReader(PrepareQuery(QueryString, Parameters));
 		}*/
+
+		public IDataReader ExecuteReader(String QueryString, params Object[] Parameters) {
+			IDbConnection connection = GetConnection();
+			try {
+				using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) {
+					return command.ExecuteReader(CommandBehavior.CloseConnection);
+				}
+			} catch {
+				connection.Dispose();
+				throw;
+			}
+		}
+
+		public IEnumerable<IDataRecord> EnumerateRows(String query, params Object[] parameters) {
+			IDbConnection connection = GetConnection();
+			try {
+				return new DataEnumerator(PrepareQuery(connection, query, parameters));
+			} catch {
+				connection.Dispose();
+				throw;
+			}
+		}
+
+		class DataEnumerator : IEnumerable<IDataRecord>, IEnumerator<IDataRecord> {
+			IDbCommand command = null;
+			IDataReader reader = null;
+			public DataEnumerator(IDbCommand command) {
+				this.command = command;
+				try {
+					this.reader = command.ExecuteReader();
+				} catch {
+					Dispose();
+					throw;
+				}
+			}
+			IEnumerator<IDataRecord> IEnumerable<IDataRecord>.GetEnumerator() {
+				return this;
+			}
+			System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
+				return this;
+			}
+			public IDataRecord Current {
+				get { return reader; }
+			}
+			object System.Collections.IEnumerator.Current {
+				get { return reader; }
+			}
+			public bool MoveNext() {
+				return reader.Read();
+			}
+			public void Reset() {
+				throw new NotSupportedException();
+			}
+			public Object[] CurrentRow {
+				get {
+					object[] array = new object[reader.FieldCount];
+					reader.GetValues(array);
+					return array;
+				}
+			}
+			public void Dispose() {
+				if (reader != null) reader.Dispose();
+				if (command != null) {
+					IDbConnection connection = command.Connection;
+					command.Dispose();
+					connection.Dispose();
+				}
+			}
+		}
 	}
 }
\ No newline at end of file
--- a/Net/HTTP.cs	Sat Jun 21 00:40:33 2014 +0200
+++ b/Net/HTTP.cs	Thu Jun 26 18:45:56 2014 +0200
@@ -156,7 +156,7 @@
 				Context.SendHeader("Transfer-Encoding", "chunked");
 				OutputStream = Context.BeginResponseData();
 				Mode = HTTPResponseStreamMode.Chunked;
-				oldbuffer.WriteTo(this);
+				if (oldbuffer != null) oldbuffer.WriteTo(this);
 			}
 
 			public override void Write(byte[] buffer, int offset, int count) {
--- a/USBLib/Windows/USB/UsbDevice.cs	Sat Jun 21 00:40:33 2014 +0200
+++ b/USBLib/Windows/USB/UsbDevice.cs	Thu Jun 26 18:45:56 2014 +0200
@@ -49,9 +49,20 @@
 		public int VendorID { get { return DeviceDescriptor.VendorID; } }
 		public int ProductID { get { return DeviceDescriptor.ProductID; } }
 
+		private short[] languages = null;
+
 		private String GetStringSafe(Byte id) {
 			if (id == 0) return null;
-			String s = UsbStringDescriptor.GetStringFromDevice(this, id, 0);
+			if (languages == null) {
+				Byte[] buff = new Byte[256];
+				int len = GetDescriptor((Byte)UsbDescriptorType.String, 0, 0, buff, 0, buff.Length);
+				if (len > 1) {
+					languages = new short[len / 2 - 1];
+					for (int i = 0; i < languages.Length; i++) languages[i] = BitConverter.ToInt16(buff, i * 2 + 2);
+				}
+			}
+			short language = (languages == null || languages.Length == 0) ? (short)0 : languages[0];
+			String s = UsbStringDescriptor.GetStringFromDevice(this, id, language);
 			if (s == null) return s;
 			return s.Trim(' ', '\0');
 		}
--- a/USBLib/Windows/USB/UsbHub.cs	Sat Jun 21 00:40:33 2014 +0200
+++ b/USBLib/Windows/USB/UsbHub.cs	Thu Jun 26 18:45:56 2014 +0200
@@ -22,9 +22,8 @@
 			if (HasNodeInformation) return;
 			NodeInformation = new USB_NODE_INFORMATION();
 			int nBytes = Marshal.SizeOf(typeof(USB_NODE_INFORMATION));
-			using (SafeFileHandle handle = OpenHandle()) 
-				if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_NODE_INFORMATION, ref NodeInformation, nBytes, out NodeInformation, nBytes, out nBytes, IntPtr.Zero))
-					throw new Win32Exception(Marshal.GetLastWin32Error());
+			using (SafeFileHandle handle = OpenHandle())
+				HasNodeInformation = Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_NODE_INFORMATION, ref NodeInformation, nBytes, out NodeInformation, nBytes, out nBytes, IntPtr.Zero);
 		}
 
 		public bool IsRootHub { get; private set; }
--- a/Util/ArrayUtil.cs	Sat Jun 21 00:40:33 2014 +0200
+++ b/Util/ArrayUtil.cs	Thu Jun 26 18:45:56 2014 +0200
@@ -158,9 +158,14 @@
 			}
 		}
 		public static int AddUnique<T>(ref T[] array, T item) {
-			int index = Array.IndexOf(array, item);
-			if (index == -1) index = Add(ref array, item);
-			return index;
+			if (array == null) {
+				array = new T[] { item };
+				return 0;
+			} else {
+				int index = Array.IndexOf(array, item);
+				if (index == -1) index = Add(ref array, item);
+				return index;
+			}
 		}
 		public static Boolean Remove<T>(ref T[] array, T item) {
 			if (array == null) return false;
--- a/Util/WorkQueue.cs	Sat Jun 21 00:40:33 2014 +0200
+++ b/Util/WorkQueue.cs	Thu Jun 26 18:45:56 2014 +0200
@@ -73,12 +73,12 @@
 			while (true) {
 				TWork item;
 				lock (queue) {
-					if (workers >= maxWorkers) {
+					if (workers > maxWorkers) {
 						workers--;
 						break;
 					}
 					if (queue.Count == 0) {
-						if (idleWorkers >= maxIdleWorkers) {
+						if (idleWorkers > maxIdleWorkers) {
 							workers--;
 							queue.TrimExcess();
 							break;