当前位置: 首页>>代码示例>>Java>>正文


Java AsynchronousSocketChannel.read方法代码示例

本文整理汇总了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));
}
 
开发者ID:SnailFastGo,项目名称:netty_op,代码行数:23,代码来源:AcceptCompletionHandler.java

示例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));
}
 
开发者ID:xuxueli,项目名称:xxl-incubator,代码行数:12,代码来源:AcceptHandler.java

示例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));
}
 
开发者ID:minilynn,项目名称:samplecode,代码行数:12,代码来源:Server.java

示例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);
	}

}
 
开发者ID:tywo45,项目名称:talent-aio,代码行数:28,代码来源:ReadCompletionHandler.java

示例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));
   }
 
开发者ID:changyuefeng,项目名称:netty-book,代码行数:8,代码来源:AcceptCompletionHandler.java

示例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));
}
 
开发者ID:Hope6537,项目名称:hope-tactical-equipment,代码行数:8,代码来源:AcceptCompletionHandler.java

示例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);
}
 
开发者ID:lifey,项目名称:compfut,代码行数:10,代码来源:AsyncServer.java

示例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();
	}
	
	
}
 
开发者ID:tywo45,项目名称:talent-aio,代码行数:79,代码来源:ConnectionCompletionHandler.java

示例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));
}
 
开发者ID:heisedebaise,项目名称:tephra,代码行数:6,代码来源:HandlerSupport.java


注:本文中的java.nio.channels.AsynchronousSocketChannel.read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。