Opc.Ua.BinaryDecoder.ReadString C# (CSharp) Method

ReadString() public method

Reads a string from the stream (throws an exception if its length exceeds the limit specified).
public ReadString ( string fieldName, int maxStringLength ) : string
fieldName string
maxStringLength int
return string
        public string ReadString(string fieldName, int maxStringLength)
        {
            int length = m_reader.ReadInt32();

            if (length == -1)
            {
                return null;
            }

            if (maxStringLength > 0 && maxStringLength < length)
            {
                throw ServiceResultException.Create(
                    StatusCodes.BadEncodingLimitsExceeded, 
                    "MaxStringLength {0} < {1}", 
                    maxStringLength,
                    length);
            }

            byte[] bytes = m_reader.ReadBytes(length);
            return new UTF8Encoding().GetString(bytes, 0, bytes.Length);
        }

Same methods

BinaryDecoder::ReadString ( string fieldName ) : string

Usage Example

コード例 #1
0
ファイル: ComAeClient.cs プロジェクト: OPCFoundation/UA-.NET
        /// <summary>
        /// Acknowledges the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="eventId">The event id.</param>
        /// <param name="comment">The comment.</param>
        /// <returns></returns>
        public uint Acknowledge(
            ServerSystemContext context,
            byte[] eventId,
            LocalizedText comment)
        {
            // get the user name from the context.
            string userName = String.Empty;

            if (context.UserIdentity != null)
            {
                userName = context.UserIdentity.DisplayName;
            }

            // get the comment.
            string commentText = String.Empty;

            if (comment != null)
            {
                commentText = comment.Text;
            }

            System.Runtime.InteropServices.ComTypes.FILETIME ftActiveTime;

            // unpack the event id.
            ServiceMessageContext messageContext = new ServiceMessageContext();

            messageContext.NamespaceUris = context.NamespaceUris;
            messageContext.ServerUris = context.ServerUris;
            messageContext.Factory = context.EncodeableFactory;

            BinaryDecoder decoder = new BinaryDecoder(eventId, messageContext);

            string source = decoder.ReadString(null);
            string conditionName = decoder.ReadString(null);
            ftActiveTime.dwHighDateTime = decoder.ReadInt32(null);
            ftActiveTime.dwLowDateTime = decoder.ReadInt32(null);
            int cookie = decoder.ReadInt32(null);

            decoder.Close();

            string methodName = "IOPCEventServer.AckCondition";

            IntPtr pErrors = IntPtr.Zero;
            
            try
            {
                IOPCEventServer server = BeginComCall<IOPCEventServer>(methodName, true);

                server.AckCondition(
                    1,
                    userName,
                    commentText,
                    new string[] { source },
                    new string[] { conditionName },
                    new System.Runtime.InteropServices.ComTypes.FILETIME[] { ftActiveTime },
                    new int[] { cookie },
                    out pErrors);
            }
            catch (Exception e)
            {
                ComCallError(methodName, e);
                return StatusCodes.BadUnexpectedError;
            }
            finally
            {
                EndComCall(methodName);
            }

            // unmarshal results.
            int[] errors = ComUtils.GetInt32s(ref pErrors, 1, true);
                        
            if (errors[0] == ResultIds.S_ALREADYACKED)
            {
                return StatusCodes.BadConditionBranchAlreadyAcked;
            }
            else if (errors[0] < 0)
            {
                return StatusCodes.BadEventIdUnknown;
            }

            return StatusCodes.Good;
        }