CloseConnection to game server implemented.

This commit is contained in:
MrOkiDoki 2023-08-11 01:38:29 +03:00
parent caf7ff6ea8
commit d89c422453
21 changed files with 36 additions and 10 deletions

View File

@ -1,5 +1,6 @@
using BattleBitAPI.Common;
using BattleBitAPI.Server;
using System.Net;
using System.Numerics;
namespace BattleBitAPI
@ -8,6 +9,7 @@ namespace BattleBitAPI
{
public ulong SteamID { get; internal set; }
public string Name { get; internal set; }
public IPAddress IP { get; internal set; }
public GameServer GameServer { get; internal set; }
public GameRole Role { get; internal set; }
public Team Team { get; internal set; }

View File

@ -53,6 +53,7 @@ namespace BattleBitAPI.Server
private bool mIsDisposed;
private mInternalResources mInternal;
private StringBuilder mBuilder;
private bool mWantsToCloseConnection;
// ---- Construction ----
public GameServer(TcpClient socket, mInternalResources resources, Func<GameServer, mInternalResources, Common.Serialization.Stream, Task> func, IPAddress iP, int port, bool isPasswordProtected, string serverName, string gamemode, string map, MapSize mapSize, MapDayNight dayNight, int currentPlayers, int inQueuePlayers, int maxPlayers, string loadingScreenText, string serverRulesText)
@ -165,6 +166,13 @@ namespace BattleBitAPI.Server
return;
}
//Did user requested to close connection?
if (this.mWantsToCloseConnection)
{
mClose(this.TerminationReason);
return;
}
var networkStream = Socket.GetStream();
//Read network packages.
@ -432,6 +440,14 @@ namespace BattleBitAPI.Server
}
// ---- Closing ----
public void CloseConnection(string additionInfo = string.Empty)
{
if (string.IsNullOrWhiteSpace(additionInfo))
this.TerminationReason = additionInfo;
else
this.TerminationReason = "User requested to terminate the connection";
this.mWantsToCloseConnection = true;
}
private void mClose(string reason)
{
if (this.IsConnected)
@ -475,6 +491,8 @@ namespace BattleBitAPI.Server
this.ServerName;
}
// ---- Internal ----
public class mInternalResources
{

View File

@ -80,5 +80,7 @@
mResources.IsDirtySettings = true;
}
}
}
}

View File

@ -578,6 +578,14 @@ namespace BattleBitAPI.Server
}
}
uint ipHash = 0;
{
readStream.Reset();
if (!await networkStream.TryRead(readStream, 4, source.Token))
throw new Exception("Unable to read the ip");
ipHash = readStream.ReadUInt32();
}
//Team
Team team;
{
@ -635,6 +643,7 @@ namespace BattleBitAPI.Server
TPlayer player = Activator.CreateInstance<TPlayer>();
player.SteamID = steamid;
player.Name = username;
player.IP = new IPAddress(ipHash);
player.GameServer = server;
player.Team = team;
player.Squad = squad;
@ -746,11 +755,12 @@ namespace BattleBitAPI.Server
{
case NetworkCommuncation.PlayerConnected:
{
if (stream.CanRead(8 + 2 + (1 + 1 + 1)))
if (stream.CanRead(8 + 2 + 4 + (1 + 1 + 1)))
{
ulong steamID = stream.ReadUInt64();
if (stream.TryReadString(out var username))
{
uint ip = stream.ReadUInt32();
Team team = (Team)stream.ReadInt8();
Squads squad = (Squads)stream.ReadInt8();
GameRole role = (GameRole)stream.ReadInt8();
@ -758,6 +768,7 @@ namespace BattleBitAPI.Server
TPlayer player = Activator.CreateInstance<TPlayer>();
player.SteamID = steamID;
player.Name = username;
player.IP = new IPAddress(ip);
player.GameServer = server;
player.Team = team;

View File

@ -3,8 +3,7 @@ using BattleBitAPI.Common;
using BattleBitAPI.Server;
using System.Diagnostics;
using System.Net;
using System.Net.Http.Headers;
using System.Numerics;
using System.Text;
class Program
{
@ -13,17 +12,11 @@ class Program
var listener = new ServerListener<MyPlayer>();
listener.Start(29294);
listener.OnAPlayerKilledAnotherPlayer += OnAPlayerKilledAnotherPlayer;
Thread.Sleep(-1);
}
private static async Task OnAPlayerKilledAnotherPlayer(OnPlayerKillArguments<MyPlayer> arg)
{
await Console.Out.WriteLineAsync(arg.Killer + " killed " + arg.Victim + " with " + arg.KillerTool + " (" + arg.BodyPart + ")");
}
}
class MyPlayer : Player
{
public int NumberOfNWord;
}