Skip to content

Commit

Permalink
Merge pull request eesast#223 from asdawej/dev-playback
Browse files Browse the repository at this point in the history
playback flush every 500 msg
  • Loading branch information
Grange007 authored Apr 13, 2024
2 parents 2bcbc4c + a7d530e commit 7297b2b
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CAPI/python_exp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# launch debug
!.vscode/
30 changes: 30 additions & 0 deletions CAPI/python_exp/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Team 0",
"type": "debugpy",
"request": "launch",
"program": "./PyAPI/main.py",
"console": "integratedTerminal",
"args": "-I localhost -P 8888 -t 0 -d -o"
},
{
"name": "Team 1",
"type": "debugpy",
"request": "launch",
"program": "./PyAPI/main.py",
"console": "integratedTerminal",
"args": "-I localhost -P 8888 -t 1 -d -o"
}
],
"compounds": [
{
"name": "Compound",
"configurations": ["Team 0", "Team 1"]
}
]
}
14 changes: 14 additions & 0 deletions CAPI/python_exp/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"python.analysis.autoImportCompletions": true,
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/Thumbs.db": true,
"**/__pycache__": true,
"**/build": true
},
"python.analysis.typeCheckingMode": "off"
}
4 changes: 3 additions & 1 deletion CAPI/python_exp/PyAPI/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ def __LoadBuffer(self, message: Message2Clients.MessageToClient) -> None:
self.__bufferState.gameInfo = Proto2THUAI7.Protobuf2THUAI7GameInfo(message.all_message)

self.__LoadBufferSelf(message)
if self.__playerType == THUAI7.Ship and isinstance(self.__bufferState.self, None):
return
for item in message.obj_message:
self.__LoadBufferCase(item)
if Setting.Asynchronous():
Expand All @@ -347,7 +349,7 @@ def __LoadBuffer(self, message: Message2Clients.MessageToClient) -> None:
self.__cvBuffer.notify()

def __LoadBufferSelf(self, message: Message2Clients.MessageToClient) -> None:
if self.__playerID != 0:
if self.__playerType == THUAI7.PlayerType.Ship:
for item in message.obj_message:
if item.WhichOneof("message_of_obj") == "ship_message":
if item.ship_message.player_id == self.__playerID:
Expand Down
14 changes: 14 additions & 0 deletions CAPI/python_exp/playback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import sys
import grpc

sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/proto")

import proto.Message2Server_pb2 as m2s # NOQA: E402
import proto.Message2Clients_pb2 as m2c # NOQA: E402
import proto.MessageType_pb2 as mt # NOQA: E402
import proto.Services_pb2_grpc as ser # NOQA: E402

stub = ser.AvailableServiceStub(grpc.insecure_channel('localhost:8888'))
for msg in stub.AddPlayer(m2s.PlayerMsg(player_id=2025)):
print(msg)
2 changes: 1 addition & 1 deletion logic/Server/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void CreateStartFile()

public override void WaitForEnd()
{
this.endGameSem.Wait();
endGameSem.Wait();
mwr?.Dispose();
}

Expand Down
42 changes: 20 additions & 22 deletions logic/Server/RpcServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,34 +186,32 @@ public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<Mess
semaDict0[request.PlayerId].Item1.Wait();
else if (request.TeamId == 1)
semaDict1[request.PlayerId].Item1.Wait();
if (ship == null || ship.IsRemoved == true)
{ }
else
//if (ship != null && ship.IsRemoved != true)
//{
try
{
try
if (currentGameInfo != null && !exitFlag)
{
if (currentGameInfo != null && !exitFlag)
{
await responseStream.WriteAsync(currentGameInfo);
Console.WriteLine("Send!");
}
}
catch
{
if (!exitFlag)
{
Console.WriteLine($"The client {request.PlayerId} exited");
exitFlag = true;
}
await responseStream.WriteAsync(currentGameInfo);
Console.WriteLine("Send!");
}
finally
}
catch
{
if (!exitFlag)
{
if (request.TeamId == 0)
semaDict0[request.PlayerId].Item2.Release();
else if (request.TeamId == 1)
semaDict1[request.PlayerId].Item2.Release();
Console.WriteLine($"The client {request.PlayerId} exited");
exitFlag = true;
}
}
finally
{
if (request.TeamId == 0)
semaDict0[request.PlayerId].Item2.Release();
else if (request.TeamId == 1)
semaDict1[request.PlayerId].Item2.Release();
}
//}
} while (game.GameMap.Timer.IsGaming);
}

Expand Down
1 change: 0 additions & 1 deletion playback/Playback/MessageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class MessageReader : IDisposable
public readonly uint playerCount;

private readonly CodedInputStream cis; // Protobuf类型二进制输入流

public bool Disposed { get; private set; } = false;

public MessageReader(string fileName)
Expand Down
12 changes: 10 additions & 2 deletions playback/Playback/MessageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,32 @@ public class MessageWriter : IDisposable
/// </summary>
public string FileName { get; }

private readonly CodedOutputStream cos; // Protobuf类型二进制输出流
/// <summary>
/// 写入消息数量
/// </summary>
public uint WrittenNum { get; private set; } = 0;
private readonly uint FlushNum; // 刷新间隔

private readonly CodedOutputStream cos; // Protobuf类型二进制输出流
public bool Disposed { get; private set; } = false;

public MessageWriter(string fileName, uint teamCount, uint playerCount)
public MessageWriter(string fileName, uint teamCount, uint playerCount, uint flushNum = 500)
{
Utils.FileNameRegular(ref fileName);
FileStream fs = File.Create(fileName);
FileName = fs.Name;
fs.WriteHeader(teamCount, playerCount);
GZipStream gzs = new(fs, CompressionMode.Compress);
cos = new(gzs);
FlushNum = flushNum;
}

public void WriteOne(MessageToClient msg)
{
if (Disposed) return;
cos.WriteMessage(msg);
WrittenNum++;
if (WrittenNum % FlushNum == 0) Flush();
}

public void Flush()
Expand Down

0 comments on commit 7297b2b

Please sign in to comment.