sharp-chat/SharpChat/SockChatConnection.cs
flashwave 5a7756894b
First bits of the Context overhaul.
Reintroduces separate contexts for users, channels, connections (now split into sessions and connections) and user-channel associations.
It builds which is as much assurance as I can give about the stability of this commit, but its also the bare minimum of what i like to commit sooooo
A lot of things still need to be broadcast through events throughout the application in order to keep states consistent but we'll cross that bridge when we get to it.
I really need to stop using that phrase thingy, I'm overusing it.
2025-05-03 02:49:51 +00:00

36 lines
1.5 KiB
C#

using Fleck;
using Microsoft.Extensions.Logging;
using SharpChat.Connections;
using SharpChat.SockChat;
using System.Net;
namespace SharpChat;
public class SockChatConnection(IWebSocketConnection sock, IPEndPoint remoteEndPoint, ILogger logger) : Connection {
public IWebSocketConnection Socket { get; } = sock;
public IPEndPoint RemoteEndPoint { get; } = remoteEndPoint;
public ILogger Logger { get; } = logger;
public async Task Send(S2CPacket packet) {
if(!Socket.IsAvailable)
return;
string data = packet.Pack();
if(!string.IsNullOrWhiteSpace(data))
await Socket.Send(data);
}
public void Close(ConnectionCloseReason reason = ConnectionCloseReason.Unexpected) {
Socket.Close(reason switch {
ConnectionCloseReason.ShuttingDown => WebSocketCloseCode.GoingAway,
ConnectionCloseReason.Error => WebSocketCloseCode.InternalError,
ConnectionCloseReason.Restarting => WebSocketCloseCode.ServiceRestart,
ConnectionCloseReason.Unavailable => WebSocketCloseCode.ServiceRestart,
ConnectionCloseReason.Unauthorized => WebSocketCloseCode.Unauthorized,
ConnectionCloseReason.TimeOut => WebSocketCloseCode.Timeout,
ConnectionCloseReason.AccessDenied => WebSocketCloseCode.Forbidden,
ConnectionCloseReason.TooManyConnections => WebSocketCloseCode.TryAgainLater,
_ => WebSocketCloseCode.NormalClosure,
});
}
}