Esempio n. 1
0
 public void receive() {
   EndPoint endPoint = connection.getEndPoint();
   HttpClient client = connection.getHttpClient();
   ByteBufferPool bufferPool = client.getByteBufferPool();
   ByteBuffer buffer = bufferPool.acquire(client.getResponseBufferSize(), true);
   try {
     while (true) {
       // Connection may be closed in a parser callback
       if (connection.isClosed()) {
         LOG.debug("{} closed", connection);
         break;
       } else {
         int read = endPoint.fill(buffer);
         LOG.debug("Read {} bytes from {}", read, connection);
         if (read > 0) {
           parse(buffer);
         } else if (read == 0) {
           fillInterested();
           break;
         } else {
           shutdown();
           break;
         }
       }
     }
   } catch (EofException x) {
     LOG.ignore(x);
     failAndClose(x);
   } catch (Exception x) {
     LOG.debug(x);
     failAndClose(x);
   } finally {
     bufferPool.release(buffer);
   }
 }
Esempio n. 2
0
 private int fill(EndPoint endPoint, ByteBuffer buffer) {
   try {
     if (endPoint.isInputShutdown()) return -1;
     return endPoint.fill(buffer);
   } catch (IOException x) {
     endPoint.close();
     throw new RuntimeIOException(x);
   }
 }
 /**
  * Read / Parse the waiting read/fill buffer
  *
  * @param buffer the buffer to fill into from the endpoint
  * @return true if there is more to read, false if reading should stop
  */
 private boolean read(ByteBuffer buffer) {
   EndPoint endPoint = getEndPoint();
   try {
     while (true) {
       int filled = endPoint.fill(buffer);
       if (filled == 0) {
         return true;
       } else if (filled < 0) {
         LOG.debug("read - EOF Reached");
         return false;
       } else {
         if (LOG.isDebugEnabled()) {
           LOG.debug("Filled {} bytes - {}", filled, BufferUtil.toDetailString(buffer));
         }
         ClientUpgradeResponse resp = parser.parse(buffer);
         if (resp != null) {
           // Got a response!
           client.setUpgradeResponse(resp);
           validateResponse(resp);
           notifyConnect(resp);
           upgradeConnection(resp);
           return false; // do no more reading
         }
       }
     }
   } catch (IOException e) {
     LOG.warn(e);
     client.failed(e);
     disconnect(false);
     return false;
   } catch (UpgradeException e) {
     LOG.warn(e);
     client.failed(e);
     disconnect(false);
     return false;
   }
 }