panjf2000 / gnet

🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go.

Home Page:https://gnet.host

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question]: ReadFrom系列函数的作用

amwfhv opened this issue · comments

Actions I've taken before I'm here

  • I've thoroughly read the documentations about this problem but still have no answer.
  • I've searched the Github Issues/Discussions but didn't find any similar problems that have been solved.
  • I've searched the internet for this problem but didn't find anything helpful.

Questions with details

我直接使用gnet-examples中的echo_tcp示例,将OnTraffic代码直接修改为:

func (es *echoServer) OnTraffic(c gnet.Conn) gnet.Action {
	buf, _ := c.Next(-1)
	//c.Write(buf)
	c.ReadFrom(bytes.NewReader(buf))
	return gnet.None
}

如果在Windows平台那么客户端可以正确的收到回包,在Linux,Macos平台客户端都无法收到回包
请问ReadForm,WriterTo,ReaderAt,WriterAt这些函数功能是什么,应该在什么情况下使用,
感谢提供这么好的网络库,期待着你的回复。

Code snippets (optional)

No response

因为 gnet 会尽量复用内存,所以你通过 c.Next(-1) 取得 []byte 然后再用它包装成一个 bytes.Reader 提交给 conn 去读,可能会导致数据错乱,你试试把 buf 拷贝一份,应该就不会有问题了。