[change] 优化重连,更换重连方案

This commit is contained in:
xinfengkun
2022-06-22 17:13:44 +08:00
parent 36dc2c85b0
commit c3346feb5e
5 changed files with 112 additions and 36 deletions

View File

@@ -78,6 +78,7 @@ import com.zhidao.adas.client.bean.Warn;
import com.zhidao.adas.client.log.ConnectStatusSave;
import com.zhidao.adas.client.log.LogSave;
import com.zhidao.adas.client.utils.Constants;
import com.zhidao.adas.client.utils.PreferencesUtils;
import com.zhidao.support.adas.high.AdasManager;
import com.zhidao.support.adas.high.AdasOptions;
import com.zhidao.support.adas.high.OnAdasConnectStatusListener;
@@ -194,6 +195,7 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
onUpdateConnectStateView();
showIPCIP();
canDrawOverlays();
showHint();
// PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
// wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
// wakeLock.acquire();
@@ -209,6 +211,28 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
// wifiLock.acquire();
}
private void showHint() {
boolean isShowHint = PreferencesUtils.getBoolean(this, "show_hint", true);
if (isShowHint) {
new AlertDialog.Builder(this)
.setTitle("警告")
.setMessage("本应用运行时会影响鹰眼司机、乘客端的连接。\n使用完成后请彻底杀掉本应用并重启鹰眼")
.setNegativeButton("不再提示",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
PreferencesUtils.putBoolean(MainActivity.this, "show_hint", false);
}
})
.setPositiveButton("知道了",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
@@ -536,7 +560,7 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String str = "后台运行:" + (BackgrounderPermission.getInstance().isPermissionLongBackgroundRunning(this) ? "<font color='red'>已优化</font>" : "<font color='red'>未优化</font>");
String str = "后台运行:" + (BackgrounderPermission.getInstance().isPermissionLongBackgroundRunning(this) ? "<font color='green'>已优化</font>" : "<font color='red'>未优化</font>");
background.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_LEGACY));
background.setOnClickListener(new View.OnClickListener() {
@Override
@@ -547,7 +571,7 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
BackgrounderPermission.getInstance().setListener(new OnAdasPermissionListener() {
@Override
public void onBackgrounderPermission(boolean isAllow) {
String str = "后台运行:" + (isAllow ? "<font color='red'>已优化</font>" : "<font color='red'>未优化</font>");
String str = "后台运行:" + (isAllow ? "<font color='green'>已优化</font>" : "<font color='red'>未优化</font>");
background.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_LEGACY));
}
});

View File

@@ -29,7 +29,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginTop="3dp"
android:text="开启池优化功能,会影响熄屏或休眠后网络连接"
android:textColor="#696969" />
</LinearLayout>

View File

@@ -0,0 +1,64 @@
package com.zhidao.support.adas.high.common;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* 重连管理器
*/
public class ReconnectManager {
private static final String TAG = ReconnectManager.class.getSimpleName();
private static final long RECONNECT_INTERVAL = 4 * 1000L;//重连间隔
private final AtomicBoolean isReconnection = new AtomicBoolean(false);//是否正在重连
private final OnReconnectListener listener;
private volatile Timer timer;
public interface OnReconnectListener {
void onReconnection();
}
public ReconnectManager(OnReconnectListener listener) {
this.listener = listener;
}
/**
* @return 是否正在重连
*/
public boolean isReconnection() {
return isReconnection.get();
}
public synchronized void start() {
if (!isReconnection.get()) {
CupidLogUtils.i(TAG, "开始重连");
isReconnection.set(true);
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (isReconnection.get()) {
if (listener != null)
listener.onReconnection();
}
}
}, 0, RECONNECT_INTERVAL);//延时
}
}
}
public synchronized void stop() {
CupidLogUtils.i(TAG, "停止重连");
isReconnection.set(false);
if (timer != null) {
timer.cancel();
timer = null;
}
}
}

View File

@@ -14,8 +14,10 @@ import androidx.annotation.NonNull;
import com.google.protobuf.InvalidProtocolBufferException;
import com.zhidao.support.adas.high.AdasChannel;
import com.zhidao.support.adas.high.common.ByteUtil;
import com.zhidao.support.adas.high.common.CupidLogUtils;
import com.zhidao.support.adas.high.common.ReceiveTimeoutManager;
import com.zhidao.support.adas.high.common.ReconnectManager;
import com.zhidao.support.adas.high.queue.WSByteQueueManager;
import com.zhidao.support.adas.high.queue.WebSocketQueueManager;
import com.zhjt.service.chain.ChainLog;
@@ -52,7 +54,7 @@ public class FpgaSocket implements IWebSocket {
private EchoWebSocketListener listener;
private IWebSocketConnectListener mWebSocketConnectListener;
private ReconnectManager reconnectManager;
private String wsHost;
private String ipAddress;
@@ -66,10 +68,7 @@ public class FpgaSocket implements IWebSocket {
* 是否是被动关闭
*/
private final AtomicBoolean isPassiveClose = new AtomicBoolean(false);
/**
* 是否启用自动重连
*/
private boolean isReconnect = true;
/**
* 接收数据超时原因 null时表示不是接收数据超时的原因
*/
@@ -92,6 +91,12 @@ public class FpgaSocket implements IWebSocket {
onPassiveClose(1001, receiveTimeoutReason);
}
});
reconnectManager = new ReconnectManager(new ReconnectManager.OnReconnectListener() {
@Override
public void onReconnection() {
connect("重连中");
}
});
client = okBuilder.build();
}
@@ -113,25 +118,18 @@ public class FpgaSocket implements IWebSocket {
Request request = new Request.Builder()
.url(wsHost)
.build();
mWebSocket = client.newWebSocket(request, listener);
client.newWebSocket(request, listener);
}
}
/**
* 重连
*/
public void reconnect() {
if (isReconnect) {
if (!isUserClose.get()) {
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
connect("重连中");
} else {
isUserClose.set(false);
}
private void reconnect() {
if (!isUserClose.get()) {
reconnectManager.start();
} else {
isUserClose.set(false);
}
}
@@ -143,6 +141,7 @@ public class FpgaSocket implements IWebSocket {
}
isUserClose.set(true);
isPassiveClose.set(false);
reconnectManager.stop();
if (mWebSocket != null) {
close(true, 1000);
} else {
@@ -164,17 +163,12 @@ public class FpgaSocket implements IWebSocket {
public boolean sendDataWebSocket(ByteString data) {
if (mWebSocket != null) {
boolean result = mWebSocket.send(data);
CupidLogUtils.i(TAG, "WebSocket send ByteString= " + data + ", result= " + result);
CupidLogUtils.i(TAG, "WebSocket send ByteString= " + ByteUtil.byteArrToHex(data.toByteArray()) + ", result= " + result);
return result;
}
return false;
}
@Override
public void setIsReconnect(boolean isReconnect) {
this.isReconnect = isReconnect;
}
public interface IWebSocketConnectListener {
/**
@@ -209,8 +203,9 @@ public class FpgaSocket implements IWebSocket {
mWebSocket = webSocket;
boolean isConnect = response.code() == 101;
if (!isConnect) {
reconnect();
onPassiveClose(1001, "協議不匹配");
} else {
reconnectManager.stop();
ReceiveTimeoutManager.getInstance().start();
CupidLogUtils.i(TAG, "WebSocket 连接成功");
if (mWebSocketConnectListener != null)
@@ -330,7 +325,7 @@ public class FpgaSocket implements IWebSocket {
}
/**
* 被动关闭 包含:服务断开 心跳超时 异常断开
* 被动关闭 包含:服务断开 心跳超时 等
*
* @param reason
*/

View File

@@ -34,13 +34,6 @@ public interface IWebSocket {
boolean sendDataWebSocket(ByteString data);
/**
* 是否启用自动重连
*
* @param isReconnect
*/
void setIsReconnect(boolean isReconnect);
}