I have an async socket callback for a UDP socket. When the callback is fired after receiving a packet, I want to map the packet to a structure I have.
note, all unsafe code blocks here.
// note, i know the following is not an entire DHCP packet, I just didn't want this msg to get too long.
[StructLayout(LayoutKind.Explicit)]
public struct DHCPPacket
{
[FieldOffset(0)] public byte op;
[FieldOffset(1)] public byte htype;
[FieldOffset(2)] public byte hlen;
[FieldOffset(3)] public byte hops;
[FieldOffset(4)] public uint xid;
[FieldOffset(8)] public ushort secs;
[FieldOffset(10)] public ushort flags;
}
When coming into this routine I only have the asyncstate available to me from the iasyncresult which I get through
unsafe public void MessageReceived( IAsyncResult ar )
{
int length = m_Socket.EndReceive( ar );
Debug.WriteLine( "Bytes received " + length.ToString() + ".");
byte[] buffer = (byte[])ar.AsyncState;
fixed( byte *inbuff = buffer )
{
Debug.WriteLine( "next line wont compile" );
DHCPPacket * packet = (DHCPPacket *)inbuff; // this line won't compile despite examples I've seen where it should
// call processing of packet here
}
StartReceivingData();
}
the problem is that despite many examples I've seen where you can map your unsafe structure to a pointer, the compiler doesn't seem to want to let me. What am I doing wrong? For that matter, is there another easy way to get from point a to point b
where I have DHCPPacket mapped to the memory area?