Example #1
0
        /// <summary>
        /// Send a reply to a GetValues request
        /// </summary>
        /// <param name="data">GetValues message body</param>
        private void SendGetValuesResult(ByteArray data)
        {
            NameValuePairCollection collection;

            //reads the collection
            using (InputStream stream = new InputStream(data))
            {
                collection = new NameValuePairCollection(stream);
            }

            //sets the value
            this.Properties.GetValues(collection);

            //sends the collection
            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    foreach (NameValuePair item in collection)
                    {
                        item.Encode(writer);
                    }

                    ByteArray body = new ByteArray(stream.ToArray());
                    this.SendMessage(new Message(MessageType.GetValuesResult, 0, body));
                }
            }
        }
Example #2
0
        /// <summary>
        /// Receives data from the lower layer
        /// </summary>
        /// <param name="data">Data recived</param>
        public virtual void Receive(ByteArray data)
        {
            _recvBuffer = _recvBuffer.Concat(data, true);

            while (_recvBuffer.Count >= MessageHeader.HeaderSize)
            {
                MessageHeader header = new MessageHeader(_recvBuffer);
                if (_recvBuffer.Count < header.MessageSize)
                {
                    break;
                }

                ByteArray messageData = _recvBuffer.SubArray(0, header.MessageSize);
                _recvBuffer = _recvBuffer.SubArray(header.MessageSize, true);

                this.ReceiveMessage(new Message(messageData));
            }
        }
Example #3
0
        /// <summary>
        /// Sends data to the network
        /// </summary>
        /// <param name="data">Data to send</param>
        public void Send(ByteArray data)
        {
            lock (_client)
            {
                var sendBuffer = _bufferManager.Allocate();

                try
                {
                    while (data.Count > 0)
                    {
                        int length = Math.Min(data.Count, sendBuffer.Length);

                        data.CopyTo(sendBuffer, 0, length);
                        _client.GetStream().Write(sendBuffer, 0, data.Count);
                        _client.GetStream().Flush();
                        data = data.SubArray(length, true);
                    }
                }
                finally
                {
                    _bufferManager.Free(sendBuffer);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Send a request output data to the output or error stream
        /// </summary>
        /// <param name="request">Request</param>
        /// <param name="streamType"></param>
        /// <param name="data"></param>
        protected virtual void SendRequestOutput(Request request, MessageType streamType, ByteArray data)
        {
            if (streamType != MessageType.StandardError &&
                streamType != MessageType.StandardOutput)
            {
                throw new ArgumentException("streamType");
            }

            while (data.Count > 0)
            {
                ushort length = (ushort)Math.Min(data.Count, Consts.MaxMessageBodySize);
                this.SendMessage(new Message(streamType, request.Id, data.SubArray(0, length)));
                data = data.SubArray(length, true);
            }
        }
Example #5
0
        /// <summary>
        /// Receives data from the lower layer
        /// </summary>
        /// <param name="data">Data recived</param>
        public virtual void Receive(ByteArray data)
        {
            _recvBuffer = _recvBuffer.Concat(data, true);

            while (_recvBuffer.Count >= MessageHeader.HeaderSize)
            {
                MessageHeader header = new MessageHeader(_recvBuffer);
                if (_recvBuffer.Count < header.MessageSize)
                    break;

                ByteArray messageData = _recvBuffer.SubArray(0, header.MessageSize);
                _recvBuffer = _recvBuffer.SubArray(header.MessageSize, true);

                this.ReceiveMessage(new Message(messageData));
            }
        }
Example #6
0
        /// <summary>
        /// Send a reply to a GetValues request
        /// </summary>
        /// <param name="data">GetValues message body</param>
        private void SendGetValuesResult(ByteArray data)
        {
            NameValuePairCollection collection;

            //reads the collection
            using (InputStream stream = new InputStream(data))
            {
                collection = new NameValuePairCollection(stream);
            }

            //sets the value
            this.Properties.GetValues(collection);

            //sends the collection
            using(MemoryStream stream = new MemoryStream())
            {
                using(BinaryWriter writer = new BinaryWriter(stream))
                {
                    foreach(NameValuePair item in collection)
                        item.Encode(writer);

                    ByteArray body = new ByteArray(stream.ToArray());
                    this.SendMessage(new Message(MessageType.GetValuesResult, 0, body));
                }
            }
        }
Example #7
0
        /// <summary>
        /// Send a request output data to the output or error stream
        /// </summary>
        /// <param name="request">Request</param>
        /// <param name="streamType"></param>
        /// <param name="data"></param>
        protected virtual void SendRequestOutput(Request request, MessageType streamType, ByteArray data)
        {
            if (streamType != MessageType.StandardError &&
                streamType != MessageType.StandardOutput)
                    throw new ArgumentException("streamType");

            while (data.Count > 0)
            {
                ushort length = (ushort)Math.Min(data.Count, Consts.MaxMessageBodySize);
                this.SendMessage(new Message(streamType, request.Id, data.SubArray(0, length)));
                data = data.SubArray(length, true);
            }
        }
Example #8
0
        /// <summary>
        /// Sends data to the network
        /// </summary>
        /// <param name="data">Data to send</param>
        public void Send(ByteArray data)
        {
            lock (_client)
            {
                var sendBuffer = _bufferManager.Allocate();

                try
                {
                    while (data.Count > 0)
                    {
                        int length = Math.Min(data.Count, sendBuffer.Length);

                        data.CopyTo(sendBuffer, 0, length);
                        _client.GetStream().Write(sendBuffer, 0, data.Count);
                        _client.GetStream().Flush();
                        data = data.SubArray(length, true);
                    }
                }
                finally
                {
                    _bufferManager.Free(sendBuffer);
                }
            }
        }