rpc, p2p/simulations: use github.com/gorilla/websocket (#20289)
* rpc: improve codec abstraction rpc.ServerCodec is an opaque interface. There was only one way to get a codec using existing APIs: rpc.NewJSONCodec. This change exports newCodec (as NewFuncCodec) and NewJSONCodec (as NewCodec). It also makes all codec methods non-public to avoid showing internals in godoc. While here, remove codec options in tests because they are not supported anymore. * p2p/simulations: use github.com/gorilla/websocket This package was the last remaining user of golang.org/x/net/websocket. Migrating to the new library wasn't straightforward because it is no longer possible to treat WebSocket connections as a net.Conn. * vendor: delete golang.org/x/net/websocket * rpc: fix godoc comments and run gofmt
This commit is contained in:
committed by
Péter Szilágyi
parent
9e71f55bfa
commit
7c4a4eb58a
56
rpc/json.go
56
rpc/json.go
@ -164,43 +164,45 @@ func (c connWithRemoteAddr) RemoteAddr() string { return c.addr }
|
||||
// jsonCodec reads and writes JSON-RPC messages to the underlying connection. It also has
|
||||
// support for parsing arguments and serializing (result) objects.
|
||||
type jsonCodec struct {
|
||||
remoteAddr string
|
||||
closer sync.Once // close closed channel once
|
||||
closed chan interface{} // closed on Close
|
||||
decode func(v interface{}) error // decoder to allow multiple transports
|
||||
encMu sync.Mutex // guards the encoder
|
||||
encode func(v interface{}) error // encoder to allow multiple transports
|
||||
conn deadlineCloser
|
||||
remote string
|
||||
closer sync.Once // close closed channel once
|
||||
closeCh chan interface{} // closed on Close
|
||||
decode func(v interface{}) error // decoder to allow multiple transports
|
||||
encMu sync.Mutex // guards the encoder
|
||||
encode func(v interface{}) error // encoder to allow multiple transports
|
||||
conn deadlineCloser
|
||||
}
|
||||
|
||||
func newCodec(conn deadlineCloser, encode, decode func(v interface{}) error) ServerCodec {
|
||||
// NewFuncCodec creates a codec which uses the given functions to read and write. If conn
|
||||
// implements ConnRemoteAddr, log messages will use it to include the remote address of
|
||||
// the connection.
|
||||
func NewFuncCodec(conn deadlineCloser, encode, decode func(v interface{}) error) ServerCodec {
|
||||
codec := &jsonCodec{
|
||||
closed: make(chan interface{}),
|
||||
encode: encode,
|
||||
decode: decode,
|
||||
conn: conn,
|
||||
closeCh: make(chan interface{}),
|
||||
encode: encode,
|
||||
decode: decode,
|
||||
conn: conn,
|
||||
}
|
||||
if ra, ok := conn.(ConnRemoteAddr); ok {
|
||||
codec.remoteAddr = ra.RemoteAddr()
|
||||
codec.remote = ra.RemoteAddr()
|
||||
}
|
||||
return codec
|
||||
}
|
||||
|
||||
// NewJSONCodec creates a codec that reads from the given connection. If conn implements
|
||||
// ConnRemoteAddr, log messages will use it to include the remote address of the
|
||||
// connection.
|
||||
func NewJSONCodec(conn Conn) ServerCodec {
|
||||
// NewCodec creates a codec on the given connection. If conn implements ConnRemoteAddr, log
|
||||
// messages will use it to include the remote address of the connection.
|
||||
func NewCodec(conn Conn) ServerCodec {
|
||||
enc := json.NewEncoder(conn)
|
||||
dec := json.NewDecoder(conn)
|
||||
dec.UseNumber()
|
||||
return newCodec(conn, enc.Encode, dec.Decode)
|
||||
return NewFuncCodec(conn, enc.Encode, dec.Decode)
|
||||
}
|
||||
|
||||
func (c *jsonCodec) RemoteAddr() string {
|
||||
return c.remoteAddr
|
||||
func (c *jsonCodec) remoteAddr() string {
|
||||
return c.remote
|
||||
}
|
||||
|
||||
func (c *jsonCodec) Read() (msg []*jsonrpcMessage, batch bool, err error) {
|
||||
func (c *jsonCodec) readBatch() (msg []*jsonrpcMessage, batch bool, err error) {
|
||||
// Decode the next JSON object in the input stream.
|
||||
// This verifies basic syntax, etc.
|
||||
var rawmsg json.RawMessage
|
||||
@ -211,8 +213,7 @@ func (c *jsonCodec) Read() (msg []*jsonrpcMessage, batch bool, err error) {
|
||||
return msg, batch, nil
|
||||
}
|
||||
|
||||
// Write sends a message to client.
|
||||
func (c *jsonCodec) Write(ctx context.Context, v interface{}) error {
|
||||
func (c *jsonCodec) writeJSON(ctx context.Context, v interface{}) error {
|
||||
c.encMu.Lock()
|
||||
defer c.encMu.Unlock()
|
||||
|
||||
@ -224,17 +225,16 @@ func (c *jsonCodec) Write(ctx context.Context, v interface{}) error {
|
||||
return c.encode(v)
|
||||
}
|
||||
|
||||
// Close the underlying connection
|
||||
func (c *jsonCodec) Close() {
|
||||
func (c *jsonCodec) close() {
|
||||
c.closer.Do(func() {
|
||||
close(c.closed)
|
||||
close(c.closeCh)
|
||||
c.conn.Close()
|
||||
})
|
||||
}
|
||||
|
||||
// Closed returns a channel which will be closed when Close is called
|
||||
func (c *jsonCodec) Closed() <-chan interface{} {
|
||||
return c.closed
|
||||
func (c *jsonCodec) closed() <-chan interface{} {
|
||||
return c.closeCh
|
||||
}
|
||||
|
||||
// parseMessage parses raw bytes as a (batch of) JSON-RPC message(s). There are no error
|
||||
|
Reference in New Issue
Block a user