Little refactoring
This commit is contained in:
145
content.go
Normal file
145
content.go
Normal 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
|
||||||
|
}
|
||||||
@@ -12,14 +12,7 @@ type HelloParams struct {
|
|||||||
Message string `msgpack:"message" json:"message"`
|
Message string `msgpack:"message" json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHelloParams() *HelloParams {
|
|
||||||
return &HelloParams{}
|
|
||||||
}
|
|
||||||
|
|
||||||
type HelloResult struct {
|
type HelloResult struct {
|
||||||
Message string `msgpack:"message" json:"message"`
|
Message string `msgpack:"message" json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHelloResult() *HelloResult {
|
|
||||||
return &HelloResult{}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -23,12 +23,13 @@ func main() {
|
|||||||
func exec() error {
|
func exec() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
params := api.NewHelloParams()
|
params := api.HelloParams{
|
||||||
params.Message = "hello, server!"
|
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, ¶ms, &result, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
module netsrv
|
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
|
||||||
|
)
|
||||||
|
|||||||
@@ -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.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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
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.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/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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"github.com/kindsoldier/dsrpc"
|
|
||||||
"netsrv/api"
|
"netsrv/api"
|
||||||
|
|
||||||
|
"github.com/kindsoldier/dsrpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -46,20 +47,19 @@ func NewController() *Controller {
|
|||||||
return &Controller{}
|
return &Controller{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cont *Controller) HelloHandler(context *dsrpc.Context) error {
|
func (cont *Controller) HelloHandler(content *dsrpc.Content) error {
|
||||||
var err error
|
var err error
|
||||||
params := api.NewHelloParams()
|
params := api.HelloParams{}
|
||||||
err = context.BindParams(params)
|
err = content.BindParams(¶ms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("hello message:", params.Message)
|
log.Println("hello message:", params.Message)
|
||||||
|
|
||||||
result := api.NewHelloResult()
|
result := api.HelloResult{
|
||||||
result.Message = "hello!"
|
Message: "hello, client!",
|
||||||
|
}
|
||||||
err = context.SendResult(result, 0)
|
err = content.SendResult(&result, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module github.com/kindsoldier/dsrpc
|
|||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/stretchr/testify v1.8.1
|
github.com/stretchr/testify v1.8.2
|
||||||
github.com/vmihailenco/msgpack/v5 v5.3.5
|
github.com/vmihailenco/msgpack/v5 v5.3.5
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -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.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.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.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
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 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
|
||||||
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
|
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 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ package dsrpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
encoder "github.com/vmihailenco/msgpack/v5"
|
encoder "github.com/vmihailenco/msgpack/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user