Package java.nio.channels

Examples of java.nio.channels.SocketChannel


        {
            // clean out cancelled key list
            this._selector.selectNow();
            Object[] arr = null;
            ConnectionHeader header = null;
            SocketChannel channel = null;
            SelectionKey sk = null;

            for ( ; count < size; count++ )
            {
                arr = (Object[]) this._aspirants.remove( 0 );
                header = (ConnectionHeader) arr[0];
                channel = header.getChannel();
                // set a previously blocking socket channel to non-blocking
                // mode to allow selector operations on it
                if ( channel.isBlocking() )
                {
                    logger.log( Level.FINEST,
                                "Setting socket temporarily to non-blocking until further connection processing..." );
                    channel.configureBlocking( false );
                }

                sk = channel.keyFor( this._selector );
                if ( sk == null )
                {
                    // register with no interest
                    sk = channel.register( this._selector, 0 );
                }

                // attach the client connection header
                sk.attach( header );
                // now set the requested interest
View Full Code Here


     */
    public int readHeader( ConnectionHeader header, long timeout ) throws IOException
    {
        ByteBuffer headerBuf = ByteBufferAllocator.allocate( EJConstants.NIO_HEADER_SIZE, false );
        int read = 0;
        SocketChannel channel = header.getChannel();

        try
        {
            nonBlockingRead( channel, headerBuf );
            headerBuf.flip();
            read = headerBuf.getInt();
            IOUtil.setReceiveBufferSize( channel.socket(), read );
        }
        catch ( IncompleteIOException ioe )
        {
            logger.log( Level.FINEST, "Incomplete header read detected, registering for read again." );
            // ioe.setIOBuffer(null);
View Full Code Here

     * @see de.netseeker.ejoe.io.DataChannel#writeHeader(de.netseeker.ejoe.ConnectionHeader, java.nio.ByteBuffer, long)
     */
    public void writeHeader( ConnectionHeader header, ByteBuffer buffer, long timeout ) throws IOException
    {
        ByteBuffer headerBuf = ByteBufferAllocator.allocate( EJConstants.NIO_HEADER_SIZE, false );
        SocketChannel channel = header.getChannel();

        int length = buffer != null ? buffer.remaining() : 0;
        headerBuf.putInt( length );
        headerBuf.flip();
        try
        {
            nonBlockingWrite( channel, headerBuf );
            IOUtil.setSendBufferSize( channel.socket(), length );
        }
        catch ( IncompleteIOException ioe )
        {
            logger.log( Level.FINEST, "Incomplete header write detected, registering for write again." );
            // ioe.setIOBuffer(null);
View Full Code Here

     * @see de.netseeker.ejoe.io.DataChannel#writeHeader(de.netseeker.ejoe.ConnectionHeader, java.nio.ByteBuffer, long)
     */
    public void writeHeader( ConnectionHeader header, ByteBuffer buffer, long timeout ) throws IOException
    {
        ByteBuffer headerBuf = null;
        SocketChannel channel = header.getChannel();
        boolean noBuffer = (buffer == null);
        int length = !noBuffer ? buffer.limit() : 0;
        if ( !noBuffer ) buffer.mark();

        if ( header.isClient() )
        {
            HttpRequest request = new HttpRequest( header, (header.getAttachementInfo() != null) ? header
                    .getAttachementInfo().toString() : HttpRequest.HTTP_POST );
            if ( !noBuffer ) request.addData( buffer );
            headerBuf = request.toByteBuffer();
            if ( logger.isLoggable( Level.FINEST ) )
            {
                logger.log( Level.FINEST, "Preparing to write client request with " + headerBuf.limit() + " bytes:\n"
                        + IOUtil.decodeToString( headerBuf ) );
            }
        }
        else
        {
            HttpResponse response = new HttpResponse( header, (header.getAttachementInfo() != null) ? header
                    .getAttachementInfo().toString() : HttpResponse.HTTP_OK );
            if ( !noBuffer ) response.addData( buffer );
            headerBuf = response.toByteBuffer();
            if ( logger.isLoggable( Level.FINEST ) )
            {
                logger.log( Level.FINEST, "Preparing to write server response with " + headerBuf.limit() + " bytes:\n"
                        + IOUtil.decodeToString( headerBuf ) );
            }
        }

        try
        {
            semiBlockingWrite( channel, headerBuf, timeout );
            IOUtil.setSendBufferSize( channel.socket(), length );
        }
        catch ( IncompleteIOException ioe )
        {
            logger.log( Level.FINEST, "Incomplete header write detected, skip this request." );
            throw new IncompleteIOException( null, SelectionKey.OP_WRITE );
View Full Code Here

  private static final marauroa.common.Logger logger = Log4J.getLogger(DisconnectHandler.class);

  private PlayerEntryContainer playerContainer = PlayerEntryContainer.getContainer();

  public void handleDelayedEvent(RPServerManager rpMan, Object data) {
    SocketChannel channel = (SocketChannel) data;

    playerContainer.getLock().requestWriteLock();

    PlayerEntry entry = playerContainer.get(channel);
    if (entry != null) {
View Full Code Here

            //TODO:考虑是否抛出异常
          }
         
          if(sk.isAcceptable()) {
           
            SocketChannel sc = null;
            try {
            sc = this.ssChannel.accept();
            sc.configureBlocking(false);
          } catch (IOException e) {
            //TODO:记录下此异常
            e.printStackTrace();
          }
         
View Full Code Here

   */
  public void handleDelayedEvent(RPServerManager rpMan, Object data) {
    LoadAllActiveCharactersCommand cmd = (LoadAllActiveCharactersCommand) data;
    Map<String, RPObject> characters = cmd.getCharacters();
    int clientid = cmd.getClientid();
    SocketChannel channel = cmd.getChannel();
    PlayerEntry entry = PlayerEntryContainer.getContainer().get(channel);

    // entry is null if the player has timed out during an extremely very long turn overflow
    if (entry != null) {
      entry.characterCounter = characters.keySet().size();
View Full Code Here

  @Override
  public void process(Message message) {
    try {
      int clientid = message.getClientID();
      int protocolVersion = message.getProtocolVersion();
      SocketChannel channel = message.getSocketChannel();

      if (message instanceof MessageC2SCreateCharacter) {
        MessageC2SCreateCharacter msg = (MessageC2SCreateCharacter) message;

        RPObject template = msg.getTemplate();
View Full Code Here

      return null;
    }

    long sessionId = acceptor.getNextSessionId();

    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);

    SocketAddress bindAddress = acceptor.getConfigure().getAddress();

    ClientIoSession session = new ClientIoSession(sessionId, sc, acceptor, bindAddress);
View Full Code Here

  }

 
  //TODO:考虑是否加入异常抛出
  private void registerIoSessionNow(IoSessionImpl session) throws IOException {
    SocketChannel channel = session.getChannel();
    if(channel == null) {
      throw new Error("通道不能为空");
    }

    /*if(channel.isRegistered()) {
      throw new RuntimeException("此通道已经主册过");
    }*/

   
    channel.configureBlocking(false);                //设置为非阻塞
   
    if(session.getClass() == IoSessionImpl.class) {
      //注册
      SelectionKey selectKey = channel.register(selector, SelectionKey.OP_READ, session)//向选择器注册通道

      session.setSelectionKey(selectKey);   //设置会话的选择键

      //session.setOwnerDispatcher(this);    //设置归属IO处理器

      this.onRegisterSession(session);
    }
   
    else if(session.getClass() == ClientIoSession.class) {  //注册连接操作
      ClientIoSession clientIoSession = (ClientIoSession) session;
      SelectionKey selectKey = channel.register(selector, SelectionKey.OP_CONNECT, session);
      channel.connect(clientIoSession.getConnectAddress());
      session.setSelectionKey(selectKey);
    }
   
    //开启定时检查
    session.lastAccessTime = System.currentTimeMillis();
View Full Code Here

TOP

Related Classes of java.nio.channels.SocketChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.