예제 #1
0
            /// <summary>
            /// Gets the request data as the byte array
            /// </summary>
            /// <returns>Request data</returns>
            public byte[] GetRequestData()
            {
                switch (this.operation)
                {
                case SetDataOperationCode.InterlockedAddIfVersion:
                {
                    byte[] data = this.prevData.Data;
                    if (data.Length != sizeof(long))
                    {
                        throw new InvalidOperationException("data in the node should be able to hold a long");
                    }

                    IoSession ios = new IoSession()
                    {
                        Buffer = data, MaxBytes = data.Length
                    };
                    IoSession res_ios = new IoSession()
                    {
                        Buffer = new byte[data.Length], MaxBytes = data.Length
                    };

                    long prevlong;
                    DataEncodingHelper.Read(ios, out prevlong);
                    prevlong += this.number;
                    ios.Pos   = 0;
                    DataEncodingHelper.Write(prevlong, res_ios);

                    return(res_ios.Buffer);
                }

                case SetDataOperationCode.InterlockedXORIfVersion:
                {
                    byte[] data = this.prevData.Data;
                    if (data.Length != sizeof(long))
                    {
                        throw new InvalidOperationException("data in the node should be able to hold a long");
                    }

                    IoSession ios = new IoSession()
                    {
                        Buffer = data, MaxBytes = data.Length
                    };
                    IoSession res_ios = new IoSession()
                    {
                        Buffer = new byte[data.Length], MaxBytes = data.Length
                    };

                    long prevlong;
                    DataEncodingHelper.Read(ios, out prevlong);
                    prevlong ^= this.number;
                    ios.Pos   = 0;
                    DataEncodingHelper.Write(prevlong, res_ios);

                    return(res_ios.Buffer);
                }

                default:
                    throw new NotImplementedException("I don't understand operation " + this.operation);
                }
            }
예제 #2
0
        /// <summary>
        /// creates a byte[] for SetData representing a "InterlockedSet(value)" operation
        /// </summary>
        /// <param name="value">value to set</param>
        /// <returns>the byte[] with the encoded operation.</returns>
        public ISetDataOperation InterlockedSet(long value)
        {
            IoSession ios = new IoSession()
            {
                Buffer = new byte[sizeof(long)], MaxBytes = Length
            };

            DataEncodingHelper.Write(value, ios);
            return(new SetDataOperation(ios.Buffer));
        }
예제 #3
0
        /// <summary>
        /// creates a byte[] for SetData representing a "InterlockedXOR(value)" operation
        /// </summary>
        /// <param name="value">value to compute the XOR with</param>
        /// <returns>the byte[] with the encoded operation.</returns>
        public ISetDataOperation InterlockedXOR(long value)
        {
            SetDataOperationCode op = SetDataOperationCode.InterlockedXORIfVersion;

            IoSession ios = new IoSession()
            {
                Buffer = new byte[Length], MaxBytes = Length
            };

            DataEncodingHelper.Write(Magic, ios);
            DataEncodingHelper.Write((ushort)op, ios);
            DataEncodingHelper.Write(value, ios);
            return(new SetDataOperation(ios.Buffer));
        }
예제 #4
0
        /// <summary>
        /// returns the value corresponding to the byte[] stored in the node
        /// </summary>
        /// <param name="bytes">the data as retrieved from the node</param>
        /// <returns>the long retrieved.</returns>
        public long GetValue(byte[] bytes)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }

            long number;

            IoSession ios = new IoSession()
            {
                Buffer = bytes, MaxBytes = bytes.Length
            };

            DataEncodingHelper.Read(ios, out number);

            return(number);
        }
예제 #5
0
        /// <summary>
        /// tries to read the operation from the byte[] (which may or may not be a "SetDataOperation").
        /// </summary>
        /// <param name="data">byte[] with the data</param>
        /// <param name="op">operation encoded in the byte[]</param>
        /// <param name="number">the number argument of the operation</param>
        /// <returns>true if the byte[] contained a setdata operation. False otherwise</returns>
        internal bool TryRead(byte[] data, out SetDataOperationCode op, out long number)
        {
            if (data == null || data.Length != Length)
            {
                op     = SetDataOperationCode.None;
                number = 0;
                return(false);
            }

            IoSession ios = new IoSession()
            {
                Buffer = data, MaxBytes = data.Length
            };

            uint cookie;

            DataEncodingHelper.Read(ios, out cookie);
            if (cookie != Magic)
            {
                op     = SetDataOperationCode.None;
                number = 0;
                return(false);
            }

            ushort opshort;

            DataEncodingHelper.Read(ios, out opshort);

            op = (SetDataOperationCode)opshort;
            if (op == (int)SetDataOperationCode.None)
            {
                number = 0;
                return(false);
            }

            DataEncodingHelper.Read(ios, out number);
            return(true);
        }