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

View file

@ -53,6 +53,7 @@ namespace BattleBitAPI.Server
private bool mIsDisposed; private bool mIsDisposed;
private mInternalResources mInternal; private mInternalResources mInternal;
private StringBuilder mBuilder; private StringBuilder mBuilder;
private bool mWantsToCloseConnection;
// ---- Construction ---- // ---- 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) 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; return;
} }
//Did user requested to close connection?
if (this.mWantsToCloseConnection)
{
mClose(this.TerminationReason);
return;
}
var networkStream = Socket.GetStream(); var networkStream = Socket.GetStream();
//Read network packages. //Read network packages.
@ -432,6 +440,14 @@ namespace BattleBitAPI.Server
} }
// ---- Closing ---- // ---- 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) private void mClose(string reason)
{ {
if (this.IsConnected) if (this.IsConnected)
@ -475,6 +491,8 @@ namespace BattleBitAPI.Server
this.ServerName; this.ServerName;
} }
// ---- Internal ---- // ---- Internal ----
public class mInternalResources public class mInternalResources
{ {

View file

@ -80,5 +80,7 @@
mResources.IsDirtySettings = true; 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 team; Team team;
{ {
@ -635,6 +643,7 @@ namespace BattleBitAPI.Server
TPlayer player = Activator.CreateInstance<TPlayer>(); TPlayer player = Activator.CreateInstance<TPlayer>();
player.SteamID = steamid; player.SteamID = steamid;
player.Name = username; player.Name = username;
player.IP = new IPAddress(ipHash);
player.GameServer = server; player.GameServer = server;
player.Team = team; player.Team = team;
player.Squad = squad; player.Squad = squad;
@ -746,11 +755,12 @@ namespace BattleBitAPI.Server
{ {
case NetworkCommuncation.PlayerConnected: case NetworkCommuncation.PlayerConnected:
{ {
if (stream.CanRead(8 + 2 + (1 + 1 + 1))) if (stream.CanRead(8 + 2 + 4 + (1 + 1 + 1)))
{ {
ulong steamID = stream.ReadUInt64(); ulong steamID = stream.ReadUInt64();
if (stream.TryReadString(out var username)) if (stream.TryReadString(out var username))
{ {
uint ip = stream.ReadUInt32();
Team team = (Team)stream.ReadInt8(); Team team = (Team)stream.ReadInt8();
Squads squad = (Squads)stream.ReadInt8(); Squads squad = (Squads)stream.ReadInt8();
GameRole role = (GameRole)stream.ReadInt8(); GameRole role = (GameRole)stream.ReadInt8();
@ -758,6 +768,7 @@ namespace BattleBitAPI.Server
TPlayer player = Activator.CreateInstance<TPlayer>(); TPlayer player = Activator.CreateInstance<TPlayer>();
player.SteamID = steamID; player.SteamID = steamID;
player.Name = username; player.Name = username;
player.IP = new IPAddress(ip);
player.GameServer = server; player.GameServer = server;
player.Team = team; player.Team = team;

View file

@ -3,8 +3,7 @@ using BattleBitAPI.Common;
using BattleBitAPI.Server; using BattleBitAPI.Server;
using System.Diagnostics; using System.Diagnostics;
using System.Net; using System.Net;
using System.Net.Http.Headers; using System.Text;
using System.Numerics;
class Program class Program
{ {
@ -13,17 +12,11 @@ class Program
var listener = new ServerListener<MyPlayer>(); var listener = new ServerListener<MyPlayer>();
listener.Start(29294); listener.Start(29294);
listener.OnAPlayerKilledAnotherPlayer += OnAPlayerKilledAnotherPlayer;
Thread.Sleep(-1); 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 class MyPlayer : Player
{ {
public int NumberOfNWord;
} }