ithewei / libhv

🔥 比libevent/libuv/asio更易用的网络库。A c/c++ network library for developing TCP/UDP/SSL/HTTP/WebSocket/MQTT client/server.

Home Page:https://github.com/ithewei/libhv/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hread() on file not work in windows?

rorschach-py opened this issue · comments

This code failed to read from file, example code in "example/hloop_test.c" also failed to read from stdin.
Tested in Windows10 x64, gcc driver version 13.2.0 (x86_64-posix-seh-rev1)

#include <stdio.h>
#include <hv/hv.h>
#include <hv/hloop.h>

void on_timer(htimer_t* timer) {
    hloop_t* loop = hevent_loop(timer);
    printf("on_timer: event_id=%llu\tpriority=%d\tuserdata=%ld\ttime=%llus\thrtime=%lluus\n",
        LLU(hevent_id(timer)), hevent_priority(timer), (long)(intptr_t)(hevent_userdata(timer)),
        LLU(hloop_now(loop)), LLU(hloop_now_hrtime(loop)));
}

void on_stdin(hio_t* io, void* buf, int readbytes) {
    printf("fd=%d readbytes=%d\n", hio_fd(io), readbytes);
}

int main() {
    printf("version:%s\n", hv_version());

    hloop_t* loop = hloop_new(0);
    htimer_t* timer = htimer_add(loop, on_timer, 1*1000, 3);
    hevent_set_userdata(timer, (void*)(intptr_t)1);

    FILE *f = fopen("a.html","rb");
    char buf[64];
    hread(loop, fileno(f), buf, sizeof(buf), on_stdin);

    hloop_run(loop);
    hloop_free(&loop);
    return 0;
}

output:

version:1.3.2
on_timer: event_id=1    priority=5      userdata=1      time=1705626973s        hrtime=253618506373us
on_timer: event_id=1    priority=5      userdata=1      time=1705626974s        hrtime=253619504261us
on_timer: event_id=1    priority=5      userdata=1      time=1705626975s        hrtime=253620501732us

Yes,Windows iowatcher not work on stdio.

// Windows iowatcher not work on stdio

It's a file, not stdio, fd==3.

int fd = open("a.html", O_RDONLY);
if(fd){
        hio_t* io = hread(loop, fd, buf, sizeof(buf), on_stdin);
        printf("hio_exists:%d, fd:%d\n", hio_exists(loop, fd), fd);
}

output:

version:1.3.2
engine:epoll
hio_exists:1, fd:3
on_timer: event_id=1    priority=5      userdata=1      time=1705978507s        hrtime=140634500652us
on_timer: event_id=1    priority=5      userdata=1      time=1705978509s        hrtime=140635511422us
on_timer: event_id=1    priority=5      userdata=1      time=1705978510s        hrtime=140636510355us

Windows iowatcher also not work on file io, just work for socket io.