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
}
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
ok = true
enc := client.ed.NewEncoder(&b)
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.Encode(req)
respSize := int64(0)
var header responseHeader
dec := client.ed.NewDecoder(client.conn)
dec.Decode(&respSize)
fmt.Println(respSize)
dec.Decode(&header)
fmt.Println(header.Size)
fmt.Println(header.Status)
respData := make([]byte, respSize)
respData := make([]byte, header.Size)
dec.Decode(&respData)
return respData
if header.Status != OK {
ok = false
}
return respData, ok
}
func (client *Client) NewDecoder(r io.Reader) IDecoder {

View File

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