mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-21 01:24:44 +01:00
add request logging
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
using Emby.Common.Implementations.Networking;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Net;
|
||||
|
||||
@@ -18,11 +19,6 @@ namespace Emby.Common.Implementations.Net
|
||||
// but that wasn't really the point so kept to YAGNI principal for now, even if the
|
||||
// interfaces are a bit ugly, specific and make assumptions.
|
||||
|
||||
/// <summary>
|
||||
/// Used by RSSDP components to create implementations of the <see cref="IUdpSocket"/> interface, to perform platform agnostic socket communications.
|
||||
/// </summary>
|
||||
private IPAddress _LocalIP;
|
||||
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public SocketFactory(ILogger logger)
|
||||
@@ -33,7 +29,6 @@ namespace Emby.Common.Implementations.Net
|
||||
}
|
||||
|
||||
_logger = logger;
|
||||
_LocalIP = IPAddress.Any;
|
||||
}
|
||||
|
||||
public ISocket CreateSocket(IpAddressFamily family, MediaBrowser.Model.Net.SocketType socketType, MediaBrowser.Model.Net.ProtocolType protocolType, bool dualMode)
|
||||
@@ -66,7 +61,7 @@ namespace Emby.Common.Implementations.Net
|
||||
try
|
||||
{
|
||||
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||||
return new UdpSocket(retVal, localPort, _LocalIP);
|
||||
return new UdpSocket(retVal, localPort, IPAddress.Any);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -80,9 +75,8 @@ namespace Emby.Common.Implementations.Net
|
||||
/// <summary>
|
||||
/// Creates a new UDP socket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
|
||||
/// </summary>
|
||||
/// <param name="localPort">An integer specifying the local port to bind the socket to.</param>
|
||||
/// <returns>An implementation of the <see cref="IUdpSocket"/> interface used by RSSDP components to perform socket operations.</returns>
|
||||
public IUdpSocket CreateSsdpUdpSocket(int localPort)
|
||||
public IUdpSocket CreateSsdpUdpSocket(IpAddressInfo localIpAddress, int localPort)
|
||||
{
|
||||
if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
|
||||
|
||||
@@ -91,8 +85,11 @@ namespace Emby.Common.Implementations.Net
|
||||
{
|
||||
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||||
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
|
||||
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), _LocalIP));
|
||||
return new UdpSocket(retVal, localPort, _LocalIP);
|
||||
|
||||
var localIp = NetworkManager.ToIPAddress(localIpAddress);
|
||||
|
||||
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), localIp));
|
||||
return new UdpSocket(retVal, localPort, localIp);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -134,10 +131,13 @@ namespace Emby.Common.Implementations.Net
|
||||
//retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
|
||||
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||||
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
|
||||
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(ipAddress), _LocalIP));
|
||||
|
||||
var localIp = IPAddress.Any;
|
||||
|
||||
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(ipAddress), localIp));
|
||||
retVal.MulticastLoopback = true;
|
||||
|
||||
return new UdpSocket(retVal, localPort, _LocalIP);
|
||||
return new UdpSocket(retVal, localPort, localIp);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
@@ -20,7 +20,6 @@ namespace Emby.Common.Implementations.Net
|
||||
|
||||
private Socket _Socket;
|
||||
private int _LocalPort;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
@@ -31,12 +30,19 @@ namespace Emby.Common.Implementations.Net
|
||||
|
||||
_Socket = socket;
|
||||
_LocalPort = localPort;
|
||||
LocalIPAddress = NetworkManager.ToIpAddressInfo(ip);
|
||||
|
||||
_Socket.Bind(new IPEndPoint(ip, _LocalPort));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public IpAddressInfo LocalIPAddress
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
#region IUdpSocket Members
|
||||
|
||||
public Task<SocketReceiveResult> ReceiveAsync()
|
||||
@@ -50,18 +56,18 @@ namespace Emby.Common.Implementations.Net
|
||||
state.TaskCompletionSource = tcs;
|
||||
|
||||
#if NETSTANDARD1_6
|
||||
_Socket.ReceiveFromAsync(new ArraySegment<Byte>(state.Buffer),SocketFlags.None, state.EndPoint)
|
||||
_Socket.ReceiveFromAsync(new ArraySegment<Byte>(state.Buffer),SocketFlags.None, state.RemoteEndPoint)
|
||||
.ContinueWith((task, asyncState) =>
|
||||
{
|
||||
if (task.Status != TaskStatus.Faulted)
|
||||
{
|
||||
var receiveState = asyncState as AsyncReceiveState;
|
||||
receiveState.EndPoint = task.Result.RemoteEndPoint;
|
||||
ProcessResponse(receiveState, () => task.Result.ReceivedBytes);
|
||||
receiveState.RemoteEndPoint = task.Result.RemoteEndPoint;
|
||||
ProcessResponse(receiveState, () => task.Result.ReceivedBytes, LocalIPAddress);
|
||||
}
|
||||
}, state);
|
||||
#else
|
||||
_Socket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, ref state.EndPoint, ProcessResponse, state);
|
||||
_Socket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, ref state.RemoteEndPoint, ProcessResponse, state);
|
||||
#endif
|
||||
|
||||
return tcs.Task;
|
||||
@@ -74,6 +80,8 @@ namespace Emby.Common.Implementations.Net
|
||||
if (buffer == null) throw new ArgumentNullException("messageData");
|
||||
if (endPoint == null) throw new ArgumentNullException("endPoint");
|
||||
|
||||
var ipEndPoint = NetworkManager.ToIPEndPoint(endPoint);
|
||||
|
||||
#if NETSTANDARD1_6
|
||||
|
||||
if (size != buffer.Length)
|
||||
@@ -83,14 +91,14 @@ namespace Emby.Common.Implementations.Net
|
||||
buffer = copy;
|
||||
}
|
||||
|
||||
_Socket.SendTo(buffer, new IPEndPoint(IPAddress.Parse(endPoint.IpAddress.ToString()), endPoint.Port));
|
||||
_Socket.SendTo(buffer, ipEndPoint);
|
||||
return Task.FromResult(true);
|
||||
#else
|
||||
var taskSource = new TaskCompletionSource<bool>();
|
||||
|
||||
try
|
||||
{
|
||||
_Socket.BeginSendTo(buffer, 0, size, SocketFlags.None, new System.Net.IPEndPoint(IPAddress.Parse(endPoint.IpAddress.ToString()), endPoint.Port), result =>
|
||||
_Socket.BeginSendTo(buffer, 0, size, SocketFlags.None, ipEndPoint, result =>
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -109,7 +117,7 @@ namespace Emby.Common.Implementations.Net
|
||||
taskSource.TrySetException(ex);
|
||||
}
|
||||
|
||||
//_Socket.SendTo(messageData, new System.Net.IPEndPoint(IPAddress.Parse(endPoint.IPAddress), endPoint.Port));
|
||||
//_Socket.SendTo(messageData, new System.Net.IPEndPoint(IPAddress.Parse(RemoteEndPoint.IPAddress), RemoteEndPoint.Port));
|
||||
|
||||
return taskSource.Task;
|
||||
#endif
|
||||
@@ -133,19 +141,20 @@ namespace Emby.Common.Implementations.Net
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private static void ProcessResponse(AsyncReceiveState state, Func<int> receiveData)
|
||||
private static void ProcessResponse(AsyncReceiveState state, Func<int> receiveData, IpAddressInfo localIpAddress)
|
||||
{
|
||||
try
|
||||
{
|
||||
var bytesRead = receiveData();
|
||||
|
||||
var ipEndPoint = state.EndPoint as IPEndPoint;
|
||||
var ipEndPoint = state.RemoteEndPoint as IPEndPoint;
|
||||
state.TaskCompletionSource.SetResult(
|
||||
new SocketReceiveResult()
|
||||
new SocketReceiveResult
|
||||
{
|
||||
Buffer = state.Buffer,
|
||||
ReceivedBytes = bytesRead,
|
||||
RemoteEndPoint = ToIpEndPointInfo(ipEndPoint)
|
||||
RemoteEndPoint = ToIpEndPointInfo(ipEndPoint),
|
||||
LocalIPAddress = localIpAddress
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -182,15 +191,16 @@ namespace Emby.Common.Implementations.Net
|
||||
var state = asyncResult.AsyncState as AsyncReceiveState;
|
||||
try
|
||||
{
|
||||
var bytesRead = state.Socket.EndReceiveFrom(asyncResult, ref state.EndPoint);
|
||||
var bytesRead = state.Socket.EndReceiveFrom(asyncResult, ref state.RemoteEndPoint);
|
||||
|
||||
var ipEndPoint = state.EndPoint as IPEndPoint;
|
||||
var ipEndPoint = state.RemoteEndPoint as IPEndPoint;
|
||||
state.TaskCompletionSource.SetResult(
|
||||
new SocketReceiveResult
|
||||
{
|
||||
Buffer = state.Buffer,
|
||||
ReceivedBytes = bytesRead,
|
||||
RemoteEndPoint = ToIpEndPointInfo(ipEndPoint)
|
||||
RemoteEndPoint = ToIpEndPointInfo(ipEndPoint),
|
||||
LocalIPAddress = LocalIPAddress
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -211,13 +221,13 @@ namespace Emby.Common.Implementations.Net
|
||||
|
||||
private class AsyncReceiveState
|
||||
{
|
||||
public AsyncReceiveState(Socket socket, EndPoint endPoint)
|
||||
public AsyncReceiveState(Socket socket, EndPoint remoteEndPoint)
|
||||
{
|
||||
this.Socket = socket;
|
||||
this.EndPoint = endPoint;
|
||||
this.RemoteEndPoint = remoteEndPoint;
|
||||
}
|
||||
|
||||
public EndPoint EndPoint;
|
||||
public EndPoint RemoteEndPoint;
|
||||
public byte[] Buffer = new byte[8192];
|
||||
|
||||
public Socket Socket { get; private set; }
|
||||
|
||||
Reference in New Issue
Block a user