/// <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); } }
/// <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); }
/// <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); }