From 2325b547fd162462f421e4fef1d220b1b4355077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E8=8B=B1=E6=9D=B0?= <2635879218@qq.com> Date: Fri, 9 Aug 2024 10:35:14 +0800 Subject: [PATCH] fix(c/libuv): Change the request calling method of echo_server --- c/libuv/_demo/echo_server/echo_server.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/c/libuv/_demo/echo_server/echo_server.go b/c/libuv/_demo/echo_server/echo_server.go index d2bb85ef..9a208a76 100644 --- a/c/libuv/_demo/echo_server/echo_server.go +++ b/c/libuv/_demo/echo_server/echo_server.go @@ -9,10 +9,10 @@ import ( var DEFAULT_PORT c.Int = 8080 var DEFAULT_BACKLOG c.Int = 128 -var ( - Req *libuv.Write +type WriteReq struct { + Req libuv.Write Buf libuv.Buf -) +} func main() { // Initialize the default event loop @@ -39,9 +39,13 @@ func main() { loop.Run(libuv.RUN_DEFAULT) } -func FreeWriteReq() { +func FreeWriteReq(req *libuv.Write) { + wr := (*WriteReq)(c.Pointer(req)) // Free the buffer base. - c.Free(c.Pointer(Buf.Base)) + if wr.Buf.Base != nil { + c.Free(c.Pointer(wr.Buf.Base)) + wr.Buf.Base = nil + } } func AllocBuffer(handle *libuv.Handle, suggestedSize uintptr, buf *libuv.Buf) { @@ -54,16 +58,16 @@ func EchoWrite(req *libuv.Write, status c.Int) { if status != 0 { c.Fprintf(c.Stderr, c.Str("Write error: %s\n"), libuv.Strerror(libuv.Errno(status))) } - FreeWriteReq() + FreeWriteReq(req) } func EchoRead(client *libuv.Stream, nread c.Long, buf *libuv.Buf) { if nread > 0 { + req := new(WriteReq) // Initialize the buffer with the data read. - Buf = libuv.InitBuf(buf.Base, c.Uint(nread)) + req.Buf = libuv.InitBuf(buf.Base, c.Uint(nread)) // Write the data back to the client. - Req = &libuv.Write{} - Req.Write(client, &Buf, 1, EchoWrite) + req.Req.Write(client, &req.Buf, 1, EchoWrite) return } if nread < 0 { @@ -96,7 +100,6 @@ func OnNewConnection(server *libuv.Stream, status c.Int) { // Initialize the client TCP handle. if libuv.InitTcp(libuv.DefaultLoop(), client) < 0 { c.Fprintf(c.Stderr, c.Str("Failed to initialize client\n")) - c.Free(c.Pointer(client)) return }