From d9ca9a8c75b2e3a24a6d4635c0aaf7585f12b9a8 Mon Sep 17 00:00:00 2001 From: Pavel Merzlyakov Date: Wed, 7 Jun 2023 12:28:27 +0300 Subject: [PATCH] rpc improvements, fix reading table_json_kv structs --- tpl/codegen_bundle.twig | 84 +++++++++++++++++++++++++++++++++++++++-- tpl/macros_rpc.twig | 41 ++++++++++++++------ tpl/macros_struct.twig | 29 +++++++++++--- 3 files changed, 134 insertions(+), 20 deletions(-) diff --git a/tpl/codegen_bundle.twig b/tpl/codegen_bundle.twig index 069a09c..ab0c9e4 100644 --- a/tpl/codegen_bundle.twig +++ b/tpl/codegen_bundle.twig @@ -4,15 +4,26 @@ package {{ package }} import ( "context" "database/sql" + "database/sql/driver" + "encoding/json" + "net" "sort" + "strconv" + "strings" "git.bit5.ru/backend/meta" + "git.bit5.ru/backend/versioning" "git.bit5.ru/gomodules/metadb" "github.com/doug-martin/goqu/v9" "github.com/doug-martin/goqu/v9/exp" "github.com/pkg/errors" ) +var _ = driver.IsScanValue +var _ = json.Marshal +var _ = strconv.Atoi +var _ = strings.Clone + {% import "macros_enum.twig" as enum_macros %} {% import "macros_struct.twig" as struct_macros %} {% import "macros_rpc.twig" as rpc_macros %} @@ -60,7 +71,7 @@ func CreateRPC(code uint32) (Rpc, error) { {%~ for u in rpcs %} {%- set name = u.object.name|lower|camel|replace({'Rpc': ''}) ~ 'Rpc' %} case {{ u.object.code }}: - return &{{ name }}{}, nil + return New{{ name }}(), nil {%~ endfor %} default: return nil, errors.Errorf("Can't find rpc for code %d", code) @@ -70,7 +81,7 @@ func CreateRPC(code uint32) (Rpc, error) { type RpcHandler interface { {%~ for u in rpcs %} {%- set name = u.object.name|lower|camel|replace({'Rpc': ''}) %} - {{ name }}({{ name ~ 'Rpc' }}) error + {{ name }}(RpcContext, {{ name ~ 'Rpc' }}) error {%~ endfor %} } @@ -81,10 +92,77 @@ type Rpc interface { Error() (int32, string) SetError(int32, string) - Execute(RpcHandler) error + Request() meta.Readable + + Execute(RpcContext, RpcHandler) error ReadRequest(meta.Reader) error WriteResponse(meta.Writer) error } type CreateRpcFunc func(uint32) (Rpc, error) + +type RpcContext struct { + ctx context.Context + ip net.IP + version versioning.Version + platform uint8 + extras map[string]string + + playerId uint32 + extId string +} + +func NewRpcContext( + ctx context.Context, + ip net.IP, + version versioning.Version, + platform uint8, + extras map[string]string, +) RpcContext { + return RpcContext{ + ctx: ctx, + ip: ip, + version: version, + platform: platform, + extras: extras, + } +} + +func (rctx RpcContext) Context() context.Context { + return rctx.ctx +} + +func (rctx RpcContext) IP() net.IP { + return rctx.ip +} + +func (rctx RpcContext) Version() versioning.Version { + return rctx.version +} + +func (rctx RpcContext) Platform() uint8 { + return rctx.platform +} + +func (rctx RpcContext) ExtraValue(key string) (string, bool) { + if rctx.extras == nil { + return "", false + } + + v, ok := rctx.extras[key] + return v, ok +} + +func (rctx *RpcContext) SetPlayer(playerId uint32, extId string) { + rctx.playerId = playerId + rctx.extId = extId +} + +func (rctx RpcContext) PlayerId() uint32 { + return rctx.playerId +} + +func (rctx RpcContext) ExtId() string { + return rctx.extId +} diff --git a/tpl/macros_rpc.twig b/tpl/macros_rpc.twig index 20df9d8..ab7f017 100644 --- a/tpl/macros_rpc.twig +++ b/tpl/macros_rpc.twig @@ -7,13 +7,20 @@ {% set rsp_name = prefix ~ 'Response' %} type {{ name }} struct { - req {{ req_name }} - rsp {{ rsp_name }} + Req *{{ req_name }} + Rsp *{{ rsp_name }} errCode int32 errMsg string } +func New{{ name }}() *{{ name }} { + rpc := {{ name }}{} + rpc.Req = New{{ req_name }}() + rpc.Rsp = New{{ rsp_name }}() + return &rpc +} + func (rpc {{ name }}) Name() string { return "{{ name }}" } @@ -31,24 +38,20 @@ func (rpc {{ name }}) Error() (int32, string) { return rpc.errCode, rpc.errMsg } -func (rpc {{ name }}) Execute(h RpcHandler) error { - return h.{{ prefix }}(rpc) +func (rpc {{ name }}) Execute(ctx RpcContext, h RpcHandler) error { + return h.{{ prefix }}(ctx, rpc) } -func (rpc {{ name }}) Request() {{ req_name }} { - return rpc.req -} - -func (rpc *{{ name }}) SetResponse(rsp {{ rsp_name }}) { - rpc.rsp = rsp +func (rpc {{ name }}) Request() meta.Readable { + return rpc.Req } func (rpc *{{ name }}) ReadRequest(reader meta.Reader) error { - return rpc.req.Read(reader) + return rpc.Req.Read(reader) } func (rpc *{{ name }}) WriteResponse(writer meta.Writer) error { - return rpc.rsp.Write(writer) + return rpc.Rsp.Write(writer) } type {{ req_name }} struct { @@ -57,7 +60,14 @@ type {{ req_name }} struct { {%~ endfor %} } +func New{{ req_name }}() *{{ req_name }} { + req := new({{ req_name }}) + req.Reset() + return req +} + {{ struct_macros.struct_required_fields(req_name, r.req.fields)}} +{{ struct_macros.reset_method(r.req, req_name) }} {{ struct_macros.meta_read(r.req, req_name) }} type {{ rsp_name }} struct { @@ -66,5 +76,12 @@ type {{ rsp_name }} struct { {%~ endfor %} } +func New{{ rsp_name }}() *{{ rsp_name }} { + rsp := new({{ rsp_name }}) + rsp.Reset() + return rsp +} + +{{ struct_macros.reset_method(r.rsp, rsp_name) }} {{ struct_macros.meta_write(r.rsp, rsp_name) }} {% endmacro rpc %} diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index 8a9c5b6..61476c5 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -51,16 +51,25 @@ type {{ json_fields_type }} struct{ {%~ endfor %} changedFields meta.ChangedFields + + {% if has_token(s, 'bitfields') %} + fieldsMask meta.FieldsMask + {% endif %} } {{ _self.json_sql_impl(json_fields_type) }} {{ _self.struct_methods(s.name, s.classid) }} {{ _self.struct_required_fields(s.name, s.fields) }} +{{ _self.reset_method(s) }} {{ _self.meta_read(s) }} {{ _self.meta_write(s) }} {# {{ _self.struct_identifier(s.name, pkey) }} #} +{% if has_token(s, 'bitfields') %} + {{ _self.bitfields_methods(s.name) }} +{% endif %} + {{ _self.table_save(_context) }} {{ _self.table_load(_context) }} {{ _self.table_delete(_context) }} @@ -547,11 +556,7 @@ func (v {{ type_name }}) WriteJsonSetExpr(b *strings.Builder) error { {% endmacro json_set_expr %} -{% macro meta_read(o, name) %} -{% set fields_len = o.fields|length %} -{% set opt_fields_len = count_optional(o.fields) %} -{% set all_fields = get_all_fields(o) %} - +{% macro reset_method(o, name) %} func (s *{{ name|default(o.name) }}) Reset() { {% if o.parent %} s.{{o.parent.name}}.Reset() @@ -580,6 +585,13 @@ func (s *{{ name|default(o.name) }}) Reset() { s.changedFields.Reset() {% endif %} } +{% endmacro reset_method %} + + +{% macro meta_read(o, name) %} +{% set fields_len = o.fields|length %} +{% set opt_fields_len = count_optional(o.fields) %} +{% set all_fields = get_all_fields(o) %} func (s *{{ name|default(o.name) }}) Read(reader meta.Reader) error { if err := reader.BeginContainer(""); err != nil { @@ -677,10 +689,16 @@ func (s *{{ name|default(o.name) }}) readFields(reader meta.Reader) error { } if !use_mask || (use_mask && s.FieldChanged({{ loop.index0 }})) { {{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, has_token(f, 'optional')) }} + {% if has_token(o, 'table') %} + s.changedFields.SetChanged("{{ f|alias }}") + {% endif %} } {% else %} contSize-- {{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, has_token(f, 'optional')) }} + {% if has_token(o, 'table') %} + s.changedFields.SetChanged("{{ f|alias }}") + {% endif %} {% endif %} {%- endfor -%} @@ -845,6 +863,7 @@ type {{ s.name }} struct { {{ _self.struct_methods(s.name, s.classid) }} {{ _self.struct_required_fields(s.name, s.fields) }} +{{ _self.reset_method(s) }} {{ _self.meta_read(s) }} {{ _self.meta_write(s) }}