Пример #1
0
 /// <summary>
 /// Releases unmanaged and - optionally - managed resources.
 /// </summary>
 public void Dispose()
 {
     try
     {
         Pause().Wait(1000);
     }
     finally
     {
         if (socket != null)
         {
             socket.Dispose();
             socket = null;
         }
     }
 }
Пример #2
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="finalize"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        public void Dispose(bool finalize)
        {
            _disposing = true;
            if (random != null)
            {
                random.Dispose();
                random = null;
            }

            if (lobbyCon != null)
            {
                lobbyCon.Dispose();
                lobbyCon = null;
            }

            if (challengeCon != null)
            {
                challengeCon.Dispose();
                challengeCon = null;
            }

            if (gameCons != null)
            {
                foreach (KeyValuePair <string, LilaGame> game in gameCons)
                {
                    log.ConditionalDebug("Disconnecting {0}", game.Key);
                    game.Value.Dispose();
                }

                gameCons = null;
            }

            if (tournamentCons != null)
            {
                foreach (KeyValuePair <string, LilaTournament> tournament in tournamentCons)
                {
                    log.ConditionalDebug("Disconnecting {0}", tournament.Key);
                    tournament.Value.Dispose();
                }

                tournamentCons = null;
            }

            if (finalize)
            {
                GC.SuppressFinalize(this);
            }
        }
Пример #3
0
        /// <summary>
        /// Joins a tournament.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        internal bool JoinTournament(TournamentData data)
        {
            if (_disposing)
            {
                return(false);
            }

            Uri host = new Uri("wss://socket.lichess.org");

            if (!Uri.TryCreate(string.Format("/tournament/{0}/socket/v2", data.Id), UriKind.Relative, out Uri relative))
            {
                return(false);
            }

            Uri absolute = new Uri(host, relative);

            if (random == null)
            {
                return(false);
            }

            UriBuilder gameBldr = new UriBuilder(absolute)
            {
                Query = string.Format("sri={0}", random.NextSri())
            };

            LilaSocket tournamentCon = new LilaSocket("Tournament-Socket", ResourceType.Thread);

            tournamentCon.AddCookies(lobbyCon.GetCookies());

            if (tournamentCon.Connect(gameBldr.Uri) && !_disposing)
            {
                //Disconnect from lobby
                if (lobbyCon.IsConnected())
                {
                    lobbyCon.Disconnect();
                }

                LilaTournament lilaTournament = new LilaTournament(this, tournamentCon, data);
                tournamentCons.TryAdd(data.Id, lilaTournament);

                Events.FireEventAsync(Events._onTournamentEnter, new TournamentEvent(this, lilaTournament));
                return(true);
            }

            tournamentCon.Dispose();
            return(false);
        }
Пример #4
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="finalize"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool finalize)
        {
            lock (disposeLock)
            {
                if (socket != null)
                {
                    socket.Dispose();
                    socket = null;
                }

                if (finalize)
                {
                    GC.SuppressFinalize(this);
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Joins a game.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <returns></returns>
        internal bool JoinGame(GameData game)
        {
            if (_disposing)
            {
                return(false);
            }

            lock (joinLock)
            {
                Uri host = new Uri("wss://socket.lichess.org");
                if (!Uri.TryCreate(game.Url.Socket, UriKind.Relative, out Uri relative))
                {
                    return(false);
                }

                Uri absolute = new Uri(host, relative);
                if (random == null)
                {
                    return(false);
                }

                UriBuilder gameBldr = new UriBuilder(absolute)
                {
                    Query = string.Format("sri={0}", random.NextSri())
                };

                LilaSocket gameCon = new LilaSocket("Game-Socket", ResourceType.Thread);
                gameCon.AddCookies(lobbyCon.GetCookies());
                if (anonymous)
                {
                    gameCon.AddCookies(new Cookie("rk2", game.Url.Socket.Substring(9, 4), "/", "lichess.org")); //Add anoncookie
                }

                if (gameCons.Count == 0 && gameCon.Connect(gameBldr.Uri) && !_disposing)
                {
                    LilaGame lilaGame = new LilaGame(this, gameCon, game);
                    gameCons.TryAdd(game.Url.Socket, lilaGame);

                    Events.FireEventAsync(Events._onJoinGame, new JoinGameEvent(this, lilaGame));
                    return(true);
                }

                gameCon.Dispose();
                return(false);
            }
        }