當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。