本文整理汇总了Java中java.nio.channels.AsynchronousSocketChannel.read方法的典型用法代码示例。如果您正苦于以下问题:Java AsynchronousSocketChannel.read方法的具体用法?Java AsynchronousSocketChannel.read怎么用?Java AsynchronousSocketChannel.read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.AsynchronousSocketChannel
的用法示例。
在下文中一共展示了AsynchronousSocketChannel.read方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: completed
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
/**
*@description 接收客户端请求成功之后回调
*@time 创建时间:2017年7月20日上午11:05:16
*@param channel
*@param serverHandler
*@author dzn
*/
@Override
public void completed(AsynchronousSocketChannel channel,
AsyncTimeServerHandler serverHandler) {
System.out.println("成功接收到客户端连接 : " + channel.toString());
//继续注册一个接收请求的处理类
serverHandler.asynchronousServerSocketChannel.accept(serverHandler, this);
//创建一个数据缓冲区
ByteBuffer bb = ByteBuffer.allocate(1024);
//去读取客户端发送的数据,并注册一个数据处理类
channel.read(bb, bb, new ReadCompletionHandler(channel));
}
示例2: completed
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
@Override
public void completed(AsynchronousSocketChannel channel, AsyncServerHandler serverHandler) {
//继续接受其他客户端的请求
Server.clientCount++;
System.out.println("连接的客户端数:" + Server.clientCount);
serverHandler.channel.accept(serverHandler, this);
//创建新的Buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
//异步读 第三个参数为接收消息回调的业务Handler
channel.read(buffer, buffer, new ReadHandler(channel));
}
示例3: completed
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
@Override
public void completed(AsynchronousSocketChannel channel, AsyncServerHandler serverHandler) {
// 继续接受其他客户端的请求
Server.clientCount++;
System.out.println("连接的客户端数:" + Server.clientCount);
serverHandler.channel.accept(serverHandler, this);
// 创建新的Buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 异步读 第三个参数为接收消息回调的业务Handler
channel.read(buffer, buffer, new ReadHandler(channel));
}
示例4: completed
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
@Override
public void completed(Integer result, ByteBuffer byteBuffer)
{
GroupContext<SessionContext, P, R> groupContext = channelContext.getGroupContext();
if (result > 0)
{
ByteBuffer newByteBuffer = ByteBufferUtils.copy(readByteBuffer, 0, readByteBuffer.position());
DecodeRunnable<SessionContext, P, R> decodeRunnable = channelContext.getDecodeRunnable();
decodeRunnable.addMsg(newByteBuffer);
groupContext.getDecodeExecutor().execute(decodeRunnable);
} else if (result == 0)
{
log.error("读到的数据长度为0");
} else if (result < 0)
{
Aio.close(channelContext, null, "读数据时返回" + result);
}
if (AioUtils.checkBeforeIO(channelContext))
{
AsynchronousSocketChannel asynchronousSocketChannel = channelContext.getAsynchronousSocketChannel();
readByteBuffer.position(0);
readByteBuffer.limit(readByteBuffer.capacity());
asynchronousSocketChannel.read(readByteBuffer, readByteBuffer, this);
}
}
示例5: completed
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
@Override
public void completed(AsynchronousSocketChannel result,
AsyncTimeServerHandler attachment) {
attachment.asynchronousServerSocketChannel.accept(attachment, this);
ByteBuffer buffer = ByteBuffer.allocate(1024);
result.read(buffer, buffer, new ReadCompletionHandler(result));
}
示例6: completed
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
@Override
public void completed(AsynchronousSocketChannel result,
AsyncTimeServerHandler attachment) {
attachment.asynchronousServerSocketChannel.accept(attachment, this);
ByteBuffer buffer = ByteBuffer.allocate(1024);
result.read(buffer, buffer, new ReadCompletionHandler(result));
}
示例7: main
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
final AsynchronousServerSocketChannel ssc =
AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("localhost", 9999));
Future<AsynchronousSocketChannel> accepted = ssc.accept();
AsynchronousSocketChannel sc2 = accepted.get();
ByteBuffer bb = ByteBuffer.allocateDirect(4096);
Future<Integer> readFuture = sc2.read(bb);
}
示例8: completed
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
/**
* @see java.nio.channels.CompletionHandler#completed(java.lang.Object, java.lang.Object)
*
* @param result
* @param attachment
* @重写人: tanyaowu
* @重写时间: 2017年2月26日 下午9:39:18
*
*/
@Override
public void completed(Void result, ConnectionCompletionVo<SessionContext, P, R> attachment)
{
synchronized (attachment)
{
try
{
boolean isReconnect = attachment.isReconnect();
ClientChannelContext<SessionContext, P, R> channelContext = attachment.getChannelContext();
AsynchronousSocketChannel asynchronousSocketChannel = attachment.getAsynchronousSocketChannel();
AioClient<SessionContext, P, R> aioClient = attachment.getAioClient();
ClientGroupContext<SessionContext, P, R> clientGroupContext = aioClient.getClientGroupContext();
Node serverNode = attachment.getServerNode();
String bindIp = attachment.getBindIp();
Integer bindPort = attachment.getBindPort();
ClientAioListener<SessionContext, P, R> clientAioListener = clientGroupContext.getClientAioListener();
if (isReconnect)
{
channelContext.setAsynchronousSocketChannel(asynchronousSocketChannel);
channelContext.getDecodeRunnable().setCanceled(false);
channelContext.getHandlerRunnableNormPrior().setCanceled(false);
// channelContext.getHandlerRunnableHighPrior().setCanceled(false);
channelContext.getSendRunnableNormPrior().setCanceled(false);
// channelContext.getSendRunnableHighPrior().setCanceled(false);
clientGroupContext.getCloseds().remove(channelContext);
} else
{
channelContext = new ClientChannelContext<>(clientGroupContext, asynchronousSocketChannel);
channelContext.setServerNode(serverNode);
channelContext.getStat().setTimeClosed(SystemTimer.currentTimeMillis());
}
channelContext.setBindIp(bindIp);
channelContext.setBindPort(bindPort);
channelContext.setReconnCount(0);
channelContext.setClosed(false);
attachment.setChannelContext(channelContext);
clientGroupContext.getConnecteds().add(channelContext);
ReadCompletionHandler<SessionContext, P, R> readCompletionHandler = channelContext.getReadCompletionHandler();
ByteBuffer readByteBuffer = readCompletionHandler.getReadByteBuffer();//ByteBuffer.allocateDirect(channelContext.getGroupContext().getReadBufferSize());
readByteBuffer.position(0);
readByteBuffer.limit(readByteBuffer.capacity());
asynchronousSocketChannel.read(readByteBuffer, readByteBuffer, readCompletionHandler);
log.info("connected to {}", serverNode);
try
{
clientAioListener.onAfterConnected(channelContext, !channelContext.isClosed(), isReconnect);
} catch (Exception e1)
{
log.error(e1.toString(), e1);
}
} catch (Exception e)
{
log.error(e.toString(), e);
}
attachment.notify();
}
}
示例9: read
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
void read(AsynchronousSocketChannel socketChannel, String sessionId, AioListener listener) {
ByteBuffer buffer = aioHelper.getBuffer(sessionId);
buffer.clear();
socketChannel.read(buffer, null, BeanFactory.getBean(ReceiveHandler.class).bind(socketChannel, buffer, sessionId, listener));
}