blob: 7131784f78de24317faf5d92dfce40f5c3762894 [file] [log] [blame]
junyulai0835a1e2019-01-08 20:04:33 +08001/*
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
17package android.net;
18
19import android.annotation.NonNull;
junyulai7e06ad42019-03-04 22:45:36 +080020import android.os.ParcelFileDescriptor;
junyulai0835a1e2019-01-08 20:04:33 +080021import android.os.RemoteException;
22import android.util.Log;
23
junyulai0835a1e2019-01-08 20:04:33 +080024import java.util.concurrent.Executor;
25
26/** @hide */
Remi NGUYEN VANe55a88d2022-04-20 15:59:16 +090027public final class TcpSocketKeepalive extends SocketKeepalive {
junyulai0835a1e2019-01-08 20:04:33 +080028
Remi NGUYEN VANe55a88d2022-04-20 15:59:16 +090029 public TcpSocketKeepalive(@NonNull IConnectivityManager service,
junyulai0835a1e2019-01-08 20:04:33 +080030 @NonNull Network network,
junyulai7e06ad42019-03-04 22:45:36 +080031 @NonNull ParcelFileDescriptor pfd,
junyulai0835a1e2019-01-08 20:04:33 +080032 @NonNull Executor executor,
33 @NonNull Callback callback) {
junyulai7e06ad42019-03-04 22:45:36 +080034 super(service, network, pfd, executor, callback);
junyulai0835a1e2019-01-08 20:04:33 +080035 }
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
junyulai97767bf2019-03-04 11:49:17 +080043 * 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.
junyulai0835a1e2019-01-08 20:04:33 +080045 * - 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.
junyulai97767bf2019-03-04 11:49:17 +080047 * 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.
junyulai0835a1e2019-01-08 20:04:33 +080051 */
52 @Override
Remi NGUYEN VANe55a88d2022-04-20 15:59:16 +090053 protected void startImpl(int intervalSec) {
junyulai070f9ff2019-01-16 20:23:34 +080054 mExecutor.execute(() -> {
55 try {
Chiachang Wang04a34b62021-01-19 15:35:03 +080056 mService.startTcpKeepalive(mNetwork, mPfd, intervalSec, mCallback);
junyulai070f9ff2019-01-16 20:23:34 +080057 } catch (RemoteException e) {
58 Log.e(TAG, "Error starting packet keepalive: ", e);
59 throw e.rethrowFromSystemServer();
60 }
61 });
junyulai0835a1e2019-01-08 20:04:33 +080062 }
63
64 @Override
Remi NGUYEN VANe55a88d2022-04-20 15:59:16 +090065 protected void stopImpl() {
junyulai070f9ff2019-01-16 20:23:34 +080066 mExecutor.execute(() -> {
67 try {
68 if (mSlot != null) {
69 mService.stopKeepalive(mNetwork, mSlot);
70 }
71 } catch (RemoteException e) {
72 Log.e(TAG, "Error stopping packet keepalive: ", e);
73 throw e.rethrowFromSystemServer();
junyulai0835a1e2019-01-08 20:04:33 +080074 }
junyulai070f9ff2019-01-16 20:23:34 +080075 });
junyulai0835a1e2019-01-08 20:04:33 +080076 }
77}