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