marshalling socket buffer

A

allen

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?
 
A

allen

ok, I figured our a way that works pretty well.

DHCPPacket packet = (DHCPPacket)Marshal.PtrToStructure( (IntPtr)inbuff, typeof( DHCPPacket ) );

seems to do just the trick.
 
S

Sami Vaaraniemi

One word of caution though: there is no guarantee that the full packet has
arrived when the system calls MessageReceived. The number of bytes arrived
could be anything between 1 to sizeof(DHCPPacket), or 0 if the connection
was closed. To make it robust you should handle the situation when the
packet arrives in multiple fragments.

Regards,
Sami

allen said:
ok, I figured our a way that works pretty well.

DHCPPacket packet = (DHCPPacket)Marshal.PtrToStructure( (IntPtr)inbuff,
typeof( DHCPPacket ) );

seems to do just the trick.


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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top