Package java.nio.channels

Examples of java.nio.channels.SocketChannel


      String initial = createInitalLineAndHeaders();     
      responseData.prepend(initial);
      headersCreated = true;
    }

    SocketChannel channel = (SocketChannel) key.channel();
    responseData.flip()// prepare for write
    try {
      channel.write(responseData.getByteBuffer());
    } catch (IOException e) {
      logger.error("ClosedChannelException during channel.write(): {}", e.getMessage());
      Closeables.closeQuietly(key.channel());
    }
    long bytesFlushed = responseData.position();
View Full Code Here


   * inserted to the HTTP response.
   *
   */
  public long finish() {
    long bytesWritten = 0;
    SocketChannel clientChannel = (SocketChannel) key.channel();
    if (clientChannel.isOpen()) {
      if (!headersCreated) {
        setEtagAndContentLength();
      }
      bytesWritten = flush();
    }
View Full Code Here

  }
 
  @Override
  public void handleAccept(SelectionKey key) throws IOException {
    logger.debug("handle accept...");
    SocketChannel clientChannel = ((ServerSocketChannel) key.channel()).accept();
    clientChannel.configureBlocking(false);
    IOLoop.INSTANCE.addHandler(clientChannel, this, SelectionKey.OP_READ, ByteBuffer.allocate(READ_BUFFER_SIZE));
  }
View Full Code Here

  }

  @Override
  public void handleRead(SelectionKey key) throws IOException {
    logger.debug("handle read...");
    SocketChannel clientChannel = (SocketChannel) key.channel();
    HttpRequest request = getHttpRequest(key, clientChannel);
   
    if (request.isKeepAlive()) {
      IOLoop.INSTANCE.addKeepAliveTimeout(
          clientChannel,
View Full Code Here

  public void handleWrite(SelectionKey key) {
    logger.debug("handle write...");
    DynamicByteBuffer dbb = (DynamicByteBuffer) key.attachment();
    logger.debug("pending data about to be written");
    ByteBuffer toSend = dbb.getByteBuffer();
    SocketChannel channel = ((SocketChannel) key.channel());
    try {
      toSend.flip()// prepare for write
      long bytesWritten = channel.write(toSend);
      if (IOLoop.INSTANCE.hasKeepAliveTimeout(channel)) {
        prolongKeepAliveTimeout(channel);
      }
      logger.debug("sent {} bytes to wire", bytesWritten);
      if (!toSend.hasRemaining()) {
View Full Code Here

   * Should only be invoked by the IOLoop
   */
  @Override
  public void handleConnect(SelectionKey key) throws IOException {
    logger.debug("handle connect...");
    SocketChannel sc = (SocketChannel) channel;
    if (sc.isConnectionPending()) {
      try {
        sc.finishConnect();
        invokeConnectSuccessfulCallback();
        interestOps &= ~SelectionKey.OP_CONNECT;
        IOLoop.INSTANCE.updateHandler(channel, interestOps |= SelectionKey.OP_READ);
      } catch (ConnectException e) {
        logger.warn("Connect failed: {}", e.getMessage());
View Full Code Here

        public void handle(SelectionKey key) throws ClosedChannelException, IOException {
            if(!key.isValid()) {
                return;
            }
            ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
            SocketChannel socketChannel = serverChannel.accept();
            if(LOG.isDebugEnabled()) {
                LOG.debug("accepted a connection from " + socketChannel.socket().getInetAddress());
            }
            socketChannel.configureBlocking(false);
            socketChannel.register(key.selector(), SelectionKey.OP_READ, nextHandler);
        }
View Full Code Here

        public void handle(SelectionKey key) throws ClosedChannelException, IOException {
            if(!key.isValid()) {
                return;
            }
            SocketChannel channel = (SocketChannel) key.channel();
            if(key.isReadable()) {
                if(doRead(channel, cmdBuffer)) {
                    key.interestOps(SelectionKey.OP_WRITE);
                } else {
                    key.selector().wakeup();
View Full Code Here

            return createSocketChannel(sockAddr, blocking, soRcvBufSize);
        }
    }

    private static SocketChannel createSocketChannel(final SocketAddress sockAddr, final boolean blocking, final int rcvbufSize) {
        final SocketChannel ch;
        try {
            ch = SocketChannel.open();
            ch.configureBlocking(blocking);
        } catch (IOException e) {
            LOG.error("Failed to open SocketChannel.", e);
            throw new IllegalStateException(e);
        }
        final Socket sock = ch.socket();
        if(rcvbufSize != -1) {
            try {
                sock.setReceiveBufferSize(rcvbufSize);
            } catch (SocketException e) {
                LOG.error("Failed to setReceiveBufferSize.", e);
                throw new IllegalStateException(e);
            }
        }
        final boolean connected;
        try {
            connected = ch.connect(sockAddr);
        } catch (IOException e) {
            LOG.error("Failed to connect socket: " + sockAddr, e);
            throw new IllegalStateException(e);
        }
        if(!connected) {
View Full Code Here

  }

  @Test
  public void sendGarbageTest() throws IOException {
    InetSocketAddress socketAddress = new InetSocketAddress(PORT);
    SocketChannel channel = SocketChannel.open(socketAddress);
    channel.write(
        ByteBuffer.wrap(
            new byte[] {1, 1, 1, 1// garbage
        )
    );
    channel.close();
  }
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.