@@ -64,6 +64,7 @@ int ReadFileToBuffer(HANDLE file, uint8_t *buf, size_t count, HANDLE asyncContex
64
64
OVERLAPPED ol = {};
65
65
DWORD nRead;
66
66
if (asyncContext) {
67
+ // For asynchronous, returns -1 for EOF
67
68
ol.hEvent = asyncContext;
68
69
if (ReadFile (file, buf, static_cast <DWORD>(count), nullptr , &ol)) {
69
70
return GetOverlappedResult (file, &ol, &nRead, FALSE ) ? nRead : -1 ;
@@ -79,6 +80,7 @@ int ReadFileToBuffer(HANDLE file, uint8_t *buf, size_t count, HANDLE asyncContex
79
80
}
80
81
}
81
82
else if (ReadFile (file, buf, static_cast <DWORD>(count), &nRead, nullptr )) {
83
+ // For synchronous, 0 means EOF
82
84
return nRead;
83
85
}
84
86
return -1 ;
@@ -105,12 +107,19 @@ int64_t SeekFile(int file, int64_t offset)
105
107
}
106
108
107
109
template <class P >
108
- int ReadFileToBuffer (int file, uint8_t *buf, size_t count, bool asyncContext, P asyncCancelProc)
110
+ int ReadFileToBuffer (int file, uint8_t *buf, size_t count, int & asyncContext, P asyncCancelProc)
109
111
{
110
112
for (;;) {
111
113
int ret = static_cast <int >(read (file, buf, count));
112
114
if (ret >= 0 || !asyncContext || (errno != EAGAIN && errno != EWOULDBLOCK) || asyncCancelProc ()) {
113
- return ret;
115
+ // Asynchronous FIFO reading can be opened before a writer opens it, then read() returns 0.
116
+ if (asyncContext && ret > 0 ) {
117
+ // Connected.
118
+ asyncContext = 2 ;
119
+ }
120
+ // For asynchronous, returns -1 for EOF
121
+ // For synchronous, 0 means EOF
122
+ return asyncContext == 2 && ret == 0 ? -1 : ret;
114
123
}
115
124
fd_set rfd;
116
125
FD_ZERO (&rfd);
@@ -123,7 +132,7 @@ int ReadFileToBuffer(int file, uint8_t *buf, size_t count, bool asyncContext, P
123
132
}
124
133
}
125
134
126
- void CloseFile (int file, bool asyncContext)
135
+ void CloseFile (int file, int asyncContext)
127
136
{
128
137
static_cast <void >(asyncContext);
129
138
if (file >= 0 ) {
@@ -296,7 +305,8 @@ int main(int argc, char **argv)
296
305
}
297
306
#else
298
307
bool traceToStdout = traceName[0 ] == ' -' && !traceName[1 ];
299
- bool asyncContext = timeoutMode == 2 ;
308
+ // 0: synchronous, 1: not connected yet, 2: connected.
309
+ int asyncContext = timeoutMode == 2 ;
300
310
int file;
301
311
int openedFile = -1 ;
302
312
if (srcName[0 ] == ' -' && !srcName[1 ]) {
@@ -407,6 +417,11 @@ int main(int argc, char **argv)
407
417
if (n < 0 ) {
408
418
completed = true ;
409
419
}
420
+ #ifndef _WIN32
421
+ else if (n <= 0 ) {
422
+ retry = true ;
423
+ }
424
+ #endif
410
425
else {
411
426
bufCount += n;
412
427
filePos += n;
@@ -420,7 +435,7 @@ int main(int argc, char **argv)
420
435
}
421
436
else {
422
437
SleepFor (std::chrono::milliseconds (200 ));
423
- if (SeekFile (file, filePos) != filePos) {
438
+ if (timeoutMode != 2 && SeekFile (file, filePos) != filePos) {
424
439
fprintf (stderr, " Warning: seek failed.\n " );
425
440
completed = true ;
426
441
}
0 commit comments