For a hobby project i've found i need to be able to do io using a handle provided by a native api which only supports overlappyed io. It should be possible to do and i took the opportunity to learn how to do Begin* and callbacks correctly. Or that's what i thought.
I've put together a little test project just to work out a framework for sending and receiving date using callbacks. I've got a listener thread, a sending thread and a receiver thread. I'm using BeginAction and IAsyncResult's throughout. I'm hitting some peculiar problems though and while i'm sure there's something i'm missing or don't understand i can't work out what it is. I'm hoping someone can point out my mistake or tell me where i need to be reading to find my error(s).
I've uploaded my test project, c# vs2k8 : https://www.box.com/s/ex3qrmxuwz2djzau4kcv
I think i've got an issue with timing. I found that the more console output i added the longer it took to reproduce the problem. To get around that i added an indirection for output so that console writes are off on their own thread, the caller formats the message (mainly to get the right threadid) then queues up the write and moves back to whatever work it should be doing.
When i run the project in a few seconds i'll hit the breakpoint setup which indicates that a socket.EndReceive call returned 0 which is supposed to indicate that the socket was closed. Except i didn't close it, and the sending socket isn't closed so when the sender thread picks it at random and tries to send it blocks. Why do i get a 0 return when there is data available? I'm stumped.
So, those of you who are bored or want some reason to avoid your family please have a look and let me know if you have any ideas or questions.
I've put together a little test project just to work out a framework for sending and receiving date using callbacks. I've got a listener thread, a sending thread and a receiver thread. I'm using BeginAction and IAsyncResult's throughout. I'm hitting some peculiar problems though and while i'm sure there's something i'm missing or don't understand i can't work out what it is. I'm hoping someone can point out my mistake or tell me where i need to be reading to find my error(s).
I've uploaded my test project, c# vs2k8 : https://www.box.com/s/ex3qrmxuwz2djzau4kcv
I think i've got an issue with timing. I found that the more console output i added the longer it took to reproduce the problem. To get around that i added an indirection for output so that console writes are off on their own thread, the caller formats the message (mainly to get the right threadid) then queues up the write and moves back to whatever work it should be doing.
When i run the project in a few seconds i'll hit the breakpoint setup which indicates that a socket.EndReceive call returned 0 which is supposed to indicate that the socket was closed. Except i didn't close it, and the sending socket isn't closed so when the sender thread picks it at random and tries to send it blocks. Why do i get a 0 return when there is data available? I'm stumped.
So, those of you who are bored or want some reason to avoid your family please have a look and let me know if you have any ideas or questions.