aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/codec/codec.go
blob: 3177f77e41e6e9af30caf3fa1620b6b20ad63fcb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package codec

import (
    "net"
    "strconv"

    "github.com/ethereum/go-ethereum/rpc/shared"
)

type Codec int

// (de)serialization support for rpc interface
type ApiCoder interface {
    // Parse message to request from underlying stream
    ReadRequest() ([]*shared.Request, bool, error)
    // Parse response message from underlying stream
    ReadResponse() (interface{}, error)
    // Encode response to encoded form in underlying stream
    WriteResponse(interface{}) error
    // Decode single message from data
    Decode([]byte, interface{}) error
    // Encode msg to encoded form
    Encode(msg interface{}) ([]byte, error)
    // close the underlying stream
    Close()
}

// supported codecs
const (
    JSON Codec = iota
    nCodecs
)

var (
    // collection with supported coders
    coders = make([]func(net.Conn) ApiCoder, nCodecs)
)

// create a new coder instance
func (c Codec) New(conn net.Conn) ApiCoder {
    switch c {
    case JSON:
        return NewJsonCoder(conn)
    }

    panic("codec: request for codec #" + strconv.Itoa(int(c)) + " is unavailable")
}