1

I'm writing a single-client (for now) server that has a main loop that interpert client request , and dispatces a proper handler.

for some taks, I'll like to use MS overlapped I/O (it's for windows only) . in one of them, I want to read from a file and write the content to a socket. as I have class wrape for that action, it's allocates a buffer, the size of the file, and then a call to ReadFile (with an actual file-system file) is made, followed by a WriteFile (this time, to a socket handle) both beeing made overlapped.

problem is: as it's async', the class d'tor is beeing called automatically, and the file's buffer get's freed, before the job is done,

obviously, that is a broken design of mine, I really like to hear for a suggestion of your's,

thanks!

Enri Lum
  • 11
  • 1
  • 5
    You'll obviously need to find a way to stall the destructor until the io has completed. – Wug Oct 03 '12 at 21:50

4 Answers4

1

Looking at the docs it seems that you want to call GetOverlappedResult with bWait being set. That will block until the overlapped operation completes.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

You could use a mutex inside the async functions and inside the destructors.

user1708860
  • 1,683
  • 13
  • 32
0

Could create a thread that works in the background to verify that file IO is still needing to be done and keeping the file's class open (even if it is just a loop to continue waiting). That way the socket can do what it needs to, and the IO can continually (through a flag you set in the class's member variable) to keep it informed of it's status.

Then in the destructor, check to see if it's unlocked and ready, otherwise another stalling loop.

M4rc
  • 473
  • 2
  • 13
0

thanks all,

it's look like I'm missing something, as using another thread for checking, or just waiting for completion, seems to be lost of the benfits of async IO.

I guess that what I'm looking for is ,the usage method that MS planned developers to use.

Enri Lum
  • 11
  • 1