diff options
Diffstat (limited to 'pacman-c++/server.cpp')
| -rw-r--r-- | pacman-c++/server.cpp | 85 |
1 files changed, 49 insertions, 36 deletions
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index 1c42174..b1877ad 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp | |||
| @@ -14,7 +14,10 @@ Server::Server(QWidget *parent) | |||
| 14 | waitForClientConnections(); | 14 | waitForClientConnections(); |
| 15 | qDebug() << "[Server] All Clients connected"; | 15 | qDebug() << "[Server] All Clients connected"; |
| 16 | 16 | ||
| 17 | updateMap(Util::createDemoMap()); | 17 | Transmission::map_t map = Util::createDemoMap(); |
| 18 | updateMap(map); | ||
| 19 | Util::deleteMap(map); | ||
| 20 | map = NULL; | ||
| 18 | 21 | ||
| 19 | QTimer *timer = new QTimer(this); | 22 | QTimer *timer = new QTimer(this); |
| 20 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); | 23 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); |
| @@ -24,11 +27,10 @@ Server::Server(QWidget *parent) | |||
| 24 | void Server::tick() | 27 | void Server::tick() |
| 25 | { | 28 | { |
| 26 | qDebug() << "[Tick] Doing server update"; | 29 | qDebug() << "[Tick] Doing server update"; |
| 27 | Transmission::map_t map = calculateUpdates(); | 30 | Transmission::map_t map = calculateUpdates(); |
| 28 | updateMap(map); | 31 | updateMap(map); |
| 29 | 32 | sendUpdate(map); | |
| 30 | QSharedPointer<ProtoBuf::MapUpdate> packet = createUpdatePacket(map); | 33 | Util::deleteMap(map); |
| 31 | sendUpdate(packet); | ||
| 32 | } | 34 | } |
| 33 | 35 | ||
| 34 | Transmission::map_t Server::calculateUpdates() | 36 | Transmission::map_t Server::calculateUpdates() |
| @@ -145,26 +147,6 @@ invalid_direction: | |||
| 145 | return map; | 147 | return map; |
| 146 | } | 148 | } |
| 147 | 149 | ||
| 148 | QSharedPointer<ProtoBuf::MapUpdate> Server::createUpdatePacket(Transmission::map_t map) | ||
| 149 | { | ||
| 150 | QSharedPointer<ProtoBuf::MapUpdate> updatePacket = | ||
| 151 | QSharedPointer<ProtoBuf::MapUpdate>(new ProtoBuf::MapUpdate); | ||
| 152 | |||
| 153 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 154 | { | ||
| 155 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 156 | updatePacket->add_field(map[x][y]); | ||
| 157 | } | ||
| 158 | |||
| 159 | for(unsigned i = 0; Color::order[i] != Color::none; ++i) | ||
| 160 | { | ||
| 161 | updatePacket->add_round_points(m_actors.value(Color::order[i])->getRoundPoints()); | ||
| 162 | updatePacket->add_game_points(m_actors.value(Color::order[i])->getGamePoints()); | ||
| 163 | } | ||
| 164 | |||
| 165 | return updatePacket; | ||
| 166 | } | ||
| 167 | |||
| 168 | void Server::waitForClientConnections() | 150 | void Server::waitForClientConnections() |
| 169 | { | 151 | { |
| 170 | QTcpServer *tcpSrv = new QTcpServer(this); | 152 | QTcpServer *tcpSrv = new QTcpServer(this); |
| @@ -182,13 +164,15 @@ void Server::waitForClientConnections() | |||
| 182 | #endif | 164 | #endif |
| 183 | bool connectionAvailable = tcpSrv->waitForNewConnection(-1); | 165 | bool connectionAvailable = tcpSrv->waitForNewConnection(-1); |
| 184 | Q_ASSERT(connectionAvailable); | 166 | Q_ASSERT(connectionAvailable); |
| 167 | Q_UNUSED(connectionAvailable); | ||
| 185 | QTcpSocket *socket = tcpSrv->nextPendingConnection(); | 168 | QTcpSocket *socket = tcpSrv->nextPendingConnection(); |
| 186 | connect(socket, SIGNAL(readyRead()), this, SLOT(keyPressUpdate())); | 169 | connect(socket, SIGNAL(readyRead()), this, SLOT(keyPressUpdate())); |
| 187 | 170 | ||
| 188 | // assign color | 171 | /* assign color */ |
| 189 | Color::Color color = Color::order[i]; | 172 | Color::Color color = Color::order[i]; |
| 190 | m_clientConnections[color] = socket; | 173 | m_clientConnections[color] = socket; |
| 191 | // notify player of color | 174 | /* notify player of color */ |
| 175 | //TODO: allocate + reuse packet | ||
| 192 | ProtoBuf::WhoAmI packet; | 176 | ProtoBuf::WhoAmI packet; |
| 193 | packet.set_color(color); | 177 | packet.set_color(color); |
| 194 | Util::sendPacket(packet, socket); | 178 | Util::sendPacket(packet, socket); |
| @@ -197,12 +181,31 @@ void Server::waitForClientConnections() | |||
| 197 | } | 181 | } |
| 198 | } | 182 | } |
| 199 | 183 | ||
| 200 | void Server::sendUpdate(QSharedPointer< ProtoBuf::MapUpdate > packet) | 184 | void Server::sendUpdate(Transmission::map_t map) |
| 201 | { | 185 | { |
| 202 | std::string dataStr = packet->SerializeAsString(); | 186 | ProtoBuf::MapUpdate packet; |
| 203 | const char *data = dataStr.c_str(); | 187 | |
| 188 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 189 | { | ||
| 190 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 191 | packet.add_field(map[x][y]); | ||
| 192 | } | ||
| 193 | |||
| 194 | for(unsigned i = 0; Color::order[i] != Color::none; ++i) | ||
| 195 | { | ||
| 196 | packet.add_round_points(m_actors.value(Color::order[i])->getRoundPoints()); | ||
| 197 | packet.add_game_points(m_actors.value(Color::order[i])->getGamePoints()); | ||
| 198 | } | ||
| 199 | |||
| 200 | QSharedPointer<QByteArray> data = Util::createPacket(packet); | ||
| 204 | foreach(QTcpSocket *socket, m_clientConnections) | 201 | foreach(QTcpSocket *socket, m_clientConnections) |
| 205 | Util::sendPacket(data, dataStr.length(), socket); | 202 | { |
| 203 | if (!Util::sendPacket(data.data(), socket)) | ||
| 204 | { | ||
| 205 | qDebug() << "[sendUpdate] Error while sending data to client, exiting..."; | ||
| 206 | qApp->quit(); | ||
| 207 | } | ||
| 208 | } | ||
| 206 | } | 209 | } |
| 207 | 210 | ||
| 208 | void Server::keyPressUpdate() | 211 | void Server::keyPressUpdate() |
| @@ -213,13 +216,15 @@ void Server::keyPressUpdate() | |||
| 213 | i.next(); | 216 | i.next(); |
| 214 | Color::Color color = i.key(); | 217 | Color::Color color = i.key(); |
| 215 | QTcpSocket *socket = i.value(); | 218 | QTcpSocket *socket = i.value(); |
| 216 | if (socket->bytesAvailable() > 0) | 219 | QDataStream in(i.value()); |
| 220 | while (socket->bytesAvailable() > (qint64)sizeof(qint64)) | ||
| 217 | { | 221 | { |
| 218 | std::string dataStr; | 222 | QSharedPointer<QByteArray> data = Util::receivePacket(socket); |
| 219 | Util::QByteArrayToStdString(socket->readAll(), dataStr); | 223 | //TODO: allocate + reuse packet |
| 220 | ProtoBuf::KeyPressUpdate packet; | 224 | ProtoBuf::KeyPressUpdate packet; |
| 221 | bool worked = packet.ParseFromString(dataStr); | 225 | bool worked = packet.ParseFromArray(data->data(), data->size()); |
| 222 | Q_ASSERT(worked); | 226 | Q_ASSERT(worked); |
| 227 | Q_UNUSED(worked); | ||
| 223 | Transmission::field_t direction = packet.newkey(); | 228 | Transmission::field_t direction = packet.newkey(); |
| 224 | qDebug() << "[KeyPress] actor=" << color << "direction=" << direction; | 229 | qDebug() << "[KeyPress] actor=" << color << "direction=" << direction; |
| 225 | m_actorMovements[color] = Util::transmissionMovementToActor(direction); | 230 | m_actorMovements[color] = Util::transmissionMovementToActor(direction); |
| @@ -231,6 +236,9 @@ bool Constants::server = true; | |||
| 231 | 236 | ||
| 232 | int main(int argc, char ** argv) | 237 | int main(int argc, char ** argv) |
| 233 | { | 238 | { |
| 239 | /* Verify that the version of the library that we linked against is | ||
| 240 | * compatible with the version of the headers we compiled against. | ||
| 241 | */ | ||
| 234 | GOOGLE_PROTOBUF_VERIFY_VERSION; | 242 | GOOGLE_PROTOBUF_VERIFY_VERSION; |
| 235 | 243 | ||
| 236 | QApplication app(argc, argv, false); | 244 | QApplication app(argc, argv, false); |
| @@ -240,5 +248,10 @@ int main(int argc, char ** argv) | |||
| 240 | qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); | 248 | qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); |
| 241 | 249 | ||
| 242 | Server Server; | 250 | Server Server; |
| 243 | return app.exec(); | 251 | int ret = app.exec(); |
| 252 | |||
| 253 | /* Delete all global objects allocated by libprotobuf */ | ||
| 254 | google::protobuf::ShutdownProtobufLibrary(); | ||
| 255 | |||
| 256 | return ret; | ||
| 244 | } | 257 | } |
