Skip to content

Instantly share code, notes, and snippets.

@zombiezen
Last active January 2, 2018 18:32
Show Gist options
  • Save zombiezen/39e7dfa6db757e4faae5ee7014600c1a to your computer and use it in GitHub Desktop.
Save zombiezen/39e7dfa6db757e4faae5ee7014600c1a to your computer and use it in GitHub Desktop.
Simple RPC skeleton for experience report
type Conn struct {
wc io.WriteCloser
// ... other internal state ...
}
func NewConn(rwc io.ReadWriteCloser) *Conn {
c := &Conn{wc: rwc}
go c.receive(rwc)
return c
}
func (c *Conn) receive(r io.Reader) {
for {
msg, err := decode(r)
if err != nil {
return
}
// ... Process message ...
}
}
func (c *Conn) SendRPC( /* ... */ ) *Answer {
if err := encode(c.wc, makeCallMsg()); err != nil {
return ErrorAnswer(err)
}
// ...
}
func main() {
rwc1, rwc2 := net.Pipe()
conn1 := NewConn(rwc1)
conn2 := NewConn(rwc2)
ans1 := conn1.SendRPC()
ans2 := conn1.SendRPC()
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment