This repository has been archived on 2024-07-06. You can view files and clone it, but cannot push or open issues or pull requests.
go-playit/proto/rpc.go
Matheus Sampaio Queiroga 37207e9678
Stash code
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
2024-06-01 16:03:52 -03:00

35 lines
988 B
Go

package proto
import (
"io"
"reflect"
"sirherobrine23.org/playit-cloud/go-playit/enc"
"sirherobrine23.org/playit-cloud/go-playit/logfile"
)
type ControlRpcMessage[T MessageEncoding] struct {
RequestID uint64
Content T // Convert with .(*type)
}
func (rpc *ControlRpcMessage[T]) WriteTo(w io.Writer) error {
defer debug.Printf("Write ControlRpcMessage[%s]: %s\n", reflect.TypeOf(rpc.Content).String(), logfile.JSONString(rpc))
if err := enc.WriteU64(w, rpc.RequestID); err != nil {
return err
} else if err = rpc.Content.WriteTo(w); err != nil {
return err
}
return nil
}
func (rpc *ControlRpcMessage[T]) ReadFrom(r io.Reader) error {
rpc.RequestID = enc.ReadU64(r)
if err := rpc.Content.ReadFrom(r); err != nil {
debug.Printf("Read ControlRpcMessage[%s] error: %s\n", reflect.TypeOf(rpc.Content).String(), err.Error())
return err
}
debug.Printf("Read ControlRpcMessage[%s]: %s\n", reflect.TypeOf(rpc.Content).String(), logfile.JSONString(rpc))
return nil
}