MCPcopy Create free account
hub / github.com/e2wugui/zeze / sendCommand

Method sendCommand

ZezeJava/ZezeJava/src/main/java/Zeze/Services/Daemon.java:189–221  ·  view source on GitHub ↗
(DatagramSocket socket, SocketAddress peer, Command cmd)

Source from the content-addressed store, hash-verified

187 }
188
189 public static void sendCommand(DatagramSocket socket, SocketAddress peer, Command cmd) throws IOException {
190 var bb = ByteBuffer.Allocate(5);
191 bb.WriteInt(cmd.command());
192 cmd.encode(bb);
193 var p = new DatagramPacket(bb.Bytes, 0, bb.WriteIndex, peer);
194 if (cmd.isRequest()) {
195 if (pendings.putIfAbsent(cmd.reliableSerialNo, new PendingPacket(socket, p)) != null)
196 throw new IllegalStateException("Duplicate ReliableSerialNo=" + cmd.reliableSerialNo);
197
198 // auto start Timer
199 if (timer == null) {
200 pendingsLock.lock();
201 try {
202 if (timer == null) {
203 timer = Task.scheduleUnsafe(1000, 1000, () -> {
204 var now = System.currentTimeMillis();
205 for (var pending : pendings) {
206 if (now - pending.sendTime > 1000) {
207 pending.sendTime = now;
208 pending.socket.send(pending.packet);
209 }
210 }
211 });
212 //noinspection DataFlowIssue
213 ShutdownHook.add(() -> timer.cancel(false));
214 }
215 } finally {
216 pendingsLock.unlock();
217 }
218 }
219 }
220 socket.send(p);
221 }
222
223 public static Command receiveCommand(DatagramSocket socket) throws IOException {
224 var buf = new byte[1024];

Callers 5

deadlockReportMethod · 0.95
onInitializeMethod · 0.95
ProcessDaemonMethod · 0.95
mainRunMethod · 0.95
runMethod · 0.80

Calls 12

AllocateMethod · 0.95
scheduleUnsafeMethod · 0.95
addMethod · 0.95
encodeMethod · 0.65
putIfAbsentMethod · 0.65
lockMethod · 0.65
sendMethod · 0.65
cancelMethod · 0.65
unlockMethod · 0.65
WriteIntMethod · 0.45
commandMethod · 0.45
isRequestMethod · 0.45

Tested by

no test coverage detected