Example #1
0
// 客户端
func client() {

	pipe := cellnet.NewEventPipe()

	evq := socket.NewConnector(pipe).Start("127.0.0.1:7101")

	socket.RegisterSessionMessage(evq, coredef.SessionConnected{}, func(content interface{}, ses cellnet.Session) {

		signal.Done(1)

		ack := &coredef.TestEchoACK{
			Content: proto.String("hello"),
		}
		ses.Send(ack)

		log.Debugf("client send: %s\n", ack.String())

	})

	socket.RegisterSessionMessage(evq, coredef.TestEchoACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.TestEchoACK)

		log.Debugf("client recv:", msg.String())

		signal.Done(3)
	})

	pipe.Start()

	signal.WaitAndExpect(1, "not connceted to gate")
	signal.WaitAndExpect(2, "not recv client msg")
	signal.WaitAndExpect(3, "not recv server msg")

}
Example #2
0
// 后台服务器
func backendServer() {

	gate.DebugMode = true

	pipe := cellnet.NewEventPipe()

	gate.StartGateConnector(pipe, []string{"127.0.0.1:7201"})

	gate.RegisterSessionMessage(coredef.SessionClosed{}, func(content interface{}, gateSes cellnet.Session, clientid int64) {
		log.Debugf("client closed gate: %d clientid: %d\n", gateSes.ID(), clientid)
	})

	gate.RegisterSessionMessage(coredef.TestEchoACK{}, func(content interface{}, gateSes cellnet.Session, clientid int64) {
		msg := content.(*coredef.TestEchoACK)

		log.Debugf("recv relay,  gate: %d clientid: %d\n", gateSes.ID(), clientid)

		signal.Done(2)

		gate.SendToClient(gateSes, clientid, &coredef.TestEchoACK{
			Content: proto.String(msg.GetContent()),
		})
	})

	pipe.Start()
}
Example #3
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
}
Example #4
0
func (self *mongoDriver) Start(rawcfg interface{}) error {
	self.Config = rawcfg.(*Config)

	if self.ConnCount == 0 {
		self.ConnCount = 1
	}

	if self.ShowLog {
		log.Debugf("[mgo] db connection %d", self.ConnCount)
	}

	self.dbChan = make(chan *mgo.Database, self.ConnCount)

	for i := 0; i < self.ConnCount; i++ {

		ses, err := mgo.Dial(self.URL)
		if err != nil {

			if self.ShowLog {
				log.Errorf("[mgo] %s", err)
			}

			return err
		}

		ses.SetMode(mgo.Monotonic, true)

		// 默认db
		self.dbChan <- ses.DB("")
	}

	return nil
}
Example #5
0
func (self *ltvSession) Send(data interface{}) {

	pkt, meta := cellnet.BuildPacket(data)

	log.Debugf("#send(%s) sid: %d %s(%d)|%s", self.FromPeer().Name(), self.ID(), meta.Name, len(pkt.Data), data.(proto.Message).String())

	self.RawSend(pkt)
}
Example #6
0
// 开启客户端侦听通道
func StartClientAcceptor(pipe cellnet.EventPipe, address string) {

	ClientAcceptor = socket.NewAcceptor(pipe)

	// 所有接收到的消息转发到后台
	ClientAcceptor.InjectData(func(data interface{}) bool {

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

			// Socket各种事件不要往后台发
			switch ev.MsgID {
			case socket.Event_SessionAccepted,
				socket.Event_SessionConnected:
				return true
			}

			// 构建路由封包
			relaypkt := cellnet.BuildPacket(&coredef.UpstreamACK{
				MsgID:    proto.Uint32(ev.MsgID),
				Data:     ev.Data,
				ClientID: proto.Int64(ev.Ses.ID()),
			})

			// TODO 按照封包和逻辑固定分发
			// TODO 非法消息直接掐线
			// TODO 心跳, 超时掐线
			BackendAcceptor.IterateSession(func(ses cellnet.Session) bool {

				if DebugMode {
					log.Debugf("[gate] client->backend, msgid: %d clientid: %d data: %v", ev.MsgID, ev.Ses.ID(), ev.Data)
				}

				ses.RawSend(relaypkt)

				return true
			})

		}

		return false
	})

	ClientAcceptor.Start(address)

}
Example #7
0
func (self *mongoDriver) Update(evq cellnet.EventQueue, collName string, selector interface{}, doc interface{}, callback func(error)) {

	self.writeTask(evq, callback, func(db *mgo.Database) error {

		if self.ShowLog {
			log.Debugf("[mgo] update '%s' sel: %s update: %v", collName, selector, doc)
		}

		err := db.C(collName).Update(selector, doc)

		if err != nil && self.ShowLog {

			log.Errorf("[mgo] update failed, %s", err)

		}

		return err
	})
}
Example #8
0
func (self *mongoDriver) FindOne(evq cellnet.EventQueue, collName string, query interface{}, callback interface{}) {

	self.readTask(evq, callback, func(db *mgo.Database, result interface{}) error {

		if self.ShowLog {
			log.Debugf("[mgo] findone '%s' query: %s", collName, query)
		}

		err := db.C(collName).Find(query).One(result)

		if err != nil && self.ShowLog {

			log.Errorf("[mgo] findone failed, %s", err)
		}

		return err
	})

}
Example #9
0
func (self *mongoDriver) Insert(evq cellnet.EventQueue, collName string, doc interface{}, callback func(error)) {

	self.writeTask(evq, callback, func(db *mgo.Database) error {

		if self.ShowLog {
			log.Debugf("[mgo] insert '%s' %v", collName, doc)
		}

		err := db.C(collName).Insert(doc)

		if err != nil && self.ShowLog {

			log.Errorf("[mgo] insert failed, %s", err)

		}

		return err
	})

}
Example #10
0
// 开启后台服务器的侦听通道
func StartBackendAcceptor(pipe cellnet.EventPipe, address string) {

	BackendAcceptor = socket.NewAcceptor(pipe)

	// 关闭客户端连接
	socket.RegisterSessionMessage(BackendAcceptor, coredef.CloseClientACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.CloseClientACK)

		if msg.ClientID == nil {

			// 关闭所有客户端
			ClientAcceptor.IterateSession(func(ses cellnet.Session) bool {

				if DebugMode {
					log.Debugf("[gate] backend->client, close clientid %d", msg.GetClientID())
				}
				ses.Close()

				return true
			})

		} else {

			// 关闭指定客户端
			clientSes := ClientAcceptor.GetSession(msg.GetClientID())

			// 找到连接并关闭
			if clientSes != nil {

				if DebugMode {
					log.Debugf("[gate] backend->client, close clientid %d", msg.GetClientID())
				}

				clientSes.Close()
			} else if DebugMode {
				log.Debugf("[gate] backend->client, client not found, close failed, clientid %d", msg.GetClientID())
			}

		}

	})

	// 广播
	socket.RegisterSessionMessage(BackendAcceptor, coredef.DownstreamACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.DownstreamACK)

		pkt := &cellnet.Packet{
			MsgID: msg.GetMsgID(),
			Data:  msg.Data,
		}

		if msg.ClientID == nil {

			// 广播给所有客户端
			ClientAcceptor.IterateSession(func(ses cellnet.Session) bool {

				if DebugMode {
					log.Debugf("[gate] backend->client, msgid: %d clientid %d", msg.GetMsgID(), msg.GetClientID())
				}

				ses.RawSend(pkt)

				return true
			})

		} else {

			// 指定客户端发送
			for _, clientid := range msg.ClientID {
				clientSes := ClientAcceptor.GetSession(clientid)

				if clientSes != nil {

					if DebugMode {
						log.Debugf("[gate] backend->client, msgid: %d clientid %d", msg.GetMsgID(), msg.GetClientID())
					}

					clientSes.RawSend(pkt)

				} else if DebugMode {

					log.Debugf("[gate] backend->client, client not found, msgid: %d clientid %d", msg.GetMsgID(), msg.GetClientID())
				}
			}
		}

	})

	BackendAcceptor.Start(address)
}