Skip to content

Commit

Permalink
raw handler
Browse files Browse the repository at this point in the history
  • Loading branch information
name5566 committed Aug 24, 2016
1 parent d24c94e commit 4987c9d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
47 changes: 40 additions & 7 deletions network/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,27 @@ type Processor struct {
}

type MsgInfo struct {
msgType reflect.Type
msgRouter *chanrpc.Server
msgHandler MsgHandler
msgType reflect.Type
msgRouter *chanrpc.Server
msgHandler MsgHandler
msgRawHandler MsgHandler
}

type MsgHandler func([]interface{})

type MsgRaw struct {
msgID string
msgRawData json.RawMessage
}

func NewProcessor() *Processor {
p := new(Processor)
p.msgInfo = make(map[string]*MsgInfo)
return p
}

// It's dangerous to call the method on routing or marshaling (unmarshaling)
func (p *Processor) Register(msg interface{}) {
func (p *Processor) Register(msg interface{}) string {
msgType := reflect.TypeOf(msg)
if msgType == nil || msgType.Kind() != reflect.Ptr {
log.Fatal("json message pointer required")
Expand All @@ -44,6 +50,7 @@ func (p *Processor) Register(msg interface{}) {
i := new(MsgInfo)
i.msgType = msgType
p.msgInfo[msgID] = i
return msgID
}

// It's dangerous to call the method on routing or marshaling (unmarshaling)
Expand Down Expand Up @@ -76,8 +83,31 @@ func (p *Processor) SetHandler(msg interface{}, msgHandler MsgHandler) {
i.msgHandler = msgHandler
}

// It's dangerous to call the method on routing or marshaling (unmarshaling)
func (p *Processor) SetRawHandler(msgID string, msgRawHandler MsgHandler) {
i, ok := p.msgInfo[msgID]
if !ok {
log.Fatal("message %v not registered", msgID)
}

i.msgRawHandler = msgRawHandler
}

// goroutine safe
func (p *Processor) Route(msg interface{}, userData interface{}) error {
// raw
if msgRaw, ok := msg.(MsgRaw); ok {
i, ok := p.msgInfo[msgRaw.msgID]
if !ok {
return fmt.Errorf("message %v not registered", msgRaw.msgID)
}
if i.msgRawHandler != nil {
i.msgRawHandler([]interface{}{msgRaw.msgID, msgRaw.msgRawData, userData})
}
return nil
}

// json
msgType := reflect.TypeOf(msg)
if msgType == nil || msgType.Kind() != reflect.Ptr {
return errors.New("json message pointer required")
Expand All @@ -87,7 +117,6 @@ func (p *Processor) Route(msg interface{}, userData interface{}) error {
if !ok {
return fmt.Errorf("message %v not registered", msgID)
}

if i.msgHandler != nil {
i.msgHandler([]interface{}{msg, userData})
}
Expand Down Expand Up @@ -115,8 +144,12 @@ func (p *Processor) Unmarshal(data []byte) (interface{}, error) {
}

// msg
msg := reflect.New(i.msgType.Elem()).Interface()
return msg, json.Unmarshal(data, msg)
if i.msgRawHandler != nil {
return MsgRaw{msgID, data}, nil
} else {
msg := reflect.New(i.msgType.Elem()).Interface()
return msg, json.Unmarshal(data, msg)
}
}

panic("bug")
Expand Down
3 changes: 1 addition & 2 deletions network/protobuf/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ func (p *Processor) SetRawHandler(id uint16, msgRawHandler MsgHandler) {

// goroutine safe
func (p *Processor) Route(msg interface{}, userData interface{}) error {
msgType := reflect.TypeOf(msg)

// raw
if msgRaw, ok := msg.(MsgRaw); ok {
if msgRaw.msgID >= uint16(len(p.msgInfo)) {
Expand All @@ -115,6 +113,7 @@ func (p *Processor) Route(msg interface{}, userData interface{}) error {
}

// protobuf
msgType := reflect.TypeOf(msg)
id, ok := p.msgID[msgType]
if !ok {
return fmt.Errorf("message %s not registered", msgType)
Expand Down

0 comments on commit 4987c9d

Please sign in to comment.