mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-13 15:14:46 +08:00
扩统rpc序列化与反序列化支持
This commit is contained in:
@@ -1,14 +1,27 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
|
||||
"encoding/json"
|
||||
|
||||
)
|
||||
import "encoding/json"
|
||||
|
||||
type JsonProcessor struct {
|
||||
}
|
||||
|
||||
type JsonRpcRequestData struct {
|
||||
//packhead
|
||||
Seq uint64 // sequence number chosen by client
|
||||
ServiceMethod string // format: "Service.Method"
|
||||
NoReply bool //是否需要返回
|
||||
//packbody
|
||||
InParam []byte
|
||||
}
|
||||
|
||||
type JsonRpcResponseData struct {
|
||||
//head
|
||||
Seq uint64 // sequence number chosen by client
|
||||
Err string
|
||||
|
||||
//returns
|
||||
Reply []byte
|
||||
}
|
||||
|
||||
|
||||
func (slf *JsonProcessor) Marshal(v interface{}) ([]byte, error){
|
||||
@@ -16,7 +29,56 @@ func (slf *JsonProcessor) Marshal(v interface{}) ([]byte, error){
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) Unmarshal(data []byte, v interface{}) error{
|
||||
|
||||
return json.Unmarshal(data,v)
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) MakeRpcRequest(seq uint64,serviceMethod string,noReply bool,inParam []byte) IRpcRequestData{
|
||||
return &JsonRpcRequestData{Seq:seq,ServiceMethod:serviceMethod,NoReply:noReply,InParam:inParam}
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) MakeRpcResponse(seq uint64,err *RpcError,reply []byte) IRpcResponseData {
|
||||
return &JsonRpcResponseData{
|
||||
Seq: seq,
|
||||
Err: err.Error(),
|
||||
Reply: reply,
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *JsonRpcRequestData) IsReply() bool{
|
||||
return slf.NoReply
|
||||
}
|
||||
|
||||
func (slf *JsonRpcRequestData) GetSeq() uint64{
|
||||
return slf.Seq
|
||||
}
|
||||
|
||||
func (slf *JsonRpcRequestData) GetServiceMethod() string{
|
||||
return slf.ServiceMethod
|
||||
}
|
||||
|
||||
func (slf *JsonRpcRequestData) GetInParam() []byte{
|
||||
return slf.InParam
|
||||
}
|
||||
|
||||
func (slf *JsonRpcResponseData) GetSeq() uint64 {
|
||||
return slf.Seq
|
||||
}
|
||||
|
||||
func (slf *JsonRpcResponseData) GetErr() *RpcError {
|
||||
if slf.Err == ""{
|
||||
return nil
|
||||
}
|
||||
|
||||
return Errorf(slf.Err)
|
||||
}
|
||||
|
||||
|
||||
func (slf *JsonRpcResponseData) GetReply() []byte{
|
||||
return slf.Reply
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user