Adopt for Android

This commit is contained in:
中成才
2020-10-29 00:27:44 +08:00
parent 8f904586a3
commit c6e0533467
7 changed files with 58 additions and 27 deletions

4
.gitignore vendored
View File

@ -11,4 +11,8 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.gradle/
.idea/
build/
local.properties

View File

@ -11,6 +11,9 @@ plugins {
id "org.sonarqube" version "2.8"
}
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
repositories {
jcenter()
}

View File

@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip

View File

@ -19,10 +19,7 @@ import static nearenough.util.BytesUtil.hexToBytes;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
@ -72,11 +69,14 @@ public final class NettyClient {
// Sends the request to the Roughtime server
ctx.writeAndFlush(new DatagramPacket(encodedMsg, addr))
.addListener(future -> {
if (!future.isSuccess()) {
System.out.println("Send failed " + future.cause().getMessage());
}
});
.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
System.out.println("Send failed " + future.cause().getMessage());
}
}
});
}
@SuppressWarnings("Duplicates")
@ -116,7 +116,12 @@ public final class NettyClient {
System.out.println("Response INVALID: " + client.invalidResponseCause().getMessage());
}
ctx.close().addListener(unused -> System.exit(0));
ctx.close().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture unused) throws Exception {
System.exit(0);
}
});
}
@Override
@ -153,12 +158,15 @@ public final class NettyClient {
});
ChannelFuture connectFuture = bootstrap.connect();
connectFuture.addListener(future -> {
if (!future.isSuccess()) {
System.out.println("Connect fail:");
System.out.println(future.cause().getMessage());
}
});
connectFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
System.out.println("Connect fail:");
System.out.println(future.cause().getMessage());
}
}
});
connectFuture.channel().closeFuture().sync();
nioEventLoopGroup.shutdownGracefully();

View File

@ -27,6 +27,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import nearenough.protocol.exceptions.InvalidNumTagsException;
import nearenough.protocol.exceptions.MessageTooShortException;
import nearenough.protocol.exceptions.MessageUnalignedException;
@ -241,14 +243,17 @@ public final class RtMessage {
if (map != null) {
map.forEach(
(tag, value) -> {
sb.append(indent2).append(tag.name()).append("(").append(value.length).append(") = ");
if (tag.isNested()) {
sb.append(fromBytes(value).toString(indentLevel + 1));
} else {
sb.append(ByteBufUtil.hexDump(value)).append('\n');
}
}
new BiConsumer<RtTag, byte[]>() {
@Override
public void accept(RtTag tag, byte[] value) {
sb.append(indent2).append(tag.name()).append("(").append(value.length).append(") = ");
if (tag.isNested()) {
sb.append(fromBytes(value).toString(indentLevel + 1));
} else {
sb.append(ByteBufUtil.hexDump(value)).append('\n');
}
}
}
);
}
sb.append(indent1).append("}\n");

View File

@ -28,7 +28,12 @@ import java.util.TreeMap;
public final class RtMessageBuilder {
private final Map<RtTag, byte[]> map = new TreeMap<>(
Comparator.comparing(RtTag::valueLE, Integer::compareUnsigned)
new Comparator<RtTag>() {
@Override
public int compare(RtTag o1, RtTag o2) {
return Integer.compareUnsigned(o1.valueLE(), o2.valueLE());
}
}
);
private ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;

View File

@ -22,6 +22,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import java.util.Iterator;
import java.util.Map;
import java.util.function.ToIntFunction;
/**
* Encodes/decodes {@link RtMessage Roughtime messages} and fields to/from their on-the-wire format.
@ -68,7 +69,12 @@ public final class RtWire {
int numTagsSum = 4;
int tagsSum = 4 * map.size();
int offsetsSum = map.size() < 2 ? 0 : (4 * (map.size() - 1));
int valuesSum = map.values().stream().mapToInt(value -> value.length).sum();
int valuesSum = map.values().stream().mapToInt(new ToIntFunction<byte[]>() {
@Override
public int applyAsInt(byte[] value) {
return value.length;
}
}).sum();
return numTagsSum + tagsSum + offsetsSum + valuesSum;
}