aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPatrick Barron <barronpm@gmail.com>2023-11-30 12:03:13 -0500
committerPatrick Barron <barronpm@gmail.com>2023-11-30 12:03:58 -0500
commitfc1e27b7549014dbf1de16f2805c65f8a624fb2b (patch)
treeba297ccb7dcbe61737d8a1be4486a52e0c85644c /src
parentf1ca1dd7cc14e59938a73a34c2561856d706312b (diff)
Move SocketFactory to Jellyfin.Networking
Diffstat (limited to 'src')
-rw-r--r--src/Jellyfin.Networking/Udp/SocketFactory.cs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/Jellyfin.Networking/Udp/SocketFactory.cs b/src/Jellyfin.Networking/Udp/SocketFactory.cs
new file mode 100644
index 000000000..c4e1bd091
--- /dev/null
+++ b/src/Jellyfin.Networking/Udp/SocketFactory.cs
@@ -0,0 +1,39 @@
+using System;
+using System.Net;
+using System.Net.Sockets;
+using MediaBrowser.Model.Net;
+
+namespace Jellyfin.Networking.Udp
+{
+ /// <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;
+ }
+ }
+ }
+}