protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { base.SerializeBody(writer); checked { // Gob creation (request) message structure: // byte: arena identifier // short: number of gobs to create, N // N bytes: arena layer indices // N ints: canonical forms of gob type names // ushort: byte count of all gob data, K // K bytes: serialised data of all gobs var writeBytes = StreamedData; if (_gobTypeNames.Count != _layerIndices.Count) throw new MessageException("_gobTypeNames.Count != _layerIndices.Count"); writer.Write((byte)ArenaID); writer.Write((short)_gobTypeNames.Count); foreach (byte layerIndex in _layerIndices) writer.Write((byte)layerIndex); foreach (var typeName in _gobTypeNames) writer.Write((CanonicalString)typeName); writer.Write((ushort)writeBytes.Length); writer.Write(writeBytes, 0, writeBytes.Length); } } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(null)) #endif checked { // Spectator or team update (request) message structure: // byte: number of spectators and teams to update, K // ushort: total byte count of spectator and team data // K bytes: identifiers of the spectators and teams // ushort: data length N // repeat K times: // ??? bytes: serialised data of a spectator or a team (content known only by the class in question) byte[] writeBytes = StreamedData; #if NETWORK_PROFILING using (new NetworkProfilingScope("SpectatorOrTeamUpdateMessageHeader")) #endif { writer.Write((byte)_ids.Count); writer.Write((ushort)writeBytes.Length); foreach (var id in _ids) writer.Write((byte)id); } writer.Write(writeBytes, 0, writeBytes.Length); } }
public void Serialize(NetworkBinaryWriter writer, SerializationModeFlags mode) { checked { var packedValue = ((int)Weapon1) | (((int)Weapon2) << 4) | (((int)ExtraDevice) << 8); writer.Write((ushort)packedValue); } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { writer.Write((int)FrameNumber); } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { // Ping request message structure (during game): // long: ticks of the timestamp writer.Write((long)Timestamp.Ticks); } }
public void Serialize(NetworkBinaryWriter writer, SerializationModeFlags mode) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { writer.Write((string)PreText); writer.Write((string)Text); writer.Write((Color)TextColor); } }
protected override void SerializeBody(NetworkBinaryWriter writer) { checked { // Game server handshake (UDP) request structure: // short: number of bytes, K // K bytes: game client key writer.Write((short)GameClientKey.Length); writer.Write(GameClientKey); } }
public void Serialize(NetworkBinaryWriter writer, SerializationModeFlags mode) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { if ((mode & SerializationModeFlags.ConstantDataFromServer) != 0) { writer.Write((float)weight); writer.Write((CanonicalString)spawnTypeName); } } }
public override void Serialize(NetworkBinaryWriter writer, SerializationModeFlags mode) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { base.Serialize(writer, mode); if ((mode & SerializationModeFlags.ConstantDataFromServer) != 0) { writer.Write((string)actionGobName); } } }
public override void Serialize(NetworkBinaryWriter writer, SerializationModeFlags mode) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { base.Serialize(writer, mode); if (mode.HasFlag(SerializationModeFlags.ConstantDataFromServer)) { writer.Write((CanonicalString)_propModelName); } } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { checked { // Spectator or team deletion (request) message structure: // sbyte: spectator or team identifier writer.Write((sbyte)SpectatorOrTeamID); } } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif checked { // Game settings request structure: // variable-length string: name of arena to play // canonical string: name of gameplay mode writer.Write((string)ArenaToPlay); writer.Write((CanonicalString)GameplayMode); } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { base.SerializeBody(writer); // Gob deletion (request) message structure: // byte: gob count, N // N * short: gob identifiers writer.Write((byte)GobIDs.Count); foreach (var gobID in GobIDs) writer.Write((short)gobID); } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { // Ping request message structure (during game): // TimeSpan: timestamp originally sent in a ping request message // TimeSpan: total game time when reply was sent, on the instance who sent the reply // int: frame number when reply was sent, on the instance who sent the reply writer.Write((TimeSpan)Timestamp); writer.Write((TimeSpan)TotalGameTimeOnReply); writer.Write((int)FrameNumberOnReply); } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { // Spectator settings reply structure: // byte: local spectator identifier // byte: non-local spectator identifier // string: fail explanation or the empty string writer.Write((byte)SpectatorLocalID); writer.Write((byte)SpectatorID); writer.Write((string)FailMessage); } }
public static void TestNetworkVsRegularWriter() { MemoryStream ms1 = new MemoryStream(); MemoryStream ms2 = new MemoryStream(); NetworkBinaryWriter writer1 = new NetworkBinaryWriter(ms1); NetworkBinaryWriter writer2 = new ProfilingNetworkBinaryWriter(ms2); writer1.Write(new byte[] { 0xca, 0xfe, 0xd0 }, 1, 2); writer2.Write(new byte[] { 0xca, 0xfe, 0xd0 }, 1, 2); writer1.Write("Testixxx"); writer2.Write("Testixxx"); writer1.Write((bool)true); writer2.Write((bool)true); writer1.Write((char)'m'); writer2.Write((char)'m'); writer1.Write((short)32100); writer2.Write((short)32100); writer1.Write((ushort)49200); writer2.Write((ushort)49200); writer1.Write((int)0x1337f00d); writer2.Write((int)0x1337f00d); writer1.Write((uint)0xdeadbeef); writer2.Write((uint)0xdeadbeef); writer1.Write((byte)0x3c); writer2.Write((byte)0x3c); writer1.Write((long)0x1337d0d0cafef00d); writer2.Write((long)0x1337d0d0cafef00d); writer1.Write((ulong)0xdead000fcafef00d); writer2.Write((ulong)0xdead000fcafef00d); writer1.Write((Half)3.141592); writer2.Write((Half)3.141592); writer1.Write((float)3.333333); writer2.Write((float)3.333333); byte[] data1 = ms1.ToArray(); byte[] data2 = ms2.ToArray(); Assert.AreEqual(data1, data2); }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { // Game server handshake (TCP) request structure: // int: number of canonical strings, K // repeat K - 1 (all but the zero-indexed canonical string) // length-prefixed string: string value // short: number of bytes, K // K bytes: game client key writer.Write((int)CanonicalStrings.Count()); foreach (var canonical in CanonicalStrings.Skip(1)) writer.Write((string)canonical); writer.Write((short)GameClientKey.Length); writer.Write(GameClientKey); } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { // Start game (request) message structure: // canonical string: name of gameplay mode // byte: arena identifier // variable-length string: name of arena to play // TimeSpan: time left to play the arena, or zero if the arena doesn't time out // int: number of wall objects in the arena writer.Write((CanonicalString)GameplayMode); writer.Write((byte)ArenaID); writer.Write((string)ArenaToPlay); writer.Write((TimeSpan)ArenaTimeLeft); writer.Write((int)WallCount); } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { base.SerializeBody(writer); checked { // Player message (request) message structure: // short: player ID // word: data length, N // N bytes: serialised message data Write(Message, SerializationModeFlags.AllFromServer); writer.Write((short)PlayerID); writer.Write((ushort)StreamedData.Length); writer.Write(StreamedData, 0, StreamedData.Length); } } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { base.SerializeBody(writer); checked { // Player controls (request) message structure: // byte: player ID // loop PlayerControls.CONTROL_COUNT times // byte: highest bit = control pulse; other bits = control force writer.Write((byte)PlayerID); foreach (var state in ControlStates) { var force7Bit = (byte)(state.Force * 127); var pulseHighBit = state.Pulse ? (byte)0x80 : (byte)0x00; var value = (byte)(force7Bit | pulseHighBit); writer.Write((byte)value); } } } }
public override void Serialize(NetworkBinaryWriter writer, SerializationModeFlags mode) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif checked { base.Serialize(writer, mode); if ((mode & SerializationModeFlags.ConstantDataFromServer) != 0) { writer.Write((float)_spawnInterval); writer.Write((byte)_spawnTypes.Length); foreach (var spawnType in _spawnTypes) spawnType.Serialize(writer, SerializationModeFlags.ConstantDataFromServer); } } }
public override void Serialize(NetworkBinaryWriter writer, SerializationModeFlags mode) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { base.Serialize(writer, mode); if ((mode & SerializationModeFlags.VaryingDataFromServer) != 0) { if (_hoverAroundPos.HasValue) writer.WriteHalf(_hoverAroundPos.Value); else writer.WriteHalf(new Vector2(float.NaN)); } } }
public virtual void Serialize(NetworkBinaryWriter writer, SerializationModeFlags mode) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif checked { if (mode.HasFlag(SerializationModeFlags.ConstantDataFromServer) || mode.HasFlag(SerializationModeFlags.ConstantDataFromClient)) { writer.Write((string)Name); } if (mode.HasFlag(SerializationModeFlags.VaryingDataFromServer)) { writer.Write((bool)IsDisconnected); writer.WriteID(Team); } StatsData.Serialize(writer, mode); ArenaStatistics.Serialize(writer, mode); } }
public override void Serialize(NetworkBinaryWriter writer, SerializationModeFlags mode) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif checked { // Dock doesn't move, so Pos, Move and Rotation are needed only at creation. var reducedMode = mode.HasFlag(SerializationModeFlags.ConstantDataFromServer) ? SerializationModeFlags.AllFromServer : SerializationModeFlags.None; base.Serialize(writer, reducedMode); if (mode.HasFlag(SerializationModeFlags.VaryingDataFromServer)) { writer.Write((byte)_lastRepairTimes.Count); foreach (var item in _lastRepairTimes) writer.Write((short)item.Key.ID); } } }
protected override void SerializeBody(NetworkBinaryWriter writer) { #if NETWORK_PROFILING using (new NetworkProfilingScope(null)) #endif { base.SerializeBody(writer); // Gob update (request) message structure: // byte: number of gobs to update, K // byte: number of collision events, N // ushort: total byte count of gob data and collision event data // K shorts: identifiers of the gobs // repeat K times: // ??? bytes: serialised data of a gob (content known only by the Gob subclass in question) // repeat N times: // ??? bytes: collision event byte[] writeBytes = StreamedData; #if NETWORK_PROFILING using (new NetworkProfilingScope("GobUpdateMessageHeader")) #endif checked { writer.Write((byte)_gobIds.Count); writer.Write((byte)_collisionEventCount); writer.Write((ushort)writeBytes.Length); foreach (var gobId in _gobIds) writer.Write((short)gobId); } writer.Write(writeBytes, 0, writeBytes.Length); } }
protected override void SerializeBody(NetworkBinaryWriter writer) { // Connection closing (request) message structure: // variable length string: info about the closing of the connection writer.Write((string)Info); }
private void SerializeHeader(NetworkBinaryWriter writer, ushort bodyLength) { // Message header structure: // byte protocol_identifier // byte message_type // byte message_flags // byte protocol_version // word message_body_length // word reserved writer.Write((byte)PROTOCOL_IDENTIFIER); var messageType = GetMessageType(GetType()); writer.Write((byte)messageType.TopicIdentifier); if (messageType.IsReply) _headerFlags |= MessageHeaderFlags.Reply; writer.Write((byte)_headerFlags); // flags writer.Write((byte)VERSION_IDENTIFIER); writer.Write((ushort)bodyLength); writer.Write((ushort)0); }
public override void Serialize(NetworkBinaryWriter writer, SerializationModeFlags mode) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif checked { base.Serialize(writer, mode); // HACK to avoid null references: // - ForwardShot using Ship.Model before LoadContent() is called // - Thrust() using _thrusterSound before Activate() is called var shipMode = mode.HasFlag(SerializationModeFlags.ConstantDataFromServer) ? mode & ~SerializationModeFlags.VaryingDataFromServer : mode; if (shipMode.HasFlag(SerializationModeFlags.VaryingDataFromServer)) { var visualThrustForceAndFlags = (byte)MathHelper.Clamp(_visualThrustForce * 127, 0, 127); var updateDeviceTypes = _deviceTypeNameUpdateTimer.IsElapsed; if (updateDeviceTypes) visualThrustForceAndFlags |= 0x80; writer.Write((byte)visualThrustForceAndFlags); _visualThrustForceSerializedThisFrame = true; Action<ShipDevice> serializeDevice = device => { var chargeData = device == null ? 0 : byte.MaxValue * device.Charge / device.ChargeMax; writer.Write((byte)chargeData); if (updateDeviceTypes) writer.Write((CanonicalString)device.TypeName); }; serializeDevice(Weapon1); serializeDevice(Weapon2); serializeDevice(ExtraDevice); DeviceUsagesToClients.Serialize(writer, mode); DeviceUsagesToClients.Clear(); } if (shipMode.HasFlag(SerializationModeFlags.VaryingDataFromClient)) { var rotationByte = unchecked((byte)((int)Math.Round(Rotation / MathHelper.TwoPi * (byte.MaxValue + 1)))); writer.Write((byte)rotationByte); } } }
public override void Serialize(NetworkBinaryWriter writer, SerializationModeFlags mode) { #if NETWORK_PROFILING using (new NetworkProfilingScope(this)) #endif { checked { base.Serialize(writer, mode); if (mode.HasFlag(SerializationModeFlags.ConstantDataFromServer) || mode.HasFlag(SerializationModeFlags.ConstantDataFromClient)) { writer.Write((CanonicalString)ShipName); writer.Write((CanonicalString)Weapon2Name); writer.Write((CanonicalString)ExtraDeviceName); } } } }
private void PopulateGobUpdateMessage(GobUpdateMessage gobMessage, IEnumerable<Gob> gobs, SerializationModeFlags serializationMode) { var now = DataEngine.ArenaTotalTime; var debugMessage = Settings.Net.HeavyDebugLog && gobs.OfType<AW2.Game.Gobs.Wall>().Any(wall => wall.ForcedNetworkUpdate) ? new System.Text.StringBuilder("Gob update ") : null; // DEBUG: catch a rare crash that seems to happen only when serializing walls. foreach (var gob in gobs) { if (!gob.ForcedNetworkUpdate) { if (!gob.IsRelevant) continue; if (gob.MoveType == MoveType.Static) continue; if (gob.NetworkUpdatePeriod == TimeSpan.Zero) continue; if (gob.LastNetworkUpdate + gob.NetworkUpdatePeriod > now) continue; } gob.ForcedNetworkUpdate = false; gob.LastNetworkUpdate = now; gobMessage.AddGob(gob.ID, gob, serializationMode); if (debugMessage != null) debugMessage.AppendFormat("{0} [{1}], ", gob.GetType().Name, gob.TypeName); // DEBUG: catch a rare crash that seems to happen only when serializing walls. } gobMessage.SetCollisionEvents(_collisionEventsToRemote, serializationMode); _collisionEventsToRemote = new List<CollisionEvent>(); if (debugMessage != null) // DEBUG: catch a rare crash that seems to happen only when serializing walls. { var writer = new NetworkBinaryWriter(new System.IO.MemoryStream(_debugBuffer)); gobMessage.Serialize(writer); debugMessage.Append(MiscHelper.BytesToString(new ArraySegment<byte>(_debugBuffer, 0, (int)writer.GetBaseStream().Position))); Log.Write(debugMessage.ToString()); } }