1 year ago

#217710

test-img

stevep

Implementing IAsyncResult Begin/End Without Delegate.BeginInvoke()

I have a class which receives data though a socket and marshals the data to an object. Synchronously, it would look like this:

class SocketTest
{
    private const int HEADER_LEN = 10;

    Socket socket;
    byte[] buffer;

    public Data ReceiveData()
    {
        /* Receive header packet */
        socket.Receive(buffer, 0, HEADER_LEN, SocketFlags.None);
        Header h = ReadFromBuffer<Header>(0);

        /* Receive data packet */
        socket.Receive(buffer, HEADER_LEN, h.DataLen, SocketFlags.None);
        return ReadFromBuffer<Data>(HEADER_LEN);
    }

    T ReadFromBuffer<T>(int offset)
    {
        /* Get IntPtr to buffer and handle offset */
        IntPtr ptr = new IntPtr(0); 
        return (T)Marshal.PtrToStructure(ptr, typeof(T));
    }

    public class Header
    {
        public int DataLen;
    }

    public class Data
    { 
    }
}

I would like to implement this asynchronously. Most examples I see suggest to simply call Delegate.BeginInvoke() like so:

    Func<Data> recv; // = new Func<Data>(ReceiveData);

    public IAsyncResult BeginReceiveData(AsyncCallback callback, object state)
    {
        return recv.BeginInvoke(callback, state);
    }

    public Data EndReceiveData(IAsyncResult result)
    {
        return recv.EndInvoke(result);
    }

However, I run into an issue when running in .NET Core. Seems like .NET Core does not support Delegate.BeginInvoke() according to this StackOverflow Question and its references.

I'd like to make a true asynchronous Begin/End implementation by calling Socket.BeginReceive() and Socket.EndReceive(), and not just BeginInvoke a synchronous method. Below is a skeleton I made. but I don't quite like it since I'll have to make and manage my own IAsyncResult class. Is there a better, truly asynchronous design pattern for doing this?

    public IAsyncResult BeginReceiveData(AsyncCallback callback, object state)
    {
        socket.BeginReceive(
            buffer, 
            0, 
            HEADER_LEN, 
            SocketFlags.None, 
            RecvCallback, 
            new RecvState());

        return null; /* Create AsyncResult Manually? */
    }

    void RecvCallback(IAsyncResult res)
    {           
        RecvState state = res.AsyncState as RecvState;

        if (state.Header == null)
        {
            /* Receive header packet */
            socket.EndReceive(res);
            state.Header = ReadFromBuffer<Header>(0);

            socket.BeginReceive(
                buffer,
                HEADER_LEN,
                state.Header.DataLen,
                SocketFlags.None,
                RecvCallback,
                state);
        }
        else
        {
            /* Receive data packet */
            socket.EndReceive(res);
            state.Data = ReadFromBuffer<Data>(HEADER_LEN);

            /* Flag async operation as done */
        }
    }

    public Data EndReceiveData(IAsyncResult result)
    {
        /* Get the Data object from async operation, and deal with
         * the user provided callback function */
        return null; 
    }

    class RecvState
    {
        public Header Header;
        public Data Data;
    }

c#

sockets

iasyncresult

0 Answers

Your Answer

Accepted video resources