This commit is contained in:
caozehui
2026-06-08 08:47:12 +08:00
parent a4858a818e
commit 7366e7815f
3 changed files with 10 additions and 4 deletions

View File

@@ -15,6 +15,7 @@ public class SntpPacketService {
private static final int MIN_PACKET_LENGTH = 48; private static final int MIN_PACKET_LENGTH = 48;
private static final int CLIENT_MODE = 3; private static final int CLIENT_MODE = 3;
private static final int SERVER_MODE = 4; private static final int SERVER_MODE = 4;
// NTP纪元偏移量2208988800秒1900年到1970年的秒数差
private static final long NTP_EPOCH_OFFSET = 2208988800L; private static final long NTP_EPOCH_OFFSET = 2208988800L;
private static final ZoneId SHANGHAI_ZONE = ZoneId.of("Asia/Shanghai"); private static final ZoneId SHANGHAI_ZONE = ZoneId.of("Asia/Shanghai");
private static final byte[] REFERENCE_ID = "LOCL".getBytes(StandardCharsets.US_ASCII); private static final byte[] REFERENCE_ID = "LOCL".getBytes(StandardCharsets.US_ASCII);
@@ -54,11 +55,12 @@ public class SntpPacketService {
return response; return response;
} }
public SntpPushMessage toPushMessage(Instant computerInstant, Instant deviceInstant) { public SntpPushMessage toPushMessage(String deviceIp, Instant computerInstant, Instant deviceInstant) {
long computerTimestampMs = computerInstant.toEpochMilli(); long computerTimestampMs = computerInstant.toEpochMilli();
long deviceTimestampMs = deviceInstant.toEpochMilli(); long deviceTimestampMs = deviceInstant.toEpochMilli();
return new SntpPushMessage( return new SntpPushMessage(
"sntp_time_update", "sntp_time_update",
deviceIp,
formatShanghaiTime(computerInstant), formatShanghaiTime(computerInstant),
formatShanghaiTime(deviceInstant), formatShanghaiTime(deviceInstant),
computerTimestampMs, computerTimestampMs,

View File

@@ -11,6 +11,8 @@ public class SntpPushMessage {
private String type = "sntp_time_update"; private String type = "sntp_time_update";
private String deviceIp;
private String computerTime; private String computerTime;
private String deviceTime; private String deviceTime;

View File

@@ -118,6 +118,7 @@ public class SntpServerManager {
} }
private void handlePacket(DatagramSocket socket, DatagramPacket packet) throws IOException { private void handlePacket(DatagramSocket socket, DatagramPacket packet) throws IOException {
Instant receiveInstant = Instant.now(); //T2服务器接收请求时间
byte[] request = Arrays.copyOf(packet.getData(), packet.getLength()); byte[] request = Arrays.copyOf(packet.getData(), packet.getLength());
InetSocketAddress clientAddress = new InetSocketAddress(packet.getAddress(), packet.getPort()); InetSocketAddress clientAddress = new InetSocketAddress(packet.getAddress(), packet.getPort());
SntpExchange exchange = sntpPacketService.parseRequest(request, clientAddress); SntpExchange exchange = sntpPacketService.parseRequest(request, clientAddress);
@@ -125,13 +126,14 @@ public class SntpServerManager {
return; return;
} }
Instant receiveInstant = Instant.now(); Instant transmitInstant = Instant.now(); //T3服务器发送响应时间
Instant transmitInstant = receiveInstant;
byte[] response = sntpPacketService.buildResponse(exchange, receiveInstant, transmitInstant); byte[] response = sntpPacketService.buildResponse(exchange, receiveInstant, transmitInstant);
DatagramPacket responsePacket = new DatagramPacket(response, response.length, packet.getAddress(), packet.getPort()); DatagramPacket responsePacket = new DatagramPacket(response, response.length, packet.getAddress(), packet.getPort());
socket.send(responsePacket); 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)); WebServiceManager.broadcast(JSON.toJSONString(pushMessage));
} }