Async Sockets Buffer Manager / Buffer Pool

M

Matijaz

Hello,

I have written async sockets based TCP Client application for CF. I have encountered the problem with segmented heap. I suppose it is caused pinned objects (byte[] buffers) which can't be compacted by GC. I saw some implementation for full framework based on ArraySegment but this doesn't seem to work for me. Can someone give me a hint how to deal with this problem or pointme out to any resources on the internet?

This is snippet of receive callback code:

private void ReceiveData(StateObject state)
{
asyncEvent.Reset();

Socket socket = state.workSocket;

if (socket != null)
socket.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, receiveCallback, state);

}

private void ReceiveCallback(IAsyncResult ar)
{
try {

// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket socket = state.workSocket;

int bytesRead = socket.EndReceive(ar);

AsyncEventSet(NotifyCommand.ReceivedData, null);

if (bytesRead > 0)
{
addDataBuffer(state.buffer, bytesRead);

// Wake up parser thread - there is a job to do
RaiseParseEvent();

// Reset timeouts
ResetTimeouts();

//Start recieving again
ReceiveData(state);
}

} catch (Exception ex) {
AsyncEventSet(NotifyCommand.Error, ex.Message);
logError("Receive exception: " + ex.Message);
}

}

Best regards
Matijaz
 
M

matijaz69

I have written async sockets based TCP Client application for CF. I have
encountered the problem with segmented heap. I suppose it is caused pinned
objects (byte[] buffers) which can't be compacted by GC. I saw some
implementation for full framework based on ArraySegment but this doesn't seem
to work for me. Can someone give me a hint how to deal with this problem or
point me out to any resources on the internet?

Just noticed I didn't make myself clear. I'm looking for buffer pool / buffer manager working solution that can be ported to .NET Compact Framework.

Best regards
Matijaz
 

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