[+] 分辨率调整
This commit is contained in:
@@ -16,7 +16,7 @@ import com.acgist.taoyao.signal.event.client.ClientCloseEvent;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 会话管理
|
||||
* 终端管理
|
||||
*
|
||||
* @author acgist
|
||||
*/
|
||||
@@ -30,7 +30,7 @@ public class ClientManager {
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
/**
|
||||
* 会话列表
|
||||
* 终端列表
|
||||
*/
|
||||
private List<Client> clients = new CopyOnWriteArrayList<>();
|
||||
|
||||
@@ -40,6 +40,8 @@ public class ClientManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 终端打开加入管理
|
||||
*
|
||||
* @param client 终端
|
||||
*/
|
||||
public void open(Client client) {
|
||||
@@ -118,7 +120,7 @@ public class ClientManager {
|
||||
/**
|
||||
* @param sn 终端标识
|
||||
*
|
||||
* @return 终端会话
|
||||
* @return 终端列表
|
||||
*/
|
||||
public List<Client> clients(String sn) {
|
||||
return this.clients().stream()
|
||||
@@ -127,7 +129,7 @@ public class ClientManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 所有终端会话
|
||||
* @return 所有终端列表
|
||||
*/
|
||||
public List<Client> clients() {
|
||||
return this.clients.stream()
|
||||
@@ -148,7 +150,7 @@ public class ClientManager {
|
||||
/**
|
||||
* @param sn 终端标识
|
||||
*
|
||||
* @return 终端状态
|
||||
* @return 终端状态列表
|
||||
*/
|
||||
public List<ClientStatus> status(String sn) {
|
||||
return this.clients(sn).stream()
|
||||
@@ -157,7 +159,7 @@ public class ClientManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 所有终端状态
|
||||
* @return 所有终端状态列表
|
||||
*/
|
||||
public List<ClientStatus> status() {
|
||||
return this.clients().stream()
|
||||
@@ -168,7 +170,7 @@ public class ClientManager {
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param instance 会话实例
|
||||
* @param instance 终端实例
|
||||
* @param message 消息
|
||||
*/
|
||||
public void send(AutoCloseable instance, Message message) {
|
||||
@@ -181,9 +183,9 @@ public class ClientManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭会话
|
||||
* 关闭终端
|
||||
*
|
||||
* @param instance 会话实例
|
||||
* @param instance 终端实例
|
||||
*/
|
||||
public void close(AutoCloseable instance) {
|
||||
final Client client = this.client(instance);
|
||||
@@ -195,7 +197,7 @@ public class ClientManager {
|
||||
instance.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("关闭会话异常", e);
|
||||
log.error("关闭终端异常", e);
|
||||
} finally {
|
||||
if(client != null) {
|
||||
// 移除管理
|
||||
@@ -207,15 +209,15 @@ public class ClientManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 定时关闭超时会话
|
||||
* 定时关闭超时终端
|
||||
*/
|
||||
private void closeTimeout() {
|
||||
log.debug("定时关闭超时会话");
|
||||
log.debug("定时关闭超时终端");
|
||||
this.clients.stream()
|
||||
.filter(v -> !v.authorized())
|
||||
.filter(v -> v.timeout(this.taoyaoProperties.getTimeout()))
|
||||
.forEach(v -> {
|
||||
log.debug("关闭超时会话:{}", v);
|
||||
log.debug("关闭超时终端:{}", v);
|
||||
this.close(v);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ public class SocketSignal {
|
||||
this.socketProperties.getThreadMin(),
|
||||
this.socketProperties.getThreadMax(),
|
||||
this.socketProperties.getKeepAliveTime(),
|
||||
TimeUnit.SECONDS,
|
||||
TimeUnit.MILLISECONDS,
|
||||
new LinkedBlockingQueue<>(this.socketProperties.getQueueSize()),
|
||||
this.newThreadFactory()
|
||||
);
|
||||
|
||||
@@ -1,24 +1,18 @@
|
||||
package com.acgist.taoyao.signal.media;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.WebSocket;
|
||||
import java.net.http.WebSocket.Listener;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -33,6 +27,7 @@ import com.acgist.taoyao.boot.model.MessageCode;
|
||||
import com.acgist.taoyao.boot.model.MessageCodeException;
|
||||
import com.acgist.taoyao.boot.property.MediaServerProperties;
|
||||
import com.acgist.taoyao.boot.property.TaoyaoProperties;
|
||||
import com.acgist.taoyao.boot.utils.HTTPUtils;
|
||||
import com.acgist.taoyao.boot.utils.JSONUtils;
|
||||
import com.acgist.taoyao.signal.protocol.Protocol;
|
||||
import com.acgist.taoyao.signal.protocol.ProtocolManager;
|
||||
@@ -104,6 +99,19 @@ public class MediaClient {
|
||||
public String name() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 心跳
|
||||
*/
|
||||
public void heartbeat() {
|
||||
final CompletableFuture<WebSocket> future = this.webSocket.sendPing(ByteBuffer.allocate(0));
|
||||
try {
|
||||
log.debug("心跳:{}", this.name);
|
||||
future.get(this.taoyaoProperties.getTimeout(), TimeUnit.MILLISECONDS);
|
||||
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||
log.error("心跳异常:{}", this.name, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接WebSocket通道
|
||||
@@ -112,14 +120,12 @@ public class MediaClient {
|
||||
final URI uri = URI.create(this.mediaServerProperties.getAddress());
|
||||
log.info("连接媒体服务:{}", uri);
|
||||
try {
|
||||
HttpClient
|
||||
.newBuilder()
|
||||
.sslContext(buildSSLContext())
|
||||
.build()
|
||||
final WebSocket webSocket = HTTPUtils.newClient()
|
||||
.newWebSocketBuilder()
|
||||
.connectTimeout(Duration.ofMillis(this.taoyaoProperties.getTimeout()))
|
||||
.buildAsync(uri, new MessageListener())
|
||||
.get();
|
||||
log.info("连接媒体服务成功:{}", webSocket);
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
log.error("连接媒体服务异常:{}", uri, e);
|
||||
this.taskScheduler.schedule(
|
||||
@@ -304,59 +310,4 @@ public class MediaClient {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* SSLContext
|
||||
*
|
||||
* @return {@link SSLContext}
|
||||
*/
|
||||
private static final SSLContext buildSSLContext() {
|
||||
try {
|
||||
// SSL协议:SSL、SSLv2、SSLv3、TLS、TLSv1、TLSv1.1、TLSv1.2、TLSv1.3
|
||||
final SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
|
||||
sslContext.init(null, new X509TrustManager[] { TaoyaoTrustManager.INSTANCE }, new SecureRandom());
|
||||
return sslContext;
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
||||
log.error("新建SSLContext异常", e);
|
||||
}
|
||||
try {
|
||||
return SSLContext.getDefault();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
log.error("新建SSLContext异常", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 证书验证
|
||||
*
|
||||
* @author acgist
|
||||
*/
|
||||
public static class TaoyaoTrustManager implements X509TrustManager {
|
||||
|
||||
private static final TaoyaoTrustManager INSTANCE = new TaoyaoTrustManager();
|
||||
|
||||
private TaoyaoTrustManager() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
if(chain == null) {
|
||||
throw new CertificateException("证书验证失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
if(chain == null) {
|
||||
throw new CertificateException("证书验证失败");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
import com.acgist.taoyao.boot.annotation.Manager;
|
||||
import com.acgist.taoyao.boot.property.MediaProperties;
|
||||
@@ -30,6 +31,11 @@ public class MediaClientManager {
|
||||
*/
|
||||
private Map<String, MediaClient> clientMap = new ConcurrentHashMap<>();
|
||||
|
||||
@Scheduled(cron = "${taoyao.scheduled.media:0 * * * * ?}")
|
||||
public void scheduled() {
|
||||
this.heartbeat();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载媒体服务终端
|
||||
*/
|
||||
@@ -52,5 +58,14 @@ public class MediaClientManager {
|
||||
public MediaClient mediaClient(String name) {
|
||||
return this.clientMap.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 心跳
|
||||
*/
|
||||
private void heartbeat() {
|
||||
this.clientMap.forEach((k, v) -> {
|
||||
v.heartbeat();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user