Merge pull request #692 from spongehah/refactor/c-libuv-remove-go-wrapper

fix(c/libuv): Change the request calling method of echo_server
This commit is contained in:
xushiwei
2024-08-27 05:23:28 +08:00
committed by GitHub

View File

@@ -9,10 +9,10 @@ import (
var DEFAULT_PORT c.Int = 8080 var DEFAULT_PORT c.Int = 8080
var DEFAULT_BACKLOG c.Int = 128 var DEFAULT_BACKLOG c.Int = 128
var ( type WriteReq struct {
Req *libuv.Write Req libuv.Write
Buf libuv.Buf Buf libuv.Buf
) }
func main() { func main() {
// Initialize the default event loop // Initialize the default event loop
@@ -39,9 +39,13 @@ func main() {
loop.Run(libuv.RUN_DEFAULT) loop.Run(libuv.RUN_DEFAULT)
} }
func FreeWriteReq() { func FreeWriteReq(req *libuv.Write) {
wr := (*WriteReq)(c.Pointer(req))
// Free the buffer base. // 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) { 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 { if status != 0 {
c.Fprintf(c.Stderr, c.Str("Write error: %s\n"), libuv.Strerror(libuv.Errno(status))) 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) { func EchoRead(client *libuv.Stream, nread c.Long, buf *libuv.Buf) {
if nread > 0 { if nread > 0 {
req := new(WriteReq)
// Initialize the buffer with the data read. // 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. // Write the data back to the client.
Req = &libuv.Write{} req.Req.Write(client, &req.Buf, 1, EchoWrite)
Req.Write(client, &Buf, 1, EchoWrite)
return return
} }
if nread < 0 { if nread < 0 {
@@ -96,7 +100,6 @@ func OnNewConnection(server *libuv.Stream, status c.Int) {
// Initialize the client TCP handle. // Initialize the client TCP handle.
if libuv.InitTcp(libuv.DefaultLoop(), client) < 0 { if libuv.InitTcp(libuv.DefaultLoop(), client) < 0 {
c.Fprintf(c.Stderr, c.Str("Failed to initialize client\n")) c.Fprintf(c.Stderr, c.Str("Failed to initialize client\n"))
c.Free(c.Pointer(client))
return return
} }