This commit is contained in:
Redkale
2020-06-24 09:25:23 +08:00
parent 730fc0a911
commit e99d43b25c
6 changed files with 68 additions and 20 deletions

View File

@@ -137,40 +137,23 @@ public abstract class AsyncConnection implements AutoCloseable {
this.readBuffer = null;
return rs;
}
// Thread thread = Thread.currentThread();
// if (thread instanceof IOThread) {
// return ((IOThread) thread).getBufferPool().get();
// }
return bufferSupplier.get();
}
public void offerBuffer(ByteBuffer buffer) {
if (buffer == null) return;
// Thread thread = Thread.currentThread();
// if (thread instanceof IOThread) {
// ((IOThread) thread).getBufferPool().accept((ByteBuffer) buffer);
// return;
// }
bufferConsumer.accept(buffer);
}
public void offerBuffer(ByteBuffer... buffers) {
if (buffers == null) return;
Consumer<ByteBuffer> consumer = this.bufferConsumer;
// Thread thread = Thread.currentThread();
// if (thread instanceof IOThread) {
// consumer = ((IOThread) thread).getBufferPool();
// }
for (ByteBuffer buffer : buffers) {
consumer.accept(buffer);
}
}
public ByteBuffer pollWriteBuffer() {
// Thread thread = Thread.currentThread();
// if (thread instanceof IOThread) {
// return ((IOThread) thread).getBufferPool().get();
// }
return bufferSupplier.get();
}

View File

@@ -6,8 +6,13 @@
package org.redkale.net.nio;
/**
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.1.0
*/
public abstract class AbstractLoop extends Thread {

View File

@@ -0,0 +1,48 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.net.nio;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.*;
/**
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.1.0
*/
class CompletionHandlerRunner<A> implements CompletionHandler<Integer, A>, Runnable {
private final CompletionHandler<Integer, A> handler;
private final A attachment;
ScheduledFuture timeoutFuture;
public CompletionHandlerRunner(CompletionHandler<Integer, A> handler, A attachment) {
this.handler = handler;
this.attachment = attachment;
}
@Override
public void completed(Integer result, A attach) {
handler.completed(result, attachment);
}
@Override
public void failed(Throwable exc, A attach) {
handler.failed(exc, attachment);
}
@Override
public void run() {
handler.failed(new TimeoutException(), attachment);
}
}

View File

@@ -10,8 +10,13 @@ import java.nio.channels.*;
import java.util.*;
/**
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.1.0
*/
public abstract class NioEventLoop extends AbstractLoop {

View File

@@ -3,7 +3,7 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.net;
package org.redkale.net.nio;
import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
@@ -16,8 +16,10 @@ import org.redkale.util.*;
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.1.0
*/
public class IOThread extends Thread {
public class NioThread extends Thread {
protected Thread localThread;
@@ -25,7 +27,7 @@ public class IOThread extends Thread {
protected ObjectPool<ByteBuffer> bufferPool;
public IOThread(ExecutorService executor, ObjectPool<ByteBuffer> bufferPool, Runnable runner) {
public NioThread(ExecutorService executor, ObjectPool<ByteBuffer> bufferPool, Runnable runner) {
super(runner);
this.executor = executor;
this.bufferPool = bufferPool;

View File

@@ -6,8 +6,13 @@
package org.redkale.net.nio;
/**
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.1.0
*/
public class NioWorkerThread extends NioEventLoop {