-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
284 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Google.Protobuf; | ||
using WPIUtil.Serialization.Protobuf; | ||
|
||
namespace NetworkTables; | ||
|
||
public interface IProtobufEntry<T> : IProtobufSubscriber<T>, IProtobufPublisher<T> where T : IProtobufSerializable<T> | ||
{ | ||
void Unpublish(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Reflection.Metadata; | ||
using Google.Protobuf; | ||
using NetworkTables.Handles; | ||
using NetworkTables.Natives; | ||
using WPIUtil.Serialization.Protobuf; | ||
|
||
namespace NetworkTables; | ||
|
||
internal sealed class ProtobufEntryImpl<T, THandle> : EntryBase<THandle>, IProtobufEntry<T> where THandle : struct, INtEntryHandle where T : IProtobufSerializable<T> | ||
{ | ||
internal ProtobufEntryImpl(ProtobufTopic<T> topic, THandle handle, T defaultValue, bool schemaPublished) : base(handle) | ||
{ | ||
Topic = topic; | ||
m_defaultValue = defaultValue; | ||
m_buf = new ProtobufBuffer<T>(); | ||
m_schemaPublished = schemaPublished; | ||
} | ||
|
||
private readonly T m_defaultValue; | ||
private readonly ProtobufBuffer<T> m_buf; | ||
private bool m_schemaPublished; | ||
private readonly object m_lockObject = new(); | ||
|
||
public override ProtobufTopic<T> Topic { get; } | ||
|
||
public T Get() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public T Get(T defaultValue) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public TimestampedObject<T> GetAtomic() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public TimestampedObject<T> GetAtomic(T defaultValue) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public bool GetInto(ref T output) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public TimestampedObject<T>[] ReadQueue() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public T[] ReadQueueValues() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public void Set(T value) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public void Set(long time, T value) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public void SetDefault(T value) | ||
{ | ||
try | ||
{ | ||
lock (m_lockObject) | ||
{ | ||
if (!m_schemaPublished) | ||
{ | ||
m_schemaPublished = true; | ||
Topic.Instance.AddSchema(m_buf.Proto); | ||
} | ||
ReadOnlySpan<byte> buf = m_buf.Write(value); | ||
NtCore.SetDefaultEntryValue(Handle, RefNetworkTableValue.MakeRaw(buf)); | ||
} | ||
} | ||
catch | ||
{ | ||
|
||
} | ||
} | ||
|
||
public void Unpublish() | ||
{ | ||
NtCore.Unpublish(Handle); | ||
} | ||
|
||
private T FromRaw(ReadOnlySpan<byte> raw, T defaultValue) | ||
{ | ||
if (raw.Length == 0) | ||
{ | ||
return defaultValue; | ||
} | ||
|
||
try | ||
{ | ||
lock (m_lockObject) | ||
{ | ||
return m_buf.Read(raw); | ||
} | ||
} | ||
catch | ||
{ | ||
return defaultValue; | ||
} | ||
} | ||
|
||
private TimestampedObject<T> FromRaw(TimestampedRaw raw, T defaultValue) | ||
{ | ||
if (raw.Value.Length == 0) | ||
{ | ||
return new TimestampedObject<T>(0, 0, defaultValue); | ||
} | ||
try | ||
{ | ||
lock (m_lockObject) | ||
{ | ||
return new TimestampedObject<T>(raw.Timestamp, raw.ServerTime, m_buf.Read(raw.Value)); | ||
} | ||
} | ||
catch | ||
{ | ||
return new TimestampedObject<T>(0, 0, defaultValue); | ||
} | ||
} | ||
|
||
private static readonly ReadOnlyMemory<byte> m_emptyRaw = Array.Empty<byte>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Google.Protobuf; | ||
using WPIUtil.Serialization.Protobuf; | ||
|
||
namespace NetworkTables; | ||
|
||
public interface IProtobufPublisher<T> : Publisher where T : IProtobufSerializable<T> | ||
{ | ||
new ProtobufTopic<T> Topic { get; } | ||
|
||
void Set(T value); | ||
|
||
void Set(long time, T value); | ||
|
||
void SetDefault(T value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Google.Protobuf; | ||
using WPIUtil.Serialization.Protobuf; | ||
|
||
namespace NetworkTables; | ||
|
||
public interface IProtobufSubscriber<T> : Subscriber where T : IProtobufSerializable<T> | ||
{ | ||
new ProtobufTopic<T> Topic { get; } | ||
|
||
T Get(); | ||
|
||
T Get(T defaultValue); | ||
|
||
bool GetInto(ref T output); | ||
|
||
TimestampedObject<T> GetAtomic(); | ||
|
||
TimestampedObject<T> GetAtomic(T defaultValue); | ||
|
||
TimestampedObject<T>[] ReadQueue(); | ||
|
||
T[] ReadQueueValues(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Google.Protobuf; | ||
using NetworkTables.Handles; | ||
using NetworkTables.Natives; | ||
using WPIUtil.Serialization.Protobuf; | ||
|
||
namespace NetworkTables; | ||
|
||
public sealed class ProtobufTopic<T> : Topic where T : IProtobufSerializable<T> | ||
{ | ||
private IProtobufBase m_proto = T.Proto; | ||
|
||
private ProtobufTopic(Topic topic) : base(topic.Instance, topic.Handle) | ||
{ | ||
} | ||
|
||
private ProtobufTopic(NetworkTableInstance inst, NtTopic handle) : base(inst, handle) | ||
{ | ||
} | ||
|
||
public IProtobufSubscriber<T> Subscribe(T defaultValue, PubSubOptions options) | ||
{ | ||
return new ProtobufEntryImpl<T, NtSubscriber>(this, NtCore.Subscribe(Handle, NetworkTableType.Raw, m_proto.TypeString, options), defaultValue, false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.