Skip to content

Commit

Permalink
Start adding protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Jan 16, 2024
1 parent 660d826 commit 643829c
Show file tree
Hide file tree
Showing 21 changed files with 1,328 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/ntcore/StructArrayTopic.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using NetworkTables.Handles;
using WPIUtil.Serialization;
using WPIUtil.Serialization.Struct;

namespace NetworkTables;

Expand Down
2 changes: 1 addition & 1 deletion src/wpiutil/DataLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Runtime.InteropServices;
using WPIUtil.Handles;
using WPIUtil.Natives;
using WPIUtil.Serialization;
using WPIUtil.Serialization.Struct;

namespace WPIUtil;

Expand Down
2 changes: 1 addition & 1 deletion src/wpiutil/Logging/StructArrayLogEntry.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Linq;
using WPIUtil.Serialization;
using WPIUtil.Serialization.Struct;

namespace WPIUtil.Logging;

Expand Down
2 changes: 1 addition & 1 deletion src/wpiutil/Logging/StructLogEntry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using WPIUtil.Serialization;
using WPIUtil.Serialization.Struct;

namespace WPIUtil.Logging;

Expand Down
20 changes: 20 additions & 0 deletions src/wpiutil/Serialization/Protobuf/IProtobuf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Google.Protobuf;
using Google.Protobuf.Reflection;

namespace WPIUtil.Serialization.Protobuf;

public interface IProtobufBase
{
string TypeString => $"proto:{Descriptor.FullName}";
MessageDescriptor Descriptor { get; }
IProtobufBase[] Nested => [];
}

public interface IProtobuf<T, MessageType> : IProtobufBase where MessageType : IMessage
{
MessageType CreateMessage();

T Unpack(MessageType msg);

void Pack(MessageType msg, T value);
}
8 changes: 8 additions & 0 deletions src/wpiutil/Serialization/Protobuf/IProtobufSerializable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Google.Protobuf;

namespace WPIUtil.Serialization.Protobuf;

public interface IProtobufSerializable<T, MessageType> : IWPISerializable<T> where MessageType : IMessage
{
public static abstract IProtobuf<T, MessageType> Proto { get; }
}
33 changes: 33 additions & 0 deletions src/wpiutil/Serialization/Protobuf/ProtobufBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using Google.Protobuf;

namespace WPIUtil.Serialization.Protobuf;

public sealed class ProtobufBuffer<T, MessageType> where MessageType : IMessage
where T : IProtobufSerializable<T, MessageType> {
private ProtobufBuffer() {
Proto = T.Proto;
m_msg = Proto.CreateMessage();
}

public static ProtobufBuffer<T, MessageType> Create() {
return new ProtobufBuffer<T, MessageType>();
}

public IProtobuf<T, MessageType> Proto {get;}

public string TypeString => Proto.TypeString;

public ReadOnlySpan<byte> Write(T value){
Proto.Pack(m_msg, value);
int size = m_msg.CalculateSize();
if (size > m_buf.Length) {
m_buf = new byte[size];
}
m_msg.WriteTo(m_buf.AsSpan()[..size]);
return m_buf.AsSpan()[..size];
}

private readonly MessageType m_msg;
private byte[] m_buf = new byte[1024];
}
Loading

0 comments on commit 643829c

Please sign in to comment.