private bool TryHost(out string gameId) { gameId = null; var timeout = _random.Next(_maxTimeout) + _minTimeout; //#1 : Wait to receive event if (_contactedByClientLock.Wait(timeout) && !_source.IsCancellationRequested) { Console.WriteLine("Showing interest in searcher!"); var showInterestToClient = new MatchMakingSyncEvent(_currentSyncId, MatchMakingSyncEvent.SyncStep.HostWantTargetPlayer); EventManager.Instance.Notice(showInterestToClient); // Try get confirmation if (_confirmedByClientLock.Wait(_maxTimeout) && !_source.IsCancellationRequested) { // Create game + send invitation var createdGame = CreateGame(); Console.WriteLine("Sending game invitation!"); var invitationEvent = new MatchMakingGameInvitation(_currentSyncId, createdGame.HashId); EventManager.Instance.Notice(invitationEvent); gameId = createdGame.HashId; return(true); } else { Console.WriteLine("The potential client did not confirmed!"); return(false); } } Console.WriteLine("No client found!"); return(false); }
private bool TryJoin() { // Send searching event var searchingEvent = new MatchMakingSearchingEvent(_currentSyncId); EventManager.Instance.Notice(searchingEvent); var timeout = _random.Next(_maxTimeout) + _minTimeout; //#1 : Wait to receive event if (_contactedByHostLock.Wait(timeout) && !_source.IsCancellationRequested) { // Send searching event Console.WriteLine("Telling host we will wait a little bit!"); var acceptHostInterest = new MatchMakingSyncEvent(_currentSyncId, MatchMakingSyncEvent.SyncStep.PlayerWillWaitForHostInvitation); EventManager.Instance.Notice(acceptHostInterest); // Try get confirmation if (_confirmedByHostLock.Wait(_maxTimeout) && !_source.IsCancellationRequested) { JoinGame(_gameIdToJoin); return(true); } else { Console.WriteLine("No potential host did not sent the invitation in time"); return(false); } } else { Console.WriteLine("No host found"); return(false); } }