Exemple #1
0
func (ls *LoginService) handleGateLogic(client *net.Client) {
	defer func() {
		if x := recover(); x != nil {
			var request net.ClientRequest
			request.Type = "Exception"
			request.Content = fmt.Sprint(x)

			client.Request <- request
			log.Debug(x, string(debug.Stack()))
		}
	}()

	if ls.busStub == nil {
		panic("BusStub of LoginService doesn't exist!")
	}

	if ls.config.Server.NeedAuth {
		var body net.BODYHEAD
		response := <-client.Response[net.NetChannelPrimary]
		err := json.Unmarshal([]byte(response.Content), &body)
		if err != nil {
			panic(err.Error())
		}
		// must auth synchronize at first
		if body.Type != "Login" {
			panic("the first request must be Login")
			return
		}

		var args CommonRequest
		args.Type = body.Type
		args.PlayerId = 0
		args.Service = "Service"
		args.Handler = "Login"
		args.Content = response.Content
		var reply CommonReply
		err = ls.busStub.CallByType("GameAuth", "GameService.Login", args, &reply)
		if err != nil {
			panic(err.Error())
			return
		}

		client.SendResponse(reply.Content)
		if reply.Result != ResultSuccess {
			return
		}

		client.Id = reply.PlayerId
		net.Clients[client.Id] = client
		log.Debugf("handleGateLogic:net.Clients(%+v)\n", net.Clients)
	}

	for {
		select {
		case response := <-client.Response[net.NetChannelPrimary]:
			var body net.BODYHEAD
			json.Unmarshal([]byte(response.Content), &body)
			switch body.Type {
			case "Beat":
				net.HandleBeat(client, response.Content)
			case "Exception":
				log.Debugf("Quit handleGateLogic : player %d\n", client.Id)
			default:
				ls.requestDispatch(client, body.Type, response.Content)
			}
		case <-time.After(time.Second * net.HeartBeatRate * 12):
			client.CloseConnection()
			log.Debugf("connection from %s timeout, closing logic thread", client.Conn.RemoteAddr())
			return
		}
	}
}