This commit is contained in:
parent
cf21c84518
commit
3cbbaa6643
|
@ -20,6 +20,7 @@ import java.util.function.BiFunction;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public enum BinaryMessageType {
|
public enum BinaryMessageType {
|
||||||
|
keepalive(null, null),
|
||||||
|
|
||||||
online(DeviceOnlineMessage.class, BinaryDeviceOnlineMessage::new),
|
online(DeviceOnlineMessage.class, BinaryDeviceOnlineMessage::new),
|
||||||
|
|
||||||
|
@ -63,10 +64,10 @@ public enum BinaryMessageType {
|
||||||
private final Map<Integer, String> cached = CacheBuilder
|
private final Map<Integer, String> cached = CacheBuilder
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.expireAfterWrite(Duration.ofSeconds(30))
|
.expireAfterWrite(Duration.ofSeconds(30))
|
||||||
.<Integer, String> build()
|
.<Integer, String>build()
|
||||||
.asMap();
|
.asMap();
|
||||||
|
|
||||||
public synchronized int next(String id) {
|
public int next(String id) {
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -98,6 +99,15 @@ public enum BinaryMessageType {
|
||||||
return write(message, msgId, data);
|
return write(message, msgId, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ByteBuf write(BinaryMessageType type, ByteBuf data) {
|
||||||
|
// 第0个字节是消息类型
|
||||||
|
data.writeByte(type.ordinal());
|
||||||
|
// 0-4字节 时间戳
|
||||||
|
data.writeLong(System.currentTimeMillis());
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
public static ByteBuf write(DeviceMessage message, int msgId, ByteBuf data) {
|
public static ByteBuf write(DeviceMessage message, int msgId, ByteBuf data) {
|
||||||
BinaryMessageType type = lookup(message);
|
BinaryMessageType type = lookup(message);
|
||||||
// 第0个字节是消息类型
|
// 第0个字节是消息类型
|
||||||
|
|
|
@ -6,10 +6,13 @@ import io.vertx.core.Vertx;
|
||||||
import io.vertx.core.buffer.Buffer;
|
import io.vertx.core.buffer.Buffer;
|
||||||
import io.vertx.core.net.NetClientOptions;
|
import io.vertx.core.net.NetClientOptions;
|
||||||
import io.vertx.core.net.NetSocket;
|
import io.vertx.core.net.NetSocket;
|
||||||
|
import io.vertx.core.parsetools.RecordParser;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
|
import org.jetlinks.core.message.AcknowledgeDeviceMessage;
|
||||||
import org.jetlinks.core.message.DeviceMessage;
|
import org.jetlinks.core.message.DeviceMessage;
|
||||||
import org.jetlinks.core.message.DeviceOnlineMessage;
|
import org.jetlinks.core.message.DeviceOnlineMessage;
|
||||||
import org.jetlinks.core.message.property.ReadPropertyMessage;
|
import org.jetlinks.core.message.property.ReadPropertyMessage;
|
||||||
|
import org.jetlinks.core.message.property.WritePropertyMessage;
|
||||||
import org.jetlinks.protocol.official.binary.BinaryDeviceOnlineMessage;
|
import org.jetlinks.protocol.official.binary.BinaryDeviceOnlineMessage;
|
||||||
import org.jetlinks.protocol.official.binary.BinaryMessageType;
|
import org.jetlinks.protocol.official.binary.BinaryMessageType;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
@ -17,6 +20,8 @@ import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
public class TcpDevice {
|
public class TcpDevice {
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
|
@ -39,6 +44,28 @@ public class TcpDevice {
|
||||||
sink.success();
|
sink.success();
|
||||||
})
|
})
|
||||||
.onSuccess(socket -> {
|
.onSuccess(socket -> {
|
||||||
|
RecordParser parser = RecordParser.newFixed(4);
|
||||||
|
AtomicReference<Buffer> buffer = new AtomicReference<>();
|
||||||
|
parser.handler(buf -> {
|
||||||
|
buffer.accumulateAndGet(buf, (a, b) -> {
|
||||||
|
if (a == null) {
|
||||||
|
parser.fixedSizeMode(buf.getInt(0));
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
parser.fixedSizeMode(4);
|
||||||
|
|
||||||
|
sink.success("tcp-off-" + i + ":" + socket.localAddress());
|
||||||
|
|
||||||
|
BinaryMessageType
|
||||||
|
.read(b.getByteBuf(),
|
||||||
|
null,
|
||||||
|
(downstream, seq) -> {
|
||||||
|
handleDownStream(downstream, seq, socket);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
});
|
||||||
socket
|
socket
|
||||||
.closeHandler((s) -> {
|
.closeHandler((s) -> {
|
||||||
System.out.println("tcp-off-" + i + ":" + socket.localAddress() + "closed");
|
System.out.println("tcp-off-" + i + ":" + socket.localAddress() + "closed");
|
||||||
|
@ -48,20 +75,7 @@ public class TcpDevice {
|
||||||
System.out.println("tcp-off-" + i + ":" + socket.localAddress() + " " + er.getMessage());
|
System.out.println("tcp-off-" + i + ":" + socket.localAddress() + " " + er.getMessage());
|
||||||
sink.success();
|
sink.success();
|
||||||
})
|
})
|
||||||
.handler(buffer -> {
|
.handler(parser);
|
||||||
sink.success("tcp-off-" + i + ":" + socket.localAddress());
|
|
||||||
|
|
||||||
ByteBuf buf = buffer.getByteBuf();
|
|
||||||
buf.readInt();
|
|
||||||
BinaryMessageType
|
|
||||||
.read(buf,
|
|
||||||
null,
|
|
||||||
(downstream, seq) -> {
|
|
||||||
handleDownStream(downstream, seq, socket);
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
DeviceOnlineMessage message = new DeviceOnlineMessage();
|
DeviceOnlineMessage message = new DeviceOnlineMessage();
|
||||||
message.addHeader(BinaryDeviceOnlineMessage.loginToken, "test");
|
message.addHeader(BinaryDeviceOnlineMessage.loginToken, "test");
|
||||||
|
@ -70,7 +84,8 @@ public class TcpDevice {
|
||||||
socket.write(Buffer.buffer(TcpDeviceMessageCodec.wrapByteByf(BinaryMessageType.write(message, Unpooled.buffer()))));
|
socket.write(Buffer.buffer(TcpDeviceMessageCodec.wrapByteByf(BinaryMessageType.write(message, Unpooled.buffer()))));
|
||||||
|
|
||||||
});
|
});
|
||||||
})
|
}),
|
||||||
|
1024
|
||||||
)
|
)
|
||||||
.count()
|
.count()
|
||||||
.subscribe(System.out::println);
|
.subscribe(System.out::println);
|
||||||
|
@ -79,21 +94,32 @@ public class TcpDevice {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void handleDownStream(DeviceMessage downstream, int seq, NetSocket socket) {
|
protected static void handleDownStream(DeviceMessage downstream, int seq, NetSocket socket) {
|
||||||
System.out.println(downstream);
|
|
||||||
|
|
||||||
|
if (!(downstream instanceof AcknowledgeDeviceMessage)) {
|
||||||
|
// System.out.println(downstream);
|
||||||
|
}
|
||||||
|
|
||||||
|
DeviceMessage reply = null;
|
||||||
if (downstream instanceof ReadPropertyMessage) {
|
if (downstream instanceof ReadPropertyMessage) {
|
||||||
socket.write(
|
reply = ((ReadPropertyMessage) downstream)
|
||||||
Buffer.buffer(TcpDeviceMessageCodec.wrapByteByf(BinaryMessageType.write(
|
|
||||||
((ReadPropertyMessage) downstream)
|
|
||||||
.newReply()
|
.newReply()
|
||||||
.success(Collections.singletonMap(
|
.success(Collections.singletonMap(
|
||||||
((ReadPropertyMessage) downstream)
|
"temp0",
|
||||||
.getProperties()
|
|
||||||
.get(0),
|
|
||||||
ThreadLocalRandom
|
ThreadLocalRandom
|
||||||
.current()
|
.current()
|
||||||
.nextFloat() * 100
|
.nextFloat() * 100
|
||||||
))
|
));
|
||||||
|
|
||||||
|
} else if (downstream instanceof WritePropertyMessage) {
|
||||||
|
reply = ((WritePropertyMessage) downstream)
|
||||||
|
.newReply()
|
||||||
|
.success(((WritePropertyMessage) downstream).getProperties());
|
||||||
|
|
||||||
|
}
|
||||||
|
if (reply != null) {
|
||||||
|
socket.write(
|
||||||
|
Buffer.buffer(TcpDeviceMessageCodec.wrapByteByf(BinaryMessageType.write(
|
||||||
|
reply
|
||||||
, seq, Unpooled.buffer())))
|
, seq, Unpooled.buffer())))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue