Exemple #1
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)

		}

	})

}
Exemple #2
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)

			}

		})
	}

}
Exemple #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.Errorln("[cellnet] unmarshaling error:\n", err)
				return
			}

			userHandler(rawMsg, ev.Ses)

		}

	})

	return msgMeta
}
Exemple #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("[cellnet] 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
}
Exemple #5
0
func (self *socketAcceptor) Start(address string) cellnet.Peer {

	ln, err := net.Listen("tcp", address)

	self.listener = ln

	if err != nil {

		log.Errorln("[socket] listen failed", err.Error())
		return self
	}

	log.Debugln("[socket] listening: ", address)

	self.running = true

	// 接受线程
	go func() {
		for self.running {
			conn, err := ln.Accept()

			if err != nil {
				continue
			}

			ses := newSession(NewPacketStream(conn), self.EventQueue, self)

			// 添加到管理器
			self.sessionMgr.Add(ses)

			// 断开后从管理器移除
			ses.OnClose = func() {
				self.sessionMgr.Remove(ses)
			}

			// 通知逻辑
			self.PostData(NewSessionEvent(Event_SessionAccepted, ses, nil))

		}

	}()

	self.PostData(NewPeerEvent(Event_PeerStart, self))

	return self
}
Exemple #6
0
func db() {
	pipe := cellnet.NewEventPipe()

	evq := pipe.AddQueue()

	pipe.Start()

	db := mgo.NewDB()

	var err error

	err = db.Start(&mgo.Config{
		URL:     "127.0.0.1:27017/test",
		ShowLog: true,
	})

	if err != nil {
		log.Errorln("db connect failed:", err)
		signal.Fail()
		return
	}

	db.FindOne(evq, "test", bson.M{"name": "davy"}, func(c *char, _ error) {

		// 没有记录, 创建
		if c == nil {
			db.Insert(evq, "test", &char{Name: "davy", HP: 10}, nil)
			db.Insert(evq, "test", &char{Name: "zerg", HP: 90}, func(_ error) {

				db.FindOne(evq, "test", bson.M{"name": "davy"}, func(c *char, _ error) {

					if c == nil {
						signal.Log("can not found record")
						signal.Fail()
					} else {
						log.Debugln(c)
						signal.Done(1)
					}

				})

			})

			// 有记录, 搞定
		} else {

			log.Debugln(c)
			signal.Done(1)

		}

	})

	db.Update(evq, "test", bson.M{"name": "davy"}, &char{Name: "davy", HP: 1}, func(err error) {

		if err != nil {
			signal.Log("update failed")
			signal.Fail()
		}

		db.FindOne(evq, "test", bson.M{"name": "davy"}, func(c *char, _ error) {

			if c == nil {
				signal.Log("update failed")
				signal.Fail()
			} else {
				if c.HP != 1 {
					signal.Fail()
				} else {
					signal.Done(2)
				}

			}

		})
	})

	signal.WaitAndExpect(1, "find failed")
	signal.WaitAndExpect(2, "update failed")

}
Exemple #7
0
func (self *socketConnector) connect(address string) {
	self.working = true

	for {

		// 开始连接
		cn, err := net.Dial("tcp", address)

		// 连不上
		if err != nil {

			log.Errorln("[socket] cononect failed", err.Error())

			// 没重连就退出
			if self.autoReconnectSec == 0 {
				break
			}

			// 有重连就等待
			time.Sleep(time.Duration(self.autoReconnectSec) * time.Second)

			// 继续连接
			continue
		}

		log.Debugln("[socket] connected: ", address)

		// 连上了, 记录连接
		self.conn = cn

		// 创建Session
		ses := newSession(NewPacketStream(cn), self.EventQueue, self)
		self.sessionMgr.Add(ses)
		self.defaultSes = ses

		// 内部断开回调
		ses.OnClose = func() {
			self.sessionMgr.Remove(ses)
			self.closeSignal <- true
		}

		// 抛出事件
		self.PostData(NewSessionEvent(Event_SessionConnected, ses, nil))

		if <-self.closeSignal {

			self.conn = nil

			// 没重连就退出
			if self.autoReconnectSec == 0 {
				break
			}

			// 有重连就等待
			time.Sleep(time.Duration(self.autoReconnectSec) * time.Second)

			// 继续连接
			continue

		}

	}

	self.working = false
}