udp优化
This commit is contained in:
@@ -150,24 +150,30 @@ class AsyncNioUdpConnection extends AsyncNioConnection {
|
||||
|
||||
@Override
|
||||
protected int implWrite(ByteBuffer src) throws IOException {
|
||||
long now = System.currentTimeMillis();
|
||||
//发送过频会丢包
|
||||
|
||||
if (clientMode) {
|
||||
long now = System.currentTimeMillis();
|
||||
if (this.writeTime + 1 > now) {
|
||||
Utility.sleep(1);
|
||||
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;
|
||||
udpServerChannel.writeLock.lock();
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
if (udpServerChannel.writeTime + 1 > now) {
|
||||
Utility.sleep(1); //发送过频会丢包
|
||||
udpServerChannel.writeTime = System.currentTimeMillis();
|
||||
} else {
|
||||
udpServerChannel.writeTime = now;
|
||||
}
|
||||
return this.channel.send(src, remoteAddress);
|
||||
} finally {
|
||||
udpServerChannel.writeLock.unlock();
|
||||
}
|
||||
return this.channel.send(src, remoteAddress);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import java.nio.channels.*;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.LongAdder;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.*;
|
||||
import java.util.logging.Level;
|
||||
import org.redkale.boot.Application;
|
||||
@@ -240,6 +241,8 @@ class AsyncNioUdpProtocolServer extends ProtocolServer {
|
||||
|
||||
ByteBufferPool unsafeBufferPool;
|
||||
|
||||
final ReentrantLock writeLock = new ReentrantLock();
|
||||
|
||||
volatile long writeTime;
|
||||
|
||||
ConcurrentHashMap<SocketAddress, AsyncNioUdpConnection> connections = new ConcurrentHashMap<>();
|
||||
|
||||
Reference in New Issue
Block a user