Beispiel #1
0
    @Override
    public final void flushNow() {
      if (inFlushNow || flushTaskInProgress != null) {
        return;
      }

      inFlushNow = true;
      ChannelHandlerContext ctx = directOutboundContext();
      Throwable cause = null;
      try {
        if (ctx.hasOutboundByteBuffer()) {
          ByteBuf out = ctx.outboundByteBuffer();
          int oldSize = out.readableBytes();
          try {
            doFlushByteBuffer(out);
          } catch (Throwable t) {
            cause = t;
          } finally {
            final int newSize = out.readableBytes();
            final int writtenBytes = oldSize - newSize;
            if (writtenBytes > 0) {
              flushFutureNotifier.increaseWriteCounter(writtenBytes);
              if (newSize == 0) {
                out.discardReadBytes();
              }
            }
          }
        } else {
          MessageBuf<Object> out = ctx.outboundMessageBuffer();
          int oldSize = out.size();
          try {
            doFlushMessageBuffer(out);
          } catch (Throwable t) {
            cause = t;
          } finally {
            flushFutureNotifier.increaseWriteCounter(oldSize - out.size());
          }
        }

        if (cause == null) {
          flushFutureNotifier.notifyFlushFutures();
        } else {
          flushFutureNotifier.notifyFlushFutures(cause);
          if (cause instanceof IOException) {
            close(voidFuture());
          }
        }
      } finally {
        inFlushNow = false;
      }
    }
  @Override
  public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
    ByteBuf in = ctx.outboundByteBuffer();

    try {
      MessageBuf<Object> out = ctx.nextOutboundMessageBuffer();
      ByteBuf payload = Unpooled.buffer(in.readableBytes());
      payload.writeBytes(in);
      out.add(new SctpMessage(streamIdentifier, protocolIdentifier, payload));
      in.discardReadBytes();
    } catch (Throwable t) {
      ctx.fireExceptionCaught(new EncoderException(t));
    }

    ctx.flush(promise);
  }
 @Override
 public ByteBuf discardReadBytes() {
   return buf.discardReadBytes();
 }
Beispiel #4
0
  @Override
  public void inboundBufferUpdated(final ChannelHandlerContext ctx) throws Exception {
    final ByteBuf in = ctx.inboundByteBuffer();

    if (in.readableBytes() < 5) {
      return;
    }

    int packetLength = getEncryptedPacketLength(in);

    if (packetLength == -1) {
      // Bad data - discard the buffer and raise an exception.
      NotSslRecordException e =
          new NotSslRecordException("not an SSL/TLS record: " + ByteBufUtil.hexDump(in));
      in.skipBytes(in.readableBytes());
      ctx.fireExceptionCaught(e);
      setHandshakeFailure(e);
      return;
    }

    assert packetLength > 0;

    final ByteBuf out = ctx.nextInboundByteBuffer();
    out.discardReadBytes();

    boolean wrapLater = false;
    int bytesProduced = 0;
    try {
      loop:
      for (; ; ) {
        SSLEngineResult result = unwrap(engine, in, out);
        bytesProduced += result.bytesProduced();

        switch (result.getStatus()) {
          case CLOSED:
            // notify about the CLOSED state of the SSLEngine. See #137
            sslCloseFuture.setClosed();
            break;
          case BUFFER_UNDERFLOW:
            break loop;
        }

        switch (result.getHandshakeStatus()) {
          case NEED_UNWRAP:
            break;
          case NEED_WRAP:
            wrapLater = true;
            break;
          case NEED_TASK:
            runDelegatedTasks();
            break;
          case FINISHED:
            setHandshakeSuccess();
            wrapLater = true;
            continue;
          case NOT_HANDSHAKING:
            break;
          default:
            throw new IllegalStateException(
                "Unknown handshake status: " + result.getHandshakeStatus());
        }

        if (result.bytesConsumed() == 0 && result.bytesProduced() == 0) {
          break;
        }
      }

      if (wrapLater) {
        flush(ctx, ctx.newFuture());
      }
    } catch (SSLException e) {
      setHandshakeFailure(e);
      throw e;
    } finally {
      if (bytesProduced > 0) {
        in.discardReadBytes();
        ctx.fireInboundBufferUpdated();
      }
    }
  }