Added /shutdown and /restart commands for server maintenance.
This commit is contained in:
parent
e6dffe06e6
commit
6f50ec66a9
3 changed files with 49 additions and 12 deletions
|
@ -20,6 +20,8 @@ namespace SharpChat {
|
|||
public DateTimeOffset LastPing { get; set; } = DateTimeOffset.MinValue;
|
||||
public ChatUser User { get; set; }
|
||||
|
||||
private static int CloseCode { get; set; } = 1000;
|
||||
|
||||
public string TargetName => @"@log";
|
||||
|
||||
|
||||
|
@ -69,6 +71,9 @@ namespace SharpChat {
|
|||
public bool HasTimedOut
|
||||
=> DateTimeOffset.Now - LastPing > SessionTimeOut;
|
||||
|
||||
public void PrepareForRestart()
|
||||
=> CloseCode = 1012;
|
||||
|
||||
public void Dispose()
|
||||
=> Dispose(true);
|
||||
|
||||
|
@ -80,7 +85,7 @@ namespace SharpChat {
|
|||
return;
|
||||
|
||||
IsDisposed = true;
|
||||
Connection.Close();
|
||||
Connection.Close(CloseCode);
|
||||
|
||||
if(disposing)
|
||||
GC.SuppressFinalize(this);
|
||||
|
|
|
@ -22,7 +22,7 @@ namespace SharpChat {
|
|||
Database.ReadConfig();
|
||||
|
||||
using ManualResetEvent mre = new ManualResetEvent(false);
|
||||
using SockChatServer scs = new SockChatServer(PORT);
|
||||
using SockChatServer scs = new SockChatServer(mre, PORT);
|
||||
Console.CancelKeyPress += (s, e) => { e.Cancel = true; mre.Set(); };
|
||||
mre.WaitOne();
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ using System.Net;
|
|||
using System.Net.Http;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace SharpChat {
|
||||
public class SockChatServer : IDisposable {
|
||||
|
@ -58,9 +59,14 @@ namespace SharpChat {
|
|||
HttpClient.DefaultRequestHeaders.UserAgent.ParseAdd(@"SharpChat");
|
||||
}
|
||||
|
||||
public SockChatServer(ushort port) {
|
||||
private ManualResetEvent Shutdown { get; }
|
||||
private bool IsShuttingDown = false;
|
||||
|
||||
public SockChatServer(ManualResetEvent mre, ushort port) {
|
||||
Logger.Write("Starting Sock Chat server...");
|
||||
|
||||
Shutdown = mre ?? throw new ArgumentNullException(nameof(mre));
|
||||
|
||||
Context = new ChatContext(this);
|
||||
|
||||
Context.Channels.Add(new ChatChannel(@"Lounge"));
|
||||
|
@ -75,6 +81,11 @@ namespace SharpChat {
|
|||
Server = new SharpChatWebSocketServer($@"ws://0.0.0.0:{port}");
|
||||
|
||||
Server.Start(sock => {
|
||||
if(IsShuttingDown || IsDisposed) {
|
||||
sock.Close(1013);
|
||||
return;
|
||||
}
|
||||
|
||||
sock.OnOpen = () => OnOpen(sock);
|
||||
sock.OnClose = () => OnClose(sock);
|
||||
sock.OnError = err => OnError(sock, err);
|
||||
|
@ -821,6 +832,25 @@ namespace SharpChat {
|
|||
user.Send(new LegacyCommandResponse(LCR.IP_ADDRESS, false, ipUser.Username, ip));
|
||||
break;
|
||||
|
||||
case @"shutdown":
|
||||
case @"restart":
|
||||
if(user.UserId != 1) {
|
||||
user.Send(new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $@"/{commandName}"));
|
||||
break;
|
||||
}
|
||||
|
||||
if(IsShuttingDown)
|
||||
break;
|
||||
IsShuttingDown = true;
|
||||
|
||||
if(commandName == @"restart")
|
||||
lock(SessionsLock)
|
||||
Sessions.ForEach(s => s.PrepareForRestart());
|
||||
|
||||
Context.Update();
|
||||
Shutdown.Set();
|
||||
break;
|
||||
|
||||
default:
|
||||
user.Send(new LegacyCommandResponse(LCR.COMMAND_NOT_FOUND, true, commandName));
|
||||
break;
|
||||
|
@ -840,7 +870,9 @@ namespace SharpChat {
|
|||
return;
|
||||
IsDisposed = true;
|
||||
|
||||
Sessions?.Clear();
|
||||
lock(SessionsLock)
|
||||
Sessions.ForEach(s => s.Dispose());
|
||||
|
||||
Server?.Dispose();
|
||||
Context?.Dispose();
|
||||
HttpClient?.Dispose();
|
||||
|
|
Loading…
Reference in a new issue