Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: make stdio usable over custom channels #19046

Merged
merged 1 commit into from
Feb 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions rpc/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,38 @@ package rpc
import (
"context"
"errors"
"io"
"net"
"os"
"time"
)

// 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 {
Expand Down