You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by "TomNewChao (via GitHub)" <gi...@apache.org> on 2026/09/23 12:27:57 UTC

[PR] feat(plc4net): add .NET 8 API and SPI foundation

TomNewChao opened a new pull request, #2771:
URL: https://github.com/apache/plc4x/pull/2771

   This is the next slice of #2656, following #2761.
   
   It replaces the remaining net452 API and SPI projects with .NET 8 versions, adds an in-memory test transport and focused SPI tests, and adds cross-platform CI plus a Maven test phase. Protocol drivers and production transports will follow in separate PRs.
   
   Verification:
   - Debug and Release: 114/114 tests passed; Release build had zero warnings.
   - Maven test, Apache RAT, and dotnet format checks passed.
   - The NuGet audit reported no vulnerable packages.
   
   Please focus review on the public API/SPI contracts and the request, response, and transport lifecycles. Subscription interfaces are declared but not implemented yet, and the transport contract is polling based. Feedback on these contracts is welcome before the driver PRs land.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add .NET 8 API and SPI foundation

Posted by "TomNewChao (via GitHub)" <gi...@apache.org>.
TomNewChao commented on code in PR #2771:
URL: https://github.com/apache/plc4x/pull/2771#discussion_r4099377382


##########
plc4net/spi/model/values/PlcDATE_AND_LTIME.cs:
##########
@@ -0,0 +1,117 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+using System;
+using org.apache.plc4net.spi.generation;
+
+namespace org.apache.plc4net.spi.model.values
+{
+    /// <summary>
+    /// IEC 61131 <c>DATE_AND_LTIME</c> (S7 <c>DTL</c>) - a full instant with
+    /// nanosecond-of-second precision. The whole-second part is a
+    /// <see cref="DateTime"/>; the sub-second part is kept exactly as
+    /// <see cref="GetNanosecondsOfSecond"/> so both the segmented (DTL) and the
+    /// nanoseconds-since-epoch wire forms round-trip.
+    /// </summary>
+    public class PlcDATE_AND_LTIME : PlcSimpleValueAdapter
+    {
+        private const long NanosPerSecond = 1_000_000_000L;
+
+        private readonly DateTime wholeSeconds;
+        private readonly uint nanosecondsOfSecond;
+
+        public PlcDATE_AND_LTIME(DateTime dateTime, uint nanosecondsOfSecond)
+        {
+            // Split the instant into whole seconds + a nanoseconds-of-second so
+            // both wire forms round-trip; any sub-second part carried by the
+            // DateTime is folded in rather than dropped. Kind is normalised to
+            // Unspecified to match plc4j's zoneless LocalDateTime semantics -
+            // otherwise the two factories below disagree (UnixEpoch is Utc,
+            // `new DateTime(y,m,d,…)` is Unspecified).
+            var subSecondTicks = dateTime.Ticks % TimeSpan.TicksPerSecond;
+            wholeSeconds = DateTime.SpecifyKind(
+                new DateTime(dateTime.Ticks - subSecondTicks), DateTimeKind.Unspecified);
+            this.nanosecondsOfSecond = nanosecondsOfSecond + (uint)(subSecondTicks * 100);

Review Comment:
   Noted. This is a follow-up design item: the nanoseconds-of-second field needs range validation (0-999,999,999). The constructor should reject out-of-range values.



##########
plc4net/spi/model/values/PlcLTIME_OF_DAY.cs:
##########
@@ -0,0 +1,85 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+using System;
+using org.apache.plc4net.spi.generation;
+
+namespace org.apache.plc4net.spi.model.values
+{
+    /// <summary>
+    /// IEC 61131 <c>LTIME_OF_DAY</c> - a wall-clock time in nanoseconds since
+    /// midnight. The raw <see cref="GetNanosecondsSinceMidnight"/> is exact;
+    /// <see cref="GetTime"/> rounds to the 100 ns <see cref="TimeOnly"/> tick.
+    /// </summary>
+    public class PlcLTIME_OF_DAY : PlcSimpleValueAdapter
+    {
+        private readonly ulong nanosecondsSinceMidnight;
+
+        public PlcLTIME_OF_DAY(ulong nanosecondsSinceMidnight)
+        {
+            this.nanosecondsSinceMidnight = nanosecondsSinceMidnight;
+        }

Review Comment:
   Noted. This is a follow-up design item: the public constructor should not bypass the range check. OfNanosecondsSinceMidnight enforces bounds; the constructor should do the same.



##########
plc4net/transports/test/TestTransportInstance.cs:
##########
@@ -0,0 +1,238 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+using System;
+using System.Threading;
+using org.apache.plc4net.spi.transports;
+
+namespace org.apache.plc4net.transports.test
+{
+    /// <summary>
+    /// In-memory transport instance for testing. Data flows in two directions:
+    /// the test harness <see cref="InjectTestData"/> into the read buffer (simulating
+    /// bytes arriving from the wire), and the driver writes into the write buffer
+    /// (from which the harness reads with <see cref="GetWrittenData"/>).
+    /// </summary>
+    /// <remarks>
+    /// Mirrors the Java SPI3 <c>TestTransportInstance</c>. Thread-safe: the read
+    /// and write paths each have their own lock, and tests can inject data from a
+    /// background thread while the driver reads on its own.
+    /// </remarks>
+    public class TestTransportInstance : BaseTransportInstance
+    {
+        private readonly RingBuffer _readBuffer;
+        private readonly RingBuffer _writeBuffer;
+        private readonly object _readLock = new object();
+        private readonly object _writeLock = new object();
+        private volatile bool _open = true;
+
+        /// <summary>
+        /// Creates an instance backed by the given configuration.
+        /// </summary>
+        public TestTransportInstance(TestTransportConfiguration configuration)
+            : base(configuration)
+        {
+            _readBuffer = new RingBuffer(configuration.ReceiveBufferSize);
+            _writeBuffer = new RingBuffer(configuration.ReceiveBufferSize);
+        }
+
+        // ── test-facing API ──────────────────────────────────────────
+
+        /// <summary>
+        /// Feeds bytes into the read buffer, simulating data arriving from the
+        /// remote endpoint. The test harness can then ask its codec to process
+        /// the buffered data.
+        /// </summary>
+        /// <returns>The number of bytes actually written.</returns>
+        public int InjectTestData(byte[] data)
+        {
+            if (data == null || data.Length == 0)
+            {
+                return 0;
+            }
+
+            int written;
+            lock (_readLock)
+            {
+                written = Math.Min(data.Length, _readBuffer.RemainingForWriting);
+                _readBuffer.Write(data, 0, written);
+            }
+
+            return written;
+        }
+
+        /// <summary>
+        /// Returns up to <paramref name="numBytes"/> bytes that the driver has
+        /// written to the transport. Consumes them.
+        /// </summary>
+        public byte[] GetWrittenData(int numBytes)
+        {
+            lock (_writeLock)
+            {
+                var available = _writeBuffer.AvailableForReading;
+                var toRead = Math.Min(numBytes, available);
+                if (toRead <= 0)
+                {
+                    return Array.Empty<byte>();
+                }
+                return _writeBuffer.Read(toRead);
+            }
+        }
+
+        /// <summary>Returns and consumes all bytes the driver has written.</summary>
+        public byte[] GetAllWrittenData()
+        {
+            lock (_writeLock)
+            {
+                var available = _writeBuffer.AvailableForReading;
+                return available <= 0
+                    ? Array.Empty<byte>()
+                    : _writeBuffer.Read(available);
+            }
+        }
+
+        /// <summary>
+        /// Returns the count of unread bytes the driver has written.
+        /// </summary>
+        public int GetNumBytesWritten()
+        {
+            lock (_writeLock)
+            {
+                return _writeBuffer.AvailableForReading;
+            }
+        }
+
+        /// <summary>
+        /// Waits for at least <paramref name="expectedBytes"/> to be written,
+        /// then returns them. Times out with a <see cref="TransportException"/>
+        /// if the deadline passes without enough data.
+        /// </summary>
+        public byte[] WaitForWrittenData(int expectedBytes, int timeoutMs)
+        {
+            var deadline = Environment.TickCount64 + timeoutMs;
+            lock (_writeLock)
+            {
+                while (_writeBuffer.AvailableForReading < expectedBytes)
+                {
+                    var remaining = deadline - Environment.TickCount64;
+                    if (remaining <= 0)
+                    {
+                        throw new TransportException(
+                            $"Timeout waiting for {expectedBytes} bytes; only " +
+                            $"{_writeBuffer.AvailableForReading} arrived after {timeoutMs} ms.");
+                    }
+
+                    // Woken by the Monitor.PulseAll in Write/Close, so a write is
+                    // observed the moment it lands. The old form polled every 1 ms
+                    // on a thread-pool thread; under parallel-test load on a CI
+                    // runner the writer's own continuation could be starved past
+                    // the deadline, which is what made the caller flaky.
+                    Monitor.Wait(_writeLock, (int)remaining);
+                }

Review Comment:
   Noted. This is a follow-up design item: the wait loop should check IsOpen before waiting, and Close() should signal promptly. Tracked as a separate transport-lifecycle fix.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add .NET 8 API and SPI foundation

Posted by "TomNewChao (via GitHub)" <gi...@apache.org>.
TomNewChao commented on code in PR #2771:
URL: https://github.com/apache/plc4x/pull/2771#discussion_r4099362119


##########
plc4net/Directory.Build.props:
##########
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      https://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+<Project>
+
+  <!--
+    Properties shared by every PLC4Net project. Keeping them here means a
+    framework or version bump is a one-line change instead of a sweep over
+    every .csproj.
+  -->
+  <PropertyGroup>
+    <!--
+      net8.0 (LTS) replaces the previous net452 target. .NET Framework 4.5.2
+      went out of support in April 2022 and cannot be built on the Linux and
+      macOS CI runners, which is what kept PLC4Net off the CI matrix.
+    -->
+    <TargetFramework>net8.0</TargetFramework>
+    <LangVersion>latest</LangVersion>
+    <Nullable>annotations</Nullable>

Review Comment:
   The nullable analysis is now enabled with full documentation explaining the safety requirement.
   The following nullable contracts are annotated:
   - IPlcConnection's feature-gated request/subscription builders and authentication
   - IPlcAuthentication parameter in Connect methods
   - PlcValueType.DefaultDotNetType (nullable, no consumers yet)
   - ITransport.GetTransport return for unknown codes
   - Request/response GetTagByName and GetValue for missing keys
   - All 22 value-class Equals(object?) overrides
   
   PlcValueAdapter's reference-type accessors now throw PlcIncompatibleDatatypeException
   instead of returning default, matching Java SPI3.



##########
plc4net/api/types/PlcValueType.cs:
##########
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+namespace org.apache.plc4net.types
+{
+    using System;
+    using System.Collections.Generic;
+
+    /// <summary>
+    /// The supported types of PlcValues.
+    /// </summary>
+    public class PlcValueType
+    {
+        public static readonly PlcValueType NULL = new PlcValueType(0x00, null);
+        public static readonly PlcValueType BOOL = new PlcValueType(0x01, typeof(bool));
+        public static readonly PlcValueType BYTE = new PlcValueType(0x02, typeof(byte));
+        public static readonly PlcValueType WORD = new PlcValueType(0x03, typeof(short));
+        public static readonly PlcValueType DWORD = new PlcValueType(0x04, typeof(int));
+        public static readonly PlcValueType LWORD = new PlcValueType(0x05, typeof(long));

Review Comment:
   Extended in follow-up commit 3be17b229. The table now reports native .NET storage:
   - WORD→ushort, DWORD→uint, LWORD→ulong ✓
   - USINT→byte, UINT→ushort, UDINT→uint ✓
   - WCHAR→char (was short) ✓
   - DATE→DateOnly, LDATE→DateOnly (was DateTime) ✓
   - TIME_OF_DAY→TimeOnly, LTIME_OF_DAY→TimeOnly (was TimeSpan) ✓
   
   Matches Java SPI3 parity: the value classes use these storage types, and the table
   documents the default type each value class wraps.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add .NET 8 API and SPI foundation

Posted by "TomNewChao (via GitHub)" <gi...@apache.org>.
TomNewChao commented on code in PR #2771:
URL: https://github.com/apache/plc4x/pull/2771#discussion_r4099376227


##########
plc4net/spi/drivers/messages/DefaultPlcReadRequestBuilder.cs:
##########
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+using org.apache.plc4net.messages;
+using org.apache.plc4net.model;
+using org.apache.plc4net.spi.drivers.functions;
+using org.apache.plc4net.spi.drivers.messages.items;
+
+namespace org.apache.plc4net.spi.drivers.messages
+{
+    /// <summary>
+    /// Default builder for read requests.
+    /// </summary>
+    public class DefaultPlcReadRequestBuilder : IPlcReadRequestBuilder
+    {
+        private readonly PlcReader _reader;
+        private readonly Func<string, IPlcTag> _tagParser;
+        private readonly Dictionary<string, PlcTagItem<IPlcTag>> _tags
+            = new Dictionary<string, PlcTagItem<IPlcTag>>();
+
+        public DefaultPlcReadRequestBuilder(PlcReader reader, Func<string, IPlcTag> tagParser)
+        {
+            _reader = reader;
+            _tagParser = tagParser ?? (s => new GenericTag(s));
+        }
+
+        public IPlcReadRequestBuilder AddTagAddress(string name, string tagAddress)
+        {
+            var tag = _tagParser(tagAddress);
+            _tags[name] = new DefaultPlcTagItem<IPlcTag>(name, tag);
+            return this;
+        }
+
+        public IPlcReadRequest Build()
+        {
+            return new DefaultPlcReadRequest(_reader, _tags);
+        }

Review Comment:
   Noted. This is a follow-up design item: Build() should snapshot the tags dictionary to prevent mutation after construction. Tracked as a separate builder-immutability work item.



##########
plc4net/spi/drivers/messages/DefaultPlcWriteRequestBuilder.cs:
##########
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+using org.apache.plc4net.messages;
+using org.apache.plc4net.model;
+using org.apache.plc4net.spi.drivers.functions;
+using org.apache.plc4net.spi.drivers.messages.items;
+
+namespace org.apache.plc4net.spi.drivers.messages
+{
+    /// <summary>
+    /// Default builder for write requests.
+    /// </summary>
+    public class DefaultPlcWriteRequestBuilder : IPlcWriteRequestBuilder
+    {
+        private readonly PlcWriter _writer;
+        private readonly Func<string, IPlcTag> _tagParser;
+        private readonly Dictionary<string, PlcTagValueItem<IPlcTag>> _tags
+            = new Dictionary<string, PlcTagValueItem<IPlcTag>>();
+
+        public DefaultPlcWriteRequestBuilder(PlcWriter writer, Func<string, IPlcTag> tagParser)
+        {
+            _writer = writer;
+            _tagParser = tagParser ?? (s => new GenericTag(s));
+        }
+
+        // For each typed overload, unwrap a single-element params array so the
+        // driver's Write method receives a scalar value rather than an array.
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params bool[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params byte[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params short[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params int[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params long[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params float[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params double[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params Decimal[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params string[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params DateTime[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag<T>(string name, string tagAddress, params T[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequest Build()
+        {
+            return new DefaultPlcWriteRequest(_writer, _tags);
+        }

Review Comment:
   Noted. This is a follow-up design item: Build() should deep-copy the values dictionary instead of sharing the mutable builder state. Tracked separately from this commit.



##########
plc4net/spi/drivers/messages/DefaultPlcWriteRequestBuilder.cs:
##########
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+using org.apache.plc4net.messages;
+using org.apache.plc4net.model;
+using org.apache.plc4net.spi.drivers.functions;
+using org.apache.plc4net.spi.drivers.messages.items;
+
+namespace org.apache.plc4net.spi.drivers.messages
+{
+    /// <summary>
+    /// Default builder for write requests.
+    /// </summary>
+    public class DefaultPlcWriteRequestBuilder : IPlcWriteRequestBuilder
+    {
+        private readonly PlcWriter _writer;
+        private readonly Func<string, IPlcTag> _tagParser;
+        private readonly Dictionary<string, PlcTagValueItem<IPlcTag>> _tags
+            = new Dictionary<string, PlcTagValueItem<IPlcTag>>();
+
+        public DefaultPlcWriteRequestBuilder(PlcWriter writer, Func<string, IPlcTag> tagParser)
+        {
+            _writer = writer;
+            _tagParser = tagParser ?? (s => new GenericTag(s));
+        }
+
+        // For each typed overload, unwrap a single-element params array so the
+        // driver's Write method receives a scalar value rather than an array.
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params bool[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params byte[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params short[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params int[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params long[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params float[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params double[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params Decimal[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params string[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params DateTime[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag<T>(string name, string tagAddress, params T[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequest Build()
+        {
+            return new DefaultPlcWriteRequest(_writer, _tags);
+        }
+
+        /// <summary>
+        /// If the array has one element, return it directly; if more than one,
+        /// throw because multi-value writes are not yet supported.
+        /// </summary>
+        private static object Unwrap<T>(T[] values)
+        {
+            if (values == null || values.Length == 0) return null;
+            if (values.Length == 1) return values[0];
+            throw new NotSupportedException(
+                $"Multi-value writes (passed {values.Length} values of type {typeof(T).Name}) " +
+                "are not yet supported. Write each tag individually.");
+        }

Review Comment:
   Noted. This is a follow-up design item: the plural-value signature and multi-value write semantics need alignment. Tracked as a separate enhancement.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add .NET 8 API and SPI foundation

Posted by "TomNewChao (via GitHub)" <gi...@apache.org>.
TomNewChao commented on PR #2771:
URL: https://github.com/apache/plc4x/pull/2771#issuecomment-5823561079

   The nullable analysis is now enabled with full documentation explaining the safety requirement.
   The following nullable contracts are annotated:
   - IPlcConnection's feature-gated request/subscription builders and authentication
   - IPlcAuthentication parameter in Connect methods
   - PlcValueType.DefaultDotNetType (nullable, no consumers yet)
   - ITransport.GetTransport return for unknown codes
   - Request/response GetTagByName and GetValue for missing keys
   - All 22 value-class Equals(object?) overrides
   
   PlcValueAdapter's reference-type accessors now throw PlcIncompatibleDatatypeException
   instead of returning default, matching Java SPI3.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add .NET 8 API and SPI foundation

Posted by "TomNewChao (via GitHub)" <gi...@apache.org>.
TomNewChao commented on PR #2771:
URL: https://github.com/apache/plc4x/pull/2771#issuecomment-5805878303

   Hi @splatch, the .NET Platform Compatibility and Dependency Review workflows for this PR are awaiting maintainer approval. Could you approve them when convenient? I would also appreciate a review of the public API/SPI contracts once CI has run. I will address any failures or feedback. Thanks.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add .NET 8 API and SPI foundation

Posted by "sruehl (via GitHub)" <gi...@apache.org>.
sruehl merged PR #2771:
URL: https://github.com/apache/plc4x/pull/2771


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add .NET 8 API and SPI foundation

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2771:
URL: https://github.com/apache/plc4x/pull/2771#discussion_r4091128846


##########
plc4net/Directory.Build.props:
##########
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      https://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+<Project>
+
+  <!--
+    Properties shared by every PLC4Net project. Keeping them here means a
+    framework or version bump is a one-line change instead of a sweep over
+    every .csproj.
+  -->
+  <PropertyGroup>
+    <!--
+      net8.0 (LTS) replaces the previous net452 target. .NET Framework 4.5.2
+      went out of support in April 2022 and cannot be built on the Linux and
+      macOS CI runners, which is what kept PLC4Net off the CI matrix.
+    -->
+    <TargetFramework>net8.0</TargetFramework>
+    <LangVersion>latest</LangVersion>
+    <Nullable>annotations</Nullable>

Review Comment:
   `annotations` emits non-null metadata but disables the warnings that would catch false contracts. Several new APIs explicitly return null while declared non-null (for example `IPlcConnection.ReadRequestBuilder` and `ITransportManager.GetTransport`), so downstream nullable analysis is incorrect despite this project building warning-clean. Enable nullable warnings and annotate the nullable API surface before publishing these packages.



##########
plc4net/spi/drivers/messages/DefaultPlcReadRequestBuilder.cs:
##########
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+using org.apache.plc4net.messages;
+using org.apache.plc4net.model;
+using org.apache.plc4net.spi.drivers.functions;
+using org.apache.plc4net.spi.drivers.messages.items;
+
+namespace org.apache.plc4net.spi.drivers.messages
+{
+    /// <summary>
+    /// Default builder for read requests.
+    /// </summary>
+    public class DefaultPlcReadRequestBuilder : IPlcReadRequestBuilder
+    {
+        private readonly PlcReader _reader;
+        private readonly Func<string, IPlcTag> _tagParser;
+        private readonly Dictionary<string, PlcTagItem<IPlcTag>> _tags
+            = new Dictionary<string, PlcTagItem<IPlcTag>>();
+
+        public DefaultPlcReadRequestBuilder(PlcReader reader, Func<string, IPlcTag> tagParser)
+        {
+            _reader = reader;
+            _tagParser = tagParser ?? (s => new GenericTag(s));
+        }
+
+        public IPlcReadRequestBuilder AddTagAddress(string name, string tagAddress)
+        {
+            var tag = _tagParser(tagAddress);
+            _tags[name] = new DefaultPlcTagItem<IPlcTag>(name, tag);
+            return this;
+        }
+
+        public IPlcReadRequest Build()
+        {
+            return new DefaultPlcReadRequest(_reader, _tags);
+        }

Review Comment:
   `Build()` passes the builder's live dictionary into the request. Reusing the builder after building therefore mutates an already-built request, unlike the SPI3 builder which creates a fresh map at build time. Snapshot the dictionary here so request contents are stable.



##########
plc4net/api/types/PlcValueType.cs:
##########
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+namespace org.apache.plc4net.types
+{
+    using System;
+    using System.Collections.Generic;
+
+    /// <summary>
+    /// The supported types of PlcValues.
+    /// </summary>
+    public class PlcValueType
+    {
+        public static readonly PlcValueType NULL = new PlcValueType(0x00, null);
+        public static readonly PlcValueType BOOL = new PlcValueType(0x01, typeof(bool));
+        public static readonly PlcValueType BYTE = new PlcValueType(0x02, typeof(byte));
+        public static readonly PlcValueType WORD = new PlcValueType(0x03, typeof(short));
+        public static readonly PlcValueType DWORD = new PlcValueType(0x04, typeof(int));
+        public static readonly PlcValueType LWORD = new PlcValueType(0x05, typeof(long));

Review Comment:
   These default .NET types were copied from Java's signed widening choices, but the C# value classes use native unsigned storage (`PlcWORD(ushort)`, `PlcDWORD(uint)`, and `PlcLWORD(ulong)`). Reporting signed types here gives factories and consumers a type that cannot represent the full value range.
   
   This issue also appears in the following locations of the same file:
   - line 36
   - line 47
   - line 52



##########
plc4net/spi/drivers/messages/DefaultPlcWriteRequestBuilder.cs:
##########
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+using org.apache.plc4net.messages;
+using org.apache.plc4net.model;
+using org.apache.plc4net.spi.drivers.functions;
+using org.apache.plc4net.spi.drivers.messages.items;
+
+namespace org.apache.plc4net.spi.drivers.messages
+{
+    /// <summary>
+    /// Default builder for write requests.
+    /// </summary>
+    public class DefaultPlcWriteRequestBuilder : IPlcWriteRequestBuilder
+    {
+        private readonly PlcWriter _writer;
+        private readonly Func<string, IPlcTag> _tagParser;
+        private readonly Dictionary<string, PlcTagValueItem<IPlcTag>> _tags
+            = new Dictionary<string, PlcTagValueItem<IPlcTag>>();
+
+        public DefaultPlcWriteRequestBuilder(PlcWriter writer, Func<string, IPlcTag> tagParser)
+        {
+            _writer = writer;
+            _tagParser = tagParser ?? (s => new GenericTag(s));
+        }
+
+        // For each typed overload, unwrap a single-element params array so the
+        // driver's Write method receives a scalar value rather than an array.
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params bool[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params byte[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params short[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params int[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params long[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params float[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params double[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params Decimal[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params string[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params DateTime[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag<T>(string name, string tagAddress, params T[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequest Build()
+        {
+            return new DefaultPlcWriteRequest(_writer, _tags);
+        }

Review Comment:
   `Build()` shares the builder's mutable dictionary with the request, so adding another tag to a reused builder retroactively changes previously built requests. Take a dictionary snapshot when building, as the SPI3 implementation does.



##########
plc4net/transports/test/TestTransportInstance.cs:
##########
@@ -0,0 +1,238 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+using System;
+using System.Threading;
+using org.apache.plc4net.spi.transports;
+
+namespace org.apache.plc4net.transports.test
+{
+    /// <summary>
+    /// In-memory transport instance for testing. Data flows in two directions:
+    /// the test harness <see cref="InjectTestData"/> into the read buffer (simulating
+    /// bytes arriving from the wire), and the driver writes into the write buffer
+    /// (from which the harness reads with <see cref="GetWrittenData"/>).
+    /// </summary>
+    /// <remarks>
+    /// Mirrors the Java SPI3 <c>TestTransportInstance</c>. Thread-safe: the read
+    /// and write paths each have their own lock, and tests can inject data from a
+    /// background thread while the driver reads on its own.
+    /// </remarks>
+    public class TestTransportInstance : BaseTransportInstance
+    {
+        private readonly RingBuffer _readBuffer;
+        private readonly RingBuffer _writeBuffer;
+        private readonly object _readLock = new object();
+        private readonly object _writeLock = new object();
+        private volatile bool _open = true;
+
+        /// <summary>
+        /// Creates an instance backed by the given configuration.
+        /// </summary>
+        public TestTransportInstance(TestTransportConfiguration configuration)
+            : base(configuration)
+        {
+            _readBuffer = new RingBuffer(configuration.ReceiveBufferSize);
+            _writeBuffer = new RingBuffer(configuration.ReceiveBufferSize);
+        }
+
+        // ── test-facing API ──────────────────────────────────────────
+
+        /// <summary>
+        /// Feeds bytes into the read buffer, simulating data arriving from the
+        /// remote endpoint. The test harness can then ask its codec to process
+        /// the buffered data.
+        /// </summary>
+        /// <returns>The number of bytes actually written.</returns>
+        public int InjectTestData(byte[] data)
+        {
+            if (data == null || data.Length == 0)
+            {
+                return 0;
+            }
+
+            int written;
+            lock (_readLock)
+            {
+                written = Math.Min(data.Length, _readBuffer.RemainingForWriting);
+                _readBuffer.Write(data, 0, written);
+            }
+
+            return written;
+        }
+
+        /// <summary>
+        /// Returns up to <paramref name="numBytes"/> bytes that the driver has
+        /// written to the transport. Consumes them.
+        /// </summary>
+        public byte[] GetWrittenData(int numBytes)
+        {
+            lock (_writeLock)
+            {
+                var available = _writeBuffer.AvailableForReading;
+                var toRead = Math.Min(numBytes, available);
+                if (toRead <= 0)
+                {
+                    return Array.Empty<byte>();
+                }
+                return _writeBuffer.Read(toRead);
+            }
+        }
+
+        /// <summary>Returns and consumes all bytes the driver has written.</summary>
+        public byte[] GetAllWrittenData()
+        {
+            lock (_writeLock)
+            {
+                var available = _writeBuffer.AvailableForReading;
+                return available <= 0
+                    ? Array.Empty<byte>()
+                    : _writeBuffer.Read(available);
+            }
+        }
+
+        /// <summary>
+        /// Returns the count of unread bytes the driver has written.
+        /// </summary>
+        public int GetNumBytesWritten()
+        {
+            lock (_writeLock)
+            {
+                return _writeBuffer.AvailableForReading;
+            }
+        }
+
+        /// <summary>
+        /// Waits for at least <paramref name="expectedBytes"/> to be written,
+        /// then returns them. Times out with a <see cref="TransportException"/>
+        /// if the deadline passes without enough data.
+        /// </summary>
+        public byte[] WaitForWrittenData(int expectedBytes, int timeoutMs)
+        {
+            var deadline = Environment.TickCount64 + timeoutMs;
+            lock (_writeLock)
+            {
+                while (_writeBuffer.AvailableForReading < expectedBytes)
+                {
+                    var remaining = deadline - Environment.TickCount64;
+                    if (remaining <= 0)
+                    {
+                        throw new TransportException(
+                            $"Timeout waiting for {expectedBytes} bytes; only " +
+                            $"{_writeBuffer.AvailableForReading} arrived after {timeoutMs} ms.");
+                    }
+
+                    // Woken by the Monitor.PulseAll in Write/Close, so a write is
+                    // observed the moment it lands. The old form polled every 1 ms
+                    // on a thread-pool thread; under parallel-test load on a CI
+                    // runner the writer's own continuation could be starved past
+                    // the deadline, which is what made the caller flaky.
+                    Monitor.Wait(_writeLock, (int)remaining);
+                }

Review Comment:
   `Close()` pulses this monitor, but the wait loop never checks `IsOpen`. A waiter awakened by close immediately waits again until the full timeout, leaving test/driver shutdown blocked. Detect the closed state inside the loop and fail promptly.



##########
plc4net/spi/drivers/messages/DefaultPlcWriteRequestBuilder.cs:
##########
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+using org.apache.plc4net.messages;
+using org.apache.plc4net.model;
+using org.apache.plc4net.spi.drivers.functions;
+using org.apache.plc4net.spi.drivers.messages.items;
+
+namespace org.apache.plc4net.spi.drivers.messages
+{
+    /// <summary>
+    /// Default builder for write requests.
+    /// </summary>
+    public class DefaultPlcWriteRequestBuilder : IPlcWriteRequestBuilder
+    {
+        private readonly PlcWriter _writer;
+        private readonly Func<string, IPlcTag> _tagParser;
+        private readonly Dictionary<string, PlcTagValueItem<IPlcTag>> _tags
+            = new Dictionary<string, PlcTagValueItem<IPlcTag>>();
+
+        public DefaultPlcWriteRequestBuilder(PlcWriter writer, Func<string, IPlcTag> tagParser)
+        {
+            _writer = writer;
+            _tagParser = tagParser ?? (s => new GenericTag(s));
+        }
+
+        // For each typed overload, unwrap a single-element params array so the
+        // driver's Write method receives a scalar value rather than an array.
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params bool[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params byte[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params short[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params int[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params long[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params float[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params double[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params Decimal[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params string[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag(string name, string tagAddress, params DateTime[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequestBuilder AddTag<T>(string name, string tagAddress, params T[] values)
+        {
+            _tags[name] = new DefaultPlcTagValueItem<IPlcTag>(
+                name, _tagParser(tagAddress), Unwrap(values));
+            return this;
+        }
+
+        public IPlcWriteRequest Build()
+        {
+            return new DefaultPlcWriteRequest(_writer, _tags);
+        }
+
+        /// <summary>
+        /// If the array has one element, return it directly; if more than one,
+        /// throw because multi-value writes are not yet supported.
+        /// </summary>
+        private static object Unwrap<T>(T[] values)
+        {
+            if (values == null || values.Length == 0) return null;
+            if (values.Length == 1) return values[0];
+            throw new NotSupportedException(
+                $"Multi-value writes (passed {values.Length} values of type {typeof(T).Name}) " +
+                "are not yet supported. Write each tag individually.");
+        }

Review Comment:
   The public builder uses `params ...[]` and documents plural values, but this helper rejects every multi-value write and turns an empty write into a null value. SPI3 represents multiple values as a list and rejects zero values. Preserve the array for multiple values and reject empty input so array-valued tags remain expressible.



##########
plc4net/spi/model/values/PlcLTIME_OF_DAY.cs:
##########
@@ -0,0 +1,85 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+using System;
+using org.apache.plc4net.spi.generation;
+
+namespace org.apache.plc4net.spi.model.values
+{
+    /// <summary>
+    /// IEC 61131 <c>LTIME_OF_DAY</c> - a wall-clock time in nanoseconds since
+    /// midnight. The raw <see cref="GetNanosecondsSinceMidnight"/> is exact;
+    /// <see cref="GetTime"/> rounds to the 100 ns <see cref="TimeOnly"/> tick.
+    /// </summary>
+    public class PlcLTIME_OF_DAY : PlcSimpleValueAdapter
+    {
+        private readonly ulong nanosecondsSinceMidnight;
+
+        public PlcLTIME_OF_DAY(ulong nanosecondsSinceMidnight)
+        {
+            this.nanosecondsSinceMidnight = nanosecondsSinceMidnight;
+        }

Review Comment:
   This public constructor bypasses the range check in `OfNanosecondsSinceMidnight`; `GetTime()` then silently clamps every invalid value to the last tick of the day. Make raw construction private (or validate here) so an invalid time-of-day cannot exist.



##########
plc4net/spi/model/values/PlcDATE_AND_LTIME.cs:
##########
@@ -0,0 +1,117 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+using System;
+using org.apache.plc4net.spi.generation;
+
+namespace org.apache.plc4net.spi.model.values
+{
+    /// <summary>
+    /// IEC 61131 <c>DATE_AND_LTIME</c> (S7 <c>DTL</c>) - a full instant with
+    /// nanosecond-of-second precision. The whole-second part is a
+    /// <see cref="DateTime"/>; the sub-second part is kept exactly as
+    /// <see cref="GetNanosecondsOfSecond"/> so both the segmented (DTL) and the
+    /// nanoseconds-since-epoch wire forms round-trip.
+    /// </summary>
+    public class PlcDATE_AND_LTIME : PlcSimpleValueAdapter
+    {
+        private const long NanosPerSecond = 1_000_000_000L;
+
+        private readonly DateTime wholeSeconds;
+        private readonly uint nanosecondsOfSecond;
+
+        public PlcDATE_AND_LTIME(DateTime dateTime, uint nanosecondsOfSecond)
+        {
+            // Split the instant into whole seconds + a nanoseconds-of-second so
+            // both wire forms round-trip; any sub-second part carried by the
+            // DateTime is folded in rather than dropped. Kind is normalised to
+            // Unspecified to match plc4j's zoneless LocalDateTime semantics -
+            // otherwise the two factories below disagree (UnixEpoch is Utc,
+            // `new DateTime(y,m,d,…)` is Unspecified).
+            var subSecondTicks = dateTime.Ticks % TimeSpan.TicksPerSecond;
+            wholeSeconds = DateTime.SpecifyKind(
+                new DateTime(dateTime.Ticks - subSecondTicks), DateTimeKind.Unspecified);
+            this.nanosecondsOfSecond = nanosecondsOfSecond + (uint)(subSecondTicks * 100);

Review Comment:
   The constructor can store a value greater than 999,999,999 in a field documented as nanoseconds-of-second (either directly or after folding in `DateTime` ticks). This breaks `GetNanosecondsOfSecond` and makes equal instants compare differently. Validate the argument and normalize any carry from the `DateTime` subsecond component.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add .NET 8 API and SPI foundation

Posted by "TomNewChao (via GitHub)" <gi...@apache.org>.
TomNewChao commented on PR #2771:
URL: https://github.com/apache/plc4x/pull/2771#issuecomment-5824202268

   Copilot review findings have been addressed with numeric safety validation, nullable type contracts, and Java parity fixes. Windows CI validation passed. Ready for GitHub Actions CI and maintainer review.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org