| 239 | } |
| 240 | |
| 241 | ssize_t async_pipe_write(hamlib_async_pipe_t *pipe, const unsigned char *buf, |
| 242 | size_t count, int timeout) |
| 243 | { |
| 244 | HANDLE event_handles[1] = |
| 245 | { |
| 246 | pipe->write_overlapped.hEvent, |
| 247 | }; |
| 248 | HANDLE write_handle = pipe->write; |
| 249 | LPOVERLAPPED overlapped = &pipe->write_overlapped; |
| 250 | DWORD wait_result; |
| 251 | int result; |
| 252 | ssize_t bytes_written = 0; |
| 253 | |
| 254 | result = WriteFile(write_handle, buf, count, NULL, overlapped); |
| 255 | |
| 256 | if (!result) |
| 257 | { |
| 258 | result = GetLastError(); |
| 259 | |
| 260 | switch (result) |
| 261 | { |
| 262 | case ERROR_SUCCESS: |
| 263 | // No error? |
| 264 | break; |
| 265 | |
| 266 | case ERROR_IO_PENDING: |
| 267 | wait_result = WaitForMultipleObjects(1, event_handles, FALSE, timeout); |
| 268 | |
| 269 | switch (wait_result) |
| 270 | { |
| 271 | case WAIT_OBJECT_0 + 0: |
| 272 | break; |
| 273 | |
| 274 | case WAIT_TIMEOUT: |
| 275 | CancelIo(write_handle); |
| 276 | return -RIG_ETIMEOUT; |
| 277 | |
| 278 | default: |
| 279 | result = GetLastError(); |
| 280 | rig_debug(RIG_DEBUG_ERR, "%s: WaitForMultipleObjects() error: %d\n", __func__, |
| 281 | result); |
| 282 | return -RIG_EINTERNAL; |
| 283 | } |
| 284 | |
| 285 | break; |
| 286 | |
| 287 | default: |
| 288 | rig_debug(RIG_DEBUG_ERR, "%s: WriteFile() error: %d\n", __func__, result); |
| 289 | return -RIG_EIO; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | result = GetOverlappedResult(write_handle, overlapped, (LPDWORD) &bytes_written, |
| 294 | FALSE); |
| 295 | |
| 296 | if (!result) |
| 297 | { |
| 298 | result = GetLastError(); |
no test coverage detected