github.com/bcnmy/go-ethereum@v1.10.27/rpc/server.go (about)

     1  // Copyright 2015 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  
    17  package rpc
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  	"sync/atomic"
    23  
    24  	mapset "github.com/deckarep/golang-set"
    25  	"github.com/ethereum/go-ethereum/log"
    26  )
    27  
    28  const MetadataApi = "rpc"
    29  const EngineApi = "engine"
    30  
    31  // CodecOption specifies which type of messages a codec supports.
    32  //
    33  // Deprecated: this option is no longer honored by Server.
    34  type CodecOption int
    35  
    36  const (
    37  	// OptionMethodInvocation is an indication that the codec supports RPC method calls
    38  	OptionMethodInvocation CodecOption = 1 << iota
    39  
    40  	// OptionSubscriptions is an indication that the codec supports RPC notifications
    41  	OptionSubscriptions = 1 << iota // support pub sub
    42  )
    43  
    44  // Server is an RPC server.
    45  type Server struct {
    46  	services serviceRegistry
    47  	idgen    func() ID
    48  	run      int32
    49  	codecs   mapset.Set
    50  }
    51  
    52  // NewServer creates a new server instance with no registered handlers.
    53  func NewServer() *Server {
    54  	server := &Server{idgen: randomIDGenerator(), codecs: mapset.NewSet(), run: 1}
    55  	// Register the default service providing meta information about the RPC service such
    56  	// as the services and methods it offers.
    57  	rpcService := &RPCService{server}
    58  	server.RegisterName(MetadataApi, rpcService)
    59  	return server
    60  }
    61  
    62  // RegisterName creates a service for the given receiver type under the given name. When no
    63  // methods on the given receiver match the criteria to be either a RPC method or a
    64  // subscription an error is returned. Otherwise a new service is created and added to the
    65  // service collection this server provides to clients.
    66  func (s *Server) RegisterName(name string, receiver interface{}) error {
    67  	return s.services.registerName(name, receiver)
    68  }
    69  
    70  // ServeCodec reads incoming requests from codec, calls the appropriate callback and writes
    71  // the response back using the given codec. It will block until the codec is closed or the
    72  // server is stopped. In either case the codec is closed.
    73  //
    74  // Note that codec options are no longer supported.
    75  func (s *Server) ServeCodec(codec ServerCodec, options CodecOption) {
    76  	defer codec.close()
    77  
    78  	// Don't serve if server is stopped.
    79  	if atomic.LoadInt32(&s.run) == 0 {
    80  		return
    81  	}
    82  
    83  	// Add the codec to the set so it can be closed by Stop.
    84  	s.codecs.Add(codec)
    85  	defer s.codecs.Remove(codec)
    86  
    87  	c := initClient(codec, s.idgen, &s.services)
    88  	<-codec.closed()
    89  	c.Close()
    90  }
    91  
    92  // serveSingleRequest reads and processes a single RPC request from the given codec. This
    93  // is used to serve HTTP connections. Subscriptions and reverse calls are not allowed in
    94  // this mode.
    95  func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
    96  	// Don't serve if server is stopped.
    97  	if atomic.LoadInt32(&s.run) == 0 {
    98  		return
    99  	}
   100  
   101  	h := newHandler(ctx, codec, s.idgen, &s.services)
   102  	h.allowSubscribe = false
   103  	defer h.close(io.EOF, nil)
   104  
   105  	reqs, batch, err := codec.readBatch()
   106  	if err != nil {
   107  		if err != io.EOF {
   108  			codec.writeJSON(ctx, errorMessage(&invalidMessageError{"parse error"}))
   109  		}
   110  		return
   111  	}
   112  	if batch {
   113  		h.handleBatch(reqs)
   114  	} else {
   115  		h.handleMsg(reqs[0])
   116  	}
   117  }
   118  
   119  // Stop stops reading new requests, waits for stopPendingRequestTimeout to allow pending
   120  // requests to finish, then closes all codecs which will cancel pending requests and
   121  // subscriptions.
   122  func (s *Server) Stop() {
   123  	if atomic.CompareAndSwapInt32(&s.run, 1, 0) {
   124  		log.Debug("RPC server shutting down")
   125  		s.codecs.Each(func(c interface{}) bool {
   126  			c.(ServerCodec).close()
   127  			return true
   128  		})
   129  	}
   130  }
   131  
   132  // RPCService gives meta information about the server.
   133  // e.g. gives information about the loaded modules.
   134  type RPCService struct {
   135  	server *Server
   136  }
   137  
   138  // Modules returns the list of RPC services with their version number
   139  func (s *RPCService) Modules() map[string]string {
   140  	s.server.services.mu.Lock()
   141  	defer s.server.services.mu.Unlock()
   142  
   143  	modules := make(map[string]string)
   144  	for name := range s.server.services.services {
   145  		modules[name] = "1.0"
   146  	}
   147  	return modules
   148  }
   149  
   150  // PeerInfo contains information about the remote end of the network connection.
   151  //
   152  // This is available within RPC method handlers through the context. Call
   153  // PeerInfoFromContext to get information about the client connection related to
   154  // the current method call.
   155  type PeerInfo struct {
   156  	// Transport is name of the protocol used by the client.
   157  	// This can be "http", "ws" or "ipc".
   158  	Transport string
   159  
   160  	// Address of client. This will usually contain the IP address and port.
   161  	RemoteAddr string
   162  
   163  	// Additional information for HTTP and WebSocket connections.
   164  	HTTP struct {
   165  		// Protocol version, i.e. "HTTP/1.1". This is not set for WebSocket.
   166  		Version string
   167  		// Header values sent by the client.
   168  		UserAgent string
   169  		Origin    string
   170  		Host      string
   171  	}
   172  }
   173  
   174  type peerInfoContextKey struct{}
   175  
   176  // PeerInfoFromContext returns information about the client's network connection.
   177  // Use this with the context passed to RPC method handler functions.
   178  //
   179  // The zero value is returned if no connection info is present in ctx.
   180  func PeerInfoFromContext(ctx context.Context) PeerInfo {
   181  	info, _ := ctx.Value(peerInfoContextKey{}).(PeerInfo)
   182  	return info
   183  }