Mercurial > stress-tester
changeset 968:0de7ca4925a3
missed NioChannelContext
author | Devel 2 |
---|---|
date | Mon, 08 Jul 2019 10:56:11 +0200 |
parents | 57e0d4394116 |
children | 7b4dfce62a6b |
files | stress-tester/src/main/java/com/passus/st/emitter/nio/NioChannelContext.java |
diffstat | 1 files changed, 83 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/emitter/nio/NioChannelContext.java Mon Jul 08 10:56:11 2019 +0200 @@ -0,0 +1,83 @@ +package com.passus.st.emitter.nio; + +import com.passus.data.ByteBuff; +import com.passus.net.SocketAddress; +import com.passus.st.emitter.ChannelContext; +import com.passus.st.emitter.SessionInfo; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.util.LinkedList; +import java.util.Queue; + +public abstract class NioChannelContext<T> implements ChannelContext { + + protected final NioEmitterWorker worker; + + protected final SessionInfo sessionInfo; + + protected final T channel; + + protected final Queue<ByteBuffer> dataQueue; + + protected SocketAddress localAddress; + + protected SocketAddress remoteAddress; + + protected SelectionKey key; + + public NioChannelContext(NioEmitterWorker worker, T channel, SocketAddress remoteAddress, SessionInfo sessionInfo) { + this.worker = worker; + this.channel = channel; + this.remoteAddress = remoteAddress; + this.sessionInfo = sessionInfo; + this.dataQueue = new LinkedList<>(); + } + + public void selectionKey(SelectionKey key) { + this.key = key; + } + + public Queue<ByteBuffer> dataQueue() { + return dataQueue; + } + + abstract boolean finishConnect() throws IOException; + + @Override + public void write(byte[] data, int offset, int length) throws IOException { + dataQueue.add(ByteBuffer.wrap(data, offset, length)); + } + + @Override + public void write(ByteBuff data) throws IOException { + dataQueue.add(data.toNioByteBuffer()); + } + + abstract int write0(ByteBuffer buffer) throws IOException; + + abstract int read0(ByteBuffer dst) throws IOException; + + @Override + public void flush() { + worker.flush(key); + } + + @Override + public void close() throws IOException { + worker.requestClose(key); + } + + @Override + public SocketAddress getRemoteAddress() { + return remoteAddress; + } + + @Override + public SessionInfo getSessionInfo() { + return sessionInfo; + } + + +}