forked from MrOkiDoki/BattleBit-Community-Server-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListenerService.cs
More file actions
83 lines (67 loc) · 2.8 KB
/
ListenerService.cs
File metadata and controls
83 lines (67 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Net;
using BattleBitAPI.Server;
using DatabaseExample.Repositories;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using LogLevel = BattleBitAPI.Common.LogLevel;
namespace DatabaseExample;
public sealed class ListenerService : IHostedService, IDisposable
{
/*
* Since this is a hosted service, we can receive things like the IServiceProvider and ILogger in our constructor
* to use them here. We want the service provider to create service scopes and resolve our repository services,
* which is also why we pass it in the constructor of MyPlayer and MyGameServer.
*/
private readonly ServerListener<MyPlayer, MyGameServer> _listener;
private readonly ILogger<ListenerService> _logger;
private readonly IServiceProvider _services;
public ListenerService(ILogger<ListenerService> logger, IServiceProvider services)
{
_services = services;
_listener = new ServerListener<MyPlayer, MyGameServer>();
_listener.LogLevel = LogLevel.All;
_listener.OnValidateGameServerToken += OnValidateGameToken;
_listener.OnCreatingGameServerInstance += OnCreatingGameServerInstance;
_listener.OnCreatingPlayerInstance += OnCreatingPlayerInstance;
_listener.OnLog += OnLog;
_logger = logger;
}
private void OnLog(LogLevel lvl, string msg, object obj)
{
_logger.LogInformation($"Log (level {lvl}): {msg}");
}
private MyPlayer OnCreatingPlayerInstance(ulong arg)
{
return new MyPlayer();
}
private MyGameServer OnCreatingGameServerInstance(IPAddress arg1, ushort arg2)
{
return new MyGameServer(_services);
}
private async Task<bool> OnValidateGameToken(IPAddress ip, ushort port, string token)
{
// We have a "whitelist" of IP + port + token entries in database.
// ExistsAsync checks if the DB has any entries with exactly this composite key of ip, port and token.
using var scope = _services.CreateScope();
var gameServers = scope.ServiceProvider.GetRequiredService<GameServerRepository>();
return await gameServers.ExistsAsync((ip, port, token));
}
// Since this is an IHostedservice, here we tell it what to do when starting and (below) stopping the service.
public Task StartAsync(CancellationToken cancellationToken)
{
_listener.Start(IPAddress.Loopback, 29294);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_listener.Stop();
return Task.CompletedTask;
}
// From IDisposable, make sure we dispose the listener when disposing this service.
public void Dispose()
{
_listener.Dispose();
GC.SuppressFinalize(this);
}
}