diff options
| author | Xiao Ma <xiaom@google.com> | 2022-06-04 20:21:37 +0900 |
|---|---|---|
| committer | Xiao Ma <xiaom@google.com> | 2022-06-09 21:11:07 +0900 |
| commit | becbc679e655085454efc9a16fddf665773433cb (patch) | |
| tree | 10bd664575abb2e6d4b13fc4b1e162b350842e95 | |
| parent | 7f63f0ff8055f0ec0cbd949fa1f2c40537b9b1e3 (diff) | |
Add handlePacketReadError method in FdEventsReader.
This method allows the subclasses of FdEventsReader to decide whether
it should stop reading packet and unregister the fd immediately or just
ignore when some specific error other than EAGAIN or EINTR happens such
as ENOBUFS.
Bug: 232680956
Test: m
Merged-In: I19ee9390e78dcd0140e45fed75a1a36942450407
Change-Id: I19ee9390e78dcd0140e45fed75a1a36942450407
| -rw-r--r-- | common/device/com/android/net/module/util/FdEventsReader.java | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/common/device/com/android/net/module/util/FdEventsReader.java b/common/device/com/android/net/module/util/FdEventsReader.java index ecf8e77..4825992 100644 --- a/common/device/com/android/net/module/util/FdEventsReader.java +++ b/common/device/com/android/net/module/util/FdEventsReader.java @@ -69,6 +69,7 @@ import java.io.IOException; * @param <BufferType> the type of the buffer used to read data. */ public abstract class FdEventsReader<BufferType> { + private static final String TAG = FdEventsReader.class.getSimpleName(); private static final int FD_EVENTS = EVENT_INPUT | EVENT_ERROR; private static final int UNREGISTER_THIS_FD = 0; @@ -167,6 +168,18 @@ public abstract class FdEventsReader<BufferType> { protected void handlePacket(@NonNull BufferType recvbuf, int length) {} /** + * Called by the subclasses of FdEventsReader, decide whether it should stop reading packet or + * just ignore the specific error other than EAGAIN or EINTR. + * + * @return {@code true} if this FdEventsReader should stop reading from the socket. + * {@code false} if it should continue. + */ + protected boolean handleReadError(@NonNull ErrnoException e) { + logError("readPacket error: ", e); + return true; // by default, stop reading on any error. + } + + /** * Called by the main loop to log errors. In some cases |e| may be null. */ protected void logError(@NonNull String msg, @Nullable Exception e) {} @@ -234,8 +247,10 @@ public abstract class FdEventsReader<BufferType> { } else if (e.errno == OsConstants.EINTR) { continue; } else { - if (isRunning()) logError("readPacket error: ", e); - break; + if (!isRunning()) break; + final boolean shouldStop = handleReadError(e); + if (shouldStop) break; + continue; } } catch (Exception e) { if (isRunning()) logError("readPacket error: ", e); @@ -246,7 +261,7 @@ public abstract class FdEventsReader<BufferType> { handlePacket(mBuffer, bytesRead); } catch (Exception e) { logError("handlePacket error: ", e); - Log.wtf(FdEventsReader.class.getSimpleName(), "Error handling packet", e); + Log.wtf(TAG, "Error handling packet", e); } } |
