LanServerDetector
sealed class
Provides a listener for the "open to LAN" announcements a Minecraft client sends every 1.5 seconds as
[MOTD]…[/MOTD][AD]…[/AD] in UDP datagrams to the multicast group 224.0.2.60:4445.
Declaration
public sealed class LanServerDetector
| Namespace | McProtoNet |
| Assembly | McProtoNet.dll |
| Source | src/McProtoNet/Lan/LanServerDetector.cs#L26 ↗ |
Remarks
The detector owns the socket, not the enumerator. The constructor binds port 4445 and joins the
group; LanServerDetector.DisposeAsync closes the socket and ends any listen in progress. Disposing an
enumerator returned by LanServerDetector.ListenAsync does not free the port.
LanServerDetector.ListenAsync yields every announcement as it arrives, so the same world appears
repeatedly. LanServerDetector.DiscoverAsync is the one-shot form and removes
the repeats itself.
Constructors
| Signature | Description |
|---|---|
LanServerDetector() | Initializes a new instance of the LanServerDetector class that listens on every interface that carries multicast. |
LanServerDetector(IPAddress? localInterface) | Initializes a new instance of the LanServerDetector class that listens on the specified interface. |
LanServerDetector()
public LanServerDetector()
Initializes a new instance of the LanServerDetector class that listens on every
interface that carries multicast.
Exceptions: SocketException - The port could not be bound, or no group could be joined.
LanServerDetector(IPAddress?)
public LanServerDetector(IPAddress? localInterface)
Initializes a new instance of the LanServerDetector class that listens on the
specified interface.
Parameters: localInterface (IPAddress)
Exceptions: ArgumentException - localInterface is not an IPv4 address.; SocketException - The port could not be bound, or the group could not be joined.
Properties
| Signature | Description |
|---|---|
static IPAddress MulticastGroup | Gets the multicast group worlds announce to. |
MulticastGroup
public static IPAddress MulticastGroup { get; }
Gets the multicast group worlds announce to.
Returns: IPAddress
Fields
| Signature | Description |
|---|---|
const int MulticastPort = 4445 | The UDP port worlds announce to. |
MulticastPort
public const int MulticastPort = 4445
The UDP port worlds announce to.
Returns: int
Methods
| Signature | Description |
|---|---|
static Task<IReadOnlyList<LanServer>> DiscoverAsync(TimeSpan window, IPAddress? localInterface, CancellationToken cancellationToken = default) | Asynchronously collects the worlds announced during the specified window, listening on the specified interface. |
static Task<IReadOnlyList<LanServer>> DiscoverAsync(TimeSpan window, CancellationToken cancellationToken = default) | Asynchronously collects the worlds announced during the specified window, listening on every interface that carries multicast. |
ValueTask DisposeAsync() | Asynchronously releases all resources used by the current instance of the LanServerDetector class. |
IAsyncEnumerable<LanServer> ListenAsync(CancellationToken cancellationToken) | Returns an asynchronous sequence of announcements as they arrive. |
DiscoverAsync(TimeSpan, IPAddress?, CancellationToken)
public static Task<IReadOnlyList<LanServer>> DiscoverAsync(TimeSpan window, IPAddress? localInterface, CancellationToken cancellationToken = default)
Asynchronously collects the worlds announced during the specified window, listening on the specified interface.
Worlds announce every 1.5 seconds, so a window shorter than two seconds can return an empty list.
Two announcements are treated as the same world when LanServer.EndPoint matches; the
source port is not compared.
Parameters: window (TimeSpan), localInterface (IPAddress), cancellationToken (CancellationToken)
Returns: Task<IReadOnlyList<LanServer>> - A task that represents the asynchronous discovery operation. The result contains each announced world once.
Exceptions: ArgumentOutOfRangeException - window is less than or equal to TimeSpan.Zero.; ArgumentException - localInterface is not an IPv4 address.; SocketException - The port could not be bound, or the group could not be joined.; OperationCanceledException - The cancellation token was canceled. This exception is stored into the returned task.
DiscoverAsync(TimeSpan, CancellationToken)
public static Task<IReadOnlyList<LanServer>> DiscoverAsync(TimeSpan window, CancellationToken cancellationToken = default)
Asynchronously collects the worlds announced during the specified window, listening on every interface that carries multicast.
Worlds announce every 1.5 seconds, so a window shorter than two seconds can return an empty list.
Two announcements are treated as the same world when LanServer.EndPoint matches; the
source port is not compared.
Parameters: window (TimeSpan), cancellationToken (CancellationToken)
Returns: Task<IReadOnlyList<LanServer>> - A task that represents the asynchronous discovery operation. The result contains each announced world once.
Exceptions: ArgumentOutOfRangeException - window is less than or equal to TimeSpan.Zero.; SocketException - The port could not be bound, or no group could be joined.; OperationCanceledException - The cancellation token was canceled. This exception is stored into the returned task.
DisposeAsync()
public ValueTask DisposeAsync()
Asynchronously releases all resources used by the current instance of the
LanServerDetector class.
The socket is closed and the port is freed. A listen in progress ends without an error.
Returns: ValueTask - A task that represents the asynchronous dispose operation.
ListenAsync(CancellationToken)
public IAsyncEnumerable<LanServer> ListenAsync(CancellationToken cancellationToken)
Returns an asynchronous sequence of announcements as they arrive.
The sequence ends when the token is canceled, when the caller stops enumerating, or when the detector is disposed. Malformed datagrams are dropped silently.
Parameters: cancellationToken (CancellationToken)
Returns: IAsyncEnumerable<LanServer> - An asynchronous sequence of the announcements received.
Exceptions: ObjectDisposedException - The current instance has already been disposed.; InvalidOperationException - Another listen is already running on this detector.