예제 #1
0
 public void rpc_PlayGame(PlayGameRequest req, OnRpcReturn <PlayGameResponse> callback)
 {
     // <--- 此处如果不加锁,假如同时两个PlayGameRequest过来,则可能会导致coinCount为负数
     this.coinLocker.LockAsync().ContinueWith((_lock) =>
     {
         //判断硬币数量够不够
         if (coinCount > req.count)
         {
             //异步调用,消耗硬币
             machine.Call <PlayGameResponse>(req, new OnRpcReturn <PlayGameResponse>((rsp, err) =>
             {
                 //消耗硬币
                 coinCount -= rsp.usedCoin;
                 //返回结果
                 callback(rsp);
                 _lock.Dispose();
             }));
         }
         else
         {
             //返回失败结果
             callback(new PlayGameResponse()
             {
                 s2c_code = Response.CODE_ERROR
             });
             _lock.Dispose();
         }
     });
 }