public static GetBytes ( System.DateTime value, bool isBigEndian ) : byte[] | ||
value | System.DateTime | a DateTime value |
isBigEndian | bool | order of bytes. If it's big endian, set it to true, else set it to false. |
return | byte[] |
/// <summary> /// Decode MSG_GETBLKS message. /// </summary> /// <param name="data">Data to be decoded.</param> /// <param name="index">Started index.</param> /// <returns>The MSG_GETBLKS struct.</returns> private MSG_GETBLKS DecodeMSG_GETBLKS(byte[] data, ref int index) { MSG_GETBLKS ret = new MSG_GETBLKS(); ret.SizeOfSegmentID = MarshalHelper.GetUInt32(data, ref index, false); ret.SegmentID = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentID); ret.ZeroPad = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentID % 4); if (ret.ZeroPad == null) { ret.ZeroPad = new byte[0]; } ret.ReqBlockRangeCount = MarshalHelper.GetUInt32(data, ref index, false); ret.ReqBlockRanges = new BLOCK_RANGE[ret.ReqBlockRangeCount]; for (int i = 0; i < ret.ReqBlockRangeCount; i++) { ret.ReqBlockRanges[i].Index = MarshalHelper.GetUInt32(data, ref index, false); ret.ReqBlockRanges[i].Count = MarshalHelper.GetUInt32(data, ref index, false); } ret.SizeOfDataForVrfBlock = MarshalHelper.GetUInt32(data, ref index, false); ret.DataForVrfBlock = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfDataForVrfBlock); if (ret.DataForVrfBlock == null) { ret.DataForVrfBlock = new byte[0]; } return(ret); }
/// <summary> /// Gets the request body byte array from the http request. /// </summary> /// <param name="httpListenerRequest">The http request.</param> /// <returns>The array of byte.</returns> private byte[] DecomposeHttpRequest(HttpListenerRequest httpListenerRequest) { if (httpListenerRequest == null) { throw new ArgumentNullException("httpListenerRequest"); } lock (this.obj) { List <byte[]> payloadList = new List <byte[]>(); List <byte> tempBuffer = new List <byte>(); Stream requestStream = null; try { requestStream = httpListenerRequest.InputStream; int readSize = 0; byte[] payloadBuffer = null; while (true) { int tempIndex = 0; payloadBuffer = new byte[this.bufferSize]; readSize = requestStream.Read(payloadBuffer, 0, payloadBuffer.Length); if (readSize == 0) { break; } else { tempBuffer.AddRange(MarshalHelper.GetBytes(payloadBuffer, ref tempIndex, readSize)); } } } catch (IOException ex) { if (this.logger != null) { this.logger.AddDebug( string.Format( "Unexpected exception receiving failed: {0}", ex.Message)); } } finally { if (requestStream != null) { requestStream.Close(); } } return(tempBuffer.ToArray()); } }
/// <summary> /// Decode MSG_BLK message. /// </summary> /// <param name="data">Data to be decoded.</param> /// <param name="index">Started index.</param> /// <returns>The MSG_BLK struct.</returns> private MSG_BLK DecodeMSG_BLK(byte[] data, ref int index) { MSG_BLK ret = new MSG_BLK(); ret.SizeOfSegmentId = MarshalHelper.GetUInt32(data, ref index, false); ret.SegmentId = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentId); ret.ZeroPad = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentId % 4); if (ret.ZeroPad == null) { ret.ZeroPad = new byte[0]; } ret.BlockIndex = MarshalHelper.GetUInt32(data, ref index, false); ret.NextBlockIndex = MarshalHelper.GetUInt32(data, ref index, false); ret.SizeOfBlock = MarshalHelper.GetUInt32(data, ref index, false); ret.Block = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfBlock); if (ret.Block == null) { ret.Block = new byte[0]; } ret.ZeroPad2 = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfBlock % 4); if (ret.ZeroPad2 == null) { ret.ZeroPad2 = new byte[0]; } ret.SizeOfVrfBlock = MarshalHelper.GetUInt32(data, ref index, false); ret.VrfBlock = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfVrfBlock); if (ret.VrfBlock == null) { ret.VrfBlock = new byte[0]; } ret.ZeroPad3 = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfVrfBlock % 4); if (ret.ZeroPad3 == null) { ret.ZeroPad3 = new byte[0]; } ret.SizeOfIVBlock = MarshalHelper.GetUInt32(data, ref index, false); ret.IVBlock = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfIVBlock); if (ret.IVBlock == null) { ret.IVBlock = new byte[0]; } return(ret); }
/// <summary> /// Encode pack. /// </summary> /// <returns>Encoded bytes.</returns> public override byte[] Encode() { RESPONSE_MESSAGE responseMessage = new RESPONSE_MESSAGE(); responseMessage.MESSAGEBODY = this.msgBLKLIST; responseMessage.MESSAGEHEADER = this.MessageHeader; byte[] ret; List <byte> temp = new List <byte>(); byte[] tempPayload; List <byte> listRet = new List <byte>(); listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLKLIST)responseMessage.MESSAGEBODY).SizeOfSegmentId, false)); listRet.AddRange(((MSG_BLKLIST)responseMessage.MESSAGEBODY).SegmentId); while (listRet.Count % 4 != 0) { listRet.Add(byte.MinValue); } listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLKLIST)responseMessage.MESSAGEBODY).BlockRangeCount, false)); for (int i = 0; i < ((MSG_BLKLIST)responseMessage.MESSAGEBODY).BlockRangeCount; i++) { listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLKLIST)responseMessage.MESSAGEBODY).BlockRanges[i].Index, false)); listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLKLIST)responseMessage.MESSAGEBODY).BlockRanges[i].Count, false)); } listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLKLIST)responseMessage.MESSAGEBODY).NextBlockIndex, false)); tempPayload = listRet.ToArray(); responseMessage.MESSAGEHEADER.MsgSize = (uint)tempPayload.Length + 16; List <byte> listRet1 = new List <byte>(); listRet1.AddRange(MarshalHelper.GetBytes((ushort)responseMessage.MESSAGEHEADER.ProtVer.MinorVersion, false)); listRet1.AddRange(MarshalHelper.GetBytes((ushort)responseMessage.MESSAGEHEADER.ProtVer.MajorVersion, false)); listRet1.AddRange(MarshalHelper.GetBytes((uint)responseMessage.MESSAGEHEADER.MsgType, false)); listRet1.AddRange(MarshalHelper.GetBytes(responseMessage.MESSAGEHEADER.MsgSize, false)); listRet1.AddRange(MarshalHelper.GetBytes((uint)responseMessage.MESSAGEHEADER.CryptoAlgoId, false)); temp.AddRange(listRet1.ToArray()); temp.AddRange(tempPayload); temp.InsertRange(0, MarshalHelper.GetBytes((uint)temp.Count, false)); ret = (byte[])temp.ToArray(); return(ret); }
/// <summary> /// Encode pack. /// </summary> /// <returns>Encode bytes.</returns> public override byte[] Encode() { REQUEST_MESSAGE requestMessage = new REQUEST_MESSAGE(); requestMessage.MESSAGEBODY = this.msgGetBLKS; requestMessage.MESSAGEHEADER = this.MessageHeader; byte[] ret; List <byte> temp = new List <byte>(); byte[] tempPayload; List <byte> listRet = new List <byte>(); listRet.AddRange(MarshalHelper.GetBytes(((MSG_GETBLKS)requestMessage.MESSAGEBODY).SizeOfSegmentID, false)); listRet.AddRange(((MSG_GETBLKS)requestMessage.MESSAGEBODY).SegmentID); while (listRet.Count % 4 != 0) { listRet.Add(byte.MinValue); } listRet.AddRange(MarshalHelper.GetBytes(((MSG_GETBLKS)requestMessage.MESSAGEBODY).ReqBlockRangeCount, false)); for (int i = 0; i < ((MSG_GETBLKS)requestMessage.MESSAGEBODY).ReqBlockRanges.Length; i++) { listRet.AddRange(MarshalHelper.GetBytes(((MSG_GETBLKS)requestMessage.MESSAGEBODY).ReqBlockRanges[i].Index, false)); listRet.AddRange(MarshalHelper.GetBytes(((MSG_GETBLKS)requestMessage.MESSAGEBODY).ReqBlockRanges[i].Count, false)); } listRet.AddRange(MarshalHelper.GetBytes(((MSG_GETBLKS)requestMessage.MESSAGEBODY).SizeOfDataForVrfBlock, false)); tempPayload = listRet.ToArray(); requestMessage.MESSAGEHEADER.MsgSize = (uint)tempPayload.Length + 16; List <byte> listRet1 = new List <byte>(); listRet1.AddRange(MarshalHelper.GetBytes((ushort)requestMessage.MESSAGEHEADER.ProtVer.MinorVersion, false)); listRet1.AddRange(MarshalHelper.GetBytes((ushort)requestMessage.MESSAGEHEADER.ProtVer.MajorVersion, false)); listRet1.AddRange(MarshalHelper.GetBytes((uint)requestMessage.MESSAGEHEADER.MsgType, false)); listRet1.AddRange(MarshalHelper.GetBytes(requestMessage.MESSAGEHEADER.MsgSize, false)); listRet1.AddRange(MarshalHelper.GetBytes((uint)requestMessage.MESSAGEHEADER.CryptoAlgoId, false)); temp.AddRange(listRet1.ToArray()); temp.AddRange(tempPayload); ret = (byte[])temp.ToArray(); return(ret); }
/// <summary> /// Decode MSG_BLKLIST message. /// </summary> /// <param name="data">The data to be decoded.</param> /// <param name="index">Started index.</param> /// <returns>The MSG_BLKLIST struct.</returns> private MSG_BLKLIST DecodeMSG_BLKLIST(byte[] data, ref int index) { MSG_BLKLIST ret = new MSG_BLKLIST(); ret.SizeOfSegmentId = MarshalHelper.GetUInt32(data, ref index, false); ret.SegmentId = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentId); ret.ZeroPad = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentId % 4); if (ret.ZeroPad == null) { ret.ZeroPad = new byte[0]; } ret.BlockRangeCount = MarshalHelper.GetUInt32(data, ref index, false); ret.BlockRanges = new BLOCK_RANGE[ret.BlockRangeCount]; for (int i = 0; i < ret.BlockRangeCount; i++) { ret.BlockRanges[i].Index = MarshalHelper.GetUInt32(data, ref index, false); ret.BlockRanges[i].Count = MarshalHelper.GetUInt32(data, ref index, false); } ret.NextBlockIndex = MarshalHelper.GetUInt32(data, ref index, false); return(ret); }
/// <summary> /// Encode pack. /// </summary> /// <returns>Encode bytes.</returns> public override byte[] Encode() { RESPONSE_MESSAGE responseMessage = new RESPONSE_MESSAGE(); responseMessage.MESSAGEBODY = this.msgNegoResp; responseMessage.MESSAGEHEADER = this.MessageHeader; byte[] ret; List <byte> temp = new List <byte>(); byte[] tempPayload; List <byte> listRet = new List <byte>(); listRet.AddRange(MarshalHelper.GetBytes(((MSG_NEGO_RESP)responseMessage.MESSAGEBODY).MinSupporteProtocolVersion.MinorVersion, false)); listRet.AddRange(MarshalHelper.GetBytes(((MSG_NEGO_RESP)responseMessage.MESSAGEBODY).MinSupporteProtocolVersion.MajorVersion, false)); listRet.AddRange(MarshalHelper.GetBytes(((MSG_NEGO_RESP)responseMessage.MESSAGEBODY).MaxSupporteProtocolVersion.MinorVersion, false)); listRet.AddRange(MarshalHelper.GetBytes(((MSG_NEGO_RESP)responseMessage.MESSAGEBODY).MaxSupporteProtocolVersion.MajorVersion, false)); tempPayload = listRet.ToArray(); responseMessage.MESSAGEHEADER.MsgSize = (uint)tempPayload.Length + 16; List <byte> listRet1 = new List <byte>(); listRet1.AddRange(MarshalHelper.GetBytes((ushort)responseMessage.MESSAGEHEADER.ProtVer.MinorVersion, false)); listRet1.AddRange(MarshalHelper.GetBytes((ushort)responseMessage.MESSAGEHEADER.ProtVer.MajorVersion, false)); listRet1.AddRange(MarshalHelper.GetBytes((uint)responseMessage.MESSAGEHEADER.MsgType, false)); listRet1.AddRange(MarshalHelper.GetBytes(responseMessage.MESSAGEHEADER.MsgSize, false)); listRet1.AddRange(MarshalHelper.GetBytes((uint)responseMessage.MESSAGEHEADER.CryptoAlgoId, false)); temp.AddRange(listRet1.ToArray()); temp.AddRange(tempPayload); temp.InsertRange(0, MarshalHelper.GetBytes((uint)temp.Count, false)); ret = (byte[])temp.ToArray(); return(ret); }