-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
doc: add a customized codec example #90
Conversation
Codecov Report
@@ Coverage Diff @@
## master #90 +/- ##
==========================================
- Coverage 87.30% 87.08% -0.23%
==========================================
Files 18 18
Lines 1347 1347
==========================================
- Hits 1176 1173 -3
- Misses 144 146 +2
- Partials 27 28 +1
Continue to review full report at Codecov.
|
@panjf2000 |
codecov 是代码覆盖率的问题,每次 run 之后覆盖率都可能会不一样,这个不用管;travis 是因为之前的代码改动导致 Windows 上有一些偶发性的错误,一般重试就能成功,我一直没找到原因,你的 PR 只是加了个 example,没有对 gnet 的功能代码进行改动,所以这个失败和你的 PR 无关,这个你也不用管。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
留了一些 comments,你看下,还有代码里有一些格式问题被自动工具检测出来了,你也点开 Files changed
这个 tab 看下,然后也解决一下吧。
|
||
func (cs *customCodecServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) { | ||
fmt.Println("frame:", string(frame)) | ||
packFrame, _ := protocol.Pack(protocol.PROTOCAL_VERSION, protocol.ACTION_DATA, frame) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你这个 Pack 方法是打包吧,React
事件函数里一般是根据 frame 进行业务处理,如果你这个示例没有业务逻辑而是直接就是打包数据的话,Pack 还是放到 codec 的 Encode 方法里吧,规范一些。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修改完之后,顺便也更新一下 READMEs 里的代码。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pack方法是打包,但是是和自定义协议的version和actionType是挂钩的,所以单独作为一个函数,在发送到对端之前使用。且encode方法签名也没有参数来给用户传入,也只能写在外部的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我看这里好像只需要 import protocol 就可以用了吧?你的 codec 也在 protocol 里,可以直接使用吧?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
譬如,actionType是可以由用户决定是什么的。他不是一个写死的固定值。。只是demo上面没有体现,如果encode方法签名预留了用户传入的参数,把Pack里面的逻辑移动到encode里面也是没问题的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actionType 只有在 React 里才能有足够的信息决定?在 Encode 里没办法确定?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
另外,你是可以在 Encode 拿到用户的传入参数的,通过:
Lines 221 to 222 in 916c255
func (c *conn) Context() interface{} { return c.ctx } | |
func (c *conn) SetContext(ctx interface{}) { c.ctx = ctx } |
SetContext(interface{})
存放你的自定义数据,然后在 Encode 里调用 Context()
取出就行了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是的。是有这种可能性的,React 会处理几种不同 header 。例如我在 demo 上的常量ActionPing/ActionPong/ActionData ,这些是需要在 react 上面由用户根据不同的header处理的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已经使用 Context()
来取代 React 的参数传递。然后 client 那里的 Pack() 函数改名为 ClientEncode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有些地方还是没改过来,再看一下吧。
|
||
func (cs *customCodecServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) { | ||
fmt.Println("frame:", string(frame)) | ||
packFrame, _ := protocol.Pack(protocol.DefaultProtocolVersion, protocol.ActionData, frame) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里为什么没有改?Pack 方法不能移到 codec 的 Encode 方法里吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@panjf2000 同上。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码我看着没啥问题了,最后确认下,修改完的代码你做过端到端 client -> server 的测试了吗?跑通了吗?
恩。可以正常运作后才提交的。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, approved it!
Fixes #89
name: Pull request
about: Propose changes to the code
title: 'Add a customized codec example'
labels: ''
assignees: ''
1. Are you opening this pull request for bug-fixs, optimizations or new feature?
2. Please describe how these code changes achieve your intention.
3. Please link to the relevant issues (if any).
4. Which documentation changes (if any) need to be made/updated because of this PR?
4. Checklist