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