Browse Source

Optimize codec code

tags/v0.1.0-rc1
liuyuecai 3 years ago
parent
commit
2695bf7f45
22 changed files with 649 additions and 1358 deletions
  1. +1
    -1
      go.mod
  2. +41
    -4
      go.sum
  3. +0
    -577
      pkg/common/binary/buf.go
  4. +0
    -90
      pkg/common/binary/mem_pool.go
  5. +0
    -54
      pkg/common/binary/pool.go
  6. +228
    -0
      pkg/common/bytes/buf.go
  7. +149
    -0
      pkg/common/bytes/buf_helper.go
  8. +17
    -34
      pkg/protocol/codec/branch_commit_req_codec.go
  9. +8
    -16
      pkg/protocol/codec/branch_commit_response_codec.go
  10. +17
    -38
      pkg/protocol/codec/branch_register_req_codec.go
  11. +20
    -29
      pkg/protocol/codec/branch_register_response_codec.go
  12. +17
    -34
      pkg/protocol/codec/branch_rollback_req_codec.go
  13. +13
    -21
      pkg/protocol/codec/branch_rollback_response_codec.go
  14. +0
    -202
      pkg/protocol/codec/codec_helper.go
  15. +13
    -24
      pkg/protocol/codec/common_global_end_request_codec.go
  16. +20
    -31
      pkg/protocol/codec/common_global_end_response_codec.go
  17. +15
    -51
      pkg/protocol/codec/common_identify_request_codec.go
  18. +12
    -18
      pkg/protocol/codec/common_identify_response_codec.go
  19. +13
    -18
      pkg/protocol/codec/global_begin_request_codec.go
  20. +22
    -41
      pkg/protocol/codec/global_begin_response_codec.go
  21. +16
    -41
      pkg/protocol/codec/register_rm_request_codec.go
  22. +27
    -34
      pkg/remoting/getty/readwriter.go

+ 1
- 1
go.mod View File

@@ -5,7 +5,7 @@ go 1.16
require (
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/apache/dubbo-getty v1.4.8
github.com/dubbogo/gost v1.11.23
github.com/dubbogo/gost v1.12.3
github.com/golang/snappy v0.0.4 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible


+ 41
- 4
go.sum View File

@@ -19,7 +19,6 @@ github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
@@ -85,8 +84,9 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dubbogo/go-zookeeper v1.0.4-0.20211212162352-f9d2183d89d5/go.mod h1:fn6n2CAEer3novYgk9ULLwAjuV8/g4DdC2ENwRb6E+c=
github.com/dubbogo/gost v1.11.23 h1:5A7N1jJ5oApcxJwjh4FkhN9iqsx8ddFWTakwCUlNJMw=
github.com/dubbogo/gost v1.11.23/go.mod h1:PhJ8+qZJx+Txjx1KthNPuVkCvUca0jRLgKWj/noGgeI=
github.com/dubbogo/gost v1.12.3 h1:0S9k4W+hM8Y7hizBHxO9urKVoAXSSBIqYNaSf1/rdO0=
github.com/dubbogo/gost v1.12.3/go.mod h1:9sQVp3l/mHfrRBOfH9+/SVTSLHrKAB4y7qs7lxmGX1o=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
@@ -118,8 +118,9 @@ github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgO
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
@@ -162,6 +163,9 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
@@ -252,6 +256,8 @@ github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f/go.mod
github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042/go.mod h1:TPpsiPUEh0zFL1Snz4crhMlBe60PYxRHr5oFF3rRYg0=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
@@ -323,6 +329,8 @@ github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6J
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
@@ -366,6 +374,8 @@ github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shirou/gopsutil v3.20.11+incompatible h1:LJr4ZQK4mPpIV5gOa4jCOKOGb4ty4DZO54I4FGqIpto=
github.com/shirou/gopsutil v3.20.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil/v3 v3.22.2 h1:wCrArWFkHYIdDxx/FSfF5RB4dpJYW6t7rcp3+zL8uks=
github.com/shirou/gopsutil/v3 v3.22.2/go.mod h1:WapW1AOOPlHyXr+yOyw3uYx36enocrtSoSBy0L5vUHY=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
@@ -400,6 +410,12 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tebeka/strftime v0.1.3/go.mod h1:7wJm3dZlpr4l/oVK0t1HYIc4rMzQ2XJlOMIUJUJH6XQ=
github.com/tklauser/go-sysconf v0.3.9/go.mod h1:11DU/5sG7UexIrp/O6g35hrWzu0JxlwQ3LSFUzyeuhs=
github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw=
github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk=
github.com/tklauser/numcpus v0.3.0/go.mod h1:yFGUr7TUHQRAhyqBcEg0Ge34zDBAsIvJJcyE6boqnA8=
github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//o=
github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@@ -411,6 +427,9 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
@@ -456,6 +475,7 @@ golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -481,6 +501,8 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -506,7 +528,9 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -538,6 +562,7 @@ golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -553,18 +578,27 @@ golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201223074533-0d417f636930/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 h1:kwrAHlwJ0DUBZwQ238v+Uod/3eZ8B2K5rYsUHBQvzmI=
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -601,9 +635,12 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20201014170642-d1624618ad65/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY=
golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=


+ 0
- 577
pkg/common/binary/buf.go View File

@@ -1,577 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package binary

import (
"encoding/binary"
"errors"
"io"
"reflect"
"unsafe"
)

const (
minScale = 128
)

//ReadN read n bytes from a reader
func ReadN(r io.Reader, n int) ([]byte, error) {
data := make([]byte, n)
_, err := r.Read(data)

if err != nil {
return nil, err
}

return data, nil
}

//ReadInt read a int value from a reader
func ReadInt(r io.Reader) (int, error) {
data, err := ReadN(r, 4)

if err != nil {
return 0, err
}

return Byte2Int(data), nil
}

// Byte2Int byte array to int value using big order
func Byte2Int(data []byte) int {
return int((int(data[0])&0xff)<<24 | (int(data[1])&0xff)<<16 | (int(data[2])&0xff)<<8 | (int(data[3]) & 0xff))
}

// Byte2Int64 byte array to int64 value using big order
func Byte2Int64(data []byte) int64 {
return int64((int64(data[0])&0xff)<<56 | (int64(data[1])&0xff)<<48 | (int64(data[2])&0xff)<<40 | (int64(data[3])&0xff)<<32 | (int64(data[4])&0xff)<<24 | (int64(data[5])&0xff)<<16 | (int64(data[6])&0xff)<<8 | (int64(data[7]) & 0xff))
}

// Byte2UInt64 byte array to int64 value using big order
func Byte2UInt64(data []byte) uint64 {
return binary.BigEndian.Uint64(data)
}

// Byte2UInt16 byte array to uint16 value using big order
func Byte2UInt16(data []byte) uint16 {
return binary.BigEndian.Uint16(data)
}

// Byte2UInt32 byte array to uint32 value using big order
func Byte2UInt32(data []byte) uint32 {
return binary.BigEndian.Uint32(data)
}

// Int2BytesTo int value to bytes array using big order
func Int2BytesTo(v int, ret []byte) {
ret[0] = byte(v >> 24)
ret[1] = byte(v >> 16)
ret[2] = byte(v >> 8)
ret[3] = byte(v)
}

// Int2Bytes int value to bytes array using big order
func Int2Bytes(v int) []byte {
ret := make([]byte, 4)
Int2BytesTo(v, ret)
return ret
}

// Int64ToBytesTo int64 value to bytes array using big order
func Int64ToBytesTo(v int64, ret []byte) {
ret[0] = byte(v >> 56)
ret[1] = byte(v >> 48)
ret[2] = byte(v >> 40)
ret[3] = byte(v >> 32)
ret[4] = byte(v >> 24)
ret[5] = byte(v >> 16)
ret[6] = byte(v >> 8)
ret[7] = byte(v)
}

// Uint64ToBytesTo uint64 value to bytes array using big order
func Uint64ToBytesTo(v uint64, ret []byte) {
binary.BigEndian.PutUint64(ret, v)
}

// Int64ToBytes int64 value to bytes array using big order
func Int64ToBytes(v int64) []byte {
ret := make([]byte, 8)
Int64ToBytesTo(v, ret)
return ret
}

// Uint32ToBytesTo uint32 value to bytes array using big order
func Uint32ToBytesTo(v uint32, ret []byte) {
binary.BigEndian.PutUint32(ret, v)
}

// UInt32ToBytes uint32 value to bytes array using big order
func UInt32ToBytes(v uint32) []byte {
ret := make([]byte, 4)
Uint32ToBytesTo(v, ret)
return ret
}

// Uint16ToBytesTo uint16 value to bytes array using big order
func Uint16ToBytesTo(v uint16, ret []byte) {
binary.BigEndian.PutUint16(ret, v)
}

// UInt16ToBytes uint16 value to bytes array using big order
func UInt16ToBytes(v uint16) []byte {
ret := make([]byte, 2)
Uint16ToBytesTo(v, ret)
return ret
}

// ByteBuf a buf with byte arrays
//
// | discardable bytes | readable bytes | writeable bytes |
// | | | |
// | | | |
// 0 <= readerIndex <= writerIndex <= capacity
//
type ByteBuf struct {
capacity int
pool Pool
buf []byte // buf data, auto +/- size
readerIndex int //
writerIndex int //
markedIndex int
}

// ErrTooLarge too larger error
var ErrTooLarge = errors.New("goetty.ByteBuf: too large")

// NewByteBuf create a new bytebuf
func NewByteBuf(capacity int) *ByteBuf {
return NewByteBufPool(capacity, getDefaultMP())
}

// NewByteBufPool create a new bytebuf using a mem pool
func NewByteBufPool(capacity int, pool Pool) *ByteBuf {
return &ByteBuf{
capacity: capacity,
buf: pool.Alloc(capacity),
readerIndex: 0,
writerIndex: 0,
pool: pool,
}
}

// RawBuf get the raw byte array
func (b *ByteBuf) RawBuf() []byte {
return b.buf
}

// Clear reset the write and read index
func (b *ByteBuf) Clear() {
b.readerIndex = 0
b.writerIndex = 0
b.markedIndex = 0
}

// Release release buf
func (b *ByteBuf) Release() {
b.pool.Free(b.buf)
b.buf = nil
}

// Resume resume the buf
func (b *ByteBuf) Resume(capacity int) {
b.buf = b.pool.Alloc(b.capacity)
}

// Capacity get the capacity
func (b *ByteBuf) Capacity() int {
return len(b.buf) // use len to avoid slice scale
}

// SetReaderIndex set the read index
func (b *ByteBuf) SetReaderIndex(newReaderIndex int) error {
if newReaderIndex < 0 || newReaderIndex > b.writerIndex {
return io.ErrShortBuffer
}

b.readerIndex = newReaderIndex

return nil
}

// GetReaderIndex get the read index
func (b *ByteBuf) GetReaderIndex() int {
return b.readerIndex
}

// GetWriteIndex get the write index
func (b *ByteBuf) GetWriteIndex() int {
return b.writerIndex
}

// GetMarkerIndex returns markerIndex
func (b *ByteBuf) GetMarkerIndex() int {
return b.markedIndex
}

// GetMarkedRemind returns size in [readerIndex, markedIndex)
func (b *ByteBuf) GetMarkedRemind() int {
return b.markedIndex - b.readerIndex
}

// GetMarkedRemindData returns data in [readerIndex, markedIndex)
func (b *ByteBuf) GetMarkedRemindData() []byte {
return b.buf[b.readerIndex:b.markedIndex]
}

// SetWriterIndex set the write index
func (b *ByteBuf) SetWriterIndex(newWriterIndex int) error {
if newWriterIndex < b.readerIndex || newWriterIndex > b.Capacity() {
return io.ErrShortBuffer
}

b.writerIndex = newWriterIndex

return nil
}

// MarkN mark a index offset based by currently read index
func (b *ByteBuf) MarkN(n int) error {
return b.MarkIndex(b.readerIndex + n)
}

// MarkIndex mark a index
func (b *ByteBuf) MarkIndex(index int) error {
if index > b.Capacity() || index <= b.readerIndex {
return io.ErrShortBuffer
}

b.markedIndex = index
return nil
}

// Skip skip bytes, after this option, read index will change to readerIndex+n
func (b *ByteBuf) Skip(n int) error {
if n > b.Readable() {
return io.ErrShortBuffer
}

b.readerIndex += n
return nil
}

// Readable current readable byte size
func (b *ByteBuf) Readable() int {
return b.writerIndex - b.readerIndex
}

// ReadByte read a byte from buf
// return byte value, error
func (b *ByteBuf) ReadByte() (byte, error) {
if b.Readable() == 0 {
return 0, nil
}

v := b.buf[b.readerIndex]
b.readerIndex++
return v, nil
}

// ReadRawBytes read bytes from buf without mem copy
// Note. If used complete, you must call b.Skip(n) to reset reader index
func (b *ByteBuf) ReadRawBytes(n int) (int, []byte, error) {
if n > b.Readable() {
return 0, nil, nil
}

return n, b.buf[b.readerIndex : b.readerIndex+n], nil
}

// ReadBytes read bytes from buf
// It's will copy the data to a new byte array
// return readedBytesCount, byte array, error
func (b *ByteBuf) ReadBytes(n int) (int, []byte, error) {
data := make([]byte, n)
n, err := b.Read(data)
return n, data, err
}

// ReadAll read all data from buf
// It's will copy the data to a new byte array
// return readedBytesCount, byte array, error
func (b *ByteBuf) ReadAll() (int, []byte, error) {
return b.ReadBytes(b.Readable())
}

// ReadMarkedBytes read data from buf in the range [readerIndex, markedIndex)
func (b *ByteBuf) ReadMarkedBytes() (int, []byte, error) {
return b.ReadBytes(b.GetMarkedRemind())
}

// MarkedBytesReaded reset reader index
func (b *ByteBuf) MarkedBytesReaded() {
b.readerIndex = b.markedIndex
}

// Read read bytes
// return readedBytesCount, byte array, error
func (b *ByteBuf) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}

size := len(p)
if len(p) > b.Readable() {
size = b.Readable()
}

n = copy(p, b.buf[b.readerIndex:b.readerIndex+size])
b.readerIndex += n
return n, nil
}

// ReadInt get int value from buf
func (b *ByteBuf) ReadInt() (int, error) {
if b.Readable() < 4 {
return 0, io.ErrShortBuffer
}

b.readerIndex += 4
return Byte2Int(b.buf[b.readerIndex-4 : b.readerIndex]), nil
}

// ReadUInt16 get uint16 value from buf
func (b *ByteBuf) ReadUInt16() (uint16, error) {
if b.Readable() < 2 {
return 0, io.ErrShortBuffer
}

b.readerIndex += 2
return Byte2UInt16(b.buf[b.readerIndex-2 : b.readerIndex]), nil
}

// ReadUInt32 get uint32 value from buf
func (b *ByteBuf) ReadUInt32() (uint32, error) {
if b.Readable() < 4 {
return 0, io.ErrShortBuffer
}

b.readerIndex += 4
return Byte2UInt32(b.buf[b.readerIndex-4 : b.readerIndex]), nil
}

// ReadInt64 get int64 value from buf
func (b *ByteBuf) ReadInt64() (int64, error) {
if b.Readable() < 8 {
return 0, io.ErrShortBuffer
}

b.readerIndex += 8
return Byte2Int64(b.buf[b.readerIndex-8 : b.readerIndex]), nil
}

// ReadUInt64 get uint64 value from buf
func (b *ByteBuf) ReadUInt64() (uint64, error) {
if b.Readable() < 8 {
return 0, io.ErrShortBuffer
}

b.readerIndex += 8
return Byte2UInt64(b.buf[b.readerIndex-8 : b.readerIndex]), nil
}

// PeekInt get int value from buf based on currently read index, after read, read index not modifed
func (b *ByteBuf) PeekInt(offset int) (int, error) {
if b.Readable() < 4+offset {
return 0, io.ErrShortBuffer
}

start := b.readerIndex + offset
return Byte2Int(b.buf[start : start+4]), nil
}

// PeekByte get byte value from buf based on currently read index, after read, read index not modifed
func (b *ByteBuf) PeekByte(offset int) (byte, error) {
if b.Readable() < offset || offset < 0 {
return 0, io.ErrShortBuffer
}

return b.buf[b.readerIndex+offset], nil
}

// PeekN get bytes from buf based on currently read index, after read, read index not modifed
func (b *ByteBuf) PeekN(offset int, n int) ([]byte, error) {
if b.Readable() < n+offset {
return nil, io.ErrShortBuffer
}

start := b.readerIndex + offset
return b.buf[start : start+n], nil
}

// ReadFrom reads data from r until EOF and appends it to the buffer, growing
// the buffer as needed. The return value n is the number of bytes read. Any
// error except io.EOF encountered during the read is also returned. If the
// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
func (b *ByteBuf) ReadFrom(r io.Reader) (n int64, err error) {
for {
b.Expansion(minScale)
m, e := r.Read(b.buf[b.writerIndex : b.writerIndex+minScale])
if m < 0 {
panic("bug: negative Read")
}

b.writerIndex += m
n += int64(m)
if e == io.EOF {
return n, nil // e is EOF, so return nil explicitly
}
if e != nil {
return n, e
}

if m > 0 {
return n, e
}
}
}

// Writeable return how many bytes can be wirte into buf
func (b *ByteBuf) Writeable() int {
return b.Capacity() - b.writerIndex
}

// Write appends the contents of p to the buffer, growing the buffer as
// needed.
func (b *ByteBuf) Write(p []byte) (int, error) {
n := len(p)
b.Expansion(n)
copy(b.buf[b.writerIndex:], p)
b.writerIndex += n
return n, nil
}

// WriteInt write int value to buf using big order
// return write bytes count, error
func (b *ByteBuf) WriteInt(v int) (n int, err error) {
b.Expansion(4)
Int2BytesTo(v, b.buf[b.writerIndex:b.writerIndex+4])
b.writerIndex += 4
return 4, nil
}

// WriteUInt16 write uint16 value to buf using big order
// return write bytes count, error
func (b *ByteBuf) WriteUInt16(v uint16) (n int, err error) {
b.Expansion(2)
Uint16ToBytesTo(v, b.buf[b.writerIndex:b.writerIndex+2])
b.writerIndex += 2
return 2, nil
}

// WriteUInt32 write uint32 value to buf using big order
// return write bytes count, error
func (b *ByteBuf) WriteUInt32(v uint32) (n int, err error) {
b.Expansion(4)
Uint32ToBytesTo(v, b.buf[b.writerIndex:b.writerIndex+4])
b.writerIndex += 4
return 4, nil
}

// WriteUInt64 write uint64 value to buf using big order
// return write bytes count, error
func (b *ByteBuf) WriteUInt64(v uint64) (n int, err error) {
b.Expansion(8)
Uint64ToBytesTo(v, b.buf[b.writerIndex:b.writerIndex+8])
b.writerIndex += 8
return 8, nil
}

// WriteInt64 write int64 value to buf using big order
// return write bytes count, error
func (b *ByteBuf) WriteInt64(v int64) (n int, err error) {
b.Expansion(8)
Int64ToBytesTo(v, b.buf[b.writerIndex:b.writerIndex+8])
b.writerIndex += 8
return 8, nil
}

// WriteUint64 write uint64 value to buf using big order
// return write bytes count, error
func (b *ByteBuf) WriteUint64(v uint64) (n int, err error) {
b.Expansion(8)
Uint64ToBytesTo(v, b.buf[b.writerIndex:b.writerIndex+8])
b.writerIndex += 8
return 8, nil
}

// WriteByte write a byte value to buf
func (b *ByteBuf) WriteByte(v byte) error {
b.Expansion(1)
b.buf[b.writerIndex] = v
b.writerIndex++
return nil
}

// WriteString write a string value to buf
func (b *ByteBuf) WriteString(v string) error {
_, err := b.Write(StringToSlice(v))
return err
}

// WriteByteBuf write all readable data to this buf
func (b *ByteBuf) WriteByteBuf(from *ByteBuf) error {
size := from.Readable()
b.Expansion(size)
copy(b.buf[b.writerIndex:b.writerIndex+size], from.buf[from.readerIndex:from.writerIndex])
b.writerIndex += size
from.readerIndex = from.writerIndex
return nil
}

// Expansion expansion buf size
func (b *ByteBuf) Expansion(n int) {
if free := b.Writeable(); free < n {
newBuf := b.pool.Alloc(b.Capacity() + n)
offset := b.writerIndex - b.readerIndex
copy(newBuf, b.buf[b.readerIndex:b.writerIndex])
b.readerIndex = 0
b.writerIndex = offset
b.pool.Free(b.buf)
b.buf = newBuf
}
}

// SliceToString slice to string with out data copy
func SliceToString(b []byte) (s string) {
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
pstring.Data = pbytes.Data
pstring.Len = pbytes.Len
return
}

// StringToSlice string to slice with out data copy
func StringToSlice(s string) (b []byte) {
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
pbytes.Data = pstring.Data
pbytes.Len = pstring.Len
pbytes.Cap = pstring.Len
return
}

+ 0
- 90
pkg/common/binary/mem_pool.go View File

@@ -1,90 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package binary

import "sync"

// Note. This implemention is copy of https://github.com/funny/slab for no dependent
// Acknowledgments https://github.com/idada

// Pool a mem pool interface
type Pool interface {
Alloc(int) []byte
Free([]byte)
}

// SyncPool is a sync.Pool base slab allocation memory pool
type SyncPool struct {
classes []sync.Pool
classesSize []int
minSize int
maxSize int
}

// NewSyncPool create a sync.Pool base slab allocation memory pool.
// minSize is the smallest chunk size.
// maxSize is the lagest chunk size.
// factor is used to control growth of chunk size.
func NewSyncPool(minSize, maxSize, factor int) *SyncPool {
n := 0
for chunkSize := minSize; chunkSize <= maxSize; chunkSize *= factor {
n++
}
pool := &SyncPool{
make([]sync.Pool, n),
make([]int, n),
minSize, maxSize,
}
n = 0
for chunkSize := minSize; chunkSize <= maxSize; chunkSize *= factor {
pool.classesSize[n] = chunkSize
pool.classes[n].New = func(size int) func() interface{} {
return func() interface{} {
buf := make([]byte, size)
return &buf
}
}(chunkSize)
n++
}
return pool
}

// Alloc try alloc a []byte from internal slab class if no free chunk in slab class Alloc will make one.
func (pool *SyncPool) Alloc(size int) []byte {
if size <= pool.maxSize {
for i := 0; i < len(pool.classesSize); i++ {
if pool.classesSize[i] >= size {
mem := pool.classes[i].Get().(*[]byte)
return (*mem)[:size]
}
}
}
return make([]byte, size)
}

// Free release a []byte that alloc from Pool.Alloc.
func (pool *SyncPool) Free(mem []byte) {
if size := cap(mem); size <= pool.maxSize {
for i := 0; i < len(pool.classesSize); i++ {
if pool.classesSize[i] >= size {
pool.classes[i].Put(&mem)
return
}
}
}
}

+ 0
- 54
pkg/common/binary/pool.go View File

@@ -1,54 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package binary

import (
"sync"
)

const (
// KB kb
KB = 1024
// MB mb
MB = 1024 * 1024
)

var (
lock sync.Mutex
mp Pool
defaultMin = 256
defaultMax = 8 * MB
)

func getDefaultMP() Pool {
lock.Lock()
if mp == nil {
useDefaultMemPool()
}
lock.Unlock()

return mp
}

func useDefaultMemPool() {
mp = NewSyncPool(
defaultMin,
defaultMax,
2,
)
}

+ 228
- 0
pkg/common/bytes/buf.go View File

@@ -0,0 +1,228 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bytes

import (
"encoding/binary"
"io"

gxbytes "github.com/dubbogo/gost/bytes"
)

type ByteBuffer struct {
buf *gxbytes.Buffer
}

func NewByteBuffer(buf []byte) *ByteBuffer {
return &ByteBuffer{
buf: gxbytes.NewBuffer(buf),
}
}

func (b *ByteBuffer) Bytes() []byte {
return b.buf.Bytes()
}

func (b *ByteBuffer) Read(p []byte) (n int, err error) {
return b.buf.Read(p)
}

func (b *ByteBuffer) ReadByte() (byte, error) {
data := make([]byte, 1)
n, err := b.Read(data)
if err != nil {
return 0, err
}
if n < 1 {
return 0, io.ErrShortBuffer
}
return data[0], nil
}

func (b *ByteBuffer) ReadInt64() (int64, error) {
data := make([]byte, 8)
n, err := b.Read(data)
if err != nil {
return 0, err
}
if n < 8 {
return 0, io.ErrShortBuffer
}
return Byte2Int64(data), nil
}

func (b *ByteBuffer) ReadUint16() (uint16, error) {
data := make([]byte, 2)
n, err := b.Read(data)
if err != nil {
return 0, err
}
if n < 2 {
return 0, io.ErrShortBuffer
}
return Byte2UInt16(data), nil
}

func (b *ByteBuffer) ReadUint32() (uint32, error) {
data := make([]byte, 4)
n, err := b.Read(data)
if err != nil {
return 0, err
}
if n < 2 {
return 0, io.ErrShortBuffer
}
return Byte2UInt32(data), nil
}

func (b *ByteBuffer) ReadUint64() (uint64, error) {
data := make([]byte, 8)
n, err := b.Read(data)
if err != nil {
return 0, err
}
if n < 2 {
return 0, io.ErrShortBuffer
}
return Byte2UInt64(data), nil
}

func (b *ByteBuffer) Write(p []byte) (n int, err error) {
return b.buf.Write(p)
}

func (b *ByteBuffer) WriteString(str string) (n int, err error) {
return b.buf.Write([]byte(str))
}

func (b *ByteBuffer) WriteByte(p byte) error {
return b.buf.WriteByte(p)
}

func (b *ByteBuffer) WriteUint16(p uint16) (n int, err error) {
return b.buf.Write(UInt16ToBytes(p))
}

func (b *ByteBuffer) WriteUint32(p uint32) (n int, err error) {
return b.buf.Write(UInt32ToBytes(p))
}

func (b *ByteBuffer) WriteUint64(p uint64) (n int, err error) {
return b.buf.Write(UInt64ToBytes(p))
}

func (b *ByteBuffer) WriteInt64(p int64) (n int, err error) {
return b.buf.Write(Int64ToBytes(p))
}

// Byte2Int64 byte array to int64 value using big order
func Byte2Int64(data []byte) int64 {
return int64((int64(data[0])&0xff)<<56 |
(int64(data[1])&0xff)<<48 |
(int64(data[2])&0xff)<<40 |
(int64(data[3])&0xff)<<32 |
(int64(data[4])&0xff)<<24 |
(int64(data[5])&0xff)<<16 |
(int64(data[6])&0xff)<<8 |
(int64(data[7]) & 0xff))
}

// Byte2UInt64 byte array to int64 value using big order
func Byte2UInt64(data []byte) uint64 {
return binary.BigEndian.Uint64(data)
}

// Byte2UInt16 byte array to uint16 value using big order
func Byte2UInt16(data []byte) uint16 {
return binary.BigEndian.Uint16(data)
}

// Byte2UInt32 byte array to uint32 value using big order
func Byte2UInt32(data []byte) uint32 {
return binary.BigEndian.Uint32(data)
}

// Int2BytesTo int value to bytes array using big order
func Int2BytesTo(v int, ret []byte) {
ret[0] = byte(v >> 24)
ret[1] = byte(v >> 16)
ret[2] = byte(v >> 8)
ret[3] = byte(v)
}

// Int2Bytes int value to bytes array using big order
func Int2Bytes(v int) []byte {
ret := make([]byte, 4)
Int2BytesTo(v, ret)
return ret
}

// Int64ToBytesTo int64 value to bytes array using big order
func Int64ToBytesTo(v int64, ret []byte) {
ret[0] = byte(v >> 56)
ret[1] = byte(v >> 48)
ret[2] = byte(v >> 40)
ret[3] = byte(v >> 32)
ret[4] = byte(v >> 24)
ret[5] = byte(v >> 16)
ret[6] = byte(v >> 8)
ret[7] = byte(v)
}

// Uint64ToBytesTo uint64 value to bytes array using big order
func Uint64ToBytesTo(v uint64, ret []byte) {
binary.BigEndian.PutUint64(ret, v)
}

// Int64ToBytes int64 value to bytes array using big order
func Int64ToBytes(v int64) []byte {
ret := make([]byte, 8)
Int64ToBytesTo(v, ret)
return ret
}

// Uint32ToBytesTo uint32 value to bytes array using big order
func Uint32ToBytesTo(v uint32, ret []byte) {
binary.BigEndian.PutUint32(ret, v)
}

// UInt32ToBytes uint32 value to bytes array using big order
func UInt32ToBytes(v uint32) []byte {
ret := make([]byte, 4)
Uint32ToBytesTo(v, ret)
return ret
}

// Uint16ToBytesTo uint16 value to bytes array using big order
func Uint16ToBytesTo(v uint16, ret []byte) {
binary.BigEndian.PutUint16(ret, v)
}

// UInt16ToBytes uint16 value to bytes array using big order
func UInt16ToBytes(v uint16) []byte {
ret := make([]byte, 2)
Uint16ToBytesTo(v, ret)
return ret
}

// UInt16ToBytes uint16 value to bytes array using big order
func UInt64ToBytes(v uint64) []byte {
ret := make([]byte, 8)
Uint64ToBytesTo(v, ret)
return ret
}

+ 149
- 0
pkg/common/bytes/buf_helper.go View File

@@ -0,0 +1,149 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bytes

func ReadBytes(n int, buf *ByteBuffer) []byte {
bytes := make([]byte, n)
buf.Read(bytes)
return bytes
}

func ReadByte(buf *ByteBuffer) byte {
value, _ := buf.ReadByte()
return value
}

func ReadUint8(buf *ByteBuffer) uint8 {
value, _ := buf.ReadByte()
return value
}

func ReadUInt16(buf *ByteBuffer) uint16 {
value, _ := buf.ReadUint16()
return value
}

func ReadUInt32(buf *ByteBuffer) uint32 {
value, _ := buf.ReadUint32()
return value
}

func ReadUInt64(buf *ByteBuffer) uint64 {
value, _ := buf.ReadUint64()
return value
}

func ReadString8(buf *ByteBuffer) string {
bytes := make([]byte, 1)
buf.Read(bytes)
return string(bytes)
}

func Read1String16(buf *ByteBuffer) string {
bytes := make([]byte, 2)
buf.Read(bytes)
return string(bytes)
}

func ReadString32(buf *ByteBuffer) string {
bytes := make([]byte, 4)
buf.Read(bytes)
return string(bytes)
}

func ReadString64(buf *ByteBuffer) string {
bytes := make([]byte, 8)
buf.Read(bytes)
return string(bytes)
}

func ReadString8Length(buf *ByteBuffer) string {
length, _ := buf.ReadByte()
if length > 0 {
p := make([]byte, length)
buf.Read(p)
return string(p)
}
return ""
}

func ReadString16Length(buf *ByteBuffer) string {
length, _ := buf.ReadUint16()
if length > 0 {
p := make([]byte, length)
buf.Read(p)
return string(p)
}
return ""
}

func ReadString32Length(buf *ByteBuffer) string {
length, _ := buf.ReadUint32()
if length > 0 {
p := make([]byte, length)
buf.Read(p)
return string(p)
}
return ""
}

func ReadString64Length(buf *ByteBuffer) string {
length, _ := buf.ReadUint64()
if length > 0 {
p := make([]byte, length)
buf.Read(p)
return string(p)
}
return ""
}

func WriteString8Length(value string, buf *ByteBuffer) {
if value != "" {
buf.WriteByte(byte(len(value)))
buf.WriteString(value)
} else {
buf.WriteByte(byte(0))
}
}

func WriteString16Length(value string, buf *ByteBuffer) {
if value != "" {
buf.WriteUint16(uint16(len(value)))
buf.WriteString(value)
} else {
buf.WriteUint16(uint16(0))
}
}

func WriteString32Length(value string, buf *ByteBuffer) {
if value != "" {
buf.WriteUint32(uint32(len(value)))
buf.WriteString(value)
} else {
buf.WriteUint32(uint32(0))
}
}

func WriteString64Length(value string, buf *ByteBuffer) {
if value != "" {
buf.WriteUint64(uint64(len(value)))
buf.WriteString(value)
} else {
buf.WriteUint64(uint64(0))
}
}

+ 17
- 34
pkg/protocol/codec/branch_commit_req_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
"github.com/seata/seata-go/pkg/protocol/branch"
"github.com/seata/seata-go/pkg/protocol/message"
)
@@ -31,46 +31,29 @@ type BranchCommitRequestCodec struct {
}

func (g *BranchCommitRequestCodec) Decode(in []byte) interface{} {
data := message.BranchCommitRequest{}
buf := bytes.NewByteBuffer(in)

res := message.BranchCommitRequest{}
data.Xid = bytes.ReadString16Length(buf)
data.BranchId = int64(bytes.ReadUInt64(buf))
data.BranchType = branch.BranchType(bytes.ReadByte(buf))
data.ResourceId = bytes.ReadString16Length(buf)
data.ApplicationData = []byte(bytes.ReadString32Length(buf))

buf := binary.NewByteBuf(len(in))
buf.Write(in)
var length uint32

length = uint32(ReadUInt16(buf))
if length > 0 {
bytes := make([]byte, length)
res.Xid = string(Read(buf, bytes))
}
res.BranchId = int64(ReadUInt64(buf))
res.BranchType = branch.BranchType(ReadByte(buf))

length = uint32(ReadUInt16(buf))
if length > 0 {
bytes := make([]byte, length)
res.ResourceId = string(Read(buf, bytes))
}
length = ReadUInt32(buf)
if length > 0 {
bytes := make([]byte, length)
res.ApplicationData = Read(buf, bytes)
}

return res
return data
}

func (g *BranchCommitRequestCodec) Encode(in interface{}) []byte {
req, _ := in.(message.BranchCommitRequest)
buf := binary.NewByteBuf(0)
data, _ := in.(message.BranchCommitRequest)
buf := bytes.NewByteBuffer([]byte{})

Write16String(req.Xid, buf)
buf.WriteInt64(req.BranchId)
buf.WriteByte(byte(req.BranchType))
Write16String(req.ResourceId, buf)
Write32String(string(req.ApplicationData), buf)
bytes.WriteString16Length(data.Xid, buf)
buf.WriteInt64(data.BranchId)
buf.WriteByte(byte(data.BranchType))
bytes.WriteString16Length(data.ResourceId, buf)
bytes.WriteString32Length(string(data.ApplicationData), buf)

return buf.RawBuf()
return buf.Bytes()
}

func (g *BranchCommitRequestCodec) GetMessageType() message.MessageType {


+ 8
- 16
pkg/protocol/codec/branch_commit_response_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
"github.com/seata/seata-go/pkg/protocol/branch"
"github.com/seata/seata-go/pkg/protocol/message"
)
@@ -31,33 +31,25 @@ type BranchCommitResponseCodec struct {
}

func (g *BranchCommitResponseCodec) Decode(in []byte) interface{} {

res := message.BranchCommitResponse{}
buf := bytes.NewByteBuffer(in)

buf := binary.NewByteBuf(len(in))
buf.Write(in)
var length uint16

length = ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
res.Xid = string(Read(buf, bytes))
}
res.BranchId = int64(ReadUInt64(buf))
res.BranchStatus = branch.BranchStatus(ReadByte(buf))
res.Xid = bytes.ReadString16Length(buf)
res.BranchId = int64(bytes.ReadUInt64(buf))
res.BranchStatus = branch.BranchStatus(bytes.ReadByte(buf))

return res
}

func (g *BranchCommitResponseCodec) Encode(in interface{}) []byte {
req, _ := in.(message.BranchCommitResponse)
buf := binary.NewByteBuf(0)
buf := bytes.NewByteBuffer([]byte{})

Write16String(req.Xid, buf)
bytes.WriteString16Length(req.Xid, buf)
buf.WriteInt64(req.BranchId)
buf.WriteByte(byte(req.BranchStatus))

return buf.RawBuf()
return buf.Bytes()
}

func (g *BranchCommitResponseCodec) GetMessageType() message.MessageType {


+ 17
- 38
pkg/protocol/codec/branch_register_req_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"

model2 "github.com/seata/seata-go/pkg/protocol/branch"
"github.com/seata/seata-go/pkg/protocol/message"
@@ -32,50 +32,29 @@ type BranchRegisterRequestCodec struct {
}

func (g *BranchRegisterRequestCodec) Decode(in []byte) interface{} {
buf := binary.NewByteBuf(len(in))
buf.Write(in)
msg := message.BranchRegisterRequest{}
data := message.BranchRegisterRequest{}
buf := bytes.NewByteBuffer(in)

length := ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
msg.Xid = string(Read(buf, bytes))
}
data.Xid = bytes.ReadString16Length(buf)
data.BranchType = model2.BranchType(bytes.ReadByte(buf))
data.ResourceId = bytes.ReadString16Length(buf)
data.LockKey = bytes.ReadString32Length(buf)
data.ApplicationData = []byte(bytes.ReadString32Length(buf))

msg.BranchType = model2.BranchType(ReadByte(buf))

length = ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
msg.ResourceId = string(Read(buf, bytes))
}

length32 := ReadUInt32(buf)
if length > 0 {
bytes := make([]byte, length32)
msg.LockKey = string(Read(buf, bytes))
}

length32 = ReadUInt32(buf)
if length > 0 {
bytes := make([]byte, length32)
msg.ApplicationData = Read(buf, bytes)
}

return msg
return data
}

func (c *BranchRegisterRequestCodec) Encode(in interface{}) []byte {
buf := binary.NewByteBuf(0)
req, _ := in.(message.BranchRegisterRequest)
data, _ := in.(message.BranchRegisterRequest)
buf := bytes.NewByteBuffer([]byte{})

Write16String(req.Xid, buf)
buf.WriteByte(byte(req.BranchType))
Write16String(req.ResourceId, buf)
Write32String(req.LockKey, buf)
Write32String(string(req.ApplicationData), buf)
bytes.WriteString16Length(data.Xid, buf)
buf.WriteByte(byte(data.BranchType))
bytes.WriteString16Length(data.ResourceId, buf)
bytes.WriteString32Length(data.LockKey, buf)
bytes.WriteString32Length(string(data.ApplicationData), buf)

return buf.RawBuf()
return buf.Bytes()
}

func (g *BranchRegisterRequestCodec) GetMessageType() message.MessageType {


+ 20
- 29
pkg/protocol/codec/branch_register_response_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
error2 "github.com/seata/seata-go/pkg/common/error"
"github.com/seata/seata-go/pkg/protocol/message"
)
@@ -31,46 +31,37 @@ type BranchRegisterResponseCodec struct {
}

func (g *BranchRegisterResponseCodec) Decode(in []byte) interface{} {
buf := binary.NewByteBuf(len(in))
buf.Write(in)
msg := message.BranchRegisterResponse{}
data := message.BranchRegisterResponse{}
buf := bytes.NewByteBuffer(in)

resultCode := ReadByte(buf)
msg.ResultCode = message.ResultCode(resultCode)
if msg.ResultCode == message.ResultCodeFailed {
length := ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
msg.Msg = string(Read(buf, bytes))
}
data.ResultCode = message.ResultCode(bytes.ReadByte(buf))
if data.ResultCode == message.ResultCodeFailed {
data.Msg = bytes.ReadString16Length(buf)
}
data.TransactionExceptionCode = error2.TransactionExceptionCode(bytes.ReadByte(buf))
data.BranchId = int64(bytes.ReadUInt64(buf))

exceptionCode := ReadByte(buf)
msg.TransactionExceptionCode = error2.TransactionExceptionCode(exceptionCode)
msg.BranchId = int64(ReadUInt64(buf))

return msg
return data
}

func (c *BranchRegisterResponseCodec) Encode(in interface{}) []byte {
buf := binary.NewByteBuf(0)
resp, _ := in.(message.BranchRegisterResponse)
data, _ := in.(message.BranchRegisterResponse)
buf := bytes.NewByteBuffer([]byte{})

buf.WriteByte(byte(resp.ResultCode))
if resp.ResultCode == message.ResultCodeFailed {
buf.WriteByte(byte(data.ResultCode))
if data.ResultCode == message.ResultCodeFailed {
var msg string
if len(resp.Msg) > 128 {
msg = resp.Msg[:128]
if len(data.Msg) > 128 {
msg = data.Msg[:128]
} else {
msg = resp.Msg
msg = data.Msg
}
Write16String(msg, buf)
bytes.WriteString16Length(msg, buf)
}
buf.WriteByte(byte(data.TransactionExceptionCode))
buf.WriteInt64(data.BranchId)

buf.WriteByte(byte(resp.TransactionExceptionCode))
branchID := uint64(resp.BranchId)
buf.WriteUInt64(branchID)
return buf.RawBuf()
return buf.Bytes()
}

func (g *BranchRegisterResponseCodec) GetMessageType() message.MessageType {


+ 17
- 34
pkg/protocol/codec/branch_rollback_req_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
"github.com/seata/seata-go/pkg/protocol/branch"
"github.com/seata/seata-go/pkg/protocol/message"
)
@@ -31,46 +31,29 @@ type BranchRollbackRequestCodec struct {
}

func (g *BranchRollbackRequestCodec) Decode(in []byte) interface{} {
data := message.BranchRollbackRequest{}
buf := bytes.NewByteBuffer(in)

res := message.BranchRollbackRequest{}
data.Xid = bytes.ReadString16Length(buf)
data.BranchId = int64(bytes.ReadUInt64(buf))
data.BranchType = branch.BranchType(bytes.ReadByte(buf))
data.ResourceId = bytes.ReadString16Length(buf)
data.ApplicationData = []byte(bytes.ReadString32Length(buf))

buf := binary.NewByteBuf(len(in))
buf.Write(in)
var length uint32

length = uint32(ReadUInt16(buf))
if length > 0 {
bytes := make([]byte, length)
res.Xid = string(Read(buf, bytes))
}
res.BranchId = int64(ReadUInt64(buf))
res.BranchType = branch.BranchType(ReadByte(buf))

length = uint32(ReadUInt16(buf))
if length > 0 {
bytes := make([]byte, length)
res.ResourceId = string(Read(buf, bytes))
}
length = ReadUInt32(buf)
if length > 0 {
bytes := make([]byte, length)
res.ApplicationData = Read(buf, bytes)
}

return res
return data
}

func (g *BranchRollbackRequestCodec) Encode(in interface{}) []byte {
req, _ := in.(message.BranchRollbackRequest)
buf := binary.NewByteBuf(0)
data, _ := in.(message.BranchRollbackRequest)
buf := bytes.NewByteBuffer([]byte{})

Write16String(req.Xid, buf)
buf.WriteInt64(req.BranchId)
buf.WriteByte(byte(req.BranchType))
Write16String(req.ResourceId, buf)
Write32String(string(req.ApplicationData), buf)
bytes.WriteString16Length(data.Xid, buf)
buf.WriteInt64(data.BranchId)
buf.WriteByte(byte(data.BranchType))
bytes.WriteString16Length(data.ResourceId, buf)
bytes.WriteString32Length(string(data.ApplicationData), buf)

return buf.RawBuf()
return buf.Bytes()
}

func (g *BranchRollbackRequestCodec) GetMessageType() message.MessageType {


+ 13
- 21
pkg/protocol/codec/branch_rollback_response_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
"github.com/seata/seata-go/pkg/protocol/branch"
"github.com/seata/seata-go/pkg/protocol/message"
)
@@ -31,33 +31,25 @@ type BranchRollbackResponseCodec struct {
}

func (g *BranchRollbackResponseCodec) Decode(in []byte) interface{} {
data := message.BranchRollbackResponse{}
buf := bytes.NewByteBuffer(in)

res := message.BranchRollbackResponse{}
data.Xid = bytes.ReadString16Length(buf)
data.BranchId = int64(bytes.ReadUInt64(buf))
data.BranchStatus = branch.BranchStatus(bytes.ReadByte(buf))

buf := binary.NewByteBuf(len(in))
buf.Write(in)
var length uint16

length = ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
res.Xid = string(Read(buf, bytes))
}
res.BranchId = int64(ReadUInt64(buf))
res.BranchStatus = branch.BranchStatus(ReadByte(buf))

return res
return data
}

func (g *BranchRollbackResponseCodec) Encode(in interface{}) []byte {
req, _ := in.(message.BranchRollbackResponse)
buf := binary.NewByteBuf(0)
data, _ := in.(message.BranchRollbackResponse)
buf := bytes.NewByteBuffer([]byte{})

Write16String(req.Xid, buf)
buf.WriteInt64(req.BranchId)
buf.WriteByte(byte(req.BranchStatus))
bytes.WriteString16Length(data.Xid, buf)
buf.WriteInt64(data.BranchId)
buf.WriteByte(byte(data.BranchStatus))

return buf.RawBuf()
return buf.Bytes()
}

func (g *BranchRollbackResponseCodec) GetMessageType() message.MessageType {


+ 0
- 202
pkg/protocol/codec/codec_helper.go View File

@@ -1,202 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package codec

import "github.com/seata/seata-go/pkg/common/binary"

// Write16String write string value with 16 byte length
func Write16String(value string, buf *binary.ByteBuf) {
if value != "" {
buf.WriteUInt16(uint16(len(value)))
buf.WriteString(value)
} else {
buf.WriteUInt16(uint16(0))
}
}

// Write16String write string value with 16 byte length
func Write32String(value string, buf *binary.ByteBuf) {
if value != "" {
buf.WriteUInt32(uint32(len(value)))
buf.WriteString(value)
} else {
buf.WriteUInt32(uint32(0))
}
}

// Write8String write string value with 8 byte length
func Write8String(value string, buf *binary.ByteBuf) {
if value != "" {
buf.WriteByte(uint8(len(value)))
buf.WriteString(value)
} else {
buf.WriteByte(uint8(0))
}
}

// ReadString read string value
func ReadString(buf *binary.ByteBuf) string {
size := ReadUInt16(buf)
if size == 0 {
return ""
}

_, value, _ := buf.ReadBytes(int(size))
return binary.SliceToString(value)
}

// MaybeReadString maybe read string value
func MaybeReadString(buf *binary.ByteBuf) (string, bool) {
if buf.Readable() < 2 {
return "", false
}

size := ReadUInt16(buf)
if size == 0 {
return "", true
}

if buf.Readable() < int(size) {
return "", false
}

_, value, _ := buf.ReadBytes(int(size))
return binary.SliceToString(value), true
}

// WriteBigString write big string
func WriteBigString(value string, buf *binary.ByteBuf) {
if value != "" {
buf.WriteInt(len(value))
buf.WriteString(value)
} else {
buf.WriteInt(0)
}
}

// ReadBigString read big string
func ReadBigString(buf *binary.ByteBuf) string {
size := ReadInt(buf)
if size == 0 {
return ""
}

_, value, _ := buf.ReadBytes(size)
return binary.SliceToString(value)
}

// MaybeReadBigString maybe read string value
func MaybeReadBigString(buf *binary.ByteBuf) (string, bool) {
if buf.Readable() < 4 {
return "", false
}

size := ReadInt(buf)
if size == 0 {
return "", true
}

if buf.Readable() < size {
return "", false
}

_, value, _ := buf.ReadBytes(int(size))
return binary.SliceToString(value), true
}

// ReadUInt64 read uint64 value
func ReadUInt64(buf *binary.ByteBuf) uint64 {
value, _ := buf.ReadUInt64()
return value
}

// ReadUInt16 read uint16 value
func ReadUInt16(buf *binary.ByteBuf) uint16 {
value, _ := buf.ReadUInt16()
return value
}

// ReadUInt32 read uint16 value
func ReadUInt32(buf *binary.ByteBuf) uint32 {
value, _ := buf.ReadUInt32()
return value
}

// ReadUInt32 read uint16 value
func Read(buf *binary.ByteBuf, p []byte) []byte {
buf.Read(p)
return p
}

// ReadInt read int value
func ReadInt(buf *binary.ByteBuf) int {
value, _ := buf.ReadInt()
return value
}

// ReadByte read byte value
func ReadByte(buf *binary.ByteBuf) byte {
value, _ := buf.ReadByte()
return value
}

// ReadBytes read bytes value
func ReadBytes(n int, buf *binary.ByteBuf) []byte {
_, value, _ := buf.ReadBytes(n)
return value
}

// WriteBool write bool value
func WriteBool(value bool, out *binary.ByteBuf) {
out.WriteByte(boolToByte(value))
}

// WriteSlice write slice value
func WriteSlice(value []byte, buf *binary.ByteBuf) {
buf.WriteUInt16(uint16(len(value)))
if len(value) > 0 {
buf.Write(value)
}
}

// ReadSlice read slice value
func ReadSlice(buf *binary.ByteBuf) []byte {
l, _ := buf.ReadUInt16()
if l == 0 {
return nil
}

_, data, _ := buf.ReadBytes(int(l))
return data
}

func boolToByte(value bool) byte {
if value {
return 1
}

return 0
}

func byteToBool(value byte) bool {
if value == 1 {
return true
}

return false
}

+ 13
- 24
pkg/protocol/codec/common_global_end_request_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
"github.com/seata/seata-go/pkg/protocol/message"
)

@@ -26,32 +26,21 @@ type CommonGlobalEndRequestCodec struct {
}

func (c *CommonGlobalEndRequestCodec) Encode(in interface{}) []byte {
req, _ := in.(message.AbstractGlobalEndRequest)
buf := binary.NewByteBuf(0)
data, _ := in.(message.AbstractGlobalEndRequest)
buf := bytes.NewByteBuffer([]byte{})

Write16String(req.Xid, buf)
Write16String(string(req.ExtraData), buf)
bytes.WriteString16Length(data.Xid, buf)
bytes.WriteString16Length(string(data.ExtraData), buf)

return buf.RawBuf()
return buf.Bytes()
}

func (c *CommonGlobalEndRequestCodec) Decode(in []byte) interface{} {
res := message.AbstractGlobalEndRequest{}

buf := binary.NewByteBuf(len(in))
buf.Write(in)
var length uint16

length = ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
res.Xid = string(Read(buf, bytes))
}
length = ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
res.ExtraData = Read(buf, bytes)
}

return res
data := message.AbstractGlobalEndRequest{}
buf := bytes.NewByteBuffer(in)

data.Xid = bytes.ReadString16Length(buf)
data.ExtraData = []byte(bytes.ReadString16Length(buf))

return data
}

+ 20
- 31
pkg/protocol/codec/common_global_end_response_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
transaction2 "github.com/seata/seata-go/pkg/common/error"
"github.com/seata/seata-go/pkg/protocol/message"
)
@@ -27,46 +27,35 @@ type CommonGlobalEndResponseCodec struct {
}

func (c *CommonGlobalEndResponseCodec) Encode(in interface{}) []byte {
buf := binary.NewByteBuf(0)
resp := in.(message.AbstractGlobalEndResponse)
data := in.(message.AbstractGlobalEndResponse)
buf := bytes.NewByteBuffer([]byte{})

buf.WriteByte(byte(resp.ResultCode))
if resp.ResultCode == message.ResultCodeFailed {
buf.WriteByte(byte(data.ResultCode))
if data.ResultCode == message.ResultCodeFailed {
var msg string
if len(resp.Msg) > 128 {
msg = resp.Msg[:128]
if len(data.Msg) > 128 {
msg = data.Msg[:128]
} else {
msg = resp.Msg
msg = data.Msg
}
Write16String(msg, buf)
bytes.WriteString16Length(msg, buf)
}
buf.WriteByte(byte(resp.TransactionExceptionCode))
buf.WriteByte(byte(resp.GlobalStatus))
buf.WriteByte(byte(data.TransactionExceptionCode))
buf.WriteByte(byte(data.GlobalStatus))

return buf.RawBuf()
return buf.Bytes()
}

func (c *CommonGlobalEndResponseCodec) Decode(in []byte) interface{} {
buf := binary.NewByteBuf(len(in))
buf.Write(in)
data := message.AbstractGlobalEndResponse{}
buf := bytes.NewByteBuffer(in)

msg := message.AbstractGlobalEndResponse{}

resultCode := ReadByte(buf)
msg.ResultCode = message.ResultCode(resultCode)
if msg.ResultCode == message.ResultCodeFailed {
length := ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
msg.Msg = string(Read(buf, bytes))
}
data.ResultCode = message.ResultCode(bytes.ReadByte(buf))
if data.ResultCode == message.ResultCodeFailed {
data.Msg = bytes.ReadString16Length(buf)
}
data.TransactionExceptionCode = transaction2.TransactionExceptionCode(bytes.ReadByte(buf))
data.GlobalStatus = message.GlobalStatus(bytes.ReadByte(buf))

exceptionCode := ReadByte(buf)
msg.TransactionExceptionCode = transaction2.TransactionExceptionCode(exceptionCode)

globalStatus := ReadByte(buf)
msg.GlobalStatus = message.GlobalStatus(globalStatus)

return msg
return data
}

+ 15
- 51
pkg/protocol/codec/common_identify_request_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
"github.com/seata/seata-go/pkg/protocol/message"
)

@@ -26,61 +26,25 @@ type AbstractIdentifyRequestCodec struct {
}

func (c *AbstractIdentifyRequestCodec) Encode(in interface{}) []byte {
req := in.(message.AbstractIdentifyRequest)
buf := binary.NewByteBuf(0)
data := in.(message.AbstractIdentifyRequest)
buf := bytes.NewByteBuffer([]byte{})

Write16String(req.Version, buf)
Write16String(req.ApplicationId, buf)
Write16String(req.TransactionServiceGroup, buf)
Write16String(string(req.ExtraData), buf)
bytes.WriteString16Length(data.Version, buf)
bytes.WriteString16Length(data.ApplicationId, buf)
bytes.WriteString16Length(data.TransactionServiceGroup, buf)
bytes.WriteString16Length(string(data.ExtraData), buf)

return buf.RawBuf()
return buf.Bytes()
}

func (c *AbstractIdentifyRequestCodec) Decode(in []byte) interface{} {
msg := message.AbstractIdentifyRequest{}
buf := binary.NewByteBuf(len(in))
buf.Write(in)
var len uint16
data := message.AbstractIdentifyRequest{}
buf := bytes.NewByteBuffer(in)

if buf.Readable() < 2 {
return msg
}
len = ReadUInt16(buf)
if uint16(buf.Readable()) < len {
return msg
}
versionBytes := make([]byte, len)
msg.Version = string(Read(buf, versionBytes))
data.Version = bytes.ReadString16Length(buf)
data.ApplicationId = bytes.ReadString16Length(buf)
data.TransactionServiceGroup = bytes.ReadString16Length(buf)
data.ExtraData = []byte(bytes.ReadString16Length(buf))

if buf.Readable() < 2 {
return msg
}
len = ReadUInt16(buf)
if uint16(buf.Readable()) < len {
return msg
}
applicationIdBytes := make([]byte, len)
msg.ApplicationId = string(Read(buf, applicationIdBytes))

if buf.Readable() < 2 {
return msg
}
len = ReadUInt16(buf)
if uint16(buf.Readable()) < len {
return msg
}
transactionServiceGroupBytes := make([]byte, len)
msg.TransactionServiceGroup = string(Read(buf, transactionServiceGroupBytes))

if buf.Readable() < 2 {
return msg
}
len = ReadUInt16(buf)
if len > 0 && uint16(buf.Readable()) >= len {
extraDataBytes := make([]byte, len)
msg.ExtraData = Read(buf, extraDataBytes)
}

return msg
return data
}

+ 12
- 18
pkg/protocol/codec/common_identify_response_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
"github.com/seata/seata-go/pkg/protocol/message"
)

@@ -26,36 +26,30 @@ type AbstractIdentifyResponseCodec struct {
}

func (c *AbstractIdentifyResponseCodec) Encode(in interface{}) []byte {
buf := binary.NewByteBuf(0)
resp := in.(message.AbstractIdentifyResponse)
data := in.(message.AbstractIdentifyResponse)
buf := bytes.NewByteBuffer([]byte{})

if resp.Identified {
if data.Identified {
buf.WriteByte(byte(1))
} else {
buf.WriteByte(byte(0))
}
bytes.WriteString16Length(data.Version, buf)

Write16String(resp.Version, buf)
return buf.RawBuf()
return buf.Bytes()
}

func (c *AbstractIdentifyResponseCodec) Decode(in []byte) interface{} {
buf := binary.NewByteBuf(len(in))
buf.Write(in)
msg := message.AbstractIdentifyResponse{}
data := message.AbstractIdentifyResponse{}
buf := bytes.NewByteBuffer(in)

identified, _ := buf.ReadByte()
if identified == byte(1) {
msg.Identified = true
data.Identified = true
} else if identified == byte(0) {
msg.Identified = false
data.Identified = false
}
data.Version = bytes.ReadString16Length(buf)

length := ReadUInt16(buf)
if length > 0 {
versionBytes := make([]byte, length)
msg.Version = string(Read(buf, versionBytes))
}

return msg
return data
}

+ 13
- 18
pkg/protocol/codec/global_begin_request_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
"github.com/seata/seata-go/pkg/protocol/message"
)

@@ -30,28 +30,23 @@ type GlobalBeginRequestCodec struct {
}

func (c *GlobalBeginRequestCodec) Encode(in interface{}) []byte {
req := in.(message.GlobalBeginRequest)
buf := binary.NewByteBuf(0)
data := in.(message.GlobalBeginRequest)
buf := bytes.NewByteBuffer([]byte{})

buf.WriteUInt32(uint32(req.Timeout))
Write16String(req.TransactionName, buf)
buf.WriteUint32(uint32(data.Timeout))
bytes.WriteString16Length(data.TransactionName, buf)

return buf.RawBuf()
return buf.Bytes()
}

func (g *GlobalBeginRequestCodec) Decode(in []byte) interface{} {
msg := message.GlobalBeginRequest{}
buf := binary.NewByteBuf(len(in))
buf.Write(in)

msg.Timeout = int32(ReadUInt32(buf))
len := ReadUInt16(buf)
if len > 0 {
transactionName := make([]byte, len)
msg.TransactionName = string(Read(buf, transactionName))
}

return msg
data := message.GlobalBeginRequest{}
buf := bytes.NewByteBuffer(in)

data.Timeout = int32(bytes.ReadUInt32(buf))
data.TransactionName = bytes.ReadString16Length(buf)

return data
}

func (g *GlobalBeginRequestCodec) GetMessageType() message.MessageType {


+ 22
- 41
pkg/protocol/codec/global_begin_response_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
error2 "github.com/seata/seata-go/pkg/common/error"
"github.com/seata/seata-go/pkg/protocol/message"
)
@@ -31,58 +31,39 @@ type GlobalBeginResponseCodec struct {
}

func (c *GlobalBeginResponseCodec) Encode(in interface{}) []byte {
buf := binary.NewByteBuf(0)
resp := in.(message.GlobalBeginResponse)
data := in.(message.GlobalBeginResponse)
buf := bytes.NewByteBuffer([]byte{})

buf.WriteByte(byte(resp.ResultCode))
if resp.ResultCode == message.ResultCodeFailed {
buf.WriteByte(byte(data.ResultCode))
if data.ResultCode == message.ResultCodeFailed {
var msg string
if len(resp.Msg) > 128 {
msg = resp.Msg[:128]
if len(data.Msg) > 128 {
msg = data.Msg[:128]
} else {
msg = resp.Msg
msg = data.Msg
}
Write16String(msg, buf)
bytes.WriteString16Length(msg, buf)
}
buf.WriteByte(byte(resp.TransactionExceptionCode))
Write16String(resp.Xid, buf)
Write16String(string(resp.ExtraData), buf)
buf.WriteByte(byte(data.TransactionExceptionCode))
bytes.WriteString16Length(data.Xid, buf)
bytes.WriteString16Length(string(data.ExtraData), buf)

return buf.RawBuf()
return buf.Bytes()
}

func (g *GlobalBeginResponseCodec) Decode(in []byte) interface{} {
var lenth uint16
buf := binary.NewByteBuf(len(in))
buf.Write(in)
msg := message.GlobalBeginResponse{}
data := message.GlobalBeginResponse{}
buf := bytes.NewByteBuffer(in)

resultCode := ReadByte(buf)
msg.ResultCode = message.ResultCode(resultCode)
if msg.ResultCode == message.ResultCodeFailed {
lenth = ReadUInt16(buf)
if lenth > 0 {
bytes := make([]byte, lenth)
msg.Msg = string(Read(buf, bytes))
}
}

exceptionCode := ReadByte(buf)
msg.TransactionExceptionCode = error2.TransactionExceptionCode(exceptionCode)

lenth = ReadUInt16(buf)
if lenth > 0 {
bytes := make([]byte, lenth)
msg.Xid = string(Read(buf, bytes))
}

lenth = ReadUInt16(buf)
if lenth > 0 {
bytes := make([]byte, lenth)
msg.ExtraData = Read(buf, bytes)
data.ResultCode = message.ResultCode(bytes.ReadByte(buf))
if data.ResultCode == message.ResultCodeFailed {
data.Msg = bytes.ReadString16Length(buf)
}
data.TransactionExceptionCode = error2.TransactionExceptionCode(bytes.ReadByte(buf))
data.Xid = bytes.ReadString16Length(buf)
data.ExtraData = []byte(bytes.ReadString16Length(buf))

return msg
return data
}

func (g *GlobalBeginResponseCodec) GetMessageType() message.MessageType {


+ 16
- 41
pkg/protocol/codec/register_rm_request_codec.go View File

@@ -18,7 +18,7 @@
package codec

import (
"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"
"github.com/seata/seata-go/pkg/protocol/message"
)

@@ -30,54 +30,29 @@ type RegisterRMRequestCodec struct {
}

func (g *RegisterRMRequestCodec) Decode(in []byte) interface{} {
buf := binary.NewByteBuf(len(in))
buf.Write(in)
msg := message.RegisterRMRequest{}
data := message.RegisterRMRequest{}
buf := bytes.NewByteBuffer(in)

length := ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
msg.Version = string(Read(buf, bytes))
}
data.Version = bytes.ReadString16Length(buf)
data.ApplicationId = bytes.ReadString16Length(buf)
data.TransactionServiceGroup = bytes.ReadString16Length(buf)
data.ExtraData = []byte(bytes.ReadString16Length(buf))
data.ResourceIds = bytes.ReadString32Length(buf)

length = ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
msg.ApplicationId = string(Read(buf, bytes))
}

length = ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
msg.TransactionServiceGroup = string(Read(buf, bytes))
}

length = ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
msg.ExtraData = Read(buf, bytes)
}

length32 := ReadUInt32(buf)
if length32 > 0 {
bytes := make([]byte, length32)
msg.ResourceIds = string(Read(buf, bytes))
}

return msg
return data
}

func (c *RegisterRMRequestCodec) Encode(in interface{}) []byte {
req := in.(message.RegisterRMRequest)
buf := binary.NewByteBuf(0)
buf := bytes.NewByteBuffer([]byte{})

Write16String(req.Version, buf)
Write16String(req.ApplicationId, buf)
Write16String(req.TransactionServiceGroup, buf)
Write16String(string(req.ExtraData), buf)
Write32String(req.ResourceIds, buf)
bytes.WriteString16Length(req.Version, buf)
bytes.WriteString16Length(req.ApplicationId, buf)
bytes.WriteString16Length(req.TransactionServiceGroup, buf)
bytes.WriteString16Length(string(req.ExtraData), buf)
bytes.WriteString32Length(req.ResourceIds, buf)

return buf.RawBuf()
return buf.Bytes()
}

func (g *RegisterRMRequestCodec) GetMessageType() message.MessageType {


+ 27
- 34
pkg/remoting/getty/readwriter.go View File

@@ -20,7 +20,7 @@ package getty
import (
"fmt"

"github.com/seata/seata-go/pkg/common/binary"
"github.com/seata/seata-go/pkg/common/bytes"

getty "github.com/apache/dubbo-getty"
"github.com/pkg/errors"
@@ -85,31 +85,24 @@ type SeataV1PackageHeader struct {
}

func (p *RpcPackageHandler) Read(ss getty.Session, data []byte) (interface{}, int, error) {
in := binary.NewByteBuf(len(data))
in.Write(data)
in := bytes.NewByteBuffer(data)

header := SeataV1PackageHeader{}
if in.Readable() < Seatav1HeaderLength {
return nil, 0, fmt.Errorf("invalid package length")
}

magic0 := codec.ReadByte(in)
magic1 := codec.ReadByte(in)
magic0 := bytes.ReadByte(in)
magic1 := bytes.ReadByte(in)
if magic0 != magics[0] || magic1 != magics[1] {
return nil, 0, fmt.Errorf("codec decode not found magic offset")
}

header.Magic0 = magic0
header.Magic1 = magic1
header.Version = codec.ReadByte(in)
header.Version = bytes.ReadByte(in)
// length of head and body
header.TotalLength = codec.ReadUInt32(in)
header.HeadLength = codec.ReadUInt16(in)
header.MessageType = message.GettyRequestType(codec.ReadByte(in))
header.CodecType = codec.ReadByte(in)
header.CompressType = codec.ReadByte(in)
header.RequestID = codec.ReadUInt32(in)

header.TotalLength = bytes.ReadUInt32(in)
header.HeadLength = bytes.ReadUInt16(in)
header.MessageType = message.GettyRequestType(bytes.ReadByte(in))
header.CodecType = bytes.ReadByte(in)
header.CompressType = bytes.ReadByte(in)
header.RequestID = bytes.ReadUInt32(in)
headMapLength := header.HeadLength - Seatav1HeaderLength
header.Meta = decodeHeapMap(in, headMapLength)
header.BodyLength = header.TotalLength - uint32(header.HeadLength)
@@ -166,44 +159,44 @@ func (p *RpcPackageHandler) Write(ss getty.Session, pkg interface{}) ([]byte, er
totalLength += len(bodyBytes)
}

buf := binary.NewByteBuf(0)
buf := bytes.NewByteBuffer([]byte{})
buf.WriteByte(message.MAGIC_CODE_BYTES[0])
buf.WriteByte(message.MAGIC_CODE_BYTES[1])
buf.WriteByte(message.VERSION)
buf.WriteUInt32(uint32(totalLength))
buf.WriteUInt16(uint16(headLength))
buf.WriteUint32(uint32(totalLength))
buf.WriteUint16(uint16(headLength))
buf.WriteByte(byte(msg.Type))
buf.WriteByte(msg.Codec)
buf.WriteByte(msg.Compressor)
buf.WriteUInt32(uint32(msg.ID))
buf.WriteUint32(uint32(msg.ID))
buf.Write(headMapBytes)
buf.Write(bodyBytes)

return buf.RawBuf(), nil
return buf.Bytes(), nil
}

func encodeHeapMap(data map[string]string) ([]byte, int) {
buf := binary.NewByteBuf(0)
buf := bytes.NewByteBuffer([]byte{})
for k, v := range data {
if k == "" {
buf.WriteUInt16(uint16(0))
buf.WriteUint16(uint16(0))
} else {
buf.WriteUInt16(uint16(len(k)))
buf.WriteUint16(uint16(len(k)))
buf.WriteString(k)
}

if v == "" {
buf.WriteUInt16(uint16(0))
buf.WriteUint16(uint16(0))
} else {
buf.WriteUInt16(uint16(len(v)))
buf.WriteUint16(uint16(len(v)))
buf.WriteString(v)
}
}
res := buf.RawBuf()
res := buf.Bytes()
return res, len(res)
}

func decodeHeapMap(in *binary.ByteBuf, length uint16) map[string]string {
func decodeHeapMap(in *bytes.ByteBuffer, length uint16) map[string]string {
res := make(map[string]string, 0)
if length == 0 {
return res
@@ -212,21 +205,21 @@ func decodeHeapMap(in *binary.ByteBuf, length uint16) map[string]string {
readedLength := uint16(0)
for readedLength < length {
var key, value string
keyLength := codec.ReadUInt16(in)
keyLength := bytes.ReadUInt16(in)
if keyLength == 0 {
key = ""
} else {
keyBytes := make([]byte, keyLength)
keyBytes = codec.Read(in, keyBytes)
in.Read(keyBytes)
key = string(keyBytes)
}

valueLength := codec.ReadUInt16(in)
valueLength := bytes.ReadUInt16(in)
if valueLength == 0 {
key = ""
} else {
valueBytes := make([]byte, valueLength)
valueBytes = codec.Read(in, valueBytes)
in.Read(valueBytes)
value = string(valueBytes)
}



Loading…
Cancel
Save