All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.rsocket.fragmentation.ReassemblyDuplexConnection Maven / Gradle / Ivy

There is a newer version: 1.1.4
Show newest version
/*
 * Copyright 2015-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.rsocket.fragmentation;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.rsocket.DuplexConnection;
import io.rsocket.frame.FrameLengthFlyweight;
import java.util.Objects;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * A {@link DuplexConnection} implementation that reassembles {@link ByteBuf}s.
 *
 * @see Fragmentation
 *     and Reassembly
 */
public class ReassemblyDuplexConnection implements DuplexConnection {
  private final DuplexConnection delegate;
  private final FrameReassembler frameReassembler;
  private final boolean decodeLength;

  public ReassemblyDuplexConnection(DuplexConnection delegate, boolean decodeLength) {
    Objects.requireNonNull(delegate, "delegate must not be null");
    this.decodeLength = decodeLength;
    this.delegate = delegate;
    this.frameReassembler = new FrameReassembler(delegate.alloc());

    delegate.onClose().doFinally(s -> frameReassembler.dispose()).subscribe();
  }

  @Override
  public Mono send(Publisher frames) {
    return delegate.send(frames);
  }

  @Override
  public Mono sendOne(ByteBuf frame) {
    return delegate.sendOne(frame);
  }

  private ByteBuf decode(ByteBuf frame) {
    if (decodeLength) {
      return FrameLengthFlyweight.frame(frame).retain();
    } else {
      return frame;
    }
  }

  @Override
  public Flux receive() {
    return delegate
        .receive()
        .handle(
            (byteBuf, sink) -> {
              ByteBuf decode = decode(byteBuf);
              frameReassembler.reassembleFrame(decode, sink);
            });
  }

  @Override
  public ByteBufAllocator alloc() {
    return delegate.alloc();
  }

  @Override
  public Mono onClose() {
    return delegate.onClose();
  }

  @Override
  public void dispose() {
    delegate.dispose();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy