]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - gob.go
Use SPDX license identifiers
[safcm/safcm.git] / gob.go
1 // RPC primitives for safcm: basic connection implementation
2
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024  Simon Ruderich
5
6 package safcm
7
8 import (
9         "encoding/gob"
10         "io"
11 )
12
13 type GobConn struct {
14         enc *gob.Encoder
15         dec *gob.Decoder
16 }
17
18 func NewGobConn(r io.Reader, w io.Writer) *GobConn {
19         return &GobConn{
20                 enc: gob.NewEncoder(w),
21                 dec: gob.NewDecoder(r),
22         }
23 }
24
25 func (c *GobConn) Send(x Msg) error {
26         // & lets Encode send the interface itself and not a concrete type
27         // which is necessary to Decode as an interface
28         return c.enc.Encode(&x)
29 }
30
31 func (c *GobConn) Recv() (Msg, error) {
32         var x Msg
33         err := c.dec.Decode(&x)
34         if err != nil {
35                 return nil, err
36         }
37         return x, nil
38 }