diff options
author | Martin Holst Swende <martin@swende.se> | 2019-02-13 01:05:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-13 01:05:28 +0800 |
commit | 8771fbf3c8166ccd4ad98d3719604f9a2e8d6492 (patch) | |
tree | ac0a6a030ac628b5057c1a2092b6cad3fd817515 /rpc | |
parent | b5d471a73905247406dcbe5ffac6087f80896374 (diff) | |
download | go-tangerine-8771fbf3c8166ccd4ad98d3719604f9a2e8d6492.tar go-tangerine-8771fbf3c8166ccd4ad98d3719604f9a2e8d6492.tar.gz go-tangerine-8771fbf3c8166ccd4ad98d3719604f9a2e8d6492.tar.bz2 go-tangerine-8771fbf3c8166ccd4ad98d3719604f9a2e8d6492.tar.lz go-tangerine-8771fbf3c8166ccd4ad98d3719604f9a2e8d6492.tar.xz go-tangerine-8771fbf3c8166ccd4ad98d3719604f9a2e8d6492.tar.zst go-tangerine-8771fbf3c8166ccd4ad98d3719604f9a2e8d6492.zip |
rpc: make stdio usable over custom channels (#19046)
Diffstat (limited to 'rpc')
-rw-r--r-- | rpc/stdio.go | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/rpc/stdio.go b/rpc/stdio.go index 8f6b7bd4b..d5dc066c9 100644 --- a/rpc/stdio.go +++ b/rpc/stdio.go @@ -19,6 +19,7 @@ package rpc import ( "context" "errors" + "io" "net" "os" "time" @@ -26,19 +27,30 @@ import ( // DialStdIO creates a client on stdin/stdout. func DialStdIO(ctx context.Context) (*Client, error) { + return DialIO(ctx, os.Stdin, os.Stdout) +} + +// DialIO creates a client which uses the given IO channels +func DialIO(ctx context.Context, in io.Reader, out io.Writer) (*Client, error) { return newClient(ctx, func(_ context.Context) (ServerCodec, error) { - return NewJSONCodec(stdioConn{}), nil + return NewJSONCodec(stdioConn{ + in: in, + out: out, + }), nil }) } -type stdioConn struct{} +type stdioConn struct { + in io.Reader + out io.Writer +} func (io stdioConn) Read(b []byte) (n int, err error) { - return os.Stdin.Read(b) + return io.in.Read(b) } func (io stdioConn) Write(b []byte) (n int, err error) { - return os.Stdout.Write(b) + return io.out.Write(b) } func (io stdioConn) Close() error { |