示例#1
0
文件: login.go 项目: skOak/chestnut
// send all requests to connector
func (ls *LoginService) requestDispatch(client *net.Client, reqType, request string) {
	log.Debugf("requestDispatch:reqType(%v),request(%v) for playerId(%d)\n", reqType, request, client.Id)

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

	var args CommonRequest
	args.Type = reqType
	args.Service = "Service"
	args.Handler = reqType
	args.PlayerId = client.Id
	args.Content = request
	var reply CommonReply
	rpcCallHandle, err := ls.busStub.GoByMethod("GameService."+reqType, args, &reply)
	//rpcCallHandle, err := busStub.GoByType("GameCommon", "GameService."+reqType, args, &reply)
	if err != nil {
		panic(err.Error())
	}
	if rpcCallHandle == nil {
		panic("rpcCallHandle nil")
	}

	// Todo:reconstruct to an independent goroutine
	go func(reply *CommonReply) {
		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()))
			}
		}()

		for {
			select {
			case rpcReplyError := <-rpcCallHandle:
				if rpcReplyError != nil {
					panic(rpcReplyError.Error())
				}
				client.SendResponse(reply)
				break
			case <-time.After(time.Second * RPCCallTimeOut):
				// Todo:mark rpc call abandoned
			}
		}
	}(&reply)
}
示例#2
0
文件: login.go 项目: skOak/chestnut
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
		}
	}
}