Skip to content
Permalink
Browse files

[297] Zugriffsfehler bei localChunkCache gefixt

  • Loading branch information...
tomwendel committed Dec 10, 2015
1 parent 1ac5ed7 commit f20b72c48dc580a8b2a89b98b38de39cf978d718
@@ -55,14 +55,14 @@ public ClientComponent(OctoGame game) : base(game)

ResourceManager.Instance.GlobalChunkCache = new GlobalChunkCache((position) =>
{
using (Stream stream = chunkClient.SubscribeChunk(position.Planet, position.ChunkIndex.X, position.ChunkIndex.Y, position.ChunkIndex.Z))
using (Stream stream = chunkClient.SubscribeChunk(connectionId, position))
{
ChunkSerializer serializer = new ChunkSerializer();
return serializer.Deserialize(stream, position);
}
}, (position, chunk) =>
{
client.UnsubscribeChunk(position.Planet, position.ChunkIndex.X, position.ChunkIndex.Y, position.ChunkIndex.Z);
client.UnsubscribeChunk(position);
});
}

@@ -42,14 +42,18 @@ public void Initialize()
{
localChunkCache.SetCenter(planet, Player.Position.ChunkIndex, (success) =>
{
localChunkCache.GetChunk(Player.Position.ChunkIndex).Entities.Add(Player);
IChunk chunk = localChunkCache.GetChunk(Player.Position.ChunkIndex);
if (chunk != null)
chunk.Entities.Add(Player);
ReadyState = success;
});
}

public void Uninitialize()
{
localChunkCache.GetChunk(Player.Position.ChunkIndex).Entities.Remove(Player);
IChunk chunk = localChunkCache.GetChunk(Player.Position.ChunkIndex);
if (chunk != null)
chunk.Entities.Remove(Player);
}

public void Update(GameTime frameTime)
@@ -193,14 +197,18 @@ public void Update(GameTime frameTime)
if (Player.Position.ChunkIndex != _oldIndex)
{
// Aus altem Chunk entfernen
localChunkCache.GetChunk(_oldIndex).Entities.Remove(Player);
IChunk oldChunk = localChunkCache.GetChunk(_oldIndex);
if (oldChunk != null)
oldChunk.Entities.Remove(Player);

_oldIndex = Player.Position.ChunkIndex;
ReadyState = false;
localChunkCache.SetCenter(planet, Player.Position.ChunkIndex, (success) =>
{
// Zu neuem Chunk hinzufügen
localChunkCache.GetChunk(Player.Position.ChunkIndex).Entities.Add(Player);
IChunk newChunk = localChunkCache.GetChunk(Player.Position.ChunkIndex);
if (newChunk != null)
newChunk.Entities.Add(Player);

// TODO: Move-Event

@@ -283,8 +291,8 @@ public void Update(GameTime frameTime)
#region Property Events

// Position
if (Position.Planet != oldPosition.Planet ||
Position.GlobalBlockIndex != oldPosition.GlobalBlockIndex ||
if (Position.Planet != oldPosition.Planet ||
Position.GlobalBlockIndex != oldPosition.GlobalBlockIndex ||
Position.BlockPosition != oldPosition.BlockPosition)
{
oldPosition = Position;
@@ -8,9 +8,11 @@ namespace OctoAwesome.Runtime
{
public class ChunkConnection : IChunkConnection
{
public Stream SubscribeChunk(int planet, int x, int y, int z)
public Stream SubscribeChunk(Guid clientId, PlanetIndex3 index)
{
IChunk chunk = ResourceManager.Instance.GlobalChunkCache.Subscribe(new PlanetIndex3(planet, x, y, z), false);
Server.Instance.SubscibeChunk(clientId, index);

IChunk chunk = ResourceManager.Instance.GlobalChunkCache.Subscribe(index, false);
ChunkSerializer serializer = new ChunkSerializer();
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, chunk);
@@ -18,10 +18,13 @@ public class Client : IConnection

public ActorHost ActorHost { get; private set; }

public List<PlanetIndex3> SubscripedChunks { get; private set; }

public Client()
{
Callback = OperationContext.Current.GetCallbackChannel<IConnectionCallback>();
ConnectionId = Guid.NewGuid();
SubscripedChunks = new List<PlanetIndex3>();
}

public void SetActorHost(ActorHost host)
@@ -163,9 +166,9 @@ public void Interact(Index3 blockIndex)
}

[OperationBehavior]
public void UnsubscribeChunk(int planet, int x, int y, int z)
public void UnsubscribeChunk(PlanetIndex3 index)
{
throw new NotImplementedException();
Server.Instance.UnsubscribeChunk(ConnectionId, index);
}
}
}
@@ -11,6 +11,6 @@ namespace OctoAwesome.Runtime
public interface IChunkConnection
{
[OperationContract]
Stream SubscribeChunk(int planet, int x, int y, int z);
Stream SubscribeChunk(Guid clientId, PlanetIndex3 index);
}
}
@@ -44,7 +44,7 @@ public interface IConnection

#region Subscription

void UnsubscribeChunk(int planet, int x, int y, int z);
void UnsubscribeChunk(PlanetIndex3 index);

#endregion
}
@@ -34,6 +34,8 @@ public static Server Instance

private List<Client> clients = new List<Client>();

private Dictionary<PlanetIndex3, List<Client>> subscriptions = new Dictionary<PlanetIndex3, List<Client>>();

private Dictionary<Client, ClientInfo> clientInfos = new Dictionary<Client, ClientInfo>();

public Server()
@@ -168,6 +170,11 @@ internal void Leave(Client client)
}
catch (Exception) { }

// Alle Chunks deabonieren
foreach (var chunk in client.SubscripedChunks)
UnsubscribeChunk(client.ConnectionId, chunk);
client.SubscripedChunks.Clear();

clients.Remove(client);
clientInfos.Remove(client);
if (OnLeave != null)
@@ -191,6 +198,35 @@ internal void Leave(Client client)
}
}

internal void SubscibeChunk(Guid clientId, PlanetIndex3 chunkIndex)
{
lock (subscriptions)
{
var client = clients.SingleOrDefault(c => c.ConnectionId == clientId);
if (client == null) return;

if (!subscriptions.ContainsKey(chunkIndex))
subscriptions.Add(chunkIndex, new List<Client>());
subscriptions[chunkIndex].Add(client);
}
}

internal void UnsubscribeChunk(Guid clientId, PlanetIndex3 chunkIndex)
{
lock (subscriptions)
{
var client = clients.SingleOrDefault(c => c.ConnectionId == clientId);
if (client == null) return;

if (subscriptions.ContainsKey(chunkIndex))
{
subscriptions[chunkIndex].Remove(client);
if (subscriptions[chunkIndex].Count == 0)
subscriptions.Remove(chunkIndex);
}
}
}

public IEnumerable<ClientInfo> Clients
{
get

0 comments on commit f20b72c

Please sign in to comment.
You can’t perform that action at this time.