udp优化

This commit is contained in:
redkale
2023-02-07 17:27:55 +08:00
parent 1d6b76b182
commit 848f33bd2b
6 changed files with 37 additions and 16 deletions

View File

@@ -33,9 +33,9 @@ public abstract class AsyncConnection implements Channel, AutoCloseable {
//SSL
protected SSLEngine sslEngine;
protected volatile long readtime;
protected volatile long readTime;
protected volatile long writetime;
protected volatile long writeTime;
private Map<String, Object> attributes; //用于存储绑定在Connection上的对象集合
@@ -145,11 +145,11 @@ public abstract class AsyncConnection implements Channel, AutoCloseable {
}
public final long getLastReadTime() {
return readtime;
return readTime;
}
public final long getLastWriteTime() {
return writetime;
return writeTime;
}
public final boolean ssl() {

View File

@@ -251,7 +251,7 @@ abstract class AsyncNioConnection extends AsyncConnection {
public void doRead(boolean direct) {
try {
this.readtime = System.currentTimeMillis();
this.readTime = System.currentTimeMillis();
int readCount = 0;
if (direct) {
if (this.readByteBuffer == null) {
@@ -284,7 +284,7 @@ abstract class AsyncNioConnection extends AsyncConnection {
public void doWrite(boolean direct) {
try {
this.writetime = System.currentTimeMillis();
this.writeTime = System.currentTimeMillis();
int totalCount = 0;
boolean hasRemain = true;
boolean writeCompleted = true;

View File

@@ -13,6 +13,7 @@ import java.util.Set;
import java.util.concurrent.ConcurrentLinkedDeque;
import javax.net.ssl.SSLContext;
import org.redkale.net.AsyncNioUdpProtocolServer.AsyncNioUdpServerChannel;
import org.redkale.util.Utility;
/**
*
@@ -149,7 +150,25 @@ class AsyncNioUdpConnection extends AsyncNioConnection {
@Override
protected int implWrite(ByteBuffer src) throws IOException {
return this.channel.send(src, remoteAddress);
long now = System.currentTimeMillis();
//发送过频会丢包
if (clientMode) {
if (this.writeTime + 1 > now) {
Utility.sleep(1);
this.writeTime = System.currentTimeMillis();
} else {
this.writeTime = now;
}
return this.channel.send(src, remoteAddress);
} else {
if (udpServerChannel.writeTime + 1 > now) {
Utility.sleep(1);
udpServerChannel.writeTime = System.currentTimeMillis();
} else {
udpServerChannel.writeTime = now;
}
return this.channel.send(src, remoteAddress);
}
}
@Override
@@ -158,7 +177,7 @@ class AsyncNioUdpConnection extends AsyncNioConnection {
for (int i = offset; i < end; i++) {
ByteBuffer buf = srcs[i];
if (buf.hasRemaining()) {
return this.channel.send(buf, remoteAddress);
return implWrite(buf);
}
}
return 0;

View File

@@ -240,6 +240,8 @@ class AsyncNioUdpProtocolServer extends ProtocolServer {
ByteBufferPool unsafeBufferPool;
volatile long writeTime;
ConcurrentHashMap<SocketAddress, AsyncNioUdpConnection> connections = new ConcurrentHashMap<>();
public AsyncNioUdpServerChannel(DatagramChannel serverChannel) {