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

doc: add a customized codec example #90

Merged
merged 6 commits into from
May 18, 2020
Merged

Conversation

theoneLee
Copy link
Contributor

@theoneLee theoneLee commented May 18, 2020

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

  • I have squashed all insignificant commits.
  • I have commented my code for explaining package types, values, functions, and non-obvious lines.
  • I have written unit tests and verified that all tests passes (if needed).
  • I have documented feature info on the README (only when this PR is adding a new feature).
  • (optional) I am willing to help maintain this change if there are issues with it later.

@codecov
Copy link

codecov bot commented May 18, 2020

Codecov Report

Merging #90 into master will decrease coverage by 0.22%.
The diff coverage is n/a.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
eventloop_unix.go 79.01% <0.00%> (-1.86%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 97d4ce4...f136c3c. Read the comment docs.

@theoneLee
Copy link
Contributor Author

theoneLee commented May 18, 2020

@panjf2000
请问这个pr的Travis CI 和codecov/project检查返回的报错,自己需要处理哪些内容呢?
点进去https://travis-ci.com/github/panjf2000/gnet/jobs/336032856 看了下,没明白问题在哪。

@panjf2000
Copy link
Owner

panjf2000 commented May 18, 2020

@panjf2000
请问这个pr的Travis CI 和codecov/project检查返回的报错,自己需要处理哪些内容呢?
点进去https://travis-ci.com/github/panjf2000/gnet/jobs/336032856 看了下,没明白问题在哪。

codecov 是代码覆盖率的问题,每次 run 之后覆盖率都可能会不一样,这个不用管;travis 是因为之前的代码改动导致 Windows 上有一些偶发性的错误,一般重试就能成功,我一直没找到原因,你的 PR 只是加了个 example,没有对 gnet 的功能代码进行改动,所以这个失败和你的 PR 无关,这个你也不用管。

Copy link
Owner

@panjf2000 panjf2000 left a 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 看下,然后也解决一下吧。

README.md Show resolved Hide resolved

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)
Copy link
Owner

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 方法里吧,规范一些。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修改完之后,顺便也更新一下 READMEs 里的代码。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pack方法是打包,但是是和自定义协议的version和actionType是挂钩的,所以单独作为一个函数,在发送到对端之前使用。且encode方法签名也没有参数来给用户传入,也只能写在外部的。

Copy link
Owner

@panjf2000 panjf2000 May 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我看这里好像只需要 import protocol 就可以用了吧?你的 codec 也在 protocol 里,可以直接使用吧?

Copy link
Contributor Author

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里面也是没问题的。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actionType 只有在 React 里才能有足够的信息决定?在 Encode 里没办法确定?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外,你是可以在 Encode 拿到用户的传入参数的,通过:

gnet/connection_unix.go

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() 取出就行了。

Copy link
Contributor Author

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处理的。

Copy link
Contributor Author

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

README.md Outdated Show resolved Hide resolved
README_ZH.md Outdated Show resolved Hide resolved
README_ZH.md Outdated Show resolved Hide resolved
@panjf2000 panjf2000 changed the title feature:增加自定义协议的codec示例 #89 doc: add a customized codec example May 18, 2020
@panjf2000 panjf2000 added pending development Requested PR owner to improve code and waiting for the result proposal Proposal for this repo waiting for response waiting for the response from commenter labels May 18, 2020
@panjf2000 panjf2000 assigned theoneLee and unassigned theoneLee May 18, 2020
@theoneLee theoneLee requested a review from panjf2000 May 18, 2020 14:21
Copy link
Owner

@panjf2000 panjf2000 left a 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么没有改?Pack 方法不能移到 codec 的 Encode 方法里吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@panjf2000 同上。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

examples/custom_codec/protocol/pb.go Outdated Show resolved Hide resolved
@theoneLee theoneLee requested a review from panjf2000 May 18, 2020 15:29
Copy link
Owner

@panjf2000 panjf2000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码我看着没啥问题了,最后确认下,修改完的代码你做过端到端 client -> server 的测试了吗?跑通了吗?

@theoneLee
Copy link
Contributor Author

恩。可以正常运作后才提交的。

Copy link
Owner

@panjf2000 panjf2000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, approved it!

@panjf2000 panjf2000 added pending merged This PR has been reviewed and approved and removed pending development Requested PR owner to improve code and waiting for the result proposal Proposal for this repo waiting for response waiting for the response from commenter labels May 18, 2020
@panjf2000 panjf2000 merged commit 0e072b3 into panjf2000:master May 18, 2020
0-haha pushed a commit to 0-haha/gnet that referenced this pull request Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending merged This PR has been reviewed and approved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

增加自定义协议的codec示例
2 participants