瀏覽代碼

fix issue #26: hio_set_readbuf in hread_cb

ithewei 5 年之前
父節點
當前提交
ff1dfa39f7
共有 2 個文件被更改,包括 19 次插入6 次删除
  1. 5 4
      event/nio.c
  2. 14 2
      examples/nc.c

+ 5 - 4
event/nio.c

@@ -351,13 +351,14 @@ static int __nio_write(hio_t* io, const void* buf, int len) {
 
 static void nio_read(hio_t* io) {
     //printd("nio_read fd=%d\n", io->fd);
+    void* buf;
+    int len, nread;
+read:
     if (io->readbuf.base == NULL || io->readbuf.len == 0) {
         hio_set_readbuf(io, io->loop->readbuf.base, io->loop->readbuf.len);
     }
-    void* buf = io->readbuf.base;
-    int   len = io->readbuf.len;
-    int   nread = 0;
-read:
+    buf = io->readbuf.base;
+    len = io->readbuf.len;
     nread = __nio_read(io, buf, len);
     //printd("read retval=%d\n", nread);
     if (nread < 0) {

+ 14 - 2
examples/nc.c

@@ -44,6 +44,18 @@ void on_recv(hio_t* io, void* buf, int readbytes) {
             SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
     }
     printf("%.*s", readbytes, (char*)buf);
+
+    // test hio_set_readbuf in hread_cb
+#if 0
+    static int total_readbytes = 0;
+    total_readbytes += readbytes;
+    if (total_readbytes >= RECV_BUFSIZE) {
+        total_readbytes = 0;
+    }
+    hio_set_readbuf(io, recvbuf + total_readbytes, RECV_BUFSIZE - total_readbytes);
+    printf("%.*s", total_readbytes, recvbuf);
+#endif
+
     fflush(stdout);
 }
 
@@ -148,8 +160,8 @@ Examples: nc 127.0.0.1 80\n\
 
     hloop_t* loop = hloop_new(HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS);
 
-    // stdin
-    stdinio = hread(loop, 0, recvbuf, RECV_BUFSIZE, on_stdin);
+    // stdin use default readbuf
+    stdinio = hread(loop, 0, NULL, 0, on_stdin);
     if (stdinio == NULL) {
         return -20;
     }