Initial registration and call mocup

This commit is contained in:
Matthias Fulz 2019-11-05 18:21:41 +01:00
parent 2b5618714c
commit 992cfd4220
1 changed files with 57 additions and 0 deletions

View File

@ -1,6 +1,10 @@
package srpc
import (
"bytes"
"fmt"
"olznet.de/ssob"
"reflect"
"sync"
)
@ -26,6 +30,13 @@ type responseHeader struct {
status responseStatus
}
type parseInputsFunc func(in []byte) (ret []reflect.Value)
type service struct {
f reflect.Value
fin parseInputsFunc
}
type Server struct {
serviceMap sync.Map
}
@ -35,3 +46,49 @@ func NewServer() *Server {
}
var DefaultServer = NewServer()
func (server *Server) RegisterName(name string, rcvr interface{}) (err error) {
ft := reflect.TypeOf(rcvr)
fv := reflect.ValueOf(rcvr)
nIn := ft.NumIn()
//nOut := ft.NumOut()
fs := service{}
f := func(in []byte) (ret []reflect.Value) {
var b bytes.Buffer
if _, err = b.Write(in); err != nil {
return nil
}
decoder := ssob.NewDecoder(&b)
ret = make([]reflect.Value, nIn)
for i := 0; i < nIn; i++ {
in := reflect.New(ft.In(i))
if err = decoder.Decode(in.Interface()); err != nil {
fmt.Println(err)
return nil
}
ret[i] = reflect.Indirect(in)
}
return ret
}
fs.fin = f
fs.f = fv
server.serviceMap.Store(name, fs)
return nil
}
func (server *Server) CallName(name string, in []byte) {
fs, _ := server.serviceMap.Load(name)
fmt.Printf("3. ")
fmt.Println(in)
inputs := fs.(service).fin(in)
fmt.Println(inputs)
fs.(service).f.Call(inputs)
}