// RPC primitives for safcm: basic connection implementation // SPDX-License-Identifier: GPL-3.0-or-later // Copyright (C) 2021-2024 Simon Ruderich package safcm import ( "encoding/gob" "io" ) type GobConn struct { enc *gob.Encoder dec *gob.Decoder } func NewGobConn(r io.Reader, w io.Writer) *GobConn { return &GobConn{ enc: gob.NewEncoder(w), dec: gob.NewDecoder(r), } } func (c *GobConn) Send(x Msg) error { // & lets Encode send the interface itself and not a concrete type // which is necessary to Decode as an interface return c.enc.Encode(&x) } func (c *GobConn) Recv() (Msg, error) { var x Msg err := c.dec.Decode(&x) if err != nil { return nil, err } return x, nil }