微调
This commit is contained in:
@@ -15,6 +15,7 @@ public class SntpPacketService {
|
||||
private static final int MIN_PACKET_LENGTH = 48;
|
||||
private static final int CLIENT_MODE = 3;
|
||||
private static final int SERVER_MODE = 4;
|
||||
// NTP纪元偏移量:2208988800秒(1900年到1970年的秒数差)
|
||||
private static final long NTP_EPOCH_OFFSET = 2208988800L;
|
||||
private static final ZoneId SHANGHAI_ZONE = ZoneId.of("Asia/Shanghai");
|
||||
private static final byte[] REFERENCE_ID = "LOCL".getBytes(StandardCharsets.US_ASCII);
|
||||
@@ -54,11 +55,12 @@ public class SntpPacketService {
|
||||
return response;
|
||||
}
|
||||
|
||||
public SntpPushMessage toPushMessage(Instant computerInstant, Instant deviceInstant) {
|
||||
public SntpPushMessage toPushMessage(String deviceIp, Instant computerInstant, Instant deviceInstant) {
|
||||
long computerTimestampMs = computerInstant.toEpochMilli();
|
||||
long deviceTimestampMs = deviceInstant.toEpochMilli();
|
||||
return new SntpPushMessage(
|
||||
"sntp_time_update",
|
||||
deviceIp,
|
||||
formatShanghaiTime(computerInstant),
|
||||
formatShanghaiTime(deviceInstant),
|
||||
computerTimestampMs,
|
||||
|
||||
@@ -11,6 +11,8 @@ public class SntpPushMessage {
|
||||
|
||||
private String type = "sntp_time_update";
|
||||
|
||||
private String deviceIp;
|
||||
|
||||
private String computerTime;
|
||||
|
||||
private String deviceTime;
|
||||
|
||||
@@ -118,6 +118,7 @@ public class SntpServerManager {
|
||||
}
|
||||
|
||||
private void handlePacket(DatagramSocket socket, DatagramPacket packet) throws IOException {
|
||||
Instant receiveInstant = Instant.now(); //T2:服务器接收请求时间
|
||||
byte[] request = Arrays.copyOf(packet.getData(), packet.getLength());
|
||||
InetSocketAddress clientAddress = new InetSocketAddress(packet.getAddress(), packet.getPort());
|
||||
SntpExchange exchange = sntpPacketService.parseRequest(request, clientAddress);
|
||||
@@ -125,13 +126,14 @@ public class SntpServerManager {
|
||||
return;
|
||||
}
|
||||
|
||||
Instant receiveInstant = Instant.now();
|
||||
Instant transmitInstant = receiveInstant;
|
||||
Instant transmitInstant = Instant.now(); //T3:服务器发送响应时间
|
||||
byte[] response = sntpPacketService.buildResponse(exchange, receiveInstant, transmitInstant);
|
||||
DatagramPacket responsePacket = new DatagramPacket(response, response.length, packet.getAddress(), packet.getPort());
|
||||
socket.send(responsePacket);
|
||||
|
||||
SntpPushMessage pushMessage = sntpPacketService.toPushMessage(transmitInstant, exchange.getDeviceInstant());
|
||||
String deviceIp = clientAddress.getAddress().getHostAddress();
|
||||
// SntpPushMessage pushMessage = sntpPacketService.toPushMessage(deviceIp, transmitInstant, exchange.getDeviceInstant());
|
||||
SntpPushMessage pushMessage = sntpPacketService.toPushMessage(deviceIp, receiveInstant, exchange.getDeviceInstant());
|
||||
WebServiceManager.broadcast(JSON.toJSONString(pushMessage));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user