In my C# code I am receiving a byte[1024] from a DLL through TCP link. Code I did for this I am attaching below.
The workflow is
- An asynchronous packet gets received at tcp port into a byte array, which then gets copied to a different array for processing.
- This copied array then gets processed according to received packet size(each message could be of different size one behind another).
- While in the meantime async method received another data which should get appended to array used for processing packet.
Problem coming is being method as asynchronous data arriving at TCP port is getting appended anytime to the processing buffer array which many times throws exceptions while its size is getting referred during Copy methods. I am unable to block this async call as after processing one packet.
/* Method to process recieved packet */ _message.OnRecieve(message);
UI is getting updated and on user's action again the send and receive methods getting called where receive buffer array gets overwritten with new data.
Sharing piece of code: