gnet-io / gnet-examples

Examples of gnet
MIT License
245 stars 77 forks source link

Example echo server bugged #3

Closed Power2All closed 3 years ago

Power2All commented 3 years ago

Was trying out this system, but I found out a issue. The "c.SendTo(returnData)" is bugged, as it somehow is getting nil, same for c.RemoteAddr(), if I access that in the go func(), it's gone or overwritten by something not able to be used. It works if I remove the go func(){}(), but then it's performance goes down the drain. Anybody able to figure out what I could do to make it properly work ?

[edit] Correction, without go func() it doesn't work either. Should work, but somehow the connection is just, gone...

func (es *echoServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
    console(fmt.Sprintf("UDP Echo server is listening on %s (multi-cores: %t, loops: %d)", srv.Addr.String(), srv.Multicore, srv.NumEventLoop), 3)
    return
}

func (es *echoServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
    // Echo asynchronously.
    data := append([]byte{}, frame...)
    connection := c.RemoteAddr()
    go func() {
        returnData, err1 := udpParseMessage(c, connection, data)
        if err1 != nil {
            console(fmt.Sprintf("[%s] UDP Request Error: %v", getCurrentFuncName(), err1.Error()), 1)
            return
        }
        err2 := c.SendTo(returnData)
        if err2 != nil {
            console(fmt.Sprintf("[%s] UDP SendTo: %v", getCurrentFuncName(), err2.Error()), 1)
            return
        }
    }()
    return
}