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

     1  // 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/doc.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  /*
    22  Package rpc provides access to the exported methods of an object across a network
    23  or other I/O connection. After creating a server instance objects can be registered,
    24  making it visible from the outside. Exported methods that follow specific
    25  conventions can be called remotely. It also has support for the publish/subscribe
    26  pattern.
    27  
    28  Methods that satisfy the following criteria are made available for remote access:
    29   - object must be exported
    30   - method must be exported
    31   - method returns 0, 1 (response or error) or 2 (response and error) values
    32   - method argument(s) must be exported or builtin types
    33   - method returned value(s) must be exported or builtin types
    34  
    35  An example method:
    36   func (s *CalcService) Add(a, b int) (int, error)
    37  
    38  When the returned error isn't nil the returned integer is ignored and the error is
    39  send back to the client. Otherwise the returned integer is send back to the client.
    40  
    41  Optional arguments are supported by accepting pointer values as arguments. E.g.
    42  if we want to do the addition in an optional finite field we can accept a mod
    43  argument as pointer value.
    44  
    45   func (s *CalService) Add(a, b int, mod *int) (int, error)
    46  
    47  This RPC method can be called with 2 integers and a null value as third argument.
    48  In that case the mod argument will be nil. Or it can be called with 3 integers,
    49  in that case mod will be pointing to the given third argument. Since the optional
    50  argument is the last argument the RPC package will also accept 2 integers as
    51  arguments. It will pass the mod argument as nil to the RPC method.
    52  
    53  The server offers the ServeCodec method which accepts a ServerCodec instance. It will
    54  read requests from the codec, process the request and sends the response back to the
    55  client using the codec. The server can execute requests concurrently. Responses
    56  can be sent back to the client out of order.
    57  
    58  An example server which uses the JSON codec:
    59   type CalculatorService struct {}
    60  
    61   func (s *CalculatorService) Add(a, b int) int {
    62  	return a + b
    63   }
    64  
    65   func (s *CalculatorService Div(a, b int) (int, error) {
    66  	if b == 0 {
    67  		return 0, errors.New("divide by zero")
    68  	}
    69  	return a/b, nil
    70   }
    71  
    72   calculator := new(CalculatorService)
    73   server := NewServer()
    74   server.RegisterName("calculator", calculator")
    75  
    76   l, _ := net.ListenUnix("unix", &net.UnixAddr{Net: "unix", Name: "/tmp/calculator.sock"})
    77   for {
    78  	c, _ := l.AcceptUnix()
    79  	codec := v2.NewJSONCodec(c)
    80  	go server.ServeCodec(codec)
    81   }
    82  
    83  The package also supports the publish subscribe pattern through the use of subscriptions.
    84  A method that is considered eligible for notifications must satisfy the following criteria:
    85   - object must be exported
    86   - method must be exported
    87   - first method argument type must be context.Context
    88   - method argument(s) must be exported or builtin types
    89   - method must return the tuple Subscription, error
    90  
    91  An example method:
    92   func (s *BlockChainService) NewBlocks(ctx context.Context) (Subscription, error) {
    93   	...
    94   }
    95  
    96  Subscriptions are deleted when:
    97   - the user sends an unsubscribe request
    98   - the connection which was used to create the subscription is closed. This can be initiated
    99     by the client and server. The server will close the connection on a write error or when
   100     the queue of buffered notifications gets too big.
   101  */
   102  package rpc