public static GamestatePacket GetPacket() { GamestatePacket packet; lock (packetPool) { if (packetPool.Count == 0) { packet = new GamestatePacket(); } else { packet = packetPool.Pop(); } } packet.hasName = false; packet.shortValues.Clear(); for (int i = 0; i < GamestatePacket.maxShorts; i++) { packet.hasShortValues[i] = false; } return(packet); }
private GamestatePacket CopyPacket(GamestatePacket packet) { GamestatePacket copiedPacket = new GamestatePacket(); copiedPacket.revisionNumber = packet.revisionNumber; copiedPacket.revisionActor = packet.revisionActor; copiedPacket.id = packet.id; copiedPacket.hasName = packet.hasName; copiedPacket.name = packet.name; copiedPacket.shortValues = new List <short>(packet.shortValues); for (int i = 0; i < GamestatePacket.maxShorts; i++) { copiedPacket.hasShortValues[i] = packet.hasShortValues[i]; } for (int i = 0; i < GamestatePacket.maxBools; i++) { copiedPacket.boolValues[i] = packet.boolValues[i]; } return(copiedPacket); }
private void GetPacket() { packet = GamestatePacketManager.GetPacket(); for (int i = 0; i < GamestatePacket.maxBools; i++) { setBools[i] = false; } }
internal void Apply(GamestatePacket packet) { T foundEntry = null; bool created = false; lock (entries) { if (packet.packetType == GamestatePacket.PacketType.Delete) { for (int i = 0; i < entries.Count; i++) { if (entries[i].id == packet.id) { entries.RemoveAt(i); foreach (DeletionListener deletionListener in deletionListeners) { deletionListener(i); } break; } } } else { foreach (T entry in entries) { if (entry.id == packet.id) { foundEntry = entry; break; } } if (foundEntry == null && packet.packetType == GamestatePacket.PacketType.Create) { foundEntry = Create(packet.id); created = true; } } } if (foundEntry != null) { foundEntry.Apply(packet); } if (created) { foreach (CreationListener creationListener in creationListeners) { foundEntry.Lock(); creationListener(foundEntry); } foundEntry.Release(); } }
public void Apply(GamestatePacket packet) { if (table != null) { table.Apply(packet); } else if (globals != null) { globals.Apply(packet); } }
public static void ReleasePacket(GamestatePacket packet) { if (packet == null) { throw new Exception("Cannot release null GameStatepacket."); } lock (packetPool) { packetPool.Push(packet); } }
void IGamestateCommitHandler.CommitPacket(GamestatePacket packet) { if (PhotonNetwork.LocalPlayer.IsMasterClient) { ApplyPacket(packet); } else { view.RPC(nameof(ApplyPacket), RpcTarget.MasterClient, packet); } }
internal void Increment(EntryErrorCallback callback) { PreparePacketForIncrement(); errorCallbacks.Add(callback); commitHandler.CommitPacket(packet); GamestatePacketManager.ReleasePacket(packet); packet = null; Release(); }
public bool AttemptApply(GamestatePacket packet) { if (table != null) { return(table.AttemptApply(packet)); } else if (globals != null) { return(globals.AttemptApply(packet)); } return(false); }
public void ApplyPacket(GamestatePacket packet) { if (PhotonNetwork.LocalPlayer.IsMasterClient) { bool succeeded = false; if (packet.table == Table.Globals) { if (packet.packetType != GamestatePacket.PacketType.Delete) { succeeded = _globals.AttemptApply(packet); } } else if (packet.table == Table.Players) { succeeded = players.AttemptApply(packet); } else if (packet.table == Table.Teams) { succeeded = teams.AttemptApply(packet); } if (succeeded) { if (bufferPackets) { view.RPC(nameof(ApplyPacket), RpcTarget.OthersBuffered, packet); } else { view.RPC(nameof(ApplyPacket), RpcTarget.Others, packet); } } } else { if (packet.table == Table.Globals) { if (packet.packetType != GamestatePacket.PacketType.Delete) { _globals.Apply(packet); } } else if (packet.table == Table.Players) { players.Apply(packet); } else if (packet.table == Table.Teams) { teams.Apply(packet); } } }
internal bool AttemptApply(GamestatePacket packet) { T foundEntry = null; bool created = false; bool succeeded = false; lock (entries) { if (packet.packetType == GamestatePacket.PacketType.Delete) { for (int i = 0; i < entries.Count; i++) { if (entries[i].id == packet.id) { entries.RemoveAt(i); succeeded = true; break; } } } else { foreach (T entry in entries) { if (entry.id == packet.id) { foundEntry = entry; break; } } if (foundEntry == null && packet.packetType == GamestatePacket.PacketType.Create) { foundEntry = Create(packet.id); created = true; } } } if (foundEntry != null) { foundEntry.Apply(packet); succeeded = true; } if (created) { foundEntry.Release(); } return(succeeded); }
internal void Commit(EntryErrorCallback callback) { PreparePacketForCommit(); errorCallbacks.Add(callback); if (errorCallbacks.Count == 1) { commitHandler.CommitPacket(packet); } GamestatePacketManager.ReleasePacket(packet); packet = null; Release(); }
public void Increment() { PreparePacketForIncrement(); if (errorCallbacks.Count == 0) { errorCallbacks.Add(null); } commitHandler.CommitPacket(packet); GamestatePacketManager.ReleasePacket(packet); packet = null; Release(); }
internal bool AttemptApply(GamestatePacket packet) { bool succeeded = false; Lock(); if (_revisionNumber + 1 == packet.revisionNumber || packet.packetType == GamestatePacket.PacketType.Increment) { succeeded = true; Apply(packet); } Release(); return(succeeded); }
public void Delete() { if (packet == null) { GetPacket(); } packet.id = id; packet.packetType = GamestatePacket.PacketType.Delete; commitHandler.CommitPacket(packet); GamestatePacketManager.ReleasePacket(packet); packet = null; Release(); }
void IGamestateCommitHandler.CommitPacket(GamestatePacket packet) { gamestateTracker.CommitPacket(packet); }
public static byte[] Serialize(object packetObject) { GamestatePacket packet = packetObject as GamestatePacket; if (packet == null) { throw new Exception("Serialised object is not a GamestatePacket"); } int nameLength = 0; int shortCount = 0; for (int i = 0; i < maxShorts; i++) { if (packet.hasShortValues[i]) { shortCount++; } } if (shortCount > packet.shortValues.Count) { throw new Exception("More short values marked as present than have been provided."); } int arraySize = 1; // Packet metadata arraySize += 2; // ID if (packet.packetType != PacketType.Delete) { if (packet.packetType != PacketType.Increment) { arraySize += 4; // Revision number } arraySize += 1; // Revision actor arraySize += 2; // Short metadata arraySize += 2; // Bool values if (packet.packetType != PacketType.Increment) { if (packet.hasName) // Name { nameLength = stringEncoder.GetByteCount(packet.name); if (nameLength > 0xFF) { throw new Exception("GamestatePacket name is too long. (max 255 bytes)"); } arraySize += 1 + nameLength; } } arraySize += shortCount * 2;// Short values } byte[] data = new byte[arraySize]; int index = 0; // Serialize metadata data[index] = (byte)(((int)packet.packetType & 0x03) | (((int)packet.table << 2) & 0x7C)); if (packet.hasName) { data[index] = (byte)(data[index] | 0x80); } index++; // Serialize ID data[index] = (byte)((packet.id >> 8) & 0xFF); data[index + 1] = (byte)(packet.id & 0xFF); index += 2; if (packet.packetType != PacketType.Delete) { if (packet.packetType != PacketType.Increment) { // Serialize revision number data[index] = (byte)((packet.revisionNumber >> 24) & 0xFF); data[index + 1] = (byte)((packet.revisionNumber >> 16) & 0xFF); data[index + 2] = (byte)((packet.revisionNumber >> 8) & 0xFF); data[index + 3] = (byte)(packet.revisionNumber & 0xFF); index += 4; } // Serialize revision actor data[index] = packet.revisionActor; index++; // Serialize short metadata byte hasShortsA = 0x00; byte hasShortsB = 0x00; for (int i = 0; i < 8; i++) { if (packet.hasShortValues[i + 8]) { hasShortsA = (byte)(hasShortsA | (0x01 << i)); } if (packet.hasShortValues[i]) { hasShortsB = (byte)(hasShortsB | (0x01 << i)); } } data[index] = hasShortsA; data[index + 1] = hasShortsB; index += 2; // Serialize bool values byte hasBoolsA = 0x00; byte hasBoolsB = 0x00; for (int i = 0; i < 8; i++) { if (packet.boolValues[i + 8]) { hasBoolsA = (byte)(hasBoolsA | (0x01 << i)); } if (packet.boolValues[i]) { hasBoolsB = (byte)(hasBoolsB | (0x01 << i)); } } data[index] = hasBoolsA; data[index + 1] = hasBoolsB; index += 2; if (packet.packetType != PacketType.Increment) { // Serialize name if (packet.hasName) { int bytes = stringEncoder.GetBytes(packet.name, 0, packet.name.Length, data, index + 1); data[index] = (byte)bytes; index += 1 + bytes; } } // Serialize short values for (int i = 0; i < shortCount; i++) { data[index] = (byte)((packet.shortValues[i] >> 8) & 0xFF); data[index + 1] = (byte)(packet.shortValues[i] & 0xFF); index += 2; } } return(data); }
public static object Deserialize(byte[] data) { GamestatePacket packet = GamestatePacketManager.GetPacket(); int index = 0; // Deserialize metadata packet.packetType = (PacketType)(data[index] & 0x03); packet.table = (GamestateTracker.Table)((data[index] & 0x7C) >> 2); packet.hasName = (data[index] & 0x80) == 0x80; index++; // Deserialize id short idA = (short)(((uint)data[index] << 8) & 0x0000FF00); short idB = (short)((uint)data[index + 1] & 0x000000FF); packet.id = (short)(idA | idB); index += 2; if (packet.packetType != PacketType.Delete) { if (packet.packetType != PacketType.Increment) { // Deserialize revision number uint revisionNumberA = ((uint)data[index] << 24) & 0xFF000000; uint revisionNumberB = ((uint)data[index + 1] << 16) & 0x00FF0000; uint revisionNumberC = ((uint)data[index + 2] << 8) & 0x0000FF00; uint revisionNumberD = (uint)data[index + 3] & 0x000000FF; packet.revisionNumber = revisionNumberA | revisionNumberB | revisionNumberC | revisionNumberD; index += 4; } // Deserialize revision actor packet.revisionActor = data[index]; index++; // Deserialize short metadata int shortCount = 0; for (int i = 0; i < 8; i++) { if (((data[index] >> i) & 0x01) == 0x01) { packet.hasShortValues[i + 8] = true; shortCount++; } else { packet.hasShortValues[i + 8] = false; } if (((data[index + 1] >> i) & 0x01) == 0x01) { packet.hasShortValues[i] = true; shortCount++; } else { packet.hasShortValues[i] = false; } } index += 2; // Deserialize bool values for (int i = 0; i < 8; i++) { if (((data[index] >> i) & 0x01) == 0x01) { packet.boolValues[i + 8] = true; } else { packet.boolValues[i + 8] = false; } if (((data[index + 1] >> i) & 0x01) == 0x01) { packet.boolValues[i] = true; } else { packet.boolValues[i] = false; } } index += 2; if (packet.packetType != PacketType.Increment) { // Deserialise name if (packet.hasName) { int nameLength = data[index]; packet.name = stringEncoder.GetString(data, index + 1, nameLength); index += nameLength + 1; } } // Deserialize short values for (int i = 0; i < shortCount; i++) { short valueA = (short)(((uint)data[index] << 8) & 0x0000FF00); short valueB = (short)((uint)data[index + 1] & 0x000000FF); packet.shortValues.Add((short)(valueA | valueB)); index += 2; } } return(packet); }
void IGamestateCommitHandler.CommitPacket(GamestatePacket packet) { GamestatePacket storedPacket = CopyPacket(packet); commitedPackets.Add(storedPacket); }
internal void Apply(GamestatePacket packet) { Lock(); if (packet.packetType == GamestatePacket.PacketType.Increment) { _revisionNumber++; int j = 0; for (int i = 0; i < shortValues.Count; i++) { if (packet.hasShortValues[i]) { shortValues[i] += packet.shortValues[j]; j++; } } } else { _revisionNumber = packet.revisionNumber; if (packet.hasName) { _name = packet.name; } int j = 0; for (int i = 0; i < shortValues.Count; i++) { if (packet.hasShortValues[i]) { shortValues[i] = packet.shortValues[j]; j++; } } } for (int i = 0; i < boolValues.Count; i++) { boolValues[i] = packet.boolValues[i]; } if (errorCallbacks.Count > 0) { if (errorCallbacks[0] != null) { Lock(); errorCallbacks[0](this, packet.revisionActor == commitHandler.actorNumber); } for (int i = 1; i < errorCallbacks.Count; i++) { Lock(); errorCallbacks[i](this, false); } errorCallbacks.Clear(); } for (int i = 0; i < listeners.Count; i++) { if (listeners[i] != null) { Lock(); listeners[i](this); } } Release(); }