mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-07 06:17:14 -04:00
40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using MediaBrowser.Model.Net;
|
|
|
|
namespace Emby.Server.Implementations.Net
|
|
{
|
|
/// <summary>
|
|
/// Factory class to create different kinds of sockets.
|
|
/// </summary>
|
|
public class SocketFactory : ISocketFactory
|
|
{
|
|
/// <inheritdoc />
|
|
public Socket CreateUdpBroadcastSocket(int localPort)
|
|
{
|
|
if (localPort < 0)
|
|
{
|
|
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
|
|
}
|
|
|
|
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|
try
|
|
{
|
|
socket.EnableBroadcast = true;
|
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
|
|
socket.Bind(new IPEndPoint(IPAddress.Any, localPort));
|
|
|
|
return socket;
|
|
}
|
|
catch
|
|
{
|
|
socket.Dispose();
|
|
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|