G
GeorgeG
Hi,
I have the following code (which works fine) to quickly deblock incoming
stream of data from sockets.
What do you think? Do you see any performance problems using interop for
such a thing?
Reminds me casting a stream of bytes to a struct in C/C++.
many thanks in advance
GeorgeG
########################### code sample starts here
###########################################
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public unsafe struct myData
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public byte[] data1;
[MarshalAs(UnmanagedType.U1, SizeConst=1)]
public byte data2;
[MarshalAs(UnmanagedType.U1, SizeConst=1)]
public byte data3;
// byte [] data : contains the data to be deserialized.
// out myData d : ref to a struct to be rehydrated with data (works fine as
a non-static method aswell).
public static unsafe void Deserialize( byte [] data, out myData d)
{
fixed ( byte * p_data = data)
{
System.IntPtr ptr = new IntPtr(p_data);
ctfTrailer = (myData)Marshal.PtrToStructure(ptr,
typeof(myData));
}
}
}
########################### code sample ends here
###########################################
I have the following code (which works fine) to quickly deblock incoming
stream of data from sockets.
What do you think? Do you see any performance problems using interop for
such a thing?
Reminds me casting a stream of bytes to a struct in C/C++.
many thanks in advance
GeorgeG
########################### code sample starts here
###########################################
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public unsafe struct myData
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public byte[] data1;
[MarshalAs(UnmanagedType.U1, SizeConst=1)]
public byte data2;
[MarshalAs(UnmanagedType.U1, SizeConst=1)]
public byte data3;
// byte [] data : contains the data to be deserialized.
// out myData d : ref to a struct to be rehydrated with data (works fine as
a non-static method aswell).
public static unsafe void Deserialize( byte [] data, out myData d)
{
fixed ( byte * p_data = data)
{
System.IntPtr ptr = new IntPtr(p_data);
ctfTrailer = (myData)Marshal.PtrToStructure(ptr,
typeof(myData));
}
}
}
########################### code sample ends here
###########################################