mirror of
				https://github.com/mik3y/usb-serial-for-android
				synced 2025-10-30 09:57:19 +00:00 
			
		
		
		
	improved error handling for read() with concurrent close() (#569)
reworked previous solution from change 8b9ad7ef / v3.7.1 because closeInt() was not working any more
This commit is contained in:
		
							parent
							
								
									b1362416f0
								
							
						
					
					
						commit
						b794092c81
					
				| @ -34,7 +34,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort { | |||||||
|     protected final int mPortNumber; |     protected final int mPortNumber; | ||||||
| 
 | 
 | ||||||
|     // non-null when open() |     // non-null when open() | ||||||
|     protected UsbDeviceConnection mConnection = null; |     protected UsbDeviceConnection mConnection; | ||||||
|     protected UsbEndpoint mReadEndpoint; |     protected UsbEndpoint mReadEndpoint; | ||||||
|     protected UsbEndpoint mWriteEndpoint; |     protected UsbEndpoint mWriteEndpoint; | ||||||
|     protected UsbRequest mUsbRequest; |     protected UsbRequest mUsbRequest; | ||||||
| @ -139,18 +139,18 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort { | |||||||
|         if (mConnection == null) { |         if (mConnection == null) { | ||||||
|             throw new IOException("Already closed"); |             throw new IOException("Already closed"); | ||||||
|         } |         } | ||||||
|         UsbDeviceConnection connection = mConnection; |         UsbRequest usbRequest = mUsbRequest; | ||||||
|         mConnection = null; |  | ||||||
|         try { |  | ||||||
|             mUsbRequest.cancel(); |  | ||||||
|         } catch(Exception ignored) {} |  | ||||||
|         mUsbRequest = null; |         mUsbRequest = null; | ||||||
|  |         try { | ||||||
|  |             usbRequest.cancel(); | ||||||
|  |         } catch(Exception ignored) {} | ||||||
|         try { |         try { | ||||||
|             closeInt(); |             closeInt(); | ||||||
|         } catch(Exception ignored) {} |         } catch(Exception ignored) {} | ||||||
|         try { |         try { | ||||||
|             connection.close(); |             mConnection.close(); | ||||||
|         } catch(Exception ignored) {} |         } catch(Exception ignored) {} | ||||||
|  |         mConnection = null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected abstract void closeInt(); |     protected abstract void closeInt(); | ||||||
| @ -163,7 +163,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected void testConnection(boolean full, String msg) throws IOException { |     protected void testConnection(boolean full, String msg) throws IOException { | ||||||
|         if(mConnection == null) { |         if(mUsbRequest == null) { | ||||||
|             throw new IOException("Connection closed"); |             throw new IOException("Connection closed"); | ||||||
|         } |         } | ||||||
|         if(!full) { |         if(!full) { | ||||||
| @ -187,9 +187,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort { | |||||||
|     public int read(final byte[] dest, final int length, final int timeout) throws IOException {return read(dest, length, timeout, true);} |     public int read(final byte[] dest, final int length, final int timeout) throws IOException {return read(dest, length, timeout, true);} | ||||||
| 
 | 
 | ||||||
|     protected int read(final byte[] dest, int length, final int timeout, boolean testConnection) throws IOException { |     protected int read(final byte[] dest, int length, final int timeout, boolean testConnection) throws IOException { | ||||||
|         if(mConnection == null) { |         testConnection(false); | ||||||
|             throw new IOException("Connection closed"); |  | ||||||
|         } |  | ||||||
|         if(length <= 0) { |         if(length <= 0) { | ||||||
|             throw new IllegalArgumentException("Read length too small"); |             throw new IllegalArgumentException("Read length too small"); | ||||||
|         } |         } | ||||||
| @ -240,9 +238,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort { | |||||||
|         long startTime = MonotonicClock.millis(); |         long startTime = MonotonicClock.millis(); | ||||||
|         length = Math.min(length, src.length); |         length = Math.min(length, src.length); | ||||||
| 
 | 
 | ||||||
|         if(mConnection == null) { |         testConnection(false); | ||||||
|             throw new IOException("Connection closed"); |  | ||||||
|         } |  | ||||||
|         while (offset < length) { |         while (offset < length) { | ||||||
|             int requestTimeout; |             int requestTimeout; | ||||||
|             final int requestLength; |             final int requestLength; | ||||||
| @ -295,7 +291,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort { | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public boolean isOpen() { |     public boolean isOpen() { | ||||||
|         return mConnection != null; |         return mUsbRequest != null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user