Class NetherNetChannel

java.lang.Object
io.netty.util.DefaultAttributeMap
io.netty.channel.AbstractChannel
org.cloudburstmc.netty.channel.nethernet.NetherNetChannel
All Implemented Interfaces:
io.netty.channel.Channel, io.netty.channel.ChannelOutboundInvoker, io.netty.util.AttributeMap, Comparable<io.netty.channel.Channel>, org.cloudburstmc.netty.channel.TransportChannel
Direct Known Subclasses:
NetherNetChildChannel, NetherNetClientChannel

public abstract class NetherNetChannel extends io.netty.channel.AbstractChannel implements org.cloudburstmc.netty.channel.TransportChannel
A NetherNet connection, carrying each message whole over a reliable and an unreliable data channel.

Writes work as on RakNet: a flushed message goes to native at once and its future completes when it is handed over, and once more than MAX_QUEUED_BYTES wait to be sent the connection ends with TransportDisconnectReason.QUEUE_TOO_LONG. Unlike RakNet, AbstractChannel.isWritable() also turns off while either data channel holds more than 512 KiB. A plain ByteBuf goes on the reliable channel, a NetherNetPacket on the one it names.

Reads ignore autoRead, as on RakNet, and each message follows a NetherNetPacket.Delivery naming its data channel. Broken framing on the reliable channel ends the connection with TransportDisconnectReason.BAD_PACKET, a message over 8 MiB, RakNet's split budget, with TransportDisconnectReason.SPLIT_QUEUE_TOO_LONG.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    One side of the selected pair carries the address the socket uses and the candidate type it was gathered as: host, srflx, prflx or relay.

    Nested classes/interfaces inherited from class io.netty.channel.AbstractChannel

    io.netty.channel.AbstractChannel.AbstractUnsafe

    Nested classes/interfaces inherited from interface io.netty.channel.Channel

    io.netty.channel.Channel.Unsafe
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
    protected @Nullable IceProxyProtocol
    The PROXY protocol relays on the ICE port, which every peer there comes through, or null when none are set.
    protected SocketAddress
     
    static final int
    Past this many bytes waiting in the data channels the connection ends, RakNet's default limit.
    protected static final io.netty.channel.ChannelMetadata
     
    protected boolean
     
    protected tel.schich.libdatachannel.PeerConnection
     
    protected boolean
    protected tel.schich.libdatachannel.DataChannel
    Read off the loop by isActive(), so it cannot be plain.
    protected SocketAddress
     
    protected SourcedAddress
    protected tel.schich.libdatachannel.DataChannel
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    NetherNetChannel(io.netty.channel.Channel parent, @Nullable SourcedAddress signaled, InetSocketAddress local)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    activate(tel.schich.libdatachannel.DataChannel reliable, tel.schich.libdatachannel.DataChannel unreliable)
    Hands this channel the data channels it carries, activating it once they are in place.
    protected @Nullable NetherNetChannel.Path
    Takes the selected pair as mediaAddress() and moves the remote address onto what reportedAddress() makes of it.
    protected void
    Starts a new attempt, so the next failure is reportable again.
    protected void
    Closes because the connection ended underneath this channel, first telling the pipeline why.
    protected void
    closePeer(@Nullable tel.schich.libdatachannel.PeerConnection peer)
    Tears the peer down, in the background, as deleting it waits for native workers.
    protected void
    Closes the data channels and peer connection, dropping their listeners first.
    io.netty.channel.ChannelConfig
     
    protected void
    Reports that this connection ended before it ever carried traffic, at most once per attempt.
    protected final void
    countReceived(int bytes)
    Counts a message delivered whole.
    protected final void
    countSent(int bytes, int segments)
    Counts a message handed to a data channel in segments segments.
    protected void
     
    protected void
    doBind(SocketAddress localAddress)
     
    protected void
     
    protected void
     
    protected void
     
    protected void
     
    protected void
    doWrite(io.netty.channel.ChannelOutboundBuffer in)
     
    protected Object
     
    protected void
    Fires channelActive for a channel that activates itself instead of through activate(tel.schich.libdatachannel.DataChannel, tel.schich.libdatachannel.DataChannel), so it reports why the connection ends like any other.
    long
     
    boolean
     
    protected boolean
    isCompatible(io.netty.channel.EventLoop loop)
     
    boolean
     
     
    protected SocketAddress
     
    int
     
    The selected pair's remote side, as taken when the channel went active.
    io.netty.channel.ChannelMetadata
     
    protected void
    readDataChannel(tel.schich.libdatachannel.DataChannel channel, boolean reliable)
    Reads a data channel from the moment the peer opens it, since libdatachannel queues what nobody reads and blocks a native worker once 1024 messages wait.
    protected void
    registerMetrics(tel.schich.libdatachannel.PeerConnection peer)
    Registers the peer callbacks that only report metrics, so neither side has to repeat them.
    Where the peer is.
    protected SocketAddress
     
    int
    The largest message the remote accepts on a data channel, or 0 while unknown.
    What remoteAddress() reports, with where it came from.
    tel.schich.libdatachannel.SctpStats
    What the SCTP association under this channel reports about itself, or null while there is none.
    protected static int
    segment(io.netty.buffer.ByteBuf framed, io.netty.buffer.ByteBufAllocator allocator, int maxPayload, Consumer<ByteBuffer> sender)
    Splits a message into segments carrying the countdown header, handing each to sender.
    The candidate pair ICE settled on, which is where the media really flows: the signaling endpoint a client dialed and the address a server saw a join from are only where the conversation started.
    void
    setDataChannels(tel.schich.libdatachannel.DataChannel reliable, tel.schich.libdatachannel.DataChannel unreliable)
    Binds the data channels this channel carries, reading any that readDataChannel(tel.schich.libdatachannel.DataChannel, boolean) does not already.
    Where signaling met the peer, kept for the channel's life: how a child's join arrived, or the address a client dialed.

    Methods inherited from class io.netty.channel.AbstractChannel

    alloc, bind, bind, bytesBeforeUnwritable, bytesBeforeWritable, close, close, closeFuture, compareTo, connect, connect, connect, connect, deregister, deregister, disconnect, disconnect, doShutdownOutput, equals, eventLoop, flush, hashCode, id, invalidateLocalAddress, invalidateRemoteAddress, isRegistered, isWritable, maxMessagesPerWrite, newChannelPipeline, newFailedFuture, newId, newProgressivePromise, newPromise, newSucceededFuture, newUnsafe, parent, pipeline, read, toString, unsafe, validateFileRegion, voidPromise, write, write, writeAndFlush, writeAndFlush

    Methods inherited from class io.netty.util.DefaultAttributeMap

    attr, hasAttr

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface io.netty.util.AttributeMap

    attr, hasAttr

    Methods inherited from interface io.netty.channel.Channel

    alloc, bytesBeforeUnwritable, bytesBeforeWritable, closeFuture, eventLoop, flush, id, isRegistered, isWritable, parent, pipeline, read, unsafe

    Methods inherited from interface io.netty.channel.ChannelOutboundInvoker

    bind, bind, close, close, connect, connect, connect, connect, deregister, deregister, disconnect, disconnect, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, voidPromise, write, write, writeAndFlush, writeAndFlush

    Methods inherited from interface java.lang.Comparable

    compareTo
  • Field Details

    • METADATA

      protected static final io.netty.channel.ChannelMetadata METADATA
    • MAX_QUEUED_BYTES

      public static final int MAX_QUEUED_BYTES
      Past this many bytes waiting in the data channels the connection ends, RakNet's default limit. What the SCTP association buffers below them does not count, so only a remote that stopped reading gets here.
      See Also:
    • config

      protected DefaultNetherChannelConfig config
    • peerConnection

      protected volatile tel.schich.libdatachannel.PeerConnection peerConnection
    • remoteAddress

      protected volatile SocketAddress remoteAddress
    • localAddress

      protected volatile SocketAddress localAddress
    • signaledAddress

      protected volatile SourcedAddress signaledAddress
    • iceRelays

      protected volatile @Nullable IceProxyProtocol iceRelays
      The PROXY protocol relays on the ICE port, which every peer there comes through, or null when none are set.
    • preferProxiedAddress

      protected volatile boolean preferProxiedAddress
    • reliableChannel

      protected volatile tel.schich.libdatachannel.DataChannel reliableChannel
      Read off the loop by isActive(), so it cannot be plain.
    • unreliableChannel

      protected tel.schich.libdatachannel.DataChannel unreliableChannel
    • open

      protected volatile boolean open
  • Constructor Details

  • Method Details

    • connectionFailed

      protected void connectionFailed(NetherConnectionFailure reason)
      Reports that this connection ended before it ever carried traffic, at most once per attempt. Both sides call it, so a failure counts the same whether this channel dialed out or was accepted.
    • clearFailureReported

      protected void clearFailureReported()
      Starts a new attempt, so the next failure is reportable again.
    • registerMetrics

      protected void registerMetrics(tel.schich.libdatachannel.PeerConnection peer)
      Registers the peer callbacks that only report metrics, so neither side has to repeat them. Call it once per peer connection, which on the client means again after every retry; closing the channel drops them with the peer's other listeners.
    • getPing

      public long getPing()
      Specified by:
      getPing in interface org.cloudburstmc.netty.channel.TransportChannel
    • sctpStats

      public tel.schich.libdatachannel.SctpStats sctpStats()
      What the SCTP association under this channel reports about itself, or null while there is none. Its counters are cumulative, so a rate comes from the difference between two reads.
      Returns:
      The association's stats, or null
    • selectedPath

      public NetherNetChannel.Path selectedPath()
      The candidate pair ICE settled on, which is where the media really flows: the signaling endpoint a client dialed and the address a server saw a join from are only where the conversation started. remoteAddress() takes the pair's remote side once active.
      Returns:
      The selected pair, or null while there is none
    • remoteMaxMessageSize

      public int remoteMaxMessageSize()
      The largest message the remote accepts on a data channel, or 0 while unknown. Larger batches are segmented before they are sent.
    • maxMessageSize

      public int maxMessageSize()
      Specified by:
      maxMessageSize in interface org.cloudburstmc.netty.channel.TransportChannel
    • activate

      protected void activate(tel.schich.libdatachannel.DataChannel reliable, tel.schich.libdatachannel.DataChannel unreliable)
      Hands this channel the data channels it carries, activating it once they are in place. Idempotent, and safe to call from a libdatachannel callback.
      Parameters:
      reliable - The reliable data channel, which this channel sends over
      unreliable - The unreliable data channel, or null when the peer opened none
    • adoptSelectedPath

      protected @Nullable NetherNetChannel.Path adoptSelectedPath()
      Takes the selected pair as mediaAddress() and moves the remote address onto what reportedAddress() makes of it. Event loop only, right before the channel goes active.
      Returns:
      The selected pair, or null when there is none and the addresses stay as they were
    • signaledAddress

      public SourcedAddress signaledAddress()
      Where signaling met the peer, kept for the channel's life: how a child's join arrived, or the address a client dialed. AddressSource.UNKNOWN when signaling did not say, as for a client dialing a network id.
    • mediaAddress

      public SourcedAddress mediaAddress()
      The selected pair's remote side, as taken when the channel went active. AddressSource.UNKNOWN before then, or when ICE reported none.
    • reportedAddress

      public SourcedAddress reportedAddress()
      What remoteAddress() reports, with where it came from. A client dialing a network id keeps that as its remote address, and this says where the peer turned out to be.
    • fireActive

      protected void fireActive()
      Fires channelActive for a channel that activates itself instead of through activate(tel.schich.libdatachannel.DataChannel, tel.schich.libdatachannel.DataChannel), so it reports why the connection ends like any other. Event loop only.
    • setDataChannels

      public void setDataChannels(tel.schich.libdatachannel.DataChannel reliable, tel.schich.libdatachannel.DataChannel unreliable)
      Binds the data channels this channel carries, reading any that readDataChannel(tel.schich.libdatachannel.DataChannel, boolean) does not already.
    • readDataChannel

      protected void readDataChannel(tel.schich.libdatachannel.DataChannel channel, boolean reliable)
      Reads a data channel from the moment the peer opens it, since libdatachannel queues what nobody reads and blocks a native worker once 1024 messages wait. Messages read before setDataChannels(tel.schich.libdatachannel.DataChannel, tel.schich.libdatachannel.DataChannel) binds it are delivered after channelActive, and past MAX_EARLY_MESSAGES the channel closes. Safe to call from a libdatachannel callback, also before this channel is registered.
      Parameters:
      channel - The data channel to read
      reliable - Whether it is the reliable one
    • closeFromTransport

      protected void closeFromTransport()
      Closes because the connection ended underneath this channel, first telling the pipeline why. Safe to call from a libdatachannel callback. Those are registered only once the channel is, and its loop stays set after it closes, so there always is a loop to close on.
    • filterOutboundMessage

      protected Object filterOutboundMessage(Object msg)
      Overrides:
      filterOutboundMessage in class io.netty.channel.AbstractChannel
    • doWrite

      protected void doWrite(io.netty.channel.ChannelOutboundBuffer in) throws Exception
      Specified by:
      doWrite in class io.netty.channel.AbstractChannel
      Throws:
      Exception
    • countReceived

      protected final void countReceived(int bytes)
      Counts a message delivered whole.
    • countSent

      protected final void countSent(int bytes, int segments)
      Counts a message handed to a data channel in segments segments.
    • segment

      protected static int segment(io.netty.buffer.ByteBuf framed, io.netty.buffer.ByteBufAllocator allocator, int maxPayload, Consumer<ByteBuffer> sender)
      Splits a message into segments carrying the countdown header, handing each to sender.

      A segment is passed as a view of a pooled buffer that is released once sender returns, so a sender that keeps the bytes must copy them. The native send does.

      Parameters:
      framed - The message to split, read absolutely so its own indexes are left alone
      allocator - Where the segment buffers come from
      maxPayload - The most payload one segment may carry, excluding the header byte
      sender - Takes each segment, in order
      Returns:
      How many segments were handed over
      Throws:
      IllegalArgumentException - as NetherNetConstants.segmentCount(int, int), before any segment is handed over
    • doRegister

      protected void doRegister() throws Exception
      Overrides:
      doRegister in class io.netty.channel.AbstractChannel
      Throws:
      Exception
    • doDeregister

      protected void doDeregister() throws Exception
      Overrides:
      doDeregister in class io.netty.channel.AbstractChannel
      Throws:
      Exception
    • doBind

      protected void doBind(SocketAddress localAddress) throws Exception
      Specified by:
      doBind in class io.netty.channel.AbstractChannel
      Throws:
      Exception
    • doDisconnect

      protected void doDisconnect() throws Exception
      Specified by:
      doDisconnect in class io.netty.channel.AbstractChannel
      Throws:
      Exception
    • doClose

      protected void doClose() throws Exception
      Specified by:
      doClose in class io.netty.channel.AbstractChannel
      Throws:
      Exception
    • closeWebRTC

      protected void closeWebRTC()
      Closes the data channels and peer connection, dropping their listeners first. The peer goes last, through closePeer(tel.schich.libdatachannel.PeerConnection), so a failing teardown cannot skip the rest.
    • closePeer

      protected void closePeer(@Nullable tel.schich.libdatachannel.PeerConnection peer)
      Tears the peer down, in the background, as deleting it waits for native workers.
      Parameters:
      peer - The peer, or null when there is none
    • doBeginRead

      protected void doBeginRead() throws Exception
      Specified by:
      doBeginRead in class io.netty.channel.AbstractChannel
      Throws:
      Exception
    • isCompatible

      protected boolean isCompatible(io.netty.channel.EventLoop loop)
      Specified by:
      isCompatible in class io.netty.channel.AbstractChannel
    • localAddress0

      protected SocketAddress localAddress0()
      Specified by:
      localAddress0 in class io.netty.channel.AbstractChannel
    • remoteAddress0

      protected SocketAddress remoteAddress0()
      Specified by:
      remoteAddress0 in class io.netty.channel.AbstractChannel
    • remoteAddress

      public SocketAddress remoteAddress()
      Where the peer is. Before channelActive, the signaled address. From channelActive on, the selected pair, where the media flows, which PROXY protocol relays on the ICE port already resolve to the client they declare. A pair through one of the peer's TURN relays, behind those relays too, keeps the signaled address instead when the signaling learned it from a connection, a trusted proxy or the host. Which candidates are relays is the peer's own claim, so a peer can always choose that address, and a LAN datagram's source, which anyone can forge, is never kept. A server can report a trusted signaling proxy's address instead, see NetherChannelOption.NETHER_SERVER_PREFER_PROXIED_ADDRESS. The pair is taken once, so a later renomination does not move it. reportedAddress() says which this is.
      Specified by:
      remoteAddress in interface io.netty.channel.Channel
      Overrides:
      remoteAddress in class io.netty.channel.AbstractChannel
    • localAddress

      public SocketAddress localAddress()
      Specified by:
      localAddress in interface io.netty.channel.Channel
      Overrides:
      localAddress in class io.netty.channel.AbstractChannel
    • config

      public io.netty.channel.ChannelConfig config()
      Specified by:
      config in interface io.netty.channel.Channel
    • isOpen

      public boolean isOpen()
      Specified by:
      isOpen in interface io.netty.channel.Channel
    • isActive

      public boolean isActive()
      Specified by:
      isActive in interface io.netty.channel.Channel
    • metadata

      public io.netty.channel.ChannelMetadata metadata()
      Specified by:
      metadata in interface io.netty.channel.Channel