Improved logging system.
This commit is contained in:
parent
d94b1cb813
commit
98d13ebbbb
24 changed files with 202 additions and 142 deletions
SharpChat
|
@ -1,48 +1,28 @@
|
|||
using Fleck;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SharpChat.SockChat;
|
||||
using System.Net;
|
||||
|
||||
namespace SharpChat;
|
||||
|
||||
public class Connection : IDisposable {
|
||||
public const int ID_LENGTH = 20;
|
||||
public class Connection(ILogger logger, IWebSocketConnection sock, IPEndPoint remoteEndPoint) : IDisposable {
|
||||
public static readonly TimeSpan SessionTimeOut = TimeSpan.FromMinutes(5);
|
||||
|
||||
#if DEBUG
|
||||
public static TimeSpan SessionTimeOut { get; } = TimeSpan.FromMinutes(1);
|
||||
#else
|
||||
public static TimeSpan SessionTimeOut { get; } = TimeSpan.FromMinutes(5);
|
||||
#endif
|
||||
public ILogger Logger { get; } = logger;
|
||||
public IWebSocketConnection Socket { get; } = sock;
|
||||
public IPEndPoint RemoteEndPoint { get; } = remoteEndPoint;
|
||||
|
||||
public IWebSocketConnection Socket { get; }
|
||||
|
||||
public string Id { get; }
|
||||
public bool IsDisposed { get; private set; }
|
||||
public DateTimeOffset LastPing { get; set; } = DateTimeOffset.Now;
|
||||
public User? User { get; set; }
|
||||
|
||||
private int CloseCode { get; set; } = 1000;
|
||||
|
||||
public IPAddress RemoteAddress { get; }
|
||||
public ushort RemotePort { get; }
|
||||
public IPAddress RemoteAddress => RemoteEndPoint.Address;
|
||||
public ushort RemotePort => (ushort)RemoteEndPoint.Port;
|
||||
|
||||
public bool IsAlive => !IsDisposed && !HasTimedOut;
|
||||
|
||||
public Connection(IWebSocketConnection sock) {
|
||||
Socket = sock;
|
||||
Id = RNG.SecureRandomString(ID_LENGTH);
|
||||
|
||||
if(!IPAddress.TryParse(sock.ConnectionInfo.ClientIpAddress, out IPAddress? addr))
|
||||
throw new Exception("Unable to parse remote address?????");
|
||||
|
||||
if(IPAddress.IsLoopback(addr)
|
||||
&& sock.ConnectionInfo.Headers.TryGetValue("X-Real-IP", out string? addrStr)
|
||||
&& IPAddress.TryParse(addrStr, out IPAddress? realAddr))
|
||||
addr = realAddr;
|
||||
|
||||
RemoteAddress = addr;
|
||||
RemotePort = (ushort)sock.ConnectionInfo.ClientPort;
|
||||
}
|
||||
|
||||
public async Task Send(S2CPacket packet) {
|
||||
if(!Socket.IsAvailable)
|
||||
return;
|
||||
|
@ -79,12 +59,4 @@ public class Connection : IDisposable {
|
|||
IsDisposed = true;
|
||||
Socket.Close(CloseCode);
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
return Id;
|
||||
}
|
||||
|
||||
public override int GetHashCode() {
|
||||
return Id.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue