Changeset 4da5aa3 in network-game for client/Client/main.cpp


Ignore:
Timestamp:
Nov 26, 2012, 10:18:25 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
73f75c1
Parents:
a4db787
Message:

Created a process message function to handle message received from the server

File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    ra4db787 r4da5aa3  
    3636void initWinSock();
    3737void shutdownWinSock();
     38void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, string &username);
     39
    3840void error(const char *);
    3941
     
    207209                     msgTo.type = MSG_TYPE_LOGIN;
    208210                     username = input;
     211
     212                     sendMessage(&msgTo, sock, &server);
     213                     receiveMessage(&msgFrom, sock, &from);
     214                     processMessage(msgFrom, state, chatConsole, username);
     215                     cout << "state: " << state << endl;
    209216
    210217                     break;
     
    222229                        msgTo.type = MSG_TYPE_CHAT;
    223230
     231                     sendMessage(&msgTo, sock, &server);
     232                     receiveMessage(&msgFrom, sock, &from);
     233                     processMessage(msgFrom, state, chatConsole, username);
     234                     cout << "state: " << state << endl;
     235
    224236                     break;
    225237                  }
    226238                  case STATE_LOGOUT:
    227239                  {
    228                      cout << "Bug: You're logged out, so you shouldn't be receiving any messages." << endl;
     240                     chatConsole.addLine("You're logged out, so you can't send any messages to the server.");
     241
     242                     cout << "You're logged out, so you can't send any messages to the server." << endl;
    229243                 
    230244                     break;
     
    237251                  }
    238252               }
    239 
    240                sendMessage(&msgTo, sock, &server);
    241 
    242                receiveMessage(&msgFrom, sock, &from);
    243                string response = string(msgFrom.buffer);
    244 
    245                // this whole select block should go in a new function.
    246                // Figure out how to pass and return params properly
    247                switch(state)
    248                {
    249                   case STATE_START:
    250                   {
    251                      chatConsole.addLine(string(msgFrom.buffer));
    252 
    253                      if (response.compare("Player has already logged in.") == 0)
    254                      {
    255                         cout << "User login failed" << endl;
    256                         username.clear();
    257                      }
    258                      else
    259                      {
    260                         cout << "User login successful" << endl;
    261                         state = STATE_LOGIN;
    262                      }
    263 
    264                      break;
    265                   }
    266                   case STATE_LOGIN:
    267                   {
    268                      chatConsole.addLine(string(msgFrom.buffer));
    269 
    270                      if (response.compare("You have been successfully logged out. You may quit the game.") == 0)
    271                      {
    272                         state = STATE_LOGOUT;
    273                      }
    274                      else
    275                      {
    276                         cout << "Added new line" << endl;
    277                      }
    278                      
    279                      break;
    280                   }
    281                   case STATE_LOGOUT:
    282                   {
    283                      cout << "Bug: You're logged out, so you shouldn't be receiving any messages." << endl;
    284                  
    285                      break;
    286                   }
    287                   default:
    288                   {
    289                      cout << "The state has an invalid value: " << state << endl;
    290 
    291                      break;
    292                   }
    293                }
    294253            }
    295254         }else {
     
    364323 
    365324   return 0;
     325}
     326
     327// need to make a function like this that works on windows
     328void error(const char *msg)
     329{
     330   perror(msg);
     331   shutdownWinSock();
     332   exit(1);
    366333}
    367334
     
    391358}
    392359
    393 // need to make a function like this that works on windows
    394 void error(const char *msg)
     360void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, string &username)
    395361{
    396    perror(msg);
    397    shutdownWinSock();
    398    exit(1);
     362   string response = string(msg.buffer);
     363
     364   // this whole select block should go in a new function.
     365   // Figure out how to pass and return params properly
     366   switch(state)
     367   {
     368      case STATE_START:
     369      {
     370         chatConsole.addLine(response);
     371
     372         if (response.compare("Player has already logged in.") == 0)
     373         {
     374            cout << "User login failed" << endl;
     375            username.clear();
     376         }
     377         else
     378         {
     379            cout << "User login successful" << endl;
     380            state = STATE_LOGIN;
     381         }
     382
     383         break;
     384      }
     385      case STATE_LOGIN:
     386      {
     387         chatConsole.addLine(response);
     388
     389         if (response.compare("You have successfully logged out. You may quit the game.") == 0)
     390         {
     391            cout << "Logged out" << endl;
     392            state = STATE_LOGOUT;
     393         }
     394         else
     395         {
     396            cout << "Added new line" << endl;
     397         }
     398                     
     399         break;
     400      }
     401      case STATE_LOGOUT:
     402      {
     403         cout << "Bug: You're logged out, so you shouldn't be receiving any messages." << endl;
     404                 
     405         break;
     406      }
     407      default:
     408      {
     409         cout << "The state has an invalid value: " << state << endl;
     410
     411         break;
     412      }
     413   }
    399414}
Note: See TracChangeset for help on using the changeset viewer.