github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/service/rpc.go (about) 1 package service 2 3 import ( 4 "net" 5 6 "github.com/keybase/client/go/libkb" 7 "github.com/keybase/go-framed-msgpack-rpc/rpc" 8 "golang.org/x/net/context" 9 ) 10 11 // connTransport implements rpc.ConnectionTransport 12 type connTransport struct { 13 libkb.Contextified 14 host string 15 conn net.Conn 16 transport rpc.Transporter 17 stagedTransport rpc.Transporter 18 } 19 20 var _ rpc.ConnectionTransport = (*connTransport)(nil) 21 22 func newConnTransport(g *libkb.GlobalContext, host string) *connTransport { 23 return &connTransport{ 24 Contextified: libkb.NewContextified(g), 25 host: host, 26 } 27 } 28 29 func (t *connTransport) Dial(context.Context) (rpc.Transporter, error) { 30 var err error 31 t.conn, err = libkb.ProxyDial(t.G().Env, "tcp", t.host) 32 if err != nil { 33 return nil, err 34 } 35 t.stagedTransport = rpc.NewTransport(t.conn, libkb.NewRPCLogFactory(t.G()), 36 t.G().RemoteNetworkInstrumenterStorage, 37 libkb.MakeWrapError(t.G()), rpc.DefaultMaxFrameLength) 38 return t.stagedTransport, nil 39 } 40 41 func (t *connTransport) IsConnected() bool { 42 return t.transport != nil && t.transport.IsConnected() 43 } 44 45 func (t *connTransport) Finalize() { 46 t.transport = t.stagedTransport 47 t.stagedTransport = nil 48 } 49 50 func (t *connTransport) Close() { 51 t.conn.Close() 52 } 53 54 func (t *connTransport) Reset() { 55 t.transport = nil 56 }