sharp-chat/SharpChat/ChatConnection.cs

96 lines
2.6 KiB
C#
Raw Normal View History

2022-08-30 15:00:58 +00:00
using Fleck;
using System;
using System.Collections.Generic;
using System.Net;
namespace SharpChat {
2023-02-16 21:25:41 +00:00
public class ChatConnection : IDisposable {
public const int ID_LENGTH = 20;
2022-08-30 15:00:58 +00:00
#if DEBUG
public static TimeSpan SessionTimeOut { get; } = TimeSpan.FromMinutes(1);
#else
public static TimeSpan SessionTimeOut { get; } = TimeSpan.FromMinutes(5);
#endif
2023-02-16 21:25:41 +00:00
public IWebSocketConnection Socket { get; }
2022-08-30 15:00:58 +00:00
public string Id { get; private set; }
public bool IsDisposed { get; private set; }
public DateTimeOffset LastPing { get; set; } = DateTimeOffset.Now;
2022-08-30 15:00:58 +00:00
public ChatUser User { get; set; }
2023-02-07 15:01:56 +00:00
private int CloseCode { get; set; } = 1000;
2022-08-30 15:00:58 +00:00
private IPAddress _RemoteAddress = null;
public IPAddress RemoteAddress {
get {
if(_RemoteAddress == null) {
2023-02-16 21:25:41 +00:00
if((Socket.ConnectionInfo.ClientIpAddress == "127.0.0.1" || Socket.ConnectionInfo.ClientIpAddress == "::1")
&& Socket.ConnectionInfo.Headers.ContainsKey("X-Real-IP"))
_RemoteAddress = IPAddress.Parse(Socket.ConnectionInfo.Headers["X-Real-IP"]);
2022-08-30 15:00:58 +00:00
else
2023-02-16 21:25:41 +00:00
_RemoteAddress = IPAddress.Parse(Socket.ConnectionInfo.ClientIpAddress);
2022-08-30 15:00:58 +00:00
}
return _RemoteAddress;
}
}
public bool IsAlive => !IsDisposed && !HasTimedOut;
public bool IsAuthed => IsAlive && User is not null;
2023-02-16 21:25:41 +00:00
public ChatConnection(IWebSocketConnection sock) {
Socket = sock;
2023-02-10 07:06:07 +00:00
Id = RNG.SecureRandomString(ID_LENGTH);
2022-08-30 15:00:58 +00:00
}
public void Send(IServerPacket packet) {
2023-02-16 21:25:41 +00:00
if(!Socket.IsAvailable)
2022-08-30 15:00:58 +00:00
return;
IEnumerable<string> data = packet.Pack();
if(data != null)
foreach(string line in data)
if(!string.IsNullOrWhiteSpace(line))
2023-02-16 21:25:41 +00:00
Socket.Send(line);
2022-08-30 15:00:58 +00:00
}
2023-02-07 15:01:56 +00:00
public void BumpPing() {
LastPing = DateTimeOffset.Now;
}
2022-08-30 15:00:58 +00:00
public bool HasTimedOut
=> DateTimeOffset.Now - LastPing > SessionTimeOut;
2023-02-07 15:01:56 +00:00
public void PrepareForRestart() {
CloseCode = 1012;
}
2023-02-16 21:25:41 +00:00
~ChatConnection() {
2023-02-07 15:01:56 +00:00
DoDispose();
}
2022-08-30 15:00:58 +00:00
2023-02-07 15:01:56 +00:00
public void Dispose() {
DoDispose();
GC.SuppressFinalize(this);
}
2022-08-30 15:00:58 +00:00
2023-02-07 15:01:56 +00:00
private void DoDispose() {
if(IsDisposed)
2022-08-30 15:00:58 +00:00
return;
IsDisposed = true;
2023-02-16 21:25:41 +00:00
Socket.Close(CloseCode);
2022-08-30 15:00:58 +00:00
}
public override int GetHashCode() {
return Id.GetHashCode();
}
2022-08-30 15:00:58 +00:00
}
}