public void CanSerializeAndDeserialize() { var infoPacket1 = new PsnInfoPacketChunk( new PsnInfoHeaderChunk(1500, 2, 1, 34, 1), new PsnInfoSystemNameChunk("Test System"), new PsnInfoTrackerListChunk( new PsnInfoTrackerChunk(0, new PsnInfoTrackerNameChunk("Test Tracker")) ) ); var infoData = infoPacket1.ToByteArray(); var infoPacket2 = PsnPacketChunk.FromByteArray(infoData); infoPacket1.Should() .Be(infoPacket2, "because the deserializing the serialized data should produce the same values"); var dataPacket1 = new PsnDataPacketChunk( new PsnDataHeaderChunk(1500, 2, 1, 34, 1), new PsnDataTrackerListChunk( new PsnDataTrackerChunk(0, new PsnDataTrackerPosChunk(0.45f, 7.56f, 2343.43f), new PsnDataTrackerSpeedChunk(0.34f, 5.76f, -876.87f), new PsnDataTrackerOriChunk(6.4f, -3.576f, 3873.3f), new PsnDataTrackerStatusChunk(54f), new PsnDataTrackerAccelChunk(4.34f, 23423.5f, 234.4f), new PsnDataTrackerTrgtPosChunk(23.3f, 4325f, 4234f), new PsnDataTrackerTimestampChunk(34524534454543543) ), new PsnDataTrackerChunk(1, new PsnDataTrackerPosChunk(-343.44f, 4.76f, 2.45f), new PsnDataTrackerSpeedChunk(34f, -23f, 5676.4f), new PsnDataTrackerOriChunk(24.7f, 3.53376f, 38.334f), new PsnDataTrackerStatusChunk(0.1f), new PsnDataTrackerAccelChunk(4234.34f, 543543.4f, 23.43f), new PsnDataTrackerTrgtPosChunk(2342.6f, 35.5f, -14545.4f), new PsnDataTrackerTimestampChunk(ulong.MaxValue) ) ) ); var dataData = dataPacket1.ToByteArray(); var dataPacket2 = PsnPacketChunk.FromByteArray(dataData); dataPacket1.Should() .Be(dataPacket2, "because the deserializing the serialized data should produce the same values"); }
/// <summary> /// Override to provide new behavior for transmission of PosiStageNet info packets /// </summary> /// <param name="trackers">Enumerable of trackers to send info packets for</param> protected virtual void OnSendInfo([NotNull] IEnumerable <PsnTracker> trackers) { var systemNameChunk = new PsnInfoSystemNameChunk(SystemName); var trackerChunks = trackers.Select(t => new PsnInfoTrackerChunk(t.TrackerId, t.ToInfoTrackerChunks())); var trackerListChunks = new List <PsnInfoTrackerListChunk>(); var currentTrackerList = new List <PsnInfoTrackerChunk>(); int trackerListLength = 0; int maxTrackerListLength = MaxPacketLength - (PsnChunk.ChunkHeaderLength // Root Chunk-Header + PsnInfoHeaderChunk.StaticChunkAndHeaderLength // Packet Header Chunk + systemNameChunk.ChunkAndHeaderLength // System Name Chunk + PsnChunk.ChunkHeaderLength); // Tracker List Chunk-Header foreach (var chunk in trackerChunks) { if (trackerListLength <= maxTrackerListLength) { currentTrackerList.Add(chunk); trackerListLength += chunk.ChunkAndHeaderLength; } else { trackerListChunks.Add(new PsnInfoTrackerListChunk(currentTrackerList)); currentTrackerList = new List <PsnInfoTrackerChunk>(); trackerListLength = 0; } } trackerListChunks.Add(new PsnInfoTrackerListChunk(currentTrackerList)); ulong timestamp = (ulong)_timeStampReference.ElapsedMilliseconds; foreach (var trackerListChunk in trackerListChunks) { var packet = new PsnInfoPacketChunk( new PsnInfoHeaderChunk(timestamp, VersionHigh, VersionLow, _frameId, trackerListChunks.Count), systemNameChunk, trackerListChunk); var data = packet.ToByteArray(); Debug.Assert(data.Length <= MaxPacketLength); _udpService.SendAsync(data, _targetEndPoint).Wait(); } }
/// <summary> /// Override to provide new behavior for receipt of a PosiStageNet info packet /// </summary> protected virtual void OnInfoPacketReceived(PsnInfoPacketChunk infoPacket) { var headerChunk = getSingleChunk <PsnInfoHeaderChunk, PsnInfoPacketChunk>(infoPacket, (ushort)PsnInfoPacketChunkId.PsnInfoHeader, "Info", "info header"); if (headerChunk == null) { return; } var systemNameChunk = getSingleChunk <PsnInfoSystemNameChunk, PsnInfoPacketChunk>(infoPacket, (ushort)PsnInfoPacketChunkId.PsnInfoSystemName, "Info", "system name"); if (systemNameChunk == null) { return; } if (_currentInfoPacketChunks.Any()) { if (headerChunk.TimeStamp != _lastInfoPacketHeader.TimeStamp || headerChunk.FramePacketCount != _lastInfoPacketHeader.FramePacketCount) { InvalidPacketReceived?.Invoke(this, new InvalidPacketsReceivedEventArgs(_currentInfoPacketChunks, false, "Incomplete packet chunk discarded, did not receive all packets for info frame")); _currentInfoPacketChunks.Clear(); } } _lastInfoPacketHeader = headerChunk; _currentInfoPacketChunks.Add(infoPacket); if (_currentInfoPacketChunks.Count == _lastInfoPacketHeader.FramePacketCount) { OnCompleteInfoFrameReceived(_lastInfoPacketHeader, systemNameChunk.SystemName, _currentInfoPacketChunks); _currentInfoPacketChunks.Clear(); } }