예제 #1
0
파일: main.go 프로젝트: huangbenyu/cellnet
func server() {
	ltvsocket.SpawnAcceptor("127.0.0.1:8001", func(self cellnet.CellID, cm interface{}) {

		switch v := cm.(type) {
		case ltvsocket.EventAccepted:

			ltvsocket.SpawnSession(v.Stream(), func(ses cellnet.CellID, sescm interface{}) {

				switch pkt := sescm.(type) {
				case *cellnet.Packet:

					log.Println("server recv:", cellnet.ReflectContent(pkt))

					v.Stream().Write(cellnet.BuildPacket(&coredef.TestEchoACK{
						Content: proto.String("world"),
					}))
				}

			})

		case IError:
			log.Println(cellnet.ReflectContent(v))
		}

	})
}
예제 #2
0
파일: req.go 프로젝트: bobbyzhu/cellnet
func Call(p cellnet.Peer, args interface{}, callback interface{}) {

	req := addCall()

	funcType := reflect.TypeOf(callback)
	req.replyType = funcType.In(0).Elem()
	req.callback = reflect.ValueOf(callback)

	ses, err := getPeerSession(p)

	if err != nil {
		log.Errorln(err)
		removeCall(req.id)
		return
	}

	pkt, _ := cellnet.BuildPacket(args)

	ses.Send(&coredef.RemoteCallREQ{
		MsgID:  proto.Uint32(pkt.MsgID),
		Data:   pkt.Data,
		CallID: proto.Int64(req.id),
	})

	// TODO rpc日志

}
예제 #3
0
파일: session.go 프로젝트: davyxu/cellnet
func (self *ltvSession) Send(data interface{}) {

	pkt, _ := cellnet.BuildPacket(data)

	msgLog("send", self, pkt)

	self.RawSend(pkt)
}
예제 #4
0
파일: session.go 프로젝트: bobbyzhu/cellnet
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)
}
예제 #5
0
파일: ack.go 프로젝트: davyxu/cellnet
func (self *response) Feedback(msg interface{}) {

	pkt, _ := cellnet.BuildPacket(msg)

	self.ses.Send(&gamedef.RemoteCallACK{
		MsgID:  pkt.MsgID,
		Data:   pkt.Data,
		CallID: self.req.CallID,
	})
}
예제 #6
0
파일: ack.go 프로젝트: FengJiaQi/cellnet
func (self *response) Feedback(msg interface{}) {

	pkt, _ := cellnet.BuildPacket(msg)

	self.ses.Send(&coredef.RemoteCallACK{
		MsgID:  proto.Uint32(pkt.MsgID),
		Data:   pkt.Data,
		CallID: proto.Int64(self.req.GetCallID()),
	})
}
예제 #7
0
// 将消息发送到客户端
func SendToClient(gateSes cellnet.Session, clientid int64, data interface{}) {

	if gateSes == nil {
		return
	}

	userpkt := cellnet.BuildPacket(data)

	gateSes.Send(&coredef.DownstreamACK{
		Data:     userpkt.Data,
		MsgID:    proto.Uint32(userpkt.MsgID),
		ClientID: []int64{clientid},
	})
}
예제 #8
0
func sendMessageToBackend(ses cellnet.Session, ev *socket.SessionEvent) {

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

	if DebugMode {
		log.Debugf("client->backend, msg: %s(%d) clientid: %d", getMsgName(ev.MsgID), ev.MsgID, ev.Ses.ID())
	}

	ses.RawSend(relaypkt)
}
예제 #9
0
// 发送给指定客户端列表的客户端
func BroadcastToClientList(data interface{}, list ClientList) {

	pkt := cellnet.BuildPacket(data)

	for ses, clientlist := range list {

		ack := &coredef.DownstreamACK{
			Data:  pkt.Data,
			MsgID: proto.Uint32(pkt.MsgID),
		}

		ack.ClientID = clientlist

		ses.Send(ack)
	}

}
예제 #10
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.Printf("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)

}
예제 #11
0
// 发送给所有gate的所有客户端
func BroadcastToClient(data interface{}) {

	pkt := cellnet.BuildPacket(data)

	ack := &coredef.DownstreamACK{
		Data:  pkt.Data,
		MsgID: proto.Uint32(pkt.MsgID),
	}

	for _, conn := range gateConnArray {
		ses := conn.(connSesManager).DefaultSession()
		if ses == nil {
			continue
		}

		ses.Send(ack)
	}
}
예제 #12
0
func (self *ltvSession) Send(data interface{}) {

	pkt, meta := cellnet.BuildPacket(data)

	if EnableMessageLog {
		msgLog(&MessageLogInfo{
			Dir:       "send",
			PeerName:  self.FromPeer().Name(),
			SessionID: self.ID(),
			Name:      meta.Name,
			ID:        meta.ID,
			Size:      int32(len(pkt.Data)),
			Data:      data.(proto.Message).String(),
		})

	}

	self.RawSend(pkt)
}
예제 #13
0
// 将消息发送到客户端
func SendToClient(routerSes cellnet.Session, clientid int64, data interface{}) {

	if routerSes == nil {
		return
	}

	msgContent := data.(interface {
		String() string
	}).String()

	userpkt, _ := cellnet.BuildPacket(data)

	log.Debugf("backend->router clientid: %d %s(%d) size: %d |%s", clientid, getMsgName(userpkt.MsgID), userpkt.MsgID, len(userpkt.Data), msgContent)

	routerSes.Send(&coredef.DownstreamACK{
		Data:     userpkt.Data,
		MsgID:    userpkt.MsgID,
		ClientID: []int64{clientid},
	})
}
예제 #14
0
파일: main.go 프로젝트: huangbenyu/cellnet
func client() {

	ltvsocket.SpawnConnector("127.0.0.1:8001", func(self cellnet.CellID, cm interface{}) {

		switch v := cm.(type) {
		case ltvsocket.EventConnected:

			// new session
			ltvsocket.SpawnSession(v.Stream(), func(ses cellnet.CellID, sescm interface{}) {

				switch pkt := sescm.(type) {
				case *cellnet.Packet:

					var ack coredef.TestEchoACK
					if err := proto.Unmarshal(pkt.Data, &ack); err != nil {
						log.Println(err)
					} else {
						log.Println("client recv", ack.String())

						done <- true
					}

				}

			})

			// send packet on connected
			v.Stream().Write(cellnet.BuildPacket(&coredef.TestEchoACK{
				Content: proto.String("hello"),
			}))

		case IError:
			log.Println(cellnet.ReflectContent(v))

		}

	})

}
예제 #15
0
// 发送给指定客户端列表的客户端
func BroadcastToClientList(data interface{}, list ClientList) {

	msgContent := data.(interface {
		String() string
	}).String()

	pkt, _ := cellnet.BuildPacket(data)

	log.Debugf("backend->router BroadcastToClientList %s(%d) size: %d|%s", getMsgName(pkt.MsgID), pkt.MsgID, len(pkt.Data), msgContent)

	for ses, clientlist := range list {

		ack := &coredef.DownstreamACK{
			Data:  pkt.Data,
			MsgID: pkt.MsgID,
		}

		ack.ClientID = clientlist

		ses.Send(ack)
	}

}
예제 #16
0
// 处理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:
				msgid = msgConnected
			case ltvsocket.EventAccepted:
				msgid = msgAccepted
			}

			ltvsocket.SpawnSession(v.Stream(), func(ses cellnet.CellID, sesev interface{}) {

				switch data := sesev.(type) {

				case cellnet.EventInit: // 初始化转通知
					disp.Call(ses, &cellnet.Packet{MsgID: msgid})
				case ltvsocket.EventClose: // 断开转通知
					disp.Call(ses, &cellnet.Packet{MsgID: msgClosed})
				case *cellnet.Packet: // 收
					disp.Call(ses, data)
				case proto.Message: // 发
					v.Stream().Write(cellnet.BuildPacket(data))
				}

			})

		case errInterface:
			log.Println(cellnet.ReflectContent(v))
		}

	}
}
예제 #17
0
파일: req.go 프로젝트: davyxu/cellnet
func newRequest(evq cellnet.EventQueue, args interface{}, callback interface{}) (*request, interface{}) {

	needRegisterClientGuard.Lock()
	if needRegisterClient {
		// 请求端
		socket.RegisterSessionMessage(evq, "gamedef.RemoteCallACK", func(content interface{}, ses cellnet.Session) {
			msg := content.(*gamedef.RemoteCallACK)

			c := getCall(msg.CallID)

			if c == nil {
				return
			}

			c.done(msg)
		})

		needRegisterClient = false
	}
	needRegisterClientGuard.Unlock()

	req := &request{}

	funcType := reflect.TypeOf(callback)
	req.replyType = funcType.In(0)
	req.callback = reflect.ValueOf(callback)

	pkt, _ := cellnet.BuildPacket(args)

	addCall(req)

	return req, &gamedef.RemoteCallREQ{
		MsgID:  pkt.MsgID,
		Data:   pkt.Data,
		CallID: req.id,
	}
}
예제 #18
0
// 发送给所有router的所有客户端
func BroadcastToClient(data interface{}) {

	msgContent := data.(interface {
		String() string
	}).String()

	pkt, _ := cellnet.BuildPacket(data)

	ack := &coredef.DownstreamACK{
		Data:  pkt.Data,
		MsgID: pkt.MsgID,
	}

	log.Debugf("backend->router BroadcastToClient %s(%d) size: %d|%s", getMsgName(pkt.MsgID), pkt.MsgID, len(pkt.Data), msgContent)

	for _, conn := range routerConnArray {
		ses := conn.(connSesManager).DefaultSession()
		if ses == nil {
			continue
		}

		ses.Send(ack)
	}
}
예제 #19
0
func (self *ltvSession) Send(data interface{}) {

	self.RawSend(cellnet.BuildPacket(data))
}