Little refactoring

This commit is contained in:
2023-04-01 00:33:59 +02:00
parent 8b3e722ea5
commit b5b293329e
9 changed files with 179 additions and 30 deletions

145
content.go Normal file
View File

@@ -0,0 +1,145 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
package dsrpc
import (
"io"
"net"
"time"
)
type Content struct {
start time.Time
remoteHost string
sockReader io.Reader
sockWriter io.Writer
reqPacket *Packet
reqHeader *Header
reqBlock *Request
resPacket *Packet
resHeader *Header
resBlock *Response
binReader io.Reader
binWriter io.Writer
}
func CreateContent(conn net.Conn) *Content {
context := &Content{
start: time.Now(),
sockReader: conn,
sockWriter: conn,
reqPacket: NewEmptyPacket(),
reqHeader: NewEmptyHeader(),
reqBlock: NewEmptyRequest(),
resPacket: NewEmptyPacket(),
resHeader: NewEmptyHeader(),
resBlock: NewEmptyResponse(),
}
return context
}
func (context *Content) Request() *Request {
return context.reqBlock
}
func (context *Content) RemoteHost() string {
return context.remoteHost
}
func (context *Content) Start() time.Time {
return context.start
}
func (context *Content) Method() string {
var method string
if context.reqBlock != nil {
method = context.reqBlock.Method
}
return method
}
func (context *Content) ReqRpcSize() int64 {
var size int64
if context.reqHeader != nil {
size = context.reqHeader.rpcSize
}
return size
}
func (context *Content) ReqBinSize() int64 {
var size int64
if context.reqHeader != nil {
size = context.reqHeader.binSize
}
return size
}
func (context *Content) ResBinSize() int64 {
var size int64
if context.resHeader != nil {
size = context.resHeader.binSize
}
return size
}
func (context *Content) ResRpcSize() int64 {
var size int64
if context.resHeader != nil {
size = context.resHeader.rpcSize
}
return size
}
func (context *Content) ReqSize() int64 {
var size int64
if context.reqHeader != nil {
size += context.reqHeader.binSize
size += context.reqHeader.rpcSize
}
return size
}
func (context *Content) ResSize() int64 {
var size int64
if context.resHeader != nil {
size += context.resHeader.binSize
size += context.resHeader.rpcSize
}
return size
}
func (context *Content) SetAuthIdent(ident []byte) {
context.reqBlock.Auth.Ident = ident
}
func (context *Content) SetAuthSalt(salt []byte) {
context.reqBlock.Auth.Salt = salt
}
func (context *Content) SetAuthHash(hash []byte) {
context.reqBlock.Auth.Hash = hash
}
func (context *Content) AuthIdent() []byte {
return context.reqBlock.Auth.Ident
}
func (context *Content) AuthSalt() []byte {
return context.reqBlock.Auth.Salt
}
func (context *Content) AuthHash() []byte {
return context.reqBlock.Auth.Hash
}
func (context *Content) Auth() *Auth {
return context.reqBlock.Auth
}

View File

@@ -12,14 +12,7 @@ type HelloParams struct {
Message string `msgpack:"message" json:"message"`
}
func NewHelloParams() *HelloParams {
return &HelloParams{}
}
type HelloResult struct {
Message string `msgpack:"message" json:"message"`
}
func NewHelloResult() *HelloResult {
return &HelloResult{}
}

View File

@@ -23,12 +23,13 @@ func main() {
func exec() error {
var err error
params := api.NewHelloParams()
params.Message = "hello, server!"
params := api.HelloParams{
Message: "hello, server!",
}
result := api.NewHelloResult()
result := api.HelloResult{}
err = dsrpc.Exec("127.0.0.1:8081", api.HelloMethod, params, result, nil)
err = dsrpc.Exec("127.0.0.1:8081", api.HelloMethod, &params, &result, nil)
if err != nil {
return err
}

View File

@@ -1,7 +1,10 @@
module netsrv
go 1.17
go 1.19
require github.com/kindsoldier/dsrpc v0.0.1
require github.com/kindsoldier/dsrpc v1.1.1
replace github.com/kindsoldier/dsrpc => ../
require (
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
)

View File

@@ -1,10 +1,16 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/kindsoldier/dsrpc v1.1.1 h1:BZWuZexJVjhORsu30u3IRx5Piz1Za0TaIIBEXDOAqAM=
github.com/kindsoldier/dsrpc v1.1.1/go.mod h1:HgludPEdSw8leTlAt9N5qzj3/u7xdMpHSvop/TapHKY=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@@ -8,8 +8,9 @@ package main
import (
"log"
"github.com/kindsoldier/dsrpc"
"netsrv/api"
"github.com/kindsoldier/dsrpc"
)
func main() {
@@ -46,20 +47,19 @@ func NewController() *Controller {
return &Controller{}
}
func (cont *Controller) HelloHandler(context *dsrpc.Context) error {
func (cont *Controller) HelloHandler(content *dsrpc.Content) error {
var err error
params := api.NewHelloParams()
err = context.BindParams(params)
params := api.HelloParams{}
err = content.BindParams(&params)
if err != nil {
return err
}
log.Println("hello message:", params.Message)
result := api.NewHelloResult()
result.Message = "hello!"
err = context.SendResult(result, 0)
result := api.HelloResult{
Message: "hello, client!",
}
err = content.SendResult(&result, 0)
if err != nil {
return err
}

2
go.mod
View File

@@ -3,7 +3,7 @@ module github.com/kindsoldier/dsrpc
go 1.19
require (
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.2
github.com/vmihailenco/msgpack/v5 v5.3.5
)

4
go.sum
View File

@@ -9,8 +9,8 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=

View File

@@ -8,6 +8,7 @@ package dsrpc
import (
"encoding/json"
encoder "github.com/vmihailenco/msgpack/v5"
)