Copilot commented on code in PR #2656:
URL: https://github.com/apache/plc4x/pull/2656#discussion_r3655094212


##########
plc4net/transports/tcp/TcpTransport.cs:
##########
@@ -0,0 +1,205 @@
+//
+// 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 System.Globalization;
+using System.Linq;
+using System.Net;
+using org.apache.plc4net.spi.transports;
+
+namespace org.apache.plc4net.transports.tcp
+{
+    /// <summary>
+    /// The "tcp" transport.
+    /// </summary>
+    public class TcpTransport : ITransport
+    {
+        public string TransportCode => "tcp";
+
+        public string TransportName => "TCP/IP Socket Transport";
+
+        public ITransportConfiguration 
CreateConfiguration(IReadOnlyDictionary<string, string> parameters)
+        {
+            var configuration = new TcpTransportConfiguration();
+            if (parameters == null)
+            {
+                return configuration;
+            }
+
+            // Transport options are prefixed with the transport code in the 
connection
+            // string ("tcp.connect-timeout=..."), matching the Java side. The 
unprefixed
+            // form is accepted too, since drivers with a single transport 
commonly use it.
+            configuration.ConnectTimeout = GetInt(parameters, 
"connect-timeout", configuration.ConnectTimeout);
+            configuration.TcpNoDelay = GetBool(parameters, "tcp-no-delay", 
configuration.TcpNoDelay);
+            configuration.KeepAlive = GetBool(parameters, "keep-alive", 
configuration.KeepAlive);
+            configuration.SendBufferSize = GetInt(parameters, 
"send-buffer-size", configuration.SendBufferSize);
+            configuration.ReceiveBufferSize = GetInt(parameters, 
"receive-buffer-size", configuration.ReceiveBufferSize);
+            configuration.LocalAddress = GetString(parameters, 
"local-address", configuration.LocalAddress);
+            configuration.LocalPort = GetInt(parameters, "local-port", 
configuration.LocalPort);
+            configuration.DefaultPort = GetInt(parameters, "default-port", 
configuration.DefaultPort);

Review Comment:
   TcpNoDelay option lookup will likely never find the intended prefixed key. 
GetString() already prefixes with "tcp." (e.g. "tcp.connect-timeout"), but 
CreateConfiguration passes "tcp-no-delay" which becomes "tcp.tcp-no-delay" when 
prefixed. This contradicts the comment and makes it hard/impossible to set via 
connection-string parameters.



##########
plc4net/spi/spi/transports/RingBuffer.cs:
##########
@@ -0,0 +1,136 @@
+//
+// 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;
+
+namespace org.apache.plc4net.spi.transports
+{
+    /// <summary>
+    /// Fixed-capacity byte ring buffer sitting between a transport's read 
loop and the
+    /// protocol codec that drains it.
+    /// </summary>
+    /// <remarks>
+    /// The codec needs to inspect a message header before it knows how long 
the message
+    /// is, so this supports <see cref="Peek"/> (look without consuming) 
alongside
+    /// <see cref="Read"/>. Callers are expected to hold the transport's read 
lock; this
+    /// type is not internally synchronised.
+    /// </remarks>
+    public class RingBuffer
+    {
+        private readonly byte[] _buffer;
+        private int _readPosition;
+        private int _writePosition;
+        private int _count;
+
+        public RingBuffer(int capacity)
+        {
+            if (capacity <= 0)
+            {
+                throw new ArgumentOutOfRangeException(nameof(capacity), 
capacity, "Capacity must be positive.");
+            }
+            _buffer = new byte[capacity];
+        }
+
+        public int Capacity => _buffer.Length;
+
+        public int AvailableForReading => _count;
+
+        public int RemainingForWriting => _buffer.Length - _count;
+
+        public void Write(byte[] data)
+        {
+            if (data == null)
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+            Write(data, 0, data.Length);
+        }
+
+        public void Write(byte[] data, int offset, int length)
+        {
+            if (data == null)
+            {
+                throw new ArgumentNullException(nameof(data));
+            }
+            if (offset < 0 || length < 0 || offset + length > data.Length)
+            {
+                throw new ArgumentOutOfRangeException(nameof(length));
+            }
+            if (length > RemainingForWriting)
+            {
+                throw new TransportException(
+                    $"Ring buffer overflow: tried to write {length} bytes, 
{RemainingForWriting} free.");
+            }
+
+            for (var i = 0; i < length; i++)
+            {
+                _buffer[_writePosition] = data[offset + i];
+                _writePosition = (_writePosition + 1) % _buffer.Length;
+            }
+            _count += length;
+        }

Review Comment:
   RingBuffer.Write copies incoming data one byte at a time and does a modulo 
operation per byte. This is on the transport hot path (TcpTransportInstance 
writes every receive into the ring buffer), so it can become a throughput 
bottleneck under sustained traffic.



##########
plc4net/api/PlcDriverManager.cs:
##########
@@ -1,104 +1,77 @@
-/*
- * 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.
- */
+//
+// 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 System.Threading.Tasks;
 using org.apache.plc4net.api;
 using org.apache.plc4net.api.authentication;
 using org.apache.plc4net.exceptions;
 
 namespace org.apache.plc4net
 {
     /// <summary>
-    /// Manages connections to PLCs
+    /// Registry of PLC protocol drivers. A driver registers itself here;
+    /// connection requests are dispatched to the driver whose protocol code
+    /// matches the connection string scheme.
     /// </summary>
     public class PlcDriverManager
     {
-        /// <summary>
-        /// Singleton instance of the manager
-        /// </summary>
         private static PlcDriverManager _instance;
 
-        /// <summary>
-        /// Get the singleton instance
-        /// </summary>
-        public static PlcDriverManager Instance => _instance ?? (_instance = 
new PlcDriverManager());
-
-        /// <summary>
-        /// Dictionary for the drivers
-        /// </summary>
-        private readonly Dictionary<string, IPlcDriver> _drivers;
+        private readonly Dictionary<string, IPlcDriver> _drivers
+            = new Dictionary<string, IPlcDriver>();
 
-        /// <summary>
-        /// Private constructor for the singleton driver manager.
-        /// </summary>
         private PlcDriverManager()
         {
-            _drivers = new Dictionary<string, IPlcDriver>();
-
-            /*
-             * TODO: Implement some mechanism to provide drivers -> MEF?
-             */
         }
 
-        /// <summary>
-        /// Get the connection to the a PLC identified by the URL
-        /// </summary>
-        /// <param name="url">URL including the schema to connect to the 
PLC</param>
-        /// <param name="authentication">Authentication to use</param>
-        /// <returns>Created PLC connection</returns>
-        public async Task<IPlcConnection> GetConnection(string url, 
IPlcAuthentication authentication)
+        public static PlcDriverManager Instance => _instance ?? (_instance = 
new PlcDriverManager());
+
+        public void RegisterDriver(IPlcDriver driver)
         {
-            var plcDriver = GetDriver(url);
-            var connection = await plcDriver.ConnectAsync(url, authentication);
-            
-            //TODO: Does the driver method already connect or is a separate 
connect needed?
-            //TODO: Should we do it like this?
-            if (!connection.IsConnected)
-            {
-                await connection.ConnectAsync();
-            }
+            _drivers[driver.ProtocolCode] = driver;
+        }
 
-            return connection;
+        public IPlcDriver GetDriver(string connectionString)
+        {
+            var parsed = ConnectionString.Parse(connectionString);
+            return GetDriverByCode(parsed.ProtocolCode);
         }
 
-        public IPlcDriver GetDriver(string url)
+        public IPlcDriver GetDriverByCode(string protocolCode)
         {
-            try
+            if (_drivers.TryGetValue(protocolCode.ToLowerInvariant(), out var 
driver))
             {
-                Uri plcUri = new Uri(url);
-                var proto = plcUri.Scheme;
-
-                _drivers.TryGetValue(proto, out var plcDriver);
+                return driver;

Review Comment:
   Driver registration and lookup use different key casing. RegisterDriver 
stores driver.ProtocolCode as-is, but GetDriverByCode lowercases the lookup 
key; this will fail for any protocol code not already lowercase (or if a driver 
uses mixed case).



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to