junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
| 19 | import android.annotation.NonNull; |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 20 | import android.os.ParcelFileDescriptor; |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import android.util.Log; |
| 23 | |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 24 | import java.util.concurrent.Executor; |
| 25 | |
| 26 | /** @hide */ |
Remi NGUYEN VAN | e55a88d | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 27 | public final class TcpSocketKeepalive extends SocketKeepalive { |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 28 | |
Remi NGUYEN VAN | e55a88d | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 29 | public TcpSocketKeepalive(@NonNull IConnectivityManager service, |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 30 | @NonNull Network network, |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 31 | @NonNull ParcelFileDescriptor pfd, |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 32 | @NonNull Executor executor, |
| 33 | @NonNull Callback callback) { |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 34 | super(service, network, pfd, executor, callback); |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Starts keepalives. {@code mSocket} must be a connected TCP socket. |
| 39 | * |
| 40 | * - The application must not write to or read from the socket after calling this method, until |
| 41 | * onDataReceived, onStopped, or onError are called. If it does, the keepalive will fail |
| 42 | * with {@link #ERROR_SOCKET_NOT_IDLE}, or {@code #ERROR_INVALID_SOCKET} if the socket |
junyulai | 97767bf | 2019-03-04 11:49:17 +0800 | [diff] [blame] | 43 | * experienced an error (as in poll(2) returned POLLERR or POLLHUP); if this happens, the data |
| 44 | * received from the socket may be invalid, and the socket can't be recovered. |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 45 | * - If the socket has data in the send or receive buffer, then this call will fail with |
| 46 | * {@link #ERROR_SOCKET_NOT_IDLE} and can be retried after the data has been processed. |
junyulai | 97767bf | 2019-03-04 11:49:17 +0800 | [diff] [blame] | 47 | * An app could ensure this by using an application-layer protocol to receive acknowledgement |
| 48 | * that indicates all data has been delivered to server, e.g. HTTP 200 OK. |
| 49 | * Then the app could go into keepalive mode after reading all remaining data within the |
| 50 | * acknowledgement. |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 51 | */ |
| 52 | @Override |
chiachangwang | 676c84e | 2023-02-14 09:22:05 +0000 | [diff] [blame] | 53 | protected void startImpl(int intervalSec, int flags, Network underpinnedNetwork) { |
chiachangwang | 9ef4ffe | 2023-01-18 01:19:27 +0000 | [diff] [blame] | 54 | if (0 != flags) { |
| 55 | throw new IllegalArgumentException("Illegal flag value for " |
| 56 | + this.getClass().getSimpleName() + " : " + flags); |
| 57 | } |
chiachangwang | c51a705 | 2023-03-13 02:25:44 +0000 | [diff] [blame] | 58 | |
| 59 | if (underpinnedNetwork != null) { |
| 60 | throw new IllegalArgumentException("Illegal underpinned network for " |
| 61 | + this.getClass().getSimpleName() + " : " + underpinnedNetwork); |
| 62 | } |
| 63 | |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 64 | mExecutor.execute(() -> { |
| 65 | try { |
Chiachang Wang | 04a34b6 | 2021-01-19 15:35:03 +0800 | [diff] [blame] | 66 | mService.startTcpKeepalive(mNetwork, mPfd, intervalSec, mCallback); |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 67 | } catch (RemoteException e) { |
| 68 | Log.e(TAG, "Error starting packet keepalive: ", e); |
| 69 | throw e.rethrowFromSystemServer(); |
| 70 | } |
| 71 | }); |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
Remi NGUYEN VAN | e55a88d | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 75 | protected void stopImpl() { |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 76 | mExecutor.execute(() -> { |
| 77 | try { |
Chalard Jean | f0b261e | 2023-02-03 22:11:20 +0900 | [diff] [blame] | 78 | mService.stopKeepalive(mCallback); |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 79 | } catch (RemoteException e) { |
| 80 | Log.e(TAG, "Error stopping packet keepalive: ", e); |
| 81 | throw e.rethrowFromSystemServer(); |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 82 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 83 | }); |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 84 | } |
| 85 | } |