views:

228

answers:

1

I am creating a little game client that will end up connecting to a server to gather some information on available games to play, how many players are playing, and all kinds of other stuff that you can imagine it ought to do.

My difficulties come in finding an effective way in dealing with the connect/retry connect sequence upon first loading.

I imagined my client would follow this process in trying to connect:

  1. Client application executed
  2. Try to establish connection
  3. If connection successful gather information - If not successful proceed to step 4
  4. Display a new dialog/form that prompts the user that a connection is trying to be established
  5. Loop till a connection has been established

I have been questioning my methods in trying to follow this sequence. I question if it is the right/most effective way to connect as well as to why my form I display in step 4 isn't working?

try
{
    sock.Connect(authenServerEP);
    // Once connected show our main client window
    this.Show();
    // Create the LoginForm once a connection has been established and display
    LoginForm loginForm = new LoginForm();
    loginForm.ShowDialog();
    if (false == loginForm.Visible)
    {
        loginForm.Dispose();
    }
}
catch (SocketException firstConnectException)
{
    // Load retrying connection form
    EstablishingConnectionForm establishingConnectionForm = new EstablishingConnectionForm();                                
    establishingConnectionForm.Show();

    bool connected = false;
    // Loop until we are connected
    while (!connected)
    {
        try
        {
            sock.Connect(authenServerEP);
            connected = true;
            establishingConnectionForm.Dispose();
         }
         catch (SocketException retryConnectException)
         {
             // Pass and retry connection
         }
     }
} // end catch (SocketException firstConnectException)

As you can see I am catching the SocketException that is raised when there is a problem connecting to the server (such as the server isn't running). I then go on to try to continuously loop till a connection is established. I dunno if I ought to be doing it this way. Are there better ways to do this?

Also when I display establishingConnectionForm with Show() it doesn't look like it all of the forms/tools initialize (initialize could be misleading). The Label that is on the form is just shaded out in white as opposed to having its text displayed. Not only that but it seems like I can't select the form/dialog and actually move it around. It sits there with the "Thinking/Working" mouse icon. Now I presume this is because I am looping trying to reconnect and its blocking because of this (I could be wrong on the blocking?). Can this problem be solved with multithreading? If so do I need to multithread? Is there an easier way to show my form/dialog and be able to interact (IE movie it around and close it with the 'X' in the upper right corner) with it while I still try to reconnect?

Thanks a bunch. I really appreciate you reading this post and am grateful for this community. :D

+3  A: 

Just an example below where I would handle any continuation logic in the catch and either break out or continue inside the while loop.

Andrew

 while (!connected)
        {
            try
            {
                sock.Connect(authenServerEP);
                connected = true;
                establishingConnectionForm.Dispose();
             }
             catch (SocketException retryConnectException)
             {
                 //Optional - add some wait time may be 5 seconds i.e. "trying again in 5 seconds"
                 //System.Threading.Thread.Sleep(5000);
                 //Here check the number of attempts and if exceeded:
                 if(numberOfTimes == 5)
                      break;
                 else{
                      numberOfTimes++; 
                      continue;
                 }
             }
         }
REA_ANDREW
I second this answer. It actually follows along what is known as the circuit breaker pattern. Attempt something until either some condition occurs or a threshold is met. It is very handy when dealing with external connections that you have no control over.
Joshua Cauble
This worked wonderfully. I appreciate your help. The one thing I have left to deal with is the fact that it seems when I bring up my establishingConnectionForm when I catch the SocketException I can never interact with it. It always shows the thinking icon and never lets me select it and move it around. Now I presume this is because I am looping trying to reconnect and its blocking because of this (I could be wrong on the blocking?). Is this a treahing problem? (IE all connecting should be done on threads so this connecting doesn't block - if it is blocking?)
Chris
Yes the while loop will cause blocking on the thread that it is on. Kicking it off on a separate thread and kick off events to notify listening entities of updated statuses!
REA_ANDREW