1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-16 04:25:23 +00:00

Added thread safety.

This commit is contained in:
Tim Sarbin 2018-12-01 00:00:17 -05:00
parent 0cabaceb48
commit 1fc0eec1be
5 changed files with 53 additions and 21 deletions

View File

@ -8,6 +8,8 @@ namespace OpenDiablo2.Common.Interfaces
{
public interface IGameState : IDisposable
{
object ThreadLocker { get; }
int Act { get; }
int Seed { get; }
string MapName { get; }

View File

@ -11,6 +11,7 @@ namespace OpenDiablo2.Common.Interfaces
{
void Initialize();
void Stop();
void JoinGame(string playerName);
void JoinGame(string playerName, Action<Guid> callback);
}
}

View File

@ -111,20 +111,24 @@ namespace OpenDiablo2.Core
lastTicks = curTicks;
getGameState().Update(ms);
getRenderWindow().Update();
currentScene.Update(ms);
if (nextScene!= null)
lock (getGameState().ThreadLocker)
{
currentScene = nextScene;
nextScene = null;
continue;
getGameState().Update(ms);
getRenderWindow().Update();
currentScene.Update(ms);
if (nextScene!= null)
{
currentScene = nextScene;
nextScene = null;
continue;
}
renderWindow.Clear();
currentScene.Render();
renderWindow.Sync();
}
renderWindow.Clear();
currentScene.Render();
renderWindow.Sync();
}
}

View File

@ -21,6 +21,7 @@ namespace OpenDiablo2.Core.GameState_
private readonly Func<IMapEngine> getMapEngine;
private readonly Func<eSessionType, ISessionManager> getSessionManager;
private Guid playerId;
private float animationTime = 0f;
private List<MapInfo> mapInfo;
private List<MapCellInfo> mapDataLookup = new List<MapCellInfo>();
@ -35,6 +36,8 @@ namespace OpenDiablo2.Core.GameState_
public int Seed { get; internal set; }
public object ThreadLocker { get; } = new object();
public GameState(
ISceneManager sceneManager,
IResourceManager resourceManager,
@ -65,7 +68,12 @@ namespace OpenDiablo2.Core.GameState_
mapInfo = new List<MapInfo>();
sceneManager.ChangeScene("Game");
sessionManager.JoinGame(characterName); // TODO: we need more attributes...
sessionManager.JoinGame(characterName, (id) =>
{
log.Info("hoo");
playerId = id;
}); // TODO: we need more attributes...
log.Info("woo");
}
private void OnSetSeedEvent(object sender, int seed)

View File

@ -18,6 +18,7 @@ namespace OpenDiablo2.ServiceBus
private readonly Func<eSessionType, ISessionServer> getSessionServer;
private readonly eSessionType sessionType;
private readonly Func<eMessageFrameType, IMessageFrame> getMessageFrame;
private readonly Func<IGameState> getGameState;
private RequestSocket requestSocket;
private AutoResetEvent resetEvent = new AutoResetEvent(false);
@ -28,11 +29,17 @@ namespace OpenDiablo2.ServiceBus
public OnSetSeedEvent OnSetSeed { get; set; }
public OnJoinGameEvent OnJoinGame { get; set; }
public SessionManager(eSessionType sessionType, Func<eSessionType, ISessionServer> getSessionServer, Func<eMessageFrameType, IMessageFrame> getMessageFrame)
public SessionManager(
eSessionType sessionType,
Func<eSessionType, ISessionServer> getSessionServer,
Func<eMessageFrameType, IMessageFrame> getMessageFrame,
Func<IGameState> getGameState
)
{
this.getSessionServer = getSessionServer;
this.sessionType = sessionType;
this.getMessageFrame = getMessageFrame;
this.getGameState = getGameState;
}
public void Initialize()
@ -115,15 +122,25 @@ namespace OpenDiablo2.ServiceBus
if (messageFrame.GetType() != typeof(T))
throw new ApplicationException("Recieved unexpected message frame!");
messageFrame.Data = frameData;
messageFrame.Process(requestSocket, this);
lock (getGameState().ThreadLocker)
{
messageFrame.Process(requestSocket, this);
}
}
public void JoinGame(string playerName)
public void JoinGame(string playerName, Action<Guid> callback)
{
var mf = new MFJoinGame(playerName);
playerId = mf.PlayerId;
Send(mf);
ProcessMessageFrame<MFSetSeed>();
Task.Run(() =>
{
var mf = new MFJoinGame(playerName);
playerId = mf.PlayerId;
Send(mf);
ProcessMessageFrame<MFSetSeed>();
lock (getGameState().ThreadLocker)
{
callback(playerId);
}
});
}
}
}