Skip to content

Commit

Permalink
Merge pull request #147 from GuoWQ222/dev
Browse files Browse the repository at this point in the history
Fix Server Connection Issue
  • Loading branch information
GuoWQ222 authored Mar 23, 2024
2 parents 4d2327d + 885bc7d commit c94fdaf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
6 changes: 3 additions & 3 deletions logic/Server/ArgumentOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public class DefaultArgumentOptions
public class ArgumentOptions
{
[Option("ip", Required = false, HelpText = "Server listening ip")]
public string ServerIP { get; set; } = "0.0.0.0";
public string ServerIP { get; set; } = "172.28.192.1";

[Option('p', "port", Required = true, HelpText = "Server listening port")]
public ushort ServerPort { get; set; } = 8888;

[Option("teamCount", Required = false, HelpText = "The number of teams, 2 by defualt")]
public ushort TeamCount { get; set; } = 2;

[Option("shipNum", Required = false, HelpText = "The max number of Ship, 3 by default")]
public ushort ShipCount { get; set; } = 3;
[Option("shipNum", Required = false, HelpText = "The max number of Ship, 4 by default")]
public ushort ShipCount { get; set; } = 4;

[Option("homeNum", Required = false, HelpText = "The number of Home , 1 by default")]
public ushort HomeCount { get; set; } = 1;
Expand Down
16 changes: 12 additions & 4 deletions logic/Server/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace Server
{
partial class GameServer : ServerBase
{
private readonly ConcurrentDictionary<long, (SemaphoreSlim, SemaphoreSlim)> semaDict = new();
private readonly ConcurrentDictionary<long, (SemaphoreSlim, SemaphoreSlim)> semaDict0 = new(); //for spectator and team0 player
private readonly ConcurrentDictionary<long, (SemaphoreSlim, SemaphoreSlim)> semaDict1 = new();
// private object semaDictLock = new();
protected readonly ArgumentOptions options;
private readonly HttpSender? httpSender;
Expand Down Expand Up @@ -164,14 +165,21 @@ public void ReportGame(GameState gameState, bool requiredGaming = true)
}
lock (spectatorJoinLock)
{
foreach (var kvp in semaDict)
foreach (var kvp in semaDict0)
{
kvp.Value.Item1.Release();
}
foreach (var kvp in semaDict1)
{
kvp.Value.Item1.Release();
}

// 若此时观战者加入,则死锁,所以需要 spectatorJoinLock

foreach (var kvp in semaDict)
foreach (var kvp in semaDict0)
{
kvp.Value.Item2.Wait();
}
foreach (var kvp in semaDict1)
{
kvp.Value.Item2.Wait();
}
Expand Down
39 changes: 29 additions & 10 deletions logic/Server/RpcServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Grpc.Core;
using Preparation.Utility;
using Protobuf;
using System.Runtime.CompilerServices;

namespace Server
{
Expand Down Expand Up @@ -63,7 +64,7 @@ public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<Mess
// 观战模式
lock (spectatorJoinLock) // 具体原因见另一个上锁的地方
{
if (semaDict.TryAdd(request.PlayerId, (new SemaphoreSlim(0, 1), new SemaphoreSlim(0, 1))))
if (semaDict0.TryAdd(request.PlayerId, (new SemaphoreSlim(0, 1), new SemaphoreSlim(0, 1))))
{
Console.WriteLine("A new spectator comes to watch this game.");
IsSpectatorJoin = true;
Expand All @@ -76,7 +77,7 @@ public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<Mess
}
do
{
semaDict[request.PlayerId].Item1.Wait();
semaDict0[request.PlayerId].Item1.Wait();
try
{
if (currentGameInfo != null)
Expand All @@ -87,7 +88,7 @@ public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<Mess
}
catch (InvalidOperationException)
{
if (semaDict.TryRemove(request.PlayerId, out var semas))
if (semaDict0.TryRemove(request.PlayerId, out var semas))
{
try
{
Expand All @@ -107,7 +108,7 @@ public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<Mess
{
try
{
semaDict[request.PlayerId].Item2.Release();
semaDict0[request.PlayerId].Item2.Release();
}
catch { }
}
Expand Down Expand Up @@ -148,11 +149,23 @@ public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<Mess
Console.WriteLine($"Id: {request.PlayerId} joins.");
lock (spectatorJoinLock) // 为了保证绝对安全,还是加上这个锁吧
{
if (semaDict.TryAdd(request.PlayerId, temp))
if (request.TeamId == 0)
{
start = Interlocked.Increment(ref playerCountNow) == playerNum;
Console.WriteLine($"PlayerCountNow: {playerCountNow}");
Console.WriteLine($"PlayerNum: {playerNum}");
if (semaDict0.TryAdd(request.PlayerId, temp))
{
start = Interlocked.Increment(ref playerCountNow) == (playerNum * 2);
Console.WriteLine($"PlayerCountNow: {playerCountNow}");
Console.WriteLine($"PlayerNum: {playerNum * 2}");
}
}
else if (request.TeamId == 1)
{
if (semaDict1.TryAdd(request.PlayerId, temp))
{
start = Interlocked.Increment(ref playerCountNow) == (playerNum * 2);
Console.WriteLine($"PlayerCountNow: {playerCountNow}");
Console.WriteLine($"PlayerNum: {playerNum * 2}");
}
}
}
if (start)
Expand All @@ -167,7 +180,10 @@ public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<Mess
bool exitFlag = false;
do
{
semaDict[request.PlayerId].Item1.Wait();
if (request.TeamId == 0)
semaDict0[request.PlayerId].Item1.Wait();
else if (request.TeamId == 1)
semaDict1[request.PlayerId].Item1.Wait();
try
{
if (currentGameInfo != null && !exitFlag)
Expand All @@ -186,7 +202,10 @@ public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<Mess
}
finally
{
semaDict[request.PlayerId].Item2.Release();
if (request.TeamId == 0)
semaDict0[request.PlayerId].Item2.Release();
else if (request.TeamId == 1)
semaDict1[request.PlayerId].Item2.Release();
}
} while (game.GameMap.Timer.IsGaming);
#if DEBUG
Expand Down

0 comments on commit c94fdaf

Please sign in to comment.