Unverified Commit 8771fbf3 authored by Martin Holst Swende's avatar Martin Holst Swende Committed by GitHub

rpc: make stdio usable over custom channels (#19046)

parent b5d471a7
...@@ -19,6 +19,7 @@ package rpc ...@@ -19,6 +19,7 @@ package rpc
import ( import (
"context" "context"
"errors" "errors"
"io"
"net" "net"
"os" "os"
"time" "time"
...@@ -26,19 +27,30 @@ import ( ...@@ -26,19 +27,30 @@ import (
// DialStdIO creates a client on stdin/stdout. // DialStdIO creates a client on stdin/stdout.
func DialStdIO(ctx context.Context) (*Client, error) { 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 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) { 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) { 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 { func (io stdioConn) Close() error {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment