github.com/klaytn/klaytn@v1.10.2/networks/rpc/ipc.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 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  // This file is derived from rpc/ipc.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package rpc
    22  
    23  import (
    24  	"context"
    25  	"net"
    26  
    27  	"github.com/klaytn/klaytn/networks/p2p/netutil"
    28  )
    29  
    30  // ServeListener accepts connections on l, serving JSON-RPC on them.
    31  func (s *Server) ServeListener(l net.Listener) error {
    32  	for {
    33  		conn, err := l.Accept()
    34  		if netutil.IsTemporaryError(err) {
    35  			logger.Warn("RPC accept error", "err", err)
    36  			continue
    37  		} else if err != nil {
    38  			return err
    39  		}
    40  		logger.Trace("Accepted connection", "addr", conn.RemoteAddr())
    41  		go s.ServeCodec(NewCodec(conn), 0)
    42  	}
    43  }
    44  
    45  // DialIPC create a new IPC client that connects to the given endpoint. On Unix it assumes
    46  // the endpoint is the full path to a unix socket, and Windows the endpoint is an
    47  // identifier for a named pipe.
    48  //
    49  // The context is used for the initial connection establishment. It does not
    50  // affect subsequent interactions with the client.
    51  func DialIPC(ctx context.Context, endpoint string) (*Client, error) {
    52  	return NewClient(ctx, func(ctx context.Context) (ServerCodec, error) {
    53  		conn, err := newIPCConnection(ctx, endpoint)
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  		return NewCodec(conn), err
    58  	})
    59  }