jellyfin/Emby.Server.Implementations/Net/NetAcceptSocket.cs

99 lines
2.3 KiB
C#
Raw Normal View History

2016-11-08 11:44:23 -07:00
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
2017-03-12 12:27:26 -07:00
using System.Threading.Tasks;
using Emby.Server.Implementations.Networking;
2016-11-08 11:44:23 -07:00
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
2016-11-08 11:44:23 -07:00
namespace Emby.Server.Implementations.Net
2016-11-08 11:44:23 -07:00
{
2017-03-02 13:50:09 -07:00
public class NetAcceptSocket : IAcceptSocket
2016-11-08 11:44:23 -07:00
{
public Socket Socket { get; private set; }
private readonly ILogger _logger;
2016-12-07 13:02:34 -07:00
public bool DualMode { get; private set; }
2017-03-02 13:50:09 -07:00
public NetAcceptSocket(Socket socket, ILogger logger, bool isDualMode)
2016-11-08 11:44:23 -07:00
{
2016-11-13 14:04:21 -07:00
if (socket == null)
{
throw new ArgumentNullException("socket");
}
if (logger == null)
{
throw new ArgumentNullException("logger");
}
2016-11-08 11:44:23 -07:00
Socket = socket;
_logger = logger;
2016-12-07 13:02:34 -07:00
DualMode = isDualMode;
2016-11-08 11:44:23 -07:00
}
public IpEndPointInfo LocalEndPoint
{
get
{
2016-11-11 00:24:36 -07:00
return NetworkManager.ToIpEndPointInfo((IPEndPoint)Socket.LocalEndPoint);
2016-11-08 11:44:23 -07:00
}
}
public IpEndPointInfo RemoteEndPoint
{
get
{
2016-11-11 00:24:36 -07:00
return NetworkManager.ToIpEndPointInfo((IPEndPoint)Socket.RemoteEndPoint);
2016-11-08 11:44:23 -07:00
}
}
2017-03-02 22:53:21 -07:00
public void Connect(IpEndPointInfo endPoint)
{
var nativeEndpoint = NetworkManager.ToIPEndPoint(endPoint);
Socket.Connect(nativeEndpoint);
}
2016-11-08 11:44:23 -07:00
public void Close()
{
#if NET46
Socket.Close();
#else
2017-03-12 12:27:26 -07:00
Socket.Dispose();
2016-11-08 11:44:23 -07:00
#endif
}
public void Shutdown(bool both)
{
if (both)
{
Socket.Shutdown(SocketShutdown.Both);
}
else
{
// Change interface if ever needed
throw new NotImplementedException();
}
}
public void Listen(int backlog)
{
Socket.Listen(backlog);
}
public void Bind(IpEndPointInfo endpoint)
{
2016-11-11 00:24:36 -07:00
var nativeEndpoint = NetworkManager.ToIPEndPoint(endpoint);
2016-11-08 11:44:23 -07:00
Socket.Bind(nativeEndpoint);
}
public void Dispose()
{
Socket.Dispose();
2017-09-05 12:49:02 -07:00
GC.SuppressFinalize(this);
2016-11-08 11:44:23 -07:00
}
}
}