Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
Killers0992 committed Nov 1, 2024
1 parent c71b165 commit 02bda9d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 64 deletions.
108 changes: 54 additions & 54 deletions XProxy.Core/Core/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using XProxy.Services;
using System.Threading;
using XProxy.Core.Monitors;
using System.Collections.Generic;

namespace XProxy.Core
{
Expand Down Expand Up @@ -53,20 +54,23 @@ public class Player : IDisposable
private NetManager _netManager;
private EventBasedNetListener _listener;

private DateTime _nextUpdate = DateTime.Now;

public Player(Listener proxy, PreAuthModel preAuth)
public Player(Listener proxy, ConnectionRequest request, PreAuthModel preAuth)
{
Proxy = proxy;
Proxy.Connections.Add(this);

_connectionRequest = request;

PreAuth = preAuth;

CancellationToken = new CancellationTokenSource();
Proxy.Players.TryAdd(Id, this);
}

public int Id { get; private set; } = -1;

public DateTime ConnectedOn { get; } = DateTime.Now;
public TimeSpan Connectiontime => DateTime.Now - ConnectedOn;

public double RemoteTimestamp { get; private set; }
public uint NetworkId { get; private set; } = Database.GetNextNetworkId();
public string UserId => PreAuth.UserID;
Expand All @@ -85,8 +89,6 @@ public Player(Listener proxy, PreAuthModel preAuth)
public BaseConnection Connection { get; set; }
public Server ServerInfo { get; set; }

public bool RejectIncomingVoicePackets { get; set; }

public CustomUnbatcher UnbatcherCurrentServer { get; private set; } = new CustomUnbatcher();
public CustomUnbatcher UnbatcherProxy { get; private set; } = new CustomUnbatcher();

Expand Down Expand Up @@ -386,32 +388,34 @@ public bool RedirectTo(Server server, bool queue = false)
public void SaveCurrentServerForNextSession(float duration = 4f) => Proxy.SaveLastServerForUser(UserId, ServerInfo.Name, duration);
public void SaveServerForNextSession(string name, float duration = 4f) => Proxy.SaveLastServerForUser(UserId, name, duration);

public void DisconnectFromProxy(string reason = null)
public void DisconnectFromProxy(string reason = null, NetDataWriter writer = null, RejectionReason? rejectionReason = null)
{
if (_connectionRequest != null)
{
if (!string.IsNullOrEmpty(reason))
{
NetDataWriter writer = new NetDataWriter();
writer.Put((byte)RejectionReason.Custom);
writer.Put(reason);
NetDataWriter customWriter = new NetDataWriter();
customWriter.Put((byte)RejectionReason.Custom);
customWriter.Put(reason);
_connectionRequest.Reject(customWriter);
}
else if (writer != null)
_connectionRequest.Reject(writer);
else if (rejectionReason.HasValue)
{
NetDataWriter customWriter = new NetDataWriter();
customWriter.Put((byte)rejectionReason.Value);
_connectionRequest.Reject(customWriter);
}
else
_connectionRequest.RejectForce();

Dispose();
}
else
_proxyPeer.Disconnect();
}

public void RejectFromProxy(NetDataWriter writer)
{
if (_connectionRequest == null) return;

_connectionRequest.Reject(writer);
InternalDestroy();
}

public void SendDataToCurrentServer(byte[] bytes, int position, int length, DeliveryMethod method)
{
if (!IsConnectedToCurrentServer) return;
Expand Down Expand Up @@ -564,12 +568,10 @@ internal void InternalReceiveDataFromProxy(NetPacketReader reader, DeliveryMetho
Connection.OnReceiveDataFromProxy(reader, method);
}

internal void InternalSetup(ConnectionRequest request, Server info)
internal void InternalSetup(Server info)
{
ServerInfo = info;

_connectionRequest = request;

TaskMonitor.RegisterTask(Task.Run(() => RunBatcher(CancellationToken.Token), CancellationToken.Token));

SetupNetManager();
Expand Down Expand Up @@ -615,29 +617,32 @@ internal void InternalConnect()

internal void InternalUpdate()
{
if (_nextUpdate < DateTime.Now && IsPlayerSpawned)
if (!IsPlayerSpawned)
return;

try
{
try
{
Update();
Connection?.Update();
}
catch (Exception ex)
{
Logger.Error(ex);
}
_nextUpdate = DateTime.Now.AddSeconds(1);
Update();
Connection?.Update();
}
catch (Exception ex)
{
Logger.Error(ex);
}
}

internal void InternalAcceptConnection(BaseConnection connection = null)
{
if (_connectionRequest == null) return;
// This should never happen.
if (_connectionRequest == null)
return;

_proxyPeer = _connectionRequest.Accept();

Id = _proxyPeer.Id;

Proxy.Players.TryAdd(Id, this);

if (!ServerInfo.PlayersById.ContainsKey(Id))
ServerInfo.PlayersById.TryAdd(Id, this);

Expand Down Expand Up @@ -673,24 +678,9 @@ internal void InternalDestroyNetwork()
}
}

internal void InternalDestroy()
{
ServerInfo.PlayersById.TryRemove(Id, out _);
Listener.PlayersByUserId.TryRemove(UserId, out _);

InternalDestroyNetwork();

Batcher = null;

if (Id != -1)
Proxy.Players.TryRemove(Id, out _);

Dispose();
}

async Task UpdateNetwork(CancellationToken cancellationToken)
{
while (_netManager != null && !cancellationToken.IsCancellationRequested)
while (!cancellationToken.IsCancellationRequested)
{
try
{
Expand All @@ -704,13 +694,15 @@ async Task UpdateNetwork(CancellationToken cancellationToken)

await Task.Delay(10, cancellationToken);
}

InternalDestroyNetwork();
}

async Task RunBatcher(CancellationToken token)
async Task RunBatcher(CancellationToken cancellationToken)
{
NetworkWriter writer = new NetworkWriter();

while (Batcher != null && !token.IsCancellationRequested)
while (!cancellationToken.IsCancellationRequested)
{
while (Batcher.GetBatch(writer))
{
Expand Down Expand Up @@ -850,8 +842,10 @@ void OnDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
break;
}

if (!cancel)
RejectFromProxy(rejectedData);
if (cancel)
return;

DisconnectFromProxy(writer: rejectedData);
return;

case DisconnectReason.Timeout:
Expand All @@ -868,14 +862,20 @@ void OnDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
Connection = new LostConnection(this, LostConnectionType.Shutdown);
Logger.Info(Proxy._config.Messages.PlayerServerShutdownMessage.Replace("%tag%", Tag).Replace("%address%", $"{ClientEndPoint}").Replace("%userid%", UserId), $"Player");
return;

}

DisconnectFromProxy();
}

public void Dispose()
{
Proxy.Connections.Remove(this);
ServerInfo.PlayersById.TryRemove(Id, out _);
Listener.PlayersByUserId.TryRemove(UserId, out _);

if (Id != -1)
Proxy.Players.TryRemove(Id, out _);

CancellationToken?.Cancel();
CancellationToken?.Dispose();
CancellationToken = null;
Expand Down
15 changes: 7 additions & 8 deletions XProxy.Core/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class Listener

public ConcurrentDictionary<int, Player> Players { get; private set; } = new ConcurrentDictionary<int, Player>();

public List<Player> Connections = new List<Player>();

public static ConcurrentDictionary<string, int> PlayersByUserId { get; private set; } = new ConcurrentDictionary<string, int>();
public static ConcurrentDictionary<string, LastServerInfo> ForceServerForUserID { get; set; } = new ConcurrentDictionary<string, LastServerInfo>();

Expand Down Expand Up @@ -265,7 +267,7 @@ public void OnConnectionRequest(ConnectionRequest request)
if (ev.IsCancelled)
return;

Player player = new Player(this, preAuth);
Player player = new Player(this, request, preAuth);

Server target;
if (HasSavedLastServer(preAuth.UserID))
Expand All @@ -276,8 +278,7 @@ public void OnConnectionRequest(ConnectionRequest request)
if (target == null)
{
Logger.Info(_config.Messages.ProxyIsFull.Replace("%address%", $"{request.RemoteEndPoint.Address}").Replace("%userid%", preAuth.UserID), "XProxy");
request.DisconnectServerFull();
player?.Dispose();
player.DisconnectFromProxy(rejectionReason: RejectionReason.ServerFull);
return;
}

Expand All @@ -288,9 +289,7 @@ public void OnConnectionRequest(ConnectionRequest request)
if (target.Settings.SendIpAddressInPreAuth)
preAuth.RawPreAuth.Put(ip);

Logger.Debug($"[{request.RemoteEndPoint.Address}] Internal setup -> connect");

player.InternalSetup(request, target);
player.InternalSetup(target);
player.InternalConnect();
}

Expand All @@ -304,7 +303,7 @@ public void OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelN

public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
{
if (!Players.TryGetValue(peer.Id, out Player player))
if (!Players.TryGetValue(peer.Id, out Player player))
return;

bool showDisconnectMessage = !player.IsRoundRestarting && !player.IsRedirecting && disconnectInfo.Reason != DisconnectReason.DisconnectPeerCalled;
Expand All @@ -322,7 +321,7 @@ public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
}
}

player.InternalDestroy();
player.Dispose();
}
}
}
2 changes: 1 addition & 1 deletion XProxy.Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using XProxy.Services;
using XProxy.Shared.Models;

[assembly: AssemblyVersion("1.6.1")]
[assembly: AssemblyVersion("1.6.2")]

namespace XProxy
{
Expand Down
2 changes: 1 addition & 1 deletion XProxy.Shared/releaseInfo.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.killers0992.xproxy",
"displayName": "XProxy",
"version": "1.6.1",
"version": "1.6.2",
"buildType": "release",
"gameVersion": "13.6.9"
}

0 comments on commit 02bda9d

Please sign in to comment.