Basic communication draft done

This commit is contained in:
Matthias Fulz 2020-02-10 02:53:05 +01:00
parent d0a8a238f6
commit 1190f9bca2
3 changed files with 132 additions and 37 deletions

View File

@ -1,7 +1,58 @@
package srpc
import (
"bytes"
"fmt"
"io"
"net"
)
var REQUEST_HEADER_SIZE = int32(8)
type requestHeader struct {
size int64
Size int64
}
type request struct {
FuncName string
Payload []byte
}
type Client struct {
ed IEncoderDecoder
conn net.Conn
}
func (client *Client) Call(funcName string, args ...interface{}) (ret []byte) {
var b bytes.Buffer
enc := client.ed.NewEncoder(&b)
for _, a := range args {
enc.Encode(a)
}
payload := b.Bytes()
req := request{funcName, payload}
fmt.Println(req)
enc = client.ed.NewEncoder(client.conn)
enc.Encode(req)
respSize := int64(0)
dec := client.ed.NewDecoder(client.conn)
dec.Decode(&respSize)
fmt.Println(respSize)
respData := make([]byte, respSize)
dec.Decode(&respData)
return respData
}
func (client *Client) NewDecoder(r io.Reader) IDecoder {
return client.ed.NewDecoder(r)
}
func NewClient(conn net.Conn) *Client {
return &Client{NewEncoderDecoder(), conn}
}

53
encoder.go Normal file
View File

@ -0,0 +1,53 @@
package srpc
import (
"io"
"olznet.de/ssob"
)
type IEncoder interface {
Encode(e interface{}) error
}
type IDecoder interface {
Decode(e interface{}) error
}
type IEncoderHandler interface {
NewEncoder(w io.Writer) IEncoder
}
type IDecoderHandler interface {
NewDecoder(r io.Reader) IDecoder
}
type IEncoderDecoder interface {
IEncoderHandler
IDecoderHandler
}
type Encoder struct {
}
func (e *Encoder) NewEncoder(w io.Writer) IEncoder {
return ssob.NewEncoder(w)
}
type Decoder struct {
}
func (d *Decoder) NewDecoder(r io.Reader) IDecoder {
return ssob.NewDecoder(r)
}
type EncoderDecoder struct {
IEncoderHandler
IDecoderHandler
}
func NewEncoderDecoder() IEncoderDecoder {
enc := new(Encoder)
dec := new(Decoder)
return &EncoderDecoder{enc, dec}
}

View File

@ -6,19 +6,12 @@ import (
"fmt"
"log"
"net"
"olznet.de/ssob"
"reflect"
"sync"
)
type argumentType uint8
const (
AIN argumentType = 0
AOUT argumentType = 1
AINOUT argumentType = 2
)
type responseStatus uint8
const (
@ -43,43 +36,35 @@ type service struct {
}
type Server struct {
ed IEncoderDecoder
serviceMap sync.Map
}
func (server *Server) ServeConn(conn net.Conn) {
var b bytes.Buffer
var fs service
size := int64(0)
name := string("")
dec := ssob.NewDecoder(&b)
dec := server.ed.NewDecoder(conn)
sizeb := make([]byte, 12)
conn.Read(sizeb)
var req request
dec.Decode(&req)
fmt.Println(req)
b.Write(sizeb)
dec.Decode(&size)
log.Println(size)
payload := make([]byte, size)
conn.Read(payload)
b.Reset()
b.Write(payload)
log.Println(b.Bytes())
dec.Decode(&name)
log.Println(name)
if fsRaw, ok := server.serviceMap.Load(name); !ok {
log.Printf("srpc: Call to unknown method: '%s'", name)
if fsRaw, ok := server.serviceMap.Load(req.FuncName); !ok {
log.Printf("srpc: Call to unknown method: '%s'", req.FuncName)
return
} else {
fs = fsRaw.(service)
}
inputs := fs.fin(b.Bytes())
inputs := fs.fin(req.Payload)
outputs := fs.f.Call(inputs)
log.Println(outputs)
respData := fs.fout(outputs)
respSize := int64(len(respData))
enc := server.ed.NewEncoder(conn)
enc.Encode(respSize)
enc.Encode(respData)
}
func (server *Server) Accept(ln net.Listener) {
@ -101,7 +86,6 @@ func (server *Server) RegisterName(name string, rcvr interface{}) (err error) {
fv := reflect.ValueOf(rcvr)
nIn := ft.NumIn()
//nOut := ft.NumOut()
fs := service{}
@ -110,7 +94,7 @@ func (server *Server) RegisterName(name string, rcvr interface{}) (err error) {
if _, err = b.Write(in); err != nil {
return nil
}
decoder := ssob.NewDecoder(&b)
decoder := server.ed.NewDecoder(&b)
ret = make([]reflect.Value, nIn)
for i := 0; i < nIn; i++ {
@ -127,7 +111,7 @@ func (server *Server) RegisterName(name string, rcvr interface{}) (err error) {
fs.fout = func(in []reflect.Value) (ret []byte) {
var b bytes.Buffer
encoder := ssob.NewEncoder(&b)
encoder := server.ed.NewEncoder(&b)
for _, v := range in {
if v.Type() == reflect.TypeOf((*error)(nil)).Elem() {
@ -162,7 +146,7 @@ func (server *Server) RegisterName(name string, rcvr interface{}) (err error) {
func (server *Server) CallName(name string, args ...interface{}) (ret []byte, err error) {
var b bytes.Buffer
var fs service
encoder := ssob.NewEncoder(&b)
encoder := server.ed.NewEncoder(&b)
if fsRaw, ok := server.serviceMap.Load(name); !ok {
return nil, errors.New("srpc: Call to unknown method: '" + name + "'")
@ -180,8 +164,15 @@ func (server *Server) CallName(name string, args ...interface{}) (ret []byte, er
return fs.fout(outputs), nil
}
func NewServer() *Server {
return &Server{}
func NewServer(ed IEncoderDecoder) *Server {
ret := &Server{}
ret.ed = ed
return ret
}
var DefaultServer = NewServer()
func NewDefaultServer() *Server {
ret := NewServer(NewEncoderDecoder())
return ret
}
var DefaultServer = NewDefaultServer()