(DatagramSocket socket, SocketAddress peer, Command cmd)
| 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]; |
no test coverage detected