tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/duplex/talk/peer.go (about) 1 package talk 2 3 import ( 4 "tractor.dev/toolkit-go/duplex/codec" 5 "tractor.dev/toolkit-go/duplex/mux" 6 "tractor.dev/toolkit-go/duplex/rpc" 7 ) 8 9 // Peer is a mux session, RPC client and responder, all in one. 10 type Peer struct { 11 mux.Session 12 *rpc.Client 13 *rpc.Server 14 *rpc.RespondMux 15 codec.Codec 16 } 17 18 // NewPeer returns a Peer based on a session and codec. 19 func NewPeer(session mux.Session, codec codec.Codec) *Peer { 20 mux := rpc.NewRespondMux() 21 return &Peer{ 22 Session: session, 23 Codec: codec, 24 Client: rpc.NewClient(session, codec), 25 Server: &rpc.Server{Handler: mux, Codec: codec}, 26 RespondMux: mux, 27 } 28 } 29 30 // Close will close the underlying session. 31 func (p *Peer) Close() error { 32 return p.Client.Close() 33 } 34 35 // Respond lets the Peer respond to incoming channels like 36 // a server, using any registered handlers. 37 func (p *Peer) Respond() { 38 p.Server.Respond(p.Session, nil) 39 }