Skip to content

Commit cf43710

Browse files
committed
additional extending network api for mqtt implementation
1 parent 90cd907 commit cf43710

6 files changed

Lines changed: 406 additions & 278 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ss.rlib.common.function;
2+
3+
/**
4+
* @author JavaSaBr
5+
*/
6+
@FunctionalInterface
7+
public interface ByteFunction<R> {
8+
9+
R apply(byte value);
10+
}

rlib-network/src/main/java/com/ss/rlib/network/packet/impl/AbstractPacket.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.ss.rlib.logger.api.Logger;
55
import com.ss.rlib.logger.api.LoggerManager;
66
import com.ss.rlib.network.packet.Packet;
7-
import com.ss.rlib.network.util.NetworkUtils;
87
import org.jetbrains.annotations.NotNull;
98

109
import java.nio.ByteBuffer;
@@ -16,19 +15,8 @@
1615
*/
1716
public abstract class AbstractPacket implements Packet {
1817

19-
@NotNull
2018
protected static final Logger LOGGER = LoggerManager.getLogger(Packet.class);
2119

22-
/**
23-
* The name of this packet.
24-
*/
25-
@NotNull
26-
protected final String name;
27-
28-
public AbstractPacket() {
29-
this.name = getNameImpl();
30-
}
31-
3220
/**
3321
* Handle the exception.
3422
*
@@ -39,30 +27,21 @@ protected void handleException(@NotNull ByteBuffer buffer, @NotNull Exception ex
3927
LOGGER.warning(this, exception);
4028

4129
if (buffer.isDirect()) {
42-
byte[] array = new byte[buffer.limit()];
30+
var array = new byte[buffer.limit()];
4331
buffer.get(array, 0, buffer.limit());
44-
LOGGER.warning(this, "buffer " + buffer + "\n" + NetworkUtils.hexDump(array, array.length));
32+
LOGGER.warning("buffer: " + buffer + "\n" + hexDump(array, array.length));
4533
} else {
46-
LOGGER.warning(this, "buffer " + buffer + "\n" + NetworkUtils.hexDump(buffer.array(), buffer.limit()));
34+
LOGGER.warning("buffer: " + buffer + "\n" + hexDump(buffer.array(), buffer.limit()));
4735
}
4836
}
4937

50-
/**
51-
* Get the name.
52-
*
53-
* @return the name
54-
*/
55-
protected @NotNull String getNameImpl() {
56-
return getClass().getName();
57-
}
58-
5938
@Override
6039
public @NotNull String getName() {
61-
return name;
40+
return getClass().getName();
6241
}
6342

6443
@Override
6544
public String toString() {
66-
return getClass().getSimpleName() + "{" + "name='" + name + '\'' + '}';
45+
return getClass().getSimpleName() + "{" + "name='" + getName() + '\'' + '}';
6746
}
6847
}

rlib-network/src/main/java/com/ss/rlib/network/packet/impl/AbstractPacketReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ else if (waitedBytes > 0) {
169169

170170
var positionBeforeRead = endPosition;
171171
var packetLength = readPacketLength(bufferToRead);
172-
var dataLength = calcDataLength(packetLength, bufferToRead.position() - endPosition, bufferToRead);
172+
var dataLength = getDataLength(packetLength, bufferToRead.position() - endPosition, bufferToRead);
173173

174174
LOGGER.debug(packetLength, positionBeforeRead,
175175
(length, pos) -> "Find next packet from position: " + pos + " with length: " + length);
@@ -300,7 +300,7 @@ else if (packetLength > readTempBuffer.capacity()) {
300300
* @param buffer the buffer.
301301
* @return the length of packet data part.
302302
*/
303-
protected int calcDataLength(int packetLength, int readBytes, @NotNull ByteBuffer buffer) {
303+
protected int getDataLength(int packetLength, int readBytes, @NotNull ByteBuffer buffer) {
304304
return packetLength - readBytes;
305305
}
306306

0 commit comments

Comments
 (0)