Пример #1
0
        /// <summary>
        ///
        /// </summary>
        public void Reset()
        {
            if (outputStream != null)
            {
                outputStream.Dispose();
            }

            if (inputStream != null)
            {
                inputStream.Dispose();
            }

            outputStream = new Google.Protobuf.CodedOutputStream(mDataBuffer);
            inputStream  = new Google.Protobuf.CodedInputStream(mDataBuffer);
            mDataBuffer.AsSpan().Fill(0);
        }
Пример #2
0
        public byte[] SerializeMessage()
        {
            var mmstream = new MemoryStream(this.Message.CalculateSize());
            var stream   = new Google.Protobuf.CodedOutputStream(mmstream);

            this.Message.WriteTo(stream);
            System.Console.WriteLine($"len: {mmstream.CanRead}, {mmstream.CanWrite}, {mmstream.Capacity}, {this.Message.CalculateSize()}, {mmstream.GetBuffer()}");
            // read from stream
            var byteArray = mmstream.GetBuffer();

            // dispose after use
            stream.Dispose();
            // return byteArr;
            return(byteArray);
        }
        public byte[] SerializeMessage()
        {
            var mmstream = new MemoryStream();
            var stream   = new Google.Protobuf.CodedOutputStream(mmstream);

            this.Message.WriteTo(stream);
            stream.Dispose();
            mmstream.Seek(0, SeekOrigin.Begin);
            var byteArray = new byte[mmstream.Length];
            var count     = mmstream.Read(byteArray, 0, 20);

            // Read the remaining bytes, byte by byte.
            while (count < mmstream.Length)
            {
                byteArray[count++] = Convert.ToByte(mmstream.ReadByte());
            }
            // return byteArr;
            return(byteArray);
        }
Пример #4
0
            /**
             * Common logic for sending messages via protocol buffers.
             *
             * @param command
             * @param message
             * @param originator
             * @param label
             * @throws SiteWhereAgentException
             */
            protected void sendMessage(Lib.SiteWhere.SiteWhere.Types.Command command, Google.Protobuf.IMessage message, String originator, String label)
            {
                System.IO.MemoryStream            stream = new System.IO.MemoryStream();
                Google.Protobuf.CodedOutputStream output = new Google.Protobuf.CodedOutputStream(stream);
                try
                {
                    Lib.SiteWhere.SiteWhere.Types.Header h = new SiteWhere.SiteWhere.Types.Header();
                    h.Command = command;

                    if (originator != null)
                    {
                        h.Originator = originator;
                    }

                    h.WriteTo(output);
                    message.WriteTo(output);

                    output.Flush();

                    //string s = "2,8,1,50,10,10,121,117,110,103,111,97,108,50,50,50,18,36,55,100,102,100,54,100,54,51,45,53,101,56,100,45,52,51,56,48,45,98,101,48,52,45,102,99,53,99,55,51,56,48,49,100,102,98";
                    //List<byte> bytes = new List<byte>();
                    //foreach(var b in s.Split(','))
                    //{
                    //    byte br;
                    //    if (int.Parse(b) < 0)
                    //        br = (byte)(0xff & int.Parse(b));
                    //    else
                    //        br = byte.Parse(b);
                    //    bytes.Add(br);
                    //}

                    //connection.Publish(getTopic(), bytes.ToArray(), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                    connection.Publish(getTopic(), stream.ToArray(), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);

                    output.Dispose();
                }
                catch (Exception e)
                {
                    LOGGER.info(e.Message);
                }
            }