Documentation
¶
Overview ¶
Package rpc support rpc call methos for both server and client.
Index ¶
- Constants
- func DebugF(format string, argvs ...interface{})
- func JSON(w http.ResponseWriter, statusCode int, v interface{}) error
- func String(w http.ResponseWriter, statusCode int, byts []byte) error
- type Client
- type ClientCodec
- type Codec
- type Error
- type Request
- type Response
- type Server
- type ServerCodec
Constants ¶
const ( // Success 0 . Success = 0 // ParseErr -32700 语法解析错误,服务端接收到无效的json。该错误发送于服务器尝试解析json文本 ParseErr = -32700 // InvalidRequest -32600 无效请求发送的json不是一个有效的请求对象。 InvalidRequest = -32600 // MethodNotFound -32601 找不到方法 该方法不存在或无效 MethodNotFound = -32601 // InvalidParamErr -32602 无效的参数 无效的方法参数。 InvalidParamErr = -32602 // InternalErr -32603 内部错误 JSON-RPC内部错误。 InternalErr = -32603 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client as a data struct to connect to server, send and recv data TODO: finish multi request support
func NewClientWithCodec ¶
func NewClientWithCodec(codec ClientCodec, tcpAddr string) *Client
NewClientWithCodec generate a Client prototype rpc.NewClientWithCodec(codec Codec, tcpAddr string) if codec is nil will use default gobCodec, tcpAddr or httpAddr is empty only when you are sure about it will never be used, otherwise it panic while using some functions.
type ClientCodec ¶
type ClientCodec interface {
// generate a single NewRequest with needed params
NewRequest(method string, argv interface{}) Request
// EncodeRequests .
EncodeRequests(v interface{}) ([]byte, error)
// parse encoded data into a Response
ReadResponse(data []byte) ([]Response, error)
// ReadResponseBody .
ReadResponseBody(respBody []byte, rcvr interface{}) error
}
ClientCodec .
type Codec ¶
type Codec interface {
ServerCodec
ClientCodec
}
Codec ... to encode and decode for client to encode request and decode response for server to encode response den decode request
type Request ¶
type Request interface {
// Method() to return the string of method name.
// exmaple, "StructDemo.Method1"
// and, should has no more form
Method() string
// Params means all ([]byte) data contains request args
// all these origin params (interface{} type)
// should be result of codec encoded
Params() []byte
// Ident to get indentify of request
Ident() string
}
Request interface contains necessary methods
type Response ¶
type Response interface {
// Error to return err that response struct contains
// if there is no any error happend, should return nil
Error() error
// ErrorCode to return errcode(int) if ok return SUCCESS(0)
ErrCode() int
// Reply means all ([]byte) data contains response body
// all these response data (interface{} type)
// should be result of codec encoded
Reply() []byte
// SetReqIdent .
SetReqIdent(ident string)
}
Response interface contains necessary methods
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server data struct to serve RPC request over TCP and HTTP
func NewServerWithCodec ¶
func NewServerWithCodec(codec ServerCodec) *Server
NewServerWithCodec generate a server to handle all tcp request from rpc client, if codec is nil will use default gobCodec
func (*Server) ListenAndServe ¶
ListenAndServe open http support can serve http request
func (*Server) Register ¶
Register parse register type and method maybe save into a Map, input value is a varible want to got varible type name, and all Method Name
func (*Server) RegisterName ¶
RegisterName ... only want to export one method of rcvr
type ServerCodec ¶
type ServerCodec interface {
// parse encoded data into a Request
ReadRequest(data []byte) ([]Request, error)
// ReadRequestBody parse params
ReadRequestBody(reqBody []byte, rcvr interface{}) error
// generate a single Response with needed params
NewResponse(replyv interface{}) Response
// ErrResponse to generate a Reponse contains error
ErrResponse(errcode int, err error) Response
// EncodeResponses .
EncodeResponses(v interface{}) ([]byte, error)
}
ServerCodec . parse request and write response to client.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
json2-array/client
command
|
|
|
json2-array/sever
command
|
|
|
json2/client
command
|
|
|
json2/server
command
|
|
|
rpc/client
command
|
|
|
rpc/server
command
|
|
|
Package jsonrpc is JSON-RPC 2.0 support codec and more functions link to url: https://www.jsonrpc.org/specification
|
Package jsonrpc is JSON-RPC 2.0 support codec and more functions link to url: https://www.jsonrpc.org/specification |
|
Package proto .
|
Package proto . |