2019-10-22 01:32:56 +02:00
|
|
|
package srpc
|
|
|
|
|
|
|
|
import (
|
2019-11-05 18:21:41 +01:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"olznet.de/ssob"
|
|
|
|
"reflect"
|
2019-10-22 01:32:56 +02:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2019-10-22 15:36:14 +02:00
|
|
|
type argumentType uint8
|
|
|
|
|
2019-10-22 01:32:56 +02:00
|
|
|
const (
|
2019-10-22 15:36:14 +02:00
|
|
|
AIN argumentType = 0
|
|
|
|
AOUT argumentType = 1
|
|
|
|
AINOUT argumentType = 2
|
2019-10-22 01:32:56 +02:00
|
|
|
)
|
|
|
|
|
2019-10-22 15:36:14 +02:00
|
|
|
type responseStatus uint8
|
|
|
|
|
2019-10-22 01:32:56 +02:00
|
|
|
const (
|
2019-10-22 15:36:14 +02:00
|
|
|
OK responseStatus = 0
|
|
|
|
ERR responseStatus = 1
|
2019-10-22 01:32:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var RESPONSE_HEADER_SIZE = int32(9)
|
|
|
|
|
|
|
|
type responseHeader struct {
|
|
|
|
size int64
|
2019-10-22 15:36:14 +02:00
|
|
|
status responseStatus
|
2019-10-22 01:32:56 +02:00
|
|
|
}
|
|
|
|
|
2019-11-05 18:21:41 +01:00
|
|
|
type parseInputsFunc func(in []byte) (ret []reflect.Value)
|
|
|
|
|
|
|
|
type service struct {
|
|
|
|
f reflect.Value
|
|
|
|
fin parseInputsFunc
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:32:56 +02:00
|
|
|
type Server struct {
|
|
|
|
serviceMap sync.Map
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer() *Server {
|
|
|
|
return &Server{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultServer = NewServer()
|
2019-11-05 18:21:41 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|