mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-02-08 17:46:24 -05:00
Added thread safety.
This commit is contained in:
parent
0cabaceb48
commit
1fc0eec1be
@ -8,6 +8,8 @@ namespace OpenDiablo2.Common.Interfaces
|
|||||||
{
|
{
|
||||||
public interface IGameState : IDisposable
|
public interface IGameState : IDisposable
|
||||||
{
|
{
|
||||||
|
object ThreadLocker { get; }
|
||||||
|
|
||||||
int Act { get; }
|
int Act { get; }
|
||||||
int Seed { get; }
|
int Seed { get; }
|
||||||
string MapName { get; }
|
string MapName { get; }
|
||||||
|
@ -11,6 +11,7 @@ namespace OpenDiablo2.Common.Interfaces
|
|||||||
{
|
{
|
||||||
void Initialize();
|
void Initialize();
|
||||||
void Stop();
|
void Stop();
|
||||||
void JoinGame(string playerName);
|
|
||||||
|
void JoinGame(string playerName, Action<Guid> callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,20 +111,24 @@ namespace OpenDiablo2.Core
|
|||||||
|
|
||||||
lastTicks = curTicks;
|
lastTicks = curTicks;
|
||||||
|
|
||||||
getGameState().Update(ms);
|
lock (getGameState().ThreadLocker)
|
||||||
getRenderWindow().Update();
|
|
||||||
currentScene.Update(ms);
|
|
||||||
if (nextScene!= null)
|
|
||||||
{
|
{
|
||||||
currentScene = nextScene;
|
getGameState().Update(ms);
|
||||||
nextScene = null;
|
getRenderWindow().Update();
|
||||||
continue;
|
currentScene.Update(ms);
|
||||||
|
|
||||||
|
if (nextScene!= null)
|
||||||
|
{
|
||||||
|
currentScene = nextScene;
|
||||||
|
nextScene = null;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
renderWindow.Clear();
|
||||||
|
currentScene.Render();
|
||||||
|
|
||||||
|
renderWindow.Sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
renderWindow.Clear();
|
|
||||||
currentScene.Render();
|
|
||||||
|
|
||||||
renderWindow.Sync();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ namespace OpenDiablo2.Core.GameState_
|
|||||||
private readonly Func<IMapEngine> getMapEngine;
|
private readonly Func<IMapEngine> getMapEngine;
|
||||||
private readonly Func<eSessionType, ISessionManager> getSessionManager;
|
private readonly Func<eSessionType, ISessionManager> getSessionManager;
|
||||||
|
|
||||||
|
private Guid playerId;
|
||||||
private float animationTime = 0f;
|
private float animationTime = 0f;
|
||||||
private List<MapInfo> mapInfo;
|
private List<MapInfo> mapInfo;
|
||||||
private List<MapCellInfo> mapDataLookup = new List<MapCellInfo>();
|
private List<MapCellInfo> mapDataLookup = new List<MapCellInfo>();
|
||||||
@ -35,6 +36,8 @@ namespace OpenDiablo2.Core.GameState_
|
|||||||
|
|
||||||
public int Seed { get; internal set; }
|
public int Seed { get; internal set; }
|
||||||
|
|
||||||
|
public object ThreadLocker { get; } = new object();
|
||||||
|
|
||||||
public GameState(
|
public GameState(
|
||||||
ISceneManager sceneManager,
|
ISceneManager sceneManager,
|
||||||
IResourceManager resourceManager,
|
IResourceManager resourceManager,
|
||||||
@ -65,7 +68,12 @@ namespace OpenDiablo2.Core.GameState_
|
|||||||
mapInfo = new List<MapInfo>();
|
mapInfo = new List<MapInfo>();
|
||||||
sceneManager.ChangeScene("Game");
|
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)
|
private void OnSetSeedEvent(object sender, int seed)
|
||||||
|
@ -18,6 +18,7 @@ namespace OpenDiablo2.ServiceBus
|
|||||||
private readonly Func<eSessionType, ISessionServer> getSessionServer;
|
private readonly Func<eSessionType, ISessionServer> getSessionServer;
|
||||||
private readonly eSessionType sessionType;
|
private readonly eSessionType sessionType;
|
||||||
private readonly Func<eMessageFrameType, IMessageFrame> getMessageFrame;
|
private readonly Func<eMessageFrameType, IMessageFrame> getMessageFrame;
|
||||||
|
private readonly Func<IGameState> getGameState;
|
||||||
|
|
||||||
private RequestSocket requestSocket;
|
private RequestSocket requestSocket;
|
||||||
private AutoResetEvent resetEvent = new AutoResetEvent(false);
|
private AutoResetEvent resetEvent = new AutoResetEvent(false);
|
||||||
@ -28,11 +29,17 @@ namespace OpenDiablo2.ServiceBus
|
|||||||
public OnSetSeedEvent OnSetSeed { get; set; }
|
public OnSetSeedEvent OnSetSeed { get; set; }
|
||||||
public OnJoinGameEvent OnJoinGame { 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.getSessionServer = getSessionServer;
|
||||||
this.sessionType = sessionType;
|
this.sessionType = sessionType;
|
||||||
this.getMessageFrame = getMessageFrame;
|
this.getMessageFrame = getMessageFrame;
|
||||||
|
this.getGameState = getGameState;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
@ -115,15 +122,25 @@ namespace OpenDiablo2.ServiceBus
|
|||||||
if (messageFrame.GetType() != typeof(T))
|
if (messageFrame.GetType() != typeof(T))
|
||||||
throw new ApplicationException("Recieved unexpected message frame!");
|
throw new ApplicationException("Recieved unexpected message frame!");
|
||||||
messageFrame.Data = frameData;
|
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);
|
Task.Run(() =>
|
||||||
playerId = mf.PlayerId;
|
{
|
||||||
Send(mf);
|
var mf = new MFJoinGame(playerName);
|
||||||
ProcessMessageFrame<MFSetSeed>();
|
playerId = mf.PlayerId;
|
||||||
|
Send(mf);
|
||||||
|
ProcessMessageFrame<MFSetSeed>();
|
||||||
|
lock (getGameState().ThreadLocker)
|
||||||
|
{
|
||||||
|
callback(playerId);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user