Commit 27a8e0c8 authored by AlexStocks's avatar AlexStocks

Add: using easyjson

parent 1b60b2bc
# getty #
---
# getty
*a netty like asynchronous network I/O library*
## INTRODUCTION ##
---
## INTRO
Getty is a asynchronous network I/O library in golang. Getty is based on "ngo" whose author is [sanbit](https://github.com/sanbit). Getty works on tcp/udp/websocket network protocol and supplies [a uniform interface](https://github.com/alexstocks/getty/blob/master/getty.go#L45).
......@@ -15,7 +14,11 @@ Whatever if you use websocket, you do not need to care about hearbeat request/re
You can get code example in https://github.com/AlexStocks/getty-examples.
## LICENCE ##
---
## RPC
Working in progress.
## LICENCE
Apache License 2.0
......@@ -181,6 +181,7 @@ func (p *GettyPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
type GettyRPCHeaderLenType uint16
//easyjson:json
type GettyRPCRequestHeader struct {
Service string `json:"service,omitempty"`
Method string `json:"method,omitempty"`
......@@ -207,7 +208,7 @@ func NewGettyRPCRequest(server *Server) *GettyRPCRequest {
}
func (req *GettyRPCRequest) Marshal(buf *bytes.Buffer) error {
headerData, err := json.Marshal(req.header)
headerData, err := req.header.MarshalJSON()
if err != nil {
return jerrors.Trace(err)
}
......@@ -247,7 +248,7 @@ func (req *GettyRPCRequest) Unmarshal(buf *bytes.Buffer) error {
header := buf.Next(int(headerLen))
body := buf.Next(buf.Len())
err = json.Unmarshal(header, &req.header)
err = (&req.header).UnmarshalJSON(header)
if err != nil {
return jerrors.Trace(err)
}
......
// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
package rpc
import (
json "encoding/json"
easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)
// suppress unused package warning
var (
_ *json.RawMessage
_ *jlexer.Lexer
_ *jwriter.Writer
_ easyjson.Marshaler
)
func easyjson38c57360DecodeGithubComAlexStocksGettyRpc(in *jlexer.Lexer, out *GettyRPCRequestHeader) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "service":
out.Service = string(in.String())
case "method":
out.Method = string(in.String())
case "call_type":
out.CallType = gettyCallType(in.Uint32())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjson38c57360EncodeGithubComAlexStocksGettyRpc(out *jwriter.Writer, in GettyRPCRequestHeader) {
out.RawByte('{')
first := true
_ = first
if in.Service != "" {
const prefix string = ",\"service\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Service))
}
if in.Method != "" {
const prefix string = ",\"method\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Method))
}
if in.CallType != 0 {
const prefix string = ",\"call_type\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Uint32(uint32(in.CallType))
}
out.RawByte('}')
}
// MarshalJSON supports json.Marshaler interface
func (v GettyRPCRequestHeader) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
easyjson38c57360EncodeGithubComAlexStocksGettyRpc(&w, v)
return w.Buffer.BuildBytes(), w.Error
}
// MarshalEasyJSON supports easyjson.Marshaler interface
func (v GettyRPCRequestHeader) MarshalEasyJSON(w *jwriter.Writer) {
easyjson38c57360EncodeGithubComAlexStocksGettyRpc(w, v)
}
// UnmarshalJSON supports json.Unmarshaler interface
func (v *GettyRPCRequestHeader) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
easyjson38c57360DecodeGithubComAlexStocksGettyRpc(&r, v)
return r.Error()
}
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (v *GettyRPCRequestHeader) UnmarshalEasyJSON(l *jlexer.Lexer) {
easyjson38c57360DecodeGithubComAlexStocksGettyRpc(l, v)
}
......@@ -46,7 +46,7 @@ func (s *Server) Register(rcvr interface{}) error {
rcvr: reflect.ValueOf(rcvr),
name: reflect.Indirect(reflect.ValueOf(rcvr)).Type().Name(),
// Install the methods
method: prepareMethods(reflect.TypeOf(rcvr)),
method: suitableMethods(reflect.TypeOf(rcvr)),
}
if svc.name == "" {
s := "rpc.Register: no service name for type " + svc.typ.String()
......@@ -104,7 +104,7 @@ func (s *Server) newSession(session getty.Session) error {
session.SetName(s.conf.GettySessionParam.SessionName)
session.SetMaxMsgLen(s.conf.GettySessionParam.MaxMsgLen)
session.SetPkgHandler(NewRpcServerPacketHandler(s))
session.SetPkgHandler(NewRpcServerPackageHandler(s))
session.SetEventListener(NewRpcServerHandler(s.conf.SessionNumber, s.conf.sessionTimeout))
session.SetRQLen(s.conf.GettySessionParam.PkgRQSize)
session.SetWQLen(s.conf.GettySessionParam.PkgWQSize)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment