Example #1
0
// 接收线程
func (self *ltvSession) recvThread(eq cellnet.EventQueue) {
	var err error
	var pkt *cellnet.Packet

	for {

		// 从Socket读取封包
		pkt, err = self.stream.Read()

		if err != nil {

			// 断开事件
			eq.PostData(NewSessionEvent(Event_SessionClosed, self, nil))
			break
		}

		// 逻辑封包
		eq.PostData(&SessionEvent{
			Packet: pkt,
			Ses:    self,
		})

	}

	if self.needNotifyWrite {

		// 通知发送线程停止
		self.writeChan <- closeWritePacket{}
	}

	// 通知接收线程ok
	self.endSync.Done()
}
Example #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)

		}

	})

}
Example #3
0
// 注册连接消息
func RegisterSessionMessage(eq cellnet.EventQueue, msgName string, userHandler func(interface{}, cellnet.Session)) *cellnet.MessageMeta {

	msgMeta := cellnet.MessageMetaByName(msgName)

	if msgMeta == nil {
		log.Errorf("message register failed, %s", msgName)
		return nil
	}

	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
			}

			userHandler(rawMsg, ev.Ses)

		}

	})

	return msgMeta
}
Example #4
0
func (self *mongoDriver) readTask(evq cellnet.EventQueue, usercallback interface{}, execCallback func(*mgo.Database, interface{}) error) {

	callbackType := reflect.TypeOf(usercallback)
	callbackValue := reflect.ValueOf(usercallback)

	resultPtrType := callbackType.In(0)
	// 取回调函数第一个参数类型
	resultType := resultPtrType.Elem()

	// 实例化
	result := reflect.New(resultType)

	db := self.fetch()

	err := execCallback(db, result.Interface())

	self.back(db)

	//将回调函数投递到主线程
	evq.PostData(func() {

		errValue := reflect.ValueOf(&err).Elem()

		// 错误时, 返回空
		if err == nil {

			// http://play.golang.org/p/TZyOLzu2y-
			callbackValue.Call([]reflect.Value{result, errValue})
		} else {
			// http://grokbase.com/t/gg/golang-nuts/13adpw445j/go-nuts-reflect-set-to-nil
			callbackValue.Call([]reflect.Value{reflect.Zero(resultPtrType), errValue})
		}

	})
}
Example #5
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
}
Example #6
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
}
Example #7
0
// 注册连接消息
func RegisterMessage(eq cellnet.EventQueue, msgName string, userHandler func(Response, interface{})) {

	msgMeta := cellnet.MessageMetaByName(msgName)

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

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

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

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

			userHandler(ev, rawMsg)

		}

	})

}
Example #8
0
func update(db *db.MongoDriver, evq cellnet.EventQueue) {
	log.Debugln("update")

	db.Execute(func(ses *mgo.Session) {

		col := ses.DB("").C("test")

		col.Update(bson.M{"name": "davy"}, &char{Name: "davy", HP: 1})

		evq.PostData(func() {
			signal.Done(2)
		})
	})

}
Example #9
0
func (self *mongoDriver) writeTask(evq cellnet.EventQueue, usercallback func(error), execCallback func(*mgo.Database) error) {

	db := self.fetch()

	err := execCallback(db)

	self.back(db)

	if usercallback != nil {
		//将回调函数投递到主线程
		evq.PostData(func() {

			usercallback(err)

		})
	}

}
Example #10
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
}
Example #11
0
// 注册连接消息
func RegisterMessage(eq cellnet.EventQueue, msgName string, userHandler func(interface{}, Response)) {

	if needRegisterServer {

		// 服务端
		socket.RegisterSessionMessage(eq, "gamedef.RemoteCallREQ", func(content interface{}, ses cellnet.Session) {
			msg := content.(*gamedef.RemoteCallREQ)

			eq.CallData(&response{
				ses: ses,
				req: msg,
			})

		})

		needRegisterServer = false
	}

	msgMeta := cellnet.MessageMetaByName(msgName)

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

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

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

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

			userHandler(rawMsg, ev)

		}

	})

}
Example #12
0
// 注册连接消息
func RegisterSessionMessage(eq cellnet.EventQueue, msgName string, userHandler func(interface{}, cellnet.Session)) *cellnet.MessageMeta {

	msgMeta := cellnet.MessageMetaByName(msgName)

	if msgMeta == nil {
		log.Errorf("message register failed, %s", msgName)
		return nil
	}

	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
			}

			if EnableMessageLog {
				msgLog(&MessageLogInfo{
					Dir:       "recv",
					PeerName:  ev.Ses.FromPeer().Name(),
					SessionID: ev.Ses.ID(),
					Name:      msgMeta.Name,
					ID:        msgMeta.ID,
					Size:      int32(len(ev.Packet.Data)),
					Data:      rawMsg.(proto.Message).String(),
				})

			}

			userHandler(rawMsg, ev.Ses)

		}

	})

	return msgMeta
}
Example #13
0
// 接收线程
func (self *ltvSession) recvThread(eq cellnet.EventQueue) {
	var err error
	var pkt *cellnet.Packet

	for {

		// 从Socket读取封包
		pkt, err = self.stream.Read()

		if err != nil {

			ev := NewSessionEvent(Event_SessionClosed, self, nil)

			msgLog("recv", self, ev.Packet)

			// 断开事件
			eq.PostData(ev)
			break
		}

		// 消息日志要多损耗一次解析性能

		msgLog("recv", self, pkt)

		// 逻辑封包
		eq.PostData(&SessionEvent{
			Packet: pkt,
			Ses:    self,
		})

	}

	if self.needNotifyWrite {
		self.Close()
	}

	// 通知接收线程ok
	self.endSync.Done()
}
Example #14
0
// 注册连接消息
func RegisterPeerMessage(eq cellnet.EventQueue, msgName string, userHandler func(interface{}, cellnet.Peer)) *cellnet.MessageMeta {

	msgMeta := cellnet.MessageMetaByName(msgName)

	if msgMeta == nil {
		log.Errorf("message register failed, %s", msgName)
		return nil
	}

	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
}