简体   繁体   English

即使缓冲区被覆盖以进行挂起操作,WriteFile也成功

[英]WriteFile succeeded even when buffer is overwritten for pending operation

In my program (posted into this question ), I have made the following changes at the client end: 在我的程序(发布到此问题中 )中,我在客户端进行了以下更改:

wretry:   
   cbToWrite = _stprintf(chBuf[0], TEXT("Message %d from Client"), retrycount - numberofsend + 1);
   cbToWrite *= sizeof(TCHAR);

   fSuccess = WriteFile(hPipe, chBuf[0], cbToWrite,
  &cbWritten, &woverlapped[retrycount-numberofsend]);

By this, now same buffer will be used for writing everytime. 这样,现在每次将使用相同的缓冲区进行写入。

In this case, WriteFile is pending every time as it returns ERROR_IO_PENDING . 在这种情况下, WriteFile每次都将挂起,因为它返回ERROR_IO_PENDING Since, the content of buffer is overwritten every time, server should receive the last message that was written into the buffer. 由于缓冲区的内容每次都会被覆盖,因此服务器应收到写入缓冲区的最后一条消息。 But server is receiving the messages without any problem. 但是服务器正在接收消息,没有任何问题。

Does it mean that the message is first copied into some internal buffer? 这是否意味着该消息首先被复制到某个内部缓冲区中? Can I destroy the buffer and expects the message will be delivered to the server? 我可以销毁缓冲区并期望消息将传递到服务器吗?

Again to quote MSDN: 再次引用MSDN:

lpBuffer [in] A pointer to the buffer containing the data to be written to the file or device. lpBuffer [in]指向缓冲区的指针,该缓冲区包含要写入文件或设备的数据。 This buffer must remain valid for the duration of the write operation. 该缓冲区必须在写操作期间保持有效。 The caller must not use this buffer until the write operation is completed. 在写操作完成之前,调用者不得使用此缓冲区。

So it seems your messages are not guaranteed to arrive safely if you mess with the buffer while the write operation is taking place. 因此,如果在进行写操作时弄乱了缓冲区,似乎无法保证安全地到达您的消息。 You should definitely not destroy the buffer 您绝对不应该破坏缓冲区

Your code is essentially using undefined behaviour. 您的代码本质上是使用未定义的行为。 From the WriteFile documentation: WriteFile文档中:

Accessing the output buffer while a write operation is using the buffer may lead to corruption of the data written from that buffer. 在写操作使用缓冲区的同时访问输出缓冲区可能会导致从该缓冲区写入的数据损坏。 Applications must not write to, reallocate, or free the output buffer that a write operation is using until the write operation completes. 在写操作完成之前,应用程序不得写入,重新分配或释放写操作正在使用的输出缓冲区。

As Raymond Chen points out, appearing to succeed is undefined behaviour 正如雷蒙德·陈(Raymond Chen)指出的那样, 看起来成功是不确定的行为

Undefined behavior means that anything can happen. 未定义的行为意味着任何事情都可能发生。 The program might crash immediately. 该程序可能会立即崩溃。 It might crash five minutes later. 五分钟后可能会崩溃。 It might send email to your boss saying that you screwed up and then read you Vogon poetry. 它可能会向您的老板发送电子邮件,说您搞砸了,然后读了Vogon诗歌。 Or maybe not. 或者可能不是。

Your code may work right now, but it might just stop working at some point in the future. 您的代码可能现在就可以工作,但是将来可能会停止工作。 It could be because you've built in release, or Microsoft changes the underlying workings of WriteFile or random chance. 可能是因为您已内置发行版,或者Microsoft更改了WriteFile的基础工作方式或随机机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM