Ejemplo n.º 1
0
// 注册连接消息
func RegisterSessionMessage(msgIns interface{}, userHandler func(interface{}, cellnet.Session, int64)) {

	msgMeta := cellnet.NewMessageMeta(msgIns)

	// 将消息注册到mapper中, 提供反射用
	socket.MapNameID(msgMeta.Name, msgMeta.ID)

	for _, conn := range gateConnArray {

		conn.RegisterCallback(msgMeta.ID, func(data interface{}) {

			if ev, ok := data.(*relayEvent); ok {

				rawMsg, err := cellnet.ParsePacket(ev.Packet, msgMeta.Type)

				if err != nil {
					log.Errorln("[gate] unmarshaling error:\n", err)
					return
				}

				userHandler(rawMsg, ev.Ses, ev.ClientID)

			}

		})
	}

}
Ejemplo n.º 2
0
// 注册连接消息
func RegisterMessage(eq cellnet.EventQueue, msgIns interface{}, userHandler func(Response, interface{})) {

	msgMeta := cellnet.NewMessageMeta(msgIns)

	// 将消息注册到mapper中, 提供反射用
	socket.MapNameID(msgMeta.Name, msgMeta.ID)

	eq.RegisterCallback(msgMeta.ID, func(data interface{}) {

		if ev, ok := data.(*response); ok {

			rawMsg, err := cellnet.ParsePacket(&cellnet.Packet{
				MsgID: ev.req.GetMsgID(),
				Data:  ev.req.Data,
			}, msgMeta.Type)

			if err != nil {
				log.Errorln("[cellnet] unmarshaling error:\n", err)
				return
			}

			userHandler(ev, rawMsg)

		}

	})

}
Ejemplo n.º 3
0
// 注册连接消息
func RegisterSessionMessage(eq cellnet.EventQueue, msgIns interface{}, userHandler func(interface{}, cellnet.Session)) *cellnet.MessageMeta {

	msgMeta := cellnet.NewMessageMeta(msgIns)

	// 将消息注册到mapper中, 提供反射用
	MapNameID(msgMeta.Name, msgMeta.ID)

	eq.RegisterCallback(msgMeta.ID, func(data interface{}) {

		if ev, ok := data.(*SessionEvent); ok {

			rawMsg, err := cellnet.ParsePacket(ev.Packet, msgMeta.Type)

			if err != nil {
				log.Printf("[cellnet] unmarshaling error:\n", err)
				return
			}

			userHandler(rawMsg, ev.Ses)

		}

	})

	return msgMeta
}
Ejemplo n.º 4
0
// 注册连接消息
func RegisterSessionMessage(eq cellnet.EventQueue, msgIns interface{}, userHandler func(interface{}, cellnet.Session)) *cellnet.MessageMeta {

	msgMeta := cellnet.NewMessageMeta(msgIns)

	eq.RegisterCallback(msgMeta.ID, func(data interface{}) {

		if ev, ok := data.(*SessionEvent); ok {

			rawMsg, err := cellnet.ParsePacket(ev.Packet, msgMeta.Type)

			if err != nil {
				log.Errorln("unmarshaling error:\n", err)
				return
			}

			log.Debugf("#recv(%s) sid: %d %s(%d)|%s", ev.Ses.FromPeer().Name(), ev.Ses.ID(), msgMeta.Name, len(ev.Packet.Data), rawMsg.(proto.Message).String())

			userHandler(rawMsg, ev.Ses)

		}

	})

	return msgMeta
}
Ejemplo n.º 5
0
// 注册连接消息
func RegisterPeerMessage(eq cellnet.EventQueue, msgIns interface{}, userHandler func(interface{}, cellnet.Peer)) *cellnet.MessageMeta {

	msgMeta := cellnet.NewMessageMeta(msgIns)

	eq.RegisterCallback(msgMeta.ID, func(data interface{}) {

		if ev, ok := data.(*PeerEvent); ok {

			rawMsg := reflect.New(msgMeta.Type).Interface()

			userHandler(rawMsg, ev.P)

		}

	})

	return msgMeta
}
Ejemplo n.º 6
0
package socket

import (
	"github.com/davyxu/cellnet"
	"github.com/davyxu/cellnet/proto/coredef"
)

var (
	Event_SessionConnected = uint32(cellnet.NewMessageMeta(&coredef.SessionConnected{}).ID)
	Event_SessionClosed    = uint32(cellnet.NewMessageMeta(&coredef.SessionClosed{}).ID)
	Event_SessionAccepted  = uint32(cellnet.NewMessageMeta(&coredef.SessionAccepted{}).ID)
	Event_PeerInit         = uint32(cellnet.NewMessageMeta(&coredef.PeerInit{}).ID)
	Event_PeerStart        = uint32(cellnet.NewMessageMeta(&coredef.PeerStart{}).ID)
	Event_PeerStop         = uint32(cellnet.NewMessageMeta(&coredef.PeerStop{}).ID)
)

// 会话事件
type SessionEvent struct {
	*cellnet.Packet
	Ses cellnet.Session
}

func NewSessionEvent(msgid uint32, s cellnet.Session, data []byte) *SessionEvent {
	return &SessionEvent{
		Packet: &cellnet.Packet{MsgID: msgid, Data: data},
		Ses:    s,
	}
}

// 端事件
type PeerEvent struct {