First communication draft

This commit is contained in:
Matthias Fulz 2020-02-14 01:51:30 +01:00
parent 1190f9bca2
commit 442072cbae
2 changed files with 32 additions and 22 deletions

View File

@ -23,8 +23,9 @@ type Client struct {
conn net.Conn conn net.Conn
} }
func (client *Client) Call(funcName string, args ...interface{}) (ret []byte) { func (client *Client) Call(funcName string, args ...interface{}) (ret []byte, ok bool) {
var b bytes.Buffer var b bytes.Buffer
ok = true
enc := client.ed.NewEncoder(&b) enc := client.ed.NewEncoder(&b)
for _, a := range args { for _, a := range args {
@ -38,15 +39,20 @@ func (client *Client) Call(funcName string, args ...interface{}) (ret []byte) {
enc = client.ed.NewEncoder(client.conn) enc = client.ed.NewEncoder(client.conn)
enc.Encode(req) enc.Encode(req)
respSize := int64(0) var header responseHeader
dec := client.ed.NewDecoder(client.conn) dec := client.ed.NewDecoder(client.conn)
dec.Decode(&respSize) dec.Decode(&header)
fmt.Println(respSize) fmt.Println(header.Size)
fmt.Println(header.Status)
respData := make([]byte, respSize) respData := make([]byte, header.Size)
dec.Decode(&respData) dec.Decode(&respData)
return respData if header.Status != OK {
ok = false
}
return respData, ok
} }
func (client *Client) NewDecoder(r io.Reader) IDecoder { func (client *Client) NewDecoder(r io.Reader) IDecoder {

View File

@ -4,8 +4,8 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"log"
"net" "net"
"olznet.de/slog"
"reflect" "reflect"
"sync" "sync"
) )
@ -22,8 +22,8 @@ const (
var RESPONSE_HEADER_SIZE = int32(9) var RESPONSE_HEADER_SIZE = int32(9)
type responseHeader struct { type responseHeader struct {
size int64 Size int64
status responseStatus Status responseStatus
} }
type parseInputsFunc func(in []byte) (ret []reflect.Value) type parseInputsFunc func(in []byte) (ret []reflect.Value)
@ -47,23 +47,27 @@ func (server *Server) ServeConn(conn net.Conn) {
var req request var req request
dec.Decode(&req) dec.Decode(&req)
fmt.Println(req)
header := responseHeader{}
var respData []byte
if fsRaw, ok := server.serviceMap.Load(req.FuncName); !ok { if fsRaw, ok := server.serviceMap.Load(req.FuncName); !ok {
log.Printf("srpc: Call to unknown method: '%s'", req.FuncName) slog.LOG_ERROR("srpc - Call to unknown method: '%s'\n", req.FuncName)
return err := fmt.Sprintf("Unknown method: '%s'", req.FuncName)
header.Status = ERR
respData = append([]byte{}, []byte(err)...)
} else { } else {
fs = fsRaw.(service) 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))
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 := server.ed.NewEncoder(conn)
enc.Encode(respSize) enc.Encode(header)
enc.Encode(respData) enc.Encode(respData)
} }
@ -71,7 +75,7 @@ func (server *Server) Accept(ln net.Listener) {
for { for {
conn, err := ln.Accept() conn, err := ln.Accept()
if err != nil { if err != nil {
log.Printf("srpc: accept: %s", err.Error()) slog.LOG_INFO("srpc - Accept: '%s'\n", err.Error())
return return
} }
go server.ServeConn(conn) go server.ServeConn(conn)
@ -149,14 +153,14 @@ func (server *Server) CallName(name string, args ...interface{}) (ret []byte, er
encoder := server.ed.NewEncoder(&b) encoder := server.ed.NewEncoder(&b)
if fsRaw, ok := server.serviceMap.Load(name); !ok { if fsRaw, ok := server.serviceMap.Load(name); !ok {
return nil, errors.New("srpc: Call to unknown method: '" + name + "'") return nil, errors.New("srpc - Call to unknown method: '" + name + "'")
} else { } else {
fs = fsRaw.(service) fs = fsRaw.(service)
} }
for _, a := range args { for _, a := range args {
if err := encoder.Encode(a); err != nil { if err := encoder.Encode(a); err != nil {
return nil, errors.New("srpc: Error: '" + err.Error() + "'") return nil, errors.New("srpc - Error: '" + err.Error() + "'")
} }
} }
inputs := fs.fin(b.Bytes()) inputs := fs.fin(b.Bytes())