github.com/klaytn/klaytn@v1.12.1/networks/rpc/stdio.go (about)

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