Changeset 3b1efcc in network-game


Ignore:
Timestamp:
Dec 9, 2012, 4:38:38 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
581058c
Parents:
b53c6b3
Message:

The server sends back an error on failed registration and sends chat messages to all players, not just the sender. The non-blocking socket function was moved to a new file in the common folder so the client can use it.

Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    rb53c6b3 r3b1efcc  
    360360   string response = string(msg.buffer);
    361361
    362    // this whole select block should go in a new function.
    363    // Figure out how to pass and return params properly
    364362   switch(state)
    365363   {
  • server/server.cpp

    rb53c6b3 r3b1efcc  
    44#include <string>
    55#include <iostream>
     6#include <sstream>
    67#include <vector>
    78#include <algorithm>
     
    2122#include "../common/Compiler.h"
    2223#include "../common/Message.h"
     24#include "../common/Common.h"
    2325
    2426#include "Player.h"
     
    2729using namespace std;
    2830
    29 void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg);
     31bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg);
    3032
    3133// this should probably go somewhere in the common folder
     
    6365}
    6466
    65 void set_nonblock(int sock)
    66 {
    67     int flags;
    68     flags = fcntl(sock, F_GETFL,0);
    69     assert(flags != -1);
    70     fcntl(sock, F_SETFL, flags | O_NONBLOCK);
    71 }
    72 
    7367int main(int argc, char *argv[])
    7468{
    7569   int sock, length, n;
    7670   struct sockaddr_in server;
    77    struct sockaddr_in from; // holds the info about the connected client
     71   struct sockaddr_in from; // info of client sending the message
    7872   NETWORK_MSG clientMsg, serverMsg;
    7973   vector<Player> vctPlayers;
    80 
    81    srand(time(NULL));
    82    int num = (rand() % 1000) + 1;
    83 
    84    cout << "num: " << num << endl;
    8574
    8675   SSL_load_error_strings();
     
    10594   set_nonblock(sock);
    10695
     96   bool broadcastMessage;
    10797   while (true) {
    10898
     
    114104         cout << "Got a message" << endl;
    115105
    116          processMessage(clientMsg, from, vctPlayers, num, serverMsg);
     106         broadcastMessage = processMessage(clientMsg, from, vctPlayers, serverMsg);
    117107
    118108         cout << "msg: " << serverMsg.buffer << endl;
    119109
    120          n = sendMessage(&serverMsg, sock, &from);
    121          if (n  < 0)
    122             error("sendMessage");
     110         if (broadcastMessage)
     111         {
     112            vector<Player>::iterator it;
     113
     114            for (it = vctPlayers.begin(); it != vctPlayers.end(); it++)
     115            {
     116               if ( sendMessage(&serverMsg, sock, &(it->addr)) < 0 )
     117                  error("sendMessage");
     118            }
     119         }
     120         else
     121         {
     122            if ( sendMessage(&serverMsg, sock, &from) < 0 )
     123               error("sendMessage");
     124         }
    123125      }
    124126
     
    128130}
    129131
    130 void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg)
     132bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg)
    131133{
    132134   DataAccess da;
     
    136138   cout << "MSG: type: " << clientMsg.type << endl;
    137139   cout << "MSG contents: " << clientMsg.buffer << endl;
     140
     141   bool broadcastResponse = false;
    138142
    139143   // Check that if an invalid message is sent, the client will correctly
     
    149153         cout << "password: " << password << endl;
    150154
    151          da.insertPlayer(username, password);
    152 
    153          strcpy(serverMsg.buffer, "Registration successful");
     155         int error = da.insertPlayer(username, password);
     156
     157         if (!error)
     158            strcpy(serverMsg.buffer, "Registration successful.");
     159         else
     160            strcpy(serverMsg.buffer, "Registration failed. Please try again.");
    154161
    155162         serverMsg.type = MSG_TYPE_REGISTER;
     
    179186
    180187            vctPlayers.push_back(newP);
    181             strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
     188            strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with outher players.");
    182189         }
    183190
     
    222229         else
    223230         {
    224             int guess = atoi(clientMsg.buffer);
    225 
    226             cout << "guess: " << guess << endl;
    227 
    228             if (guess < 1 || guess > 1000) {
    229                strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
    230             }else if(guess > num)
    231                strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
    232             else if(guess < num)
    233                strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
    234             else if(guess == num) {
    235                strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
    236                num = (rand() % 1000) + 1;
    237             }
     231            broadcastResponse = true;
     232
     233            stringstream ss;
     234            ss << p->name << ": " << clientMsg.buffer << endl;
     235
     236            strcpy(serverMsg.buffer, ss.str().c_str());
    238237         }     
    239238
     
    250249         break;
    251250      }
    252    }
    253 }
     251
     252      return broadcastResponse;
     253   }
     254}
Note: See TracChangeset for help on using the changeset viewer.