Beispiel #1
0
func notifyOtherClientQueueInfo() error {
	// 有客户端断连接,就通知其他客户端当前排队情况
	for element := queue.QueuedClients.Front(); element != nil; element = element.Next() {
		qc := element.Value.(*queue.QueueClient)

		buf := new(bytes.Buffer)
		packer := binpacker.NewPacker(buf, binary.BigEndian)
		packer.PushByte(0x05)
		packer.PushInt32(10012)
		len := 1 + 4 + 4 + 4 // flag + queue inQueue time

		interVal := queue.GetInterVal()
		var que, inQueue, time int
		var flag byte
		que = queue.GetQueueClients()
		if que <= 0 {
			flag = '0'
		}
		inQueue = queue.GetQueuedIndex(qc.ID)
		time = (int)(interVal) * (inQueue + 1)

		packer.PushInt32((int32)(len))
		packer.PushByte(flag)
		packer.PushInt32((int32)(que))
		packer.PushInt32((int32)(inQueue))
		packer.PushInt32((int32)(time))

		if _, err := notice(qc.Conn, buf.Bytes()); err != nil {
			ServerLogger.Info("failed to send[%v] response ->%s", qc.Conn.RemoteAddr().String(), err)
			//return err
		}
	}
	return nil
}
Beispiel #2
0
func SendLoginPakcetWithKey(conn net.Conn, uuid string) {
	// todo
	// 向LoginServer发送登录信息
	buf := new(bytes.Buffer)
	packer := binpacker.NewPacker(buf, binary.BigEndian)
	packer.PushByte(0x05)
	packer.PushInt32(10013)
	var flag byte
	flag = '1'
	accout := "account"
	passwd := "passwd"
	key := uuid
	len := 1 + len(accout) + len(passwd) + len(uuid)
	packer.PushInt32((int32)(len))
	packer.PushByte(flag)
	packer.PushString(key)
	packer.PushString(accout).PushString(passwd)
	if err := packer.Error(); err != nil {
		fmt.Printf("make msg err [%v]\n", err)
		panic(err)
	}

	fmt.Printf("client reconnect to loginserver c2slogin packet buf[%x] dataLen[%v]\n", buf.Bytes(), len)

	if _, err := QueueSend2Login(conn, buf.Bytes()); err != nil {
		fmt.Printf("send c2slogin packet err[%v] \n", err)
		panic(err)
	}
}
Beispiel #3
0
func clientMsgPumpQueue(client net.Conn, startedChan chan bool) {
	close(startedChan)

	hbTickerQueue := time.NewTicker(msgs.C2QueueServerHB)
	hbChanQueue := hbTickerQueue.C

	ppTickerQueue := time.NewTicker(msgs.C2QueueServerPP)
	ppChanQueue := ppTickerQueue.C
	for {
		select {
		case <-hbChanQueue:
			//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("clientMsgPumpQueue make msg err [%v]\n", err)
				ExitChanQueue <- 1
				break
			}

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

			if _, err := Send2Queue(client, buf.Bytes()); err != nil {
				fmt.Printf("clientMsgPumpQueue send packet err[%v] \n", err)
				ExitChanQueue <- 1
			}
		case <-ppChanQueue:
			////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("clientMsgPumpQueue make msgPing err [%v]\n", err)
			//	ExitChanQueue <- 1
			//}
			//
			////fmt.Printf("clientMsgPumpQueue msgPing buf[%x] \n", buf.Bytes())
			//
			//if _, err := Send2Queue(client, buf.Bytes()); err != nil {
			//	fmt.Printf("clientMsgPumpQueue send packetPing err[%v] \n", err)
			//	ExitChanQueue <- 1
			//}
		case msg, ok := <-MsgChanQueue:
			if ok {
				//fmt.Printf("clientMsgPumpQueue 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("clientMsgPumpQueue unpacker msgPing flag err[%v]\n", err)
						ExitChanQueue <- 1
					}
					//fmt.Printf("clientMsgPumpQueue 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("clientMsgPumpQueue unpacker msgPing err[%v]\n", err)
							ExitChanQueue <- 1
						}
						//fmt.Printf("clientMsgPumpQueue  queue[%v] waitting No.[%v] time[%v]\n", queue, inQueue, time)
						fmt.Printf("当前排队总人数[%v]  前面等待人数[%v] 估计登录用时[%vs]\n", 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("clientMsgPumpQueue unpacker msgPing login err[%v]\n", err)
							ExitChanQueue <- 1
						}
						fmt.Printf("clientMsgPumpQueue msgPing uuid[%v] addr[%v]\n", uuid, addr)

						// todo 拿着uuid登录loginServer
						conn := QueueConnect2LoginServer(addr)
						SendLoginPakcetWithKey(conn, uuid) // 10s以后发送登录包
						ExitChanQueue <- 1
					}
				}
			} else {
				fmt.Printf("clientMsgPumpQueue from MsgChan not ok\n")
				ExitChanQueue <- 1
			}
		case <-ExitChanQueue:
			goto exit
		}
	}

exit:
	client.Close()
	hbTickerQueue.Stop()
	ppTickerQueue.Stop()
	close(ExitChanQueue)
}
Beispiel #4
0
func clientMsgPumpLogin(client net.Conn, startedChan chan bool) {
	close(startedChan)

	hbTickerLogin := time.NewTicker(QueueServer2LoginServerHB)
	hbChanLogin := hbTickerLogin.C

	syncTickerLogin := time.NewTicker(QueueServer2LoginServerPP)
	syncChanLogin := syncTickerLogin.C
	for {
		select {
		case <-hbChanLogin:
			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("clientMsgPumpLogin make msg err [%v]\n", err)
				ExitChanLogin <- 1
			}

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

			if _, err := Send2Login(client, buf.Bytes()); err != nil {
				fmt.Printf("clientMsgPumpLogin send heartbeat packet err[%v] \n", err)
				ExitChanLogin <- 1
			}
		case <-syncChanLogin:
			//var hb msgs.MsgSync
			//hb.Header = 0x05
			//hb.Cmd = 60000
			//hb.Len = 0
			buf := new(bytes.Buffer)
			packer := binpacker.NewPacker(buf, binary.BigEndian)
			packer.PushByte(0x05)
			packer.PushInt32(60000)
			packer.PushInt32(0)
			if err := packer.Error(); err != nil {
				fmt.Printf("clientMsgPumpQueue make msgSync err [%v]\n", err)
				ExitChanLogin <- 1
			}

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

			if _, err := Send2Login(client, buf.Bytes()); err != nil {
				fmt.Printf("clientMsgPumpQueue send msgSync err[%v] \n", err)
				ExitChanLogin <- 1
			}
		case msg, ok := <-MsgChanLogin:
			if ok {
				//fmt.Printf("clientMsgPumpLogin msgChan msg[%v] body[%v]\n", msg.ID, msg.Body)
				if msg.ID == 60001 {
					buf := new(bytes.Buffer)
					packer := binpacker.NewPacker(buf, binary.BigEndian)
					packer.PushString(string(msg.Body[:]))
					unpacker := binpacker.NewUnpacker(buf, binary.BigEndian)
					var max, auth, time int32
					unpacker.FetchInt32(&max).FetchInt32(&auth).FetchInt32(&time)
					if err := unpacker.Error(); err != nil {
						ExitChanLogin <- 1
					}
					queue.SetMaxClients(max)
					queue.SetAuthClients(auth)
					queue.SetInterVal(time)
					// todo NOTE 发送消息的设置时机
					queue.Timer = (int)(time)

					// 通知定时器检查是否发送
					queue.NotifyChan <- 1
					//fmt.Printf("clientMsgPumpLogin msgsync login2queue max[%v] auth[%v] time[%v]\n", max, auth, time)
				}

			} else {
				fmt.Printf("clientMsgPumpLogin from MsgChan not ok\n")
				ExitChanLogin <- 1
			}
		case <-ExitChanLogin:
			fmt.Printf("clientMsgPumpLogin exitChan recv EXIT\n")
			goto exit
		}
	}

exit:
	client.Close()
	hbTickerLogin.Stop()
	close(ExitChanLogin)

	defer func() {
		fmt.Printf("clientMsgPumpLogin exit\n")
	}()
}
Beispiel #5
0
func clientMsgPumpLogin(client net.Conn, startedChan chan bool) {
	close(startedChan)

	hbTickerLogin := time.NewTicker(msgs.C2LoginServerHB)
	hbChanLogin := hbTickerLogin.C
	for {
		select {
		case <-hbChanLogin:
			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("clientMsgPumpLogin make msg err [%v]\n", err)
				ExitChanLoginQueue <- 1
			}

			//fmt.Printf("clientMsgPumpLogin heartbeat cid[%v] buf[%x] \n", client.LocalAddr().String(), buf.Bytes())

			if n, err := QueueSend2Login(client, buf.Bytes()); err != nil || n != 9 {
				fmt.Printf("clientMsgPumpLogin send heartbeat packet err[%v] \n", err)
				ExitChanLoginQueue <- 1
			} else {
				//fmt.Printf("msg hb len[%v]\n", n)
			}
		case msg, ok := <-MsgChanLoginQueue:
			if ok {
				//fmt.Printf("clientMsgPumpLogin cid[%v] msgChan msg[%v] body[%v]\n", client.LocalAddr().String(), msg.ID, msg.Body)
				if msg.ID == 10014 {
					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("clientMsgPumpLogin unpacker err[%v]\n", err)
						ExitChanLoginQueue <- 1
					}

					//fmt.Printf("clientMsgPumpLogin cid[%v] flag[%v]\n", client.LocalAddr().String(), flag)
					if flag == 48 {
						//todo
						// login server return err, and connect to queue server
						var addr string
						len := uint64(msg.Len - 1)
						if err := unpacker.FetchString(len, &addr).Error(); err != nil {
							fmt.Printf("clientMsgPumpLogin login failed and get queue server addr err\n")
						}
						fmt.Printf("clientMsgPumpLogin login failed and redirect to queue server[%v]\n", addr)
						Connect2QueueServer(addr)
						ExitChanLoginQueue <- 1
					} else {
						var uid int64
						var name string
						len := uint64(msg.Len - 1 - 8)
						if err := unpacker.FetchInt64(&uid).FetchString(len, &name).Error(); err != nil {
							fmt.Printf("clientMsgPumpLogin login success but unpack failed [%v]\n", err)
							ExitChanLoginQueue <- 1
						}
						fmt.Printf("clientMsgPumpLogin login success uid[%v] name[%v]\n", uid, name)
					}
				}
			} else {
				fmt.Printf("clientMsgPumpLogin from MsgChan not ok\n")
				ExitChanLoginQueue <- 1
			}
		case <-ExitChanLoginQueue:
			fmt.Printf("clientMsgPumpLogin exitChan recv EXIT\n")
			goto exit
		}
	}

exit:
	client.Close()
	hbTickerLogin.Stop()
	close(ExitChanLoginQueue)

	defer func() {
		fmt.Printf("clientMsgPumpLogin exit\n")
	}()
}
Beispiel #6
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()
}