//The below methods contain the actual client-server message-passing code.
        //They are written to line up with the messaging code in Server.cs.

        private void TalkToServer_FirstContact(TcpClient client, NetworkStream serverStream,
                                               BinaryWriter serverWriter, BinaryReader serverReader)
        {
            //Ask the server to find a game for me.
            var msg_findGame = new Networking.Messages.FindGame(clientUI.MyName,
                                                                GameID);

            if (!Try(() => GameMessageBase.Write(msg_findGame, serverWriter)))
            {
                clientUI.SetErrorMsg("Couldn't send 'FindGame' to server");
                return;
            }

            //Check the response.
            var queueResult = TryReadMsg <Networking.Messages.SuccessfullyInQueue>(
                serverReader,
                "getting 'FindGame' response");

            if (queueResult == null)
            {
                return;
            }

            //Store the result.
            var outData = new ClientMatchmakingData((IPEndPoint)client.Client.RemoteEndPoint,
                                                    queueResult.PlayerID);

            SetMatchmakingData(outData);
        }
 private void SetMatchmakingData(ClientMatchmakingData data)
 {
     try
     {
         using (var stream = File.OpenWrite(matchmakingDataFilePath))
             using (var writer = new BinaryWriter(stream))
                 data.Serialize(writer);
     }
     catch (Exception e)
     {
         clientUI.SetErrorMsg("Failure writing " + matchmakingDataFilePath +
                              ": " + e.Message);
     }
 }
 /// <summary>
 /// Reads from disk the data structure containing the current status
 ///     of finding a match on the server.
 /// </summary>
 private ClientMatchmakingData GetMatchmakingData()
 {
     try
     {
         var data = new ClientMatchmakingData();
         using (var stream = File.OpenRead(matchmakingDataFilePath))
             using (var reader = new BinaryReader(stream))
                 data.Deserialize(reader);
         return(data);
     }
     catch (Exception e)
     {
         clientUI.SetErrorMsg("Failure reading " + matchmakingDataFilePath +
                              ": " + e.Message);
         return(new ClientMatchmakingData());
     }
 }