Example #1
0
func (qs *QueueServer) Start() error {
	if _, err := msgs.Send(qs, []byte("  V1")); err != nil {
		return fmt.Errorf("send protocol [  v1]err [%v]\n", err)
	}

	return nil
}
Example #2
0
func (qs *QueueServer) msgPump(startedChan chan bool) {
	close(startedChan)

	hbTicker := time.NewTicker(msgs.C2QueueServerHB)
	hbChan := hbTicker.C

	ppTicker := time.NewTicker(msgs.C2QueueServerPP)
	ppChan := ppTicker.C
	for {
		select {
		case <-hbChan:
			//var hb msgs.MsgHeartbeat
			//hb.Header = 0x05
			//hb.Cmd = 10010
			//hb.Len = 0
			buf := new(bytes.Buffer)
			packer := binpacker.NewPacker(buf, binary.BigEndian)
			packer.PushByte(0x05)
			packer.PushInt32(10010)
			packer.PushInt32(0)
			if err := packer.Error(); err != nil {
				fmt.Printf("QueueServerMsgPump make msg err [%v]\n", err)
				qs.ExitChan <- 1
				break
			}

			//fmt.Printf("QueueServerMsgPump buf[%x] \n", buf.Bytes())

			if _, err := msgs.Send(qs, buf.Bytes()); err != nil {
				fmt.Printf("QueueServerMsgPump send packet err[%v] \n", err)
				qs.ExitChan <- 1
			}
		case <-ppChan:
		////var hb msgs.MsgPing
		////hb.Header = 0x05
		////hb.Cmd = 10011
		////hb.Len = 0
		//buf := new(bytes.Buffer)
		//packer := binpacker.NewPacker(buf, binary.BigEndian)
		//packer.PushByte(0x05)
		//packer.PushInt32(10011)
		//packer.PushInt32(0)
		//if err := packer.Error(); err != nil {
		//	fmt.Printf("QueueServerMsgPump make msgPing err [%v]\n", err)
		//	ExitChanQueue <- 1
		//}
		//
		////fmt.Printf("QueueServerMsgPump msgPing buf[%x] \n", buf.Bytes())
		//
		//if _, err := Send2Queue(client, buf.Bytes()); err != nil {
		//	fmt.Printf("QueueServerMsgPump send packetPing err[%v] \n", err)
		//	ExitChanQueue <- 1
		//}
		case msg, ok := <-qs.MsgChan:
			if ok {
				//fmt.Printf("QueueServerMsgPump msg[%v] body[%v]\n", msg.ID, msg.Body)
				if msg.ID == 10012 {
					buf := new(bytes.Buffer)
					packer := binpacker.NewPacker(buf, binary.BigEndian)
					packer.PushString(string(msg.Body[:]))
					unpacker := binpacker.NewUnpacker(buf, binary.BigEndian)
					var flag byte
					if err := unpacker.FetchByte(&flag).Error(); err != nil {
						fmt.Printf("QueueServerMsgPump unpacker msgPing flag err[%v]\n", err)
						qs.ExitChan <- 1
					}
					//fmt.Printf("QueueServerMsgPump flag[%v]\n", flag)
					if flag == 0 {
						var queue, inQueue, time int32
						if err := unpacker.FetchInt32(&queue).FetchInt32(&inQueue).FetchInt32(&time).Error(); err != nil {
							fmt.Printf("QueueServerMsgPump unpacker msgPing err[%v]\n", err)
							qs.ExitChan <- 1
						}
						//fmt.Printf("QueueServerMsgPump  queue[%v] waitting No.[%v] time[%v]\n", queue, inQueue, time)
						fmt.Printf("[%v]当前排队总人数[%v]  前面等待人数[%v] 估计登录用时[%vs]\n", qs.Conn.LocalAddr(), queue, inQueue, time)
					} else {
						uuidlen := 36
						addrlen := uint64(msg.Len - 1 - uuidlen)
						var uuid, addr string
						if err := unpacker.FetchString(36, &uuid).FetchString(addrlen, &addr).Error(); err != nil {
							fmt.Printf("QueueServerMsgPump unpacker msgPing login err[%v]\n", err)
							qs.ExitChan <- 1
						}
						fmt.Printf("QueueServerMsgPump msgPing uuid[%v] addr[%v]\n", uuid, addr)

						// todo 拿着uuid登录loginServer

						var ls *LoginServer
						var err error
						if ls, err = NewLoginSer(); err != nil {
							fmt.Printf("new loginserver error[%v]\n", err)
							return
						}

						if err = ls.Connect2LoginServer(addr); err != nil {
							fmt.Printf("login server connect error[%v]\n", err)
							return
						}

						if err = ls.Start(); err != nil {
							fmt.Printf("login server start error[%v]\n", err)
						}

						// todo Check Server Run or Not and then send login packet
						ls.Run()

						if err = ls.SendLoginPakcetWithKey(uuid); err != nil {
							fmt.Printf("send login packet err[%v]\n", err)
							ls.Stop()
						}

						qs.ExitChan <- 1
					}
				}
			} else {
				fmt.Printf("QueueServerMsgPump from MsgChan not ok\n")
				qs.ExitChan <- 1
			}
		case <-qs.ExitChan:
			goto exit
		}
	}

exit:
	hbTicker.Stop()
	ppTicker.Stop()
	qs.Stop()
}