+static bool write_packet(vpn_packet_t *packet) {
+ DWORD lenout;
+ static vpn_packet_t queue;
+
+ ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
+ packet->len, device_info);
+
+ /* Check if there is something in progress */
+
+ if(queue.len) {
+ DWORD size;
+ BOOL success = GetOverlappedResult(device_handle, &w_overlapped, &size, FALSE);
+
+ if(success) {
+ ResetEvent(&w_overlapped);
+ queue.len = 0;
+ } else {
+ int err = GetLastError();
+
+ if(err != ERROR_IO_INCOMPLETE) {
+ ifdebug(TRAFFIC) logger(LOG_DEBUG, "Error completing previously queued write: %s", winerror(err));
+ ResetEvent(&w_overlapped);
+ queue.len = 0;
+ } else {
+ ifdebug(TRAFFIC) logger(LOG_DEBUG, "Previous overlapped write still in progress");
+ // drop this packet
+ return true;
+ }
+ }
+ }
+
+ /* Otherwise, try to write. */