How to asynchronously receive complex object in C#

2019-03-01 13:16发布

EDIT: A more concise explanation of what I was trying to do here and with answers

I'm using c# asynchronous sockets to receive data from a source.

My problem is how and where to store received data if there are more to be received?

When a string is received, I can just use a string builder to receive and store just like this from msdn:

private void ReceiveCallback_onQuery(IAsyncResult ar)
        {
            try
            {
                // Retrieve the state object and the client socket 
                // from the asynchronous state object.
                StateObject state = (StateObject)ar.AsyncState;
                Socket client = state.workSocket;

                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);

                if (bytesRead > 0)
                {
                    // There might be more data, so store the data received so far.
                    dataReceived += state.buffer; //Does not work (dataReceived is byte[] type)

                    // Get the rest of the data.
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReceiveCallback_onQuery), state);
                }
                else
                {
                    // All the data has arrived; put it in response.
                    if (dataReceived > 1)
                    {
                        response_onQueryHistory = ByteArrayToObject(dataReceived)
                    }
                    // Signal that all bytes have been received.
                    receiveDoneQuery.Set();
                    }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

I dont want to convert my received data into a string as in my case, I am receiving a complex object.

The data sent was serialized and I'm also fine with deserializing.

My question how to 'continually' receive data from the socket without using the string builder to store it.

Thanks!

2条回答
再贱就再见
2楼-- · 2019-03-01 13:25
My problem is how and where to store received data if there are more to be received?

Can use Buffer.BlockCopy and queue it up, for eg,

           int rbytes = client.EndReceive(ar);

            if (rbytes > state.buffer)
            {
                byte[] bytesReceived = new byte[rbytes];
                Buffer.BlockCopy(state.buffer, 0, bytesReceived, 0, rbytes);
                state.myQueue.Enqueue(bytesReceived);                
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(ReceiveCallback_onQuery), state)
            }
查看更多
混吃等死
3楼-- · 2019-03-01 13:43

This depends on how the complex thing is serialized prior to being pushed down the wire in the broken down bytes, you will receive those bytes and use the same algorithm/technique used to serialize the thing to deserialize it back to its original state.

For a more specific answer I'd ask that you be more specific yourself.

查看更多
登录 后发表回答