Skip to content

Commit

Permalink
change raw handler impl
Browse files Browse the repository at this point in the history
  • Loading branch information
name5566 committed Aug 25, 2016
1 parent 4987c9d commit fc93f26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
10 changes: 7 additions & 3 deletions network/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewProcessor() *Processor {
}

// It's dangerous to call the method on routing or marshaling (unmarshaling)
func (p *Processor) Register(msg interface{}) string {
func (p *Processor) Register(msg interface{}) {
msgType := reflect.TypeOf(msg)
if msgType == nil || msgType.Kind() != reflect.Ptr {
log.Fatal("json message pointer required")
Expand All @@ -50,7 +50,6 @@ func (p *Processor) Register(msg interface{}) string {
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 @@ -84,7 +83,12 @@ func (p *Processor) SetHandler(msg interface{}, msgHandler MsgHandler) {
}

// It's dangerous to call the method on routing or marshaling (unmarshaling)
func (p *Processor) SetRawHandler(msgID string, msgRawHandler MsgHandler) {
func (p *Processor) SetRawHandler(msg interface{}, msgRawHandler MsgHandler) {
msgType := reflect.TypeOf(msg)
if msgType == nil || msgType.Kind() != reflect.Ptr {
log.Fatal("json message pointer required")
}
msgID := msgType.Elem().Name()
i, ok := p.msgInfo[msgID]
if !ok {
log.Fatal("message %v not registered", msgID)
Expand Down
14 changes: 7 additions & 7 deletions network/protobuf/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (p *Processor) SetByteOrder(littleEndian bool) {
}

// It's dangerous to call the method on routing or marshaling (unmarshaling)
func (p *Processor) Register(msg proto.Message) uint16 {
func (p *Processor) Register(msg proto.Message) {
msgType := reflect.TypeOf(msg)
if msgType == nil || msgType.Kind() != reflect.Ptr {
log.Fatal("protobuf message pointer required")
Expand All @@ -62,9 +62,7 @@ func (p *Processor) Register(msg proto.Message) uint16 {
i := new(MsgInfo)
i.msgType = msgType
p.msgInfo = append(p.msgInfo, i)
id := uint16(len(p.msgInfo) - 1)
p.msgID[msgType] = id
return id
p.msgID[msgType] = uint16(len(p.msgInfo) - 1)
}

// It's dangerous to call the method on routing or marshaling (unmarshaling)
Expand All @@ -90,9 +88,11 @@ func (p *Processor) SetHandler(msg proto.Message, msgHandler MsgHandler) {
}

// It's dangerous to call the method on routing or marshaling (unmarshaling)
func (p *Processor) SetRawHandler(id uint16, msgRawHandler MsgHandler) {
if id >= uint16(len(p.msgInfo)) {
log.Fatal("message id %v not registered", id)
func (p *Processor) SetRawHandler(msg proto.Message, msgRawHandler MsgHandler) {
msgType := reflect.TypeOf(msg)
id, ok := p.msgID[msgType]
if !ok {
log.Fatal("message %s not registered", msgType)
}

p.msgInfo[id].msgRawHandler = msgRawHandler
Expand Down

0 comments on commit fc93f26

Please sign in to comment.