stdio.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rpc
  17. import (
  18. "context"
  19. "errors"
  20. "io"
  21. "net"
  22. "os"
  23. "time"
  24. )
  25. // DialStdIO creates a client on stdin/stdout.
  26. func DialStdIO(ctx context.Context) (*Client, error) {
  27. return DialIO(ctx, os.Stdin, os.Stdout)
  28. }
  29. // DialIO creates a client which uses the given IO channels
  30. func DialIO(ctx context.Context, in io.Reader, out io.Writer) (*Client, error) {
  31. return newClient(ctx, func(_ context.Context) (ServerCodec, error) {
  32. return NewCodec(stdioConn{
  33. in: in,
  34. out: out,
  35. }), nil
  36. })
  37. }
  38. type stdioConn struct {
  39. in io.Reader
  40. out io.Writer
  41. }
  42. func (io stdioConn) Read(b []byte) (n int, err error) {
  43. return io.in.Read(b)
  44. }
  45. func (io stdioConn) Write(b []byte) (n int, err error) {
  46. return io.out.Write(b)
  47. }
  48. func (io stdioConn) Close() error {
  49. return nil
  50. }
  51. func (io stdioConn) RemoteAddr() string {
  52. return "/dev/stdin"
  53. }
  54. func (io stdioConn) SetWriteDeadline(t time.Time) error {
  55. return &net.OpError{Op: "set", Net: "stdio", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  56. }