Class Tunnel
- Namespace
- VpnHood.Core.Tunneling
- Assembly
- VpnHood.Core.Tunneling.dll
Represents a session's tunnel: it passes each packet straight on, to its UDP channel while UDP is on and otherwise to one of its stream channels, and hands up what either receives.
public class Tunnel : PacketTransportBase, IPacketTransport, IDisposable
- Inheritance
-
Tunnel
- Implements
- Derived
- Inherited Members
Remarks
A session with a send limit uses a ThrottledTunnel, which paces the packets first; the limit and the traffic count are the session's.
Constructors
Tunnel(TunnelOptions)
Initializes a new instance of the Tunnel class.
public Tunnel(TunnelOptions options)
Parameters
optionsTunnelOptionsThe options.
Exceptions
- ArgumentException
The options' most packet channels is less than 1.
Properties
MaxPacketChannelCount
Gets or sets the most packet channels the tunnel holds at once.
public int MaxPacketChannelCount { get; set; }
Property Value
Exceptions
- ArgumentException
The value is less than 1.
Mtu
Gets or sets the largest packet the other end takes, as the packet is: a larger one that has a channel to go on gets an ICMP "packet too big" reply, which names this size, and is dropped.
public int Mtu { get; set; }
Property Value
PacketChannelCount
Gets the number of packet channels the tunnel holds, its UDP channel aside; a removed one still closing is not counted.
public int PacketChannelCount { get; }
Property Value
StreamForwardingChannelCount
Gets the number of stream forwarding channels the tunnel holds.
public int StreamForwardingChannelCount { get; }
Property Value
UseUdp
Gets or sets a value indicating whether every packet goes on the UDP channel; the stream channels stay, and take the packets again once it is off.
public bool UseUdp { get; set; }
Property Value
Remarks
A server's stream channel whose close was lost stays until the session ends.
Exceptions
- InvalidOperationException
It is set on, and the tunnel has no UDP channel.
Methods
AddChannel(IChannel, bool)
Takes a channel and starts it: a packet channel, which carries packets, or a stream forwarding channel. The tunnel disposes it at its own end.
public void AddChannel(IChannel channel, bool disposeIfFailed = false)
Parameters
channelIChannelThe channel.
disposeIfFailedbooltrue to dispose the channel where it cannot be taken.
Remarks
Where the tunnel then holds more packet channels than its most, its closing and dead ones go first, then its oldest.
Exceptions
- ArgumentOutOfRangeException
The channel is neither a stream packet channel nor a stream forwarding channel.
- ObjectDisposedException
The tunnel is disposed.
- InvalidOperationException
The channel is started already.
- Exception
The tunnel holds the channel already.
DisposeManaged()
Releases managed resources; the base detaches the PacketReceived handlers. A derived class must catch its own errors.
protected override void DisposeManaged()
Remarks
It disposes its UDP channel, then its other channels.
GetPacketChannels()
Gets the tunnel's packet channels.
public IReadOnlyList<IStreamPacketChannel> GetPacketChannels()
Returns
- IReadOnlyList<IStreamPacketChannel>
A copy of the list, as channels are added and cleaned up meanwhile.
RemoveChannels<T>(bool)
Removes the tunnel's channels of a type, its UDP channel aside.
public void RemoveChannels<T>(bool dispose = true) where T : IChannel
Parameters
Type Parameters
TThe channels' type.
Exceptions
- ObjectDisposedException
The tunnel is disposed, and it has such a channel.
SendPacketsAsync(IReadOnlyList<IpPacket>)
Sends a batch of packets: one packet in single mode. It runs on the sending loop, one batch at a time; in passthrough mode, on the sender's thread.
protected override ValueTask SendPacketsAsync(IReadOnlyList<IpPacket> ipPackets)
Parameters
ipPacketsIReadOnlyList<IpPacket>The batch. The list is reused after the call.
Returns
- ValueTask
A task that represents the asynchronous operation.
Remarks
It must end once the transport is disposed. A throw counts the whole batch dropped and disposes it, except in passthrough mode without AutoDisposePackets, where the derived class disposes what it does not hand on; so it must not throw after handing a packet on. A packet it gives up without failing the batch, it reports with DropPacket(IpPacket), or the packet counts as sent. In passthrough mode it must complete at once and never send on its own transport.