Exemplo n.º 1
0
 public async Task Remove(UserConnection conn)
 {
   await Lock.WaitAsync();
   try
   {
     Connections.Remove(conn);
   }
   finally
   {
     Lock.Release();
   }
 }
Exemplo n.º 2
0
    static async Task ReadWebsocket(Websocket ws, BufferBlock<GameTask> gameTasks, UserConnection uc, PlayerState player)
    {
      string next;
      while ((next = await ws.ReadFrame()) != null)
      {
        var update = JSON.Deserialize<PlayerCommand>(next);

        if (player.Dead)
        {
          //this is now checked in two places, but at least dead players won't send move commands to the main work channel
          continue;
        }

        dynamic command = ParseCommand(update);

        if (command == null)
        {
          continue;
        }

        var updatetask = new UpdateTask(command, player);
        await gameTasks.SendAsync(updatetask);
      }

      uc.Closed = true;
      await uc.SendAsync(null);
    }
Exemplo n.º 3
0
    static async Task ProcessWebSocket(Websocket ws, ConnectionHub connectionHub, BufferBlock<GameTask> gameTasks)
    {
      var uc = new UserConnection(ws);
      await connectionHub.Add(uc);

      var hash = Guid.NewGuid().ToString();
      var player = new PlayerState() { Name = "player", Color = "blue", Hash = hash };


      var first = new PlayerInit() { Hash = hash, Height = 0, Width = 0 };
      var json = JSON.Serialize(first);

      var initPacket = new UpdatePacket() { Type = "init", Data = json };
      var ip = JSON.Serialize(initPacket);
      await uc.SendAsync(ip);

      ReadWebsocket(ws, gameTasks, uc, player);

      var initTask = new InitTask(uc, player);
      await gameTasks.SendAsync(initTask);

      await uc.Worker();
      Console.WriteLine("disconnected");
      
      //FIXME: this is never pushed to currently connected clients
      player.Dead = true;
      await connectionHub.Remove(uc);
    }