blob: 436397ea7754c5de57c5ae4dced3645039d0067f [file] [log] [blame]
junyulai352dc2f2019-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;
junyulai0c666972019-03-04 22:45:36 +080020import android.os.ParcelFileDescriptor;
junyulai352dc2f2019-01-08 20:04:33 +080021import android.os.RemoteException;
22import android.util.Log;
23
24import java.io.FileDescriptor;
junyulai352dc2f2019-01-08 20:04:33 +080025import java.util.concurrent.Executor;
26
27/** @hide */
28final class TcpSocketKeepalive extends SocketKeepalive {
29
junyulai352dc2f2019-01-08 20:04:33 +080030 TcpSocketKeepalive(@NonNull IConnectivityManager service,
31 @NonNull Network network,
junyulai0c666972019-03-04 22:45:36 +080032 @NonNull ParcelFileDescriptor pfd,
junyulai352dc2f2019-01-08 20:04:33 +080033 @NonNull Executor executor,
34 @NonNull Callback callback) {
junyulai0c666972019-03-04 22:45:36 +080035 super(service, network, pfd, executor, callback);
junyulai352dc2f2019-01-08 20:04:33 +080036 }
37
38 /**
39 * Starts keepalives. {@code mSocket} must be a connected TCP socket.
40 *
41 * - The application must not write to or read from the socket after calling this method, until
42 * onDataReceived, onStopped, or onError are called. If it does, the keepalive will fail
43 * with {@link #ERROR_SOCKET_NOT_IDLE}, or {@code #ERROR_INVALID_SOCKET} if the socket
junyulai1d5cd192019-03-04 11:49:17 +080044 * experienced an error (as in poll(2) returned POLLERR or POLLHUP); if this happens, the data
45 * received from the socket may be invalid, and the socket can't be recovered.
junyulai352dc2f2019-01-08 20:04:33 +080046 * - If the socket has data in the send or receive buffer, then this call will fail with
47 * {@link #ERROR_SOCKET_NOT_IDLE} and can be retried after the data has been processed.
junyulai1d5cd192019-03-04 11:49:17 +080048 * An app could ensure this by using an application-layer protocol to receive acknowledgement
49 * that indicates all data has been delivered to server, e.g. HTTP 200 OK.
50 * Then the app could go into keepalive mode after reading all remaining data within the
51 * acknowledgement.
junyulai352dc2f2019-01-08 20:04:33 +080052 */
53 @Override
54 void startImpl(int intervalSec) {
junyulai7c469172019-01-16 20:23:34 +080055 mExecutor.execute(() -> {
56 try {
junyulai0c666972019-03-04 22:45:36 +080057 final FileDescriptor fd = mPfd.getFileDescriptor();
junyulai7c469172019-01-16 20:23:34 +080058 mService.startTcpKeepalive(mNetwork, fd, intervalSec, mCallback);
59 } catch (RemoteException e) {
60 Log.e(TAG, "Error starting packet keepalive: ", e);
61 throw e.rethrowFromSystemServer();
62 }
63 });
junyulai352dc2f2019-01-08 20:04:33 +080064 }
65
66 @Override
67 void stopImpl() {
junyulai7c469172019-01-16 20:23:34 +080068 mExecutor.execute(() -> {
69 try {
70 if (mSlot != null) {
71 mService.stopKeepalive(mNetwork, mSlot);
72 }
73 } catch (RemoteException e) {
74 Log.e(TAG, "Error stopping packet keepalive: ", e);
75 throw e.rethrowFromSystemServer();
junyulai352dc2f2019-01-08 20:04:33 +080076 }
junyulai7c469172019-01-16 20:23:34 +080077 });
junyulai352dc2f2019-01-08 20:04:33 +080078 }
79}