source: network-game/server/server.cpp@ 2488852

Last change on this file since 2488852 was 2488852, checked in by dportnoy <dmp1488@…>, 12 years ago

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

  • Property mode set to 100644
File size: 4.5 KB
Line 
1#include "../common/compiler.h"
2
3#include <sys/types.h>
4#include <cstdlib>
5#include <unistd.h>
6#include <sys/socket.h>
7#include <netinet/in.h>
8#include <string>
9#include <netdb.h>
10#include <cstdio>
11#include <iostream>
12#include <vector>
13#include <algorithm>
14
15#include <mysql/mysql.h>
16
17#include <openssl/bio.h>
18#include <openssl/ssl.h>
19#include <openssl/err.h>
20
21#include "player.h"
22#include "../common/message.h"
23///////////
24using namespace std;
25
26void error(const char *msg)
27{
28 perror(msg);
29 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;
43}
44
45int main(int argc, char *argv[])
46{
47 int sock, length, n;
48 socklen_t fromlen;
49 struct sockaddr_in server;
50 struct sockaddr_in from;
51 NETWORK_MSG clientMsg, serverMsg;
52 vector<player> vctPlayers;
53
54 srand(time(NULL));
55 int num = (rand() % 1000) + 1;
56
57 cout << "num: " << num << endl;
58
59 SSL_load_error_strings();
60 ERR_load_BIO_strings();
61 OpenSSL_add_all_algorithms();
62
63 if (argc < 2) {
64 fprintf(stderr, "ERROR, no port provided\n");
65 exit(0);
66 }
67
68 sock=socket(AF_INET, SOCK_DGRAM, 0);
69 if (sock < 0) error("Opening socket");
70 length = sizeof(server);
71 bzero(&server,length);
72 server.sin_family=AF_INET;
73 server.sin_port=htons(atoi(argv[1]));
74 server.sin_addr.s_addr=INADDR_ANY;
75 if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
76 error("binding");
77 fromlen = sizeof(struct sockaddr_in);
78 while (true) {
79 // if n == 0, means the client disconnected. may want to check this
80 n = receiveMessage(&clientMsg, sock, &from);
81 if (n < 0)
82 error("recieveMessage");
83 cout << "msg: " << clientMsg.buffer << endl;
84
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
101 if (strcmp(clientMsg.buffer, "Hello") == 0)
102 {
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 }
114 }else {
115 int guess = atoi(clientMsg.buffer);
116
117 cout << "guess: " << guess << endl;
118
119 if (guess < 1 || guess > 1000) {
120 strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
121 }else if(guess > num)
122 strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
123 else if(guess < num)
124 strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
125 else if(guess == num) {
126 strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
127 num = (rand() % 1000) + 1;
128 }
129 }
130
131 cout << "msg: " << serverMsg.buffer << endl;
132
133 n = sendMessage(&serverMsg, sock, &from);
134 if (n < 0)
135 error("sendMessage");
136 }
137 return 0;
138}
139
140int dbtest()
141{
142 MYSQL *connection, mysql;
143 MYSQL_RES *result;
144 MYSQL_ROW row;
145 int query_state;
146
147 mysql_init(&mysql);
148
149 connection = mysql_real_connect(&mysql,"localhost","pythonAdmin","pyMaster09*","pythondb",0,0,0);
150
151 if (connection == NULL) {
152 cout << mysql_error(&mysql) << endl;
153 return 1;
154 }else
155 cout << "Connection successful" << endl;
156
157 query_state = mysql_query(connection, "SELECT * FROM users");
158
159 if (query_state !=0) {
160 cout << mysql_error(connection) << endl;
161 return 1;
162 }
163
164 result = mysql_store_result(connection);
165
166 while ( ( row = mysql_fetch_row(result)) != NULL ) {
167 cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
168 }
169
170 mysql_free_result(result);
171 mysql_close(connection);
172
173 cout << "Test finished" << endl;
174
175 return 0;
176}
Note: See TracBrowser for help on using the repository browser.