srpc/server.go

183 lines
3.6 KiB
Go

package srpc
import (
"bytes"
"errors"
"fmt"
"net"
"olznet.de/slog"
"reflect"
"sync"
)
type argumentType uint8
type responseStatus uint8
const (
OK responseStatus = 0
ERR responseStatus = 1
)
var RESPONSE_HEADER_SIZE = int32(9)
type responseHeader struct {
Size int64
Status responseStatus
}
type parseInputsFunc func(in []byte) (ret []reflect.Value)
type parseOutputsFunc func(in []reflect.Value) (ret []byte)
type service struct {
f reflect.Value
fin parseInputsFunc
fout parseOutputsFunc
}
type Server struct {
ed IEncoderDecoder
serviceMap sync.Map
}
func (server *Server) ServeConn(conn net.Conn) {
var fs service
dec := server.ed.NewDecoder(conn)
var req request
dec.Decode(&req)
header := responseHeader{}
var respData []byte
if fsRaw, ok := server.serviceMap.Load(req.FuncName); !ok {
slog.LOG_ERROR("srpc - Call to unknown method: '%s'\n", req.FuncName)
err := fmt.Sprintf("Unknown method: '%s'", req.FuncName)
header.Status = ERR
respData = append([]byte{}, []byte(err)...)
} else {
fs = fsRaw.(service)
inputs := fs.fin(req.Payload)
outputs := fs.f.Call(inputs)
slog.LOG_DEBUG("%v\n", outputs)
header.Status = OK
respData = fs.fout(outputs)
}
header.Size = int64(len(respData))
enc := server.ed.NewEncoder(conn)
enc.Encode(header)
enc.Encode(respData)
}
func (server *Server) Accept(ln net.Listener) {
for {
conn, err := ln.Accept()
if err != nil {
slog.LOG_INFO("srpc - Accept: '%s'\n", err.Error())
return
}
go server.ServeConn(conn)
}
}
func (server *Server) RegisterName(name string, rcvr interface{}) (err error) {
if _, ok := server.serviceMap.Load(name); ok {
return errors.New("srpc: Function with name '" + name + "' already registerd")
}
ft := reflect.TypeOf(rcvr)
fv := reflect.ValueOf(rcvr)
nIn := ft.NumIn()
fs := service{}
fs.fin = func(in []byte) (ret []reflect.Value) {
var b bytes.Buffer
if _, err = b.Write(in); err != nil {
return nil
}
decoder := server.ed.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.fout = func(in []reflect.Value) (ret []byte) {
var b bytes.Buffer
encoder := server.ed.NewEncoder(&b)
for _, v := range in {
if v.Type() == reflect.TypeOf((*error)(nil)).Elem() {
if v.IsNil() {
if err := encoder.Encode(string("")); err != nil {
fmt.Println(err)
return nil
}
} else {
if err := encoder.Encode(v.Interface().(error).Error()); err != nil {
fmt.Println(err)
return nil
}
}
} else {
if err := encoder.Encode(v.Interface()); err != nil {
fmt.Println(err)
return nil
}
}
}
return b.Bytes()
}
fs.f = fv
server.serviceMap.Store(name, fs)
return nil
}
func (server *Server) CallName(name string, args ...interface{}) (ret []byte, err error) {
var b bytes.Buffer
var fs service
encoder := server.ed.NewEncoder(&b)
if fsRaw, ok := server.serviceMap.Load(name); !ok {
return nil, errors.New("srpc - Call to unknown method: '" + name + "'")
} else {
fs = fsRaw.(service)
}
for _, a := range args {
if err := encoder.Encode(a); err != nil {
return nil, errors.New("srpc - Error: '" + err.Error() + "'")
}
}
inputs := fs.fin(b.Bytes())
outputs := fs.f.Call(inputs)
return fs.fout(outputs), nil
}
func NewServer(ed IEncoderDecoder) *Server {
ret := &Server{}
ret.ed = ed
return ret
}
func NewDefaultServer() *Server {
ret := NewServer(NewEncoderDecoder())
return ret
}
var DefaultServer = NewDefaultServer()