示例#1
0
package socket

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

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

// 会话事件
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 {
示例#2
0
package dispatcher

import (
	"github.com/davyxu/cellnet"
	"github.com/davyxu/cellnet/ltvsocket"
	"github.com/davyxu/cellnet/proto/coredef"
	"github.com/golang/protobuf/proto"
	"log"
)

type errInterface interface {
	Error() string
}

var (
	msgConnected = uint32(cellnet.Type2ID(&coredef.ConnectedACK{}))
	msgAccepted  = uint32(cellnet.Type2ID(&coredef.AcceptedACK{}))
	msgClosed    = uint32(cellnet.Type2ID(&coredef.ClosedACK{}))
)

// 处理Peer的新会话及会话的消息处理
func PeerHandler(disp *PacketDispatcher) func(cellnet.CellID, interface{}) {

	return func(peer cellnet.CellID, peerev interface{}) {

		switch v := peerev.(type) {
		case ltvsocket.EventNewSession: // 新的连接生成

			var msgid uint32
			switch peerev.(type) {
			case ltvsocket.EventConnected: