Changeset 2488852 in network-game for server


Ignore:
Timestamp:
Nov 26, 2012, 5:45:25 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
0cc431d
Parents:
6475138
Message:

Added the player class, added a list of logged-in players, and changed the makefile to work properly, and made git ignore all build artifacts

Location:
server
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • server/.gitignore

    r6475138 r2488852  
    11server
     2*.o
  • server/makefile

    r6475138 r2488852  
    1 server : ../common/message.cpp server.cpp
    2         g++ -o $@ $? -lssl -lmysqlclient
     1CC = g++
     2LIB_FLAGS = -lssl -lmysqlclient
     3FLAGS = $(LIB_FLAGS)
     4COMMON_PATH = ../common
     5DEPENDENCIES = message.o player.o
     6
     7server : server.cpp $(DEPENDENCIES)
     8        $(CC) -o $@ $+ $(FLAGS)
     9
     10message.o : $(COMMON_PATH)/message.cpp
     11        $(CC) -c -o $@ $?
     12
     13%.o : %.cpp
     14        $(CC) -c -o $@ $?
     15
     16clean:
     17        rm *.o
     18        rm server
  • server/server.cpp

    r6475138 r2488852  
    22
    33#include <sys/types.h>
    4 #include <stdlib.h>
     4#include <cstdlib>
    55#include <unistd.h>
    66#include <sys/socket.h>
    77#include <netinet/in.h>
    8 #include <string.h>
     8#include <string>
    99#include <netdb.h>
    10 #include <stdio.h>
     10#include <cstdio>
    1111#include <iostream>
     12#include <vector>
     13#include <algorithm>
    1214
    1315#include <mysql/mysql.h>
     
    1618#include <openssl/ssl.h>
    1719#include <openssl/err.h>
     20
     21#include "player.h"
    1822#include "../common/message.h"
    19 
     23///////////
    2024using namespace std;
    2125
     
    2428    perror(msg);
    2529    exit(0);
     30}
     31
     32player *findPlayerByName(vector<player> &vec, string name)
     33{
     34   vector<player>::iterator it;
     35
     36   for (it = vec.begin(); it != vec.end(); it++)
     37   {
     38      if ( it->name.compare(name) == 0 )
     39         return &(*it);
     40   }
     41
     42   return NULL;
    2643}
    2744
     
    3350   struct sockaddr_in from;
    3451   NETWORK_MSG clientMsg, serverMsg;
     52   vector<player> vctPlayers;
    3553
    3654   srand(time(NULL));
     
    5371   bzero(&server,length);
    5472   server.sin_family=AF_INET;
     73   server.sin_port=htons(atoi(argv[1]));
    5574   server.sin_addr.s_addr=INADDR_ANY;
    56    server.sin_port=htons(atoi(argv[1]));
    57    if (bind(sock,(struct sockaddr *)&server,length)<0)
     75   if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
    5876      error("binding");
    5977   fromlen = sizeof(struct sockaddr_in);
    6078   while (true) {
     79      // if n == 0, means the client disconnected. may want to check this
    6180      n = receiveMessage(&clientMsg, sock, &from);
    6281      if (n < 0)
     
    6483      cout << "msg: " << clientMsg.buffer << endl;
    6584
     85      // Ptoyovol Design
     86      //
     87      // Client sends a login message
     88      // Server replies with client's position in the world and positions of
     89      // oall other logged in players
     90      // Merver sends player ids along with locations
     91      // This means a newly logged in client will need to know which id is
     92      // assigned to it
     93      // So server needs to send an id message, wait for an ack, and then send
     94      // the location messages
     95
     96      // When a client shuts down, it sends a message to indicate this so the
     97      // server can remove it from the list of connected clients
     98      // Eventually, there'll need to be a way to detect timeouts from clients
     99      // (if they crashed or otherwise failed to send the logout message)
     100
    66101      if (strcmp(clientMsg.buffer, "Hello") == 0)
    67102      {
    68          strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
     103         player *p = findPlayerByName(vctPlayers, "Boberty");
     104
     105         if (p == NULL)
     106         {
     107            vctPlayers.push_back(player("Boberty", from));
     108            strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
     109         }
     110         else
     111         {
     112            strcpy(serverMsg.buffer, "Player has already logged in.");
     113         }
    69114      }else {
    70115         int guess = atoi(clientMsg.buffer);
Note: See TracChangeset for help on using the changeset viewer.