C# .. Asynchronous Sockets .. where is the State Object Stored?
A simple question really, but one where I cannot find any anwsers too.
When I execute an Asynchronous Socket operation, such as :
socket.BeginSend
(
new byte[]{6}, // byte[] | buffer
0, // int | data to send buffer offset
开发者_如何学运维 1, // int | data to send length
SocketFlags.None, // enum | dunno :)
new AsyncCallback(OnSend), // AsyncCallback | callback method
STATEOBJECT // object | ..state object..
);
It works, and when complete it invokes the AsyncCallback parameter, passing with it an IAsyncResult.
void OnSend(IAsyncResult ar)
{
object STATEOBJECT = ar.AsyncState as object;
/*
Process the socket operation
*/
}
SO..
When the socket operation is being executed 'asynchronously' I know from various sources that the buffer is pinned in memory.
However I do not know where the 'state object' is stored?
why? because I am wondering what the effect of large 'state object's will have?
Taa!
Is C#, the object is stored where is allocated :)
The real question should be: who has a reference to the object during the async operation? The coarse answer is: the framework. A more precise answer is that your object is ultimately referenced by the OVERLAPPED structure, through a mixture of managed and native data types, and the OVERLAPPED structure is kept in a list in the kernel.
It is stored where you created it, on the Heap. And it will not move unless the GarbageCollector finds that necessary. You are just handing out a reference to the BeginSend() method, and you get it back in the OnSend[Complete]() method.
精彩评论