# HG changeset patch # User Ivo Smits # Date 1403801156 -7200 # Node ID ebdff34b9e4fc6d7fde216de61684ca0d8edf573 # Parent 9b898d8b2541558891838b2af2e3493854bd0480# Parent 3c1bba376dca53c28b079319147a1370d7f5daa8 Merge diff -r 3c1bba376dca -r ebdff34b9e4f Database.cs --- 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 EnumerateRows(String query, params Object[] parameters) { + IDbConnection connection = GetConnection(); + try { + return new DataEnumerator(PrepareQuery(connection, query, parameters)); + } catch { + connection.Dispose(); + throw; + } + } + + class DataEnumerator : IEnumerable, IEnumerator { + IDbCommand command = null; + IDataReader reader = null; + public DataEnumerator(IDbCommand command) { + this.command = command; + try { + this.reader = command.ExecuteReader(); + } catch { + Dispose(); + throw; + } + } + IEnumerator IEnumerable.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 diff -r 3c1bba376dca -r ebdff34b9e4f Net/HTTP.cs --- 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) { diff -r 3c1bba376dca -r ebdff34b9e4f USBLib/Windows/USB/UsbDevice.cs --- 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'); } diff -r 3c1bba376dca -r ebdff34b9e4f USBLib/Windows/USB/UsbHub.cs --- 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; } diff -r 3c1bba376dca -r ebdff34b9e4f Util/ArrayUtil.cs --- 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(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(ref T[] array, T item) { if (array == null) return false; diff -r 3c1bba376dca -r ebdff34b9e4f Util/WorkQueue.cs --- 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;