Last active
January 2, 2018 18:32
-
-
Save zombiezen/39e7dfa6db757e4faae5ee7014600c1a to your computer and use it in GitHub Desktop.
Simple RPC skeleton for experience report
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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