Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReadPacket后,未调用 OnMessage #18

Closed
jdwang001 opened this issue May 9, 2017 · 9 comments
Closed

ReadPacket后,未调用 OnMessage #18

jdwang001 opened this issue May 9, 2017 · 9 comments

Comments

@jdwang001
Copy link

仿照echo server写了接收程序,在ReadPacket可正常接收数据,而数据时常无法传递到OnMessage。
已经设置PacketReceiveChanLimit=200000
不知道其他朋友是否遇到此问题,打算尝试gtcp

@gansidui
Copy link
Owner

gansidui commented May 9, 2017

看下是不是ReadPacket里面解包出错了,这个打印点log看看就知道。
PacketReceiveChanLimit 不用设置这么大,这个可是实实在在的内存消耗,参考demo。

@jdwang001
Copy link
Author

嗯,已经在ReadPacket进行验证,得出了以上的结论,也是很费解。所以提了这个issues。
哈哈,就是玩玩,尝试了这么大,但是问题依旧,现在PacketReceiveChanLimit=1,直接在ReadPacket进行的处理,OnMessage直接返回true。所以想问问,是否发现过这类问题。
我是wireshark和程序同时验证的
并且,OnConnect和OnClose能正常接收到连接事件。也是由此,发现OnMessage未响应。

@gansidui
Copy link
Owner

gansidui commented May 9, 2017

在conn.go里面读写packetReceiveChan 的地方打印log调试

@jdwang001
Copy link
Author

OnConnect: 192.168.12.200:44388
ReadPacket is over
handleLoop receive packetReceiveChan
Get OnMessage
readloop 0 0xc42006e360
OnClose: 192.168.12.200:44388


OnConnect: 192.168.12.200:45300
ReadPacket is over
readloop 0 0xc42006e480
handleLoop receive packetReceiveChan
OnClose: 192.168.12.200:45300
handleLoop receive packetReceiveChan IsClosed
OnConnect: 192.168.12.200:45302
ReadPacket is over
readloop 0 0xc4201460c0
handleLoop receive packetReceiveChan
OnClose: 192.168.12.200:45302
handleLoop receive packetReceiveChan IsClosed
OnConnect: 192.168.12.200:45303
ReadPacket is over
readloop 0 0xc42006e5a0
handleLoop receive packetReceiveChan
handleLoop receive packetReceiveChan IsClosed
OnClose: 192.168.12.200:45303
OnConnect: 192.168.12.200:45304
ReadPacket is over
readloop 0 0xc4201461e0
handleLoop receive packetReceiveChan
handleLoop receive packetReceiveChan IsClosed
OnClose: 192.168.12.200:45304


OnConnect: 192.168.12.200:45305
ReadPacket is over
readloop 0 0xc4201be0c0
OnClose: 192.168.12.200:45305
handleLoop receive packetReceiveChan
handleLoop receive packetReceiveChan IsClosed

@jdwang001
Copy link
Author

func (c *Conn) handleLoop() {
	defer func() {
		recover()
		c.Close()
	}()

	for {
		select {
		case <-c.srv.exitChan:
			return

		case <-c.closeChan:
			return

		case p := <-c.packetReceiveChan:
			fmt.Printf("handleLoop receive packetReceiveChan \n")
			if c.IsClosed() {
				fmt.Println("handleLoop receive packetReceiveChan IsClosed")
				return
			}
			if !c.srv.callback.OnMessage(c, p) {
				fmt.Println("handleLoop receive packetReceiveChan OnMessage")
				return
			}
		}
	}
}


func (this *Callback) OnMessage(c *gotcp.Conn, p gotcp.Packet) bool  {
	fmt.Println("Get OnMessage")
	return true
}

比较无法理解。。。。。。。

@gansidui
Copy link
Owner

gansidui commented May 9, 2017

OnClose: 192.168.12.200:45300
handleLoop receive packetReceiveChan IsClosed
客户端发送数据后立即断开连接了。

go if c.IsClosed() { fmt.Println("handleLoop receive packetReceiveChan IsClosed") return }
这里return了,没法执行下面的 OnMessage。

这个地方其实是个bug,#14 ,之前有人提过这个问题,还没有修改好

@gansidui
Copy link
Owner

gansidui commented May 9, 2017

可以在 func (c *Conn) Close() 这个函数里面把未处理的message回调出去。

@jdwang001
Copy link
Author

		case p := <-c.packetReceiveChan:
			fmt.Printf("handleLoop receive packetReceiveChan \n")
			if !c.srv.callback.OnMessage(c, p) {
				fmt.Println("handleLoop receive packetReceiveChan OnMessage")
				return
			}
			if c.IsClosed() {
				fmt.Println("handleLoop receive packetReceiveChan IsClosed")
				return
			}
		}

如果先读取,而后在判断是否close,会造成什么结果? 数据长度过长,分开处理?

@gansidui
Copy link
Owner

用户可能在OnMessage里面通过conn来发送数据,所以这里 是先判断close,再通过OnMessage回调

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants