//method: GetLeaderBoard //description: retrieves leaderboard information //returns: void public void GetLeaderboard() { if(stopped) return; Task.Factory.StartNew(() => { try { lock (socketLock) { //request leaderboard writer.WriteLine("GET_LEADERBOARD"); writer.Flush(); //get leaderboard List<UserSession> rows = new List<UserSession>(); while(reader.PeekLine() != null && reader.PeekLine().StartsWith("SESSIONID:")) { UserSession r = new UserSession(); r.sessionId = int.Parse(reader.ReadLine().Substring("SESSIONID:".Length)); r.userName = reader.ReadLine().Substring("USERNAME:"******"SCORE:".Length)); rows.Add(r); } string writerDump = reader.ReadLine(); //dump for "DONE" string from server owner.Invoke((MethodInvoker)( () => { LeaderboardReceived(this, rows); } )); } } catch(Exception) { stopped = true; socket.Close(); owner.Invoke((MethodInvoker)(() => { ConnectionLost(this, new EventArgs()); })); } }); }
//method: ConnectionReader //description: reads data from the server //params: none //returns: void private void ConnectionReader() { try { while(!stopped) { if(reader.PeekLine().StartsWith("QUESTION:")) {//QUESTION //get question QuestionEventArgs question = new QuestionEventArgs(); question.questionText = reader.ReadLine().Substring("QUESTION:".Length); //get answers question.answers = new List<string>(); while(reader.PeekLine().StartsWith("ANSWER:")) { question.answers.Add(reader.ReadLine().Substring("ANSWER:".Length)); } question.timeLimit = int.Parse(reader.ReadLine().Substring("TIME:".Length)); owner.Invoke((MethodInvoker)( () => { QuestionReceived(this, question); } )); } else if(reader.PeekLine().StartsWith("RESULT:")) {//QUESTION RESULTS List<QuestionResult> results = new List<QuestionResult>(); while(reader.PeekLine().StartsWith("RESULT:")) { QuestionResult r = new QuestionResult(); r.questionText = reader.ReadLine().Substring("RESULT:".Length); r.correctAnswer = reader.ReadLine(); r.userAnswer = reader.ReadLine(); r.score = int.Parse(reader.ReadLine()); results.Add(r); } owner.Invoke((MethodInvoker)( () => { ResultsReceived(this, results); } )); } else if(reader.PeekLine().StartsWith("SESSIONID:")) {//LEADERBOARD List<UserSession> rows = new List<UserSession>(); while(reader.PeekLine() != null && reader.PeekLine().StartsWith("SESSIONID:")) { UserSession r = new UserSession(); r.sessionId = int.Parse(reader.ReadLine().Substring("SESSIONID:".Length)); r.userName = reader.ReadLine().Substring("USERNAME:"******"SCORE:".Length)); rows.Add(r); } owner.Invoke((MethodInvoker)( () => { LeaderboardReceived(this, rows); } )); } else if(reader.PeekLine().StartsWith("YOUR_SESSIONID:")) { int sessionId = int.Parse(reader.ReadLine().Substring("YOUR_SESSIONID:".Length)); owner.Invoke((MethodInvoker)( () => { SessionIdReceived(this, sessionId); } )); } else { //nothing we can handle reader.ReadLine();//consume } Thread.Sleep(50); } } catch (Exception ex) { if(ex is ArgumentException || ex is SocketException || ex is IOException || ex is NullReferenceException) { stopped = true; owner.Invoke((MethodInvoker)( () => { ConnectionLost(this, new EventArgs()); } )); } else { throw; } } finally { socket.Close(); } }