diff options
Diffstat (limited to 'pacman-c++/server/server.cpp')
| -rw-r--r-- | pacman-c++/server/server.cpp | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/pacman-c++/server/server.cpp b/pacman-c++/server/server.cpp index 877c4a6..316b719 100644 --- a/pacman-c++/server/server.cpp +++ b/pacman-c++/server/server.cpp | |||
| @@ -84,8 +84,8 @@ void Server::tick() | |||
| 84 | return; | 84 | return; |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | /* fetch key updates */ | 87 | /* fetch client updates */ |
| 88 | keyPressUpdate(); | 88 | readClientUpdates(); |
| 89 | 89 | ||
| 90 | /* let the bots move */ | 90 | /* let the bots move */ |
| 91 | foreach (Color::Color color, m_bots) | 91 | foreach (Color::Color color, m_bots) |
| @@ -402,7 +402,7 @@ bool Server::waitForClientConnections() | |||
| 402 | } | 402 | } |
| 403 | 403 | ||
| 404 | char buf[1024]; | 404 | char buf[1024]; |
| 405 | enet_address_get_host_ip(&m_host->address, buf, 1024); | 405 | enet_address_get_host_ip(&m_host->address, buf, sizeof(buf)); |
| 406 | std::cout << "[Server] Listening on: " | 406 | std::cout << "[Server] Listening on: " |
| 407 | << qPrintable(QString("%1:%2").arg(buf).arg(m_host->address.port)) << std::endl; | 407 | << qPrintable(QString("%1:%2").arg(buf).arg(m_host->address.port)) << std::endl; |
| 408 | 408 | ||
| @@ -431,14 +431,18 @@ bool Server::waitForClientConnections() | |||
| 431 | m_clientConnections[event.peer] = color; | 431 | m_clientConnections[event.peer] = color; |
| 432 | packet.set_color(color); | 432 | packet.set_color(color); |
| 433 | Util::sendPacket(packet, event.peer, m_host); | 433 | Util::sendPacket(packet, event.peer, m_host); |
| 434 | std::cout << "[Connect] New Player: color=" << qPrintable(Util::colorToString(color)) << std::endl; | 434 | |
| 435 | char buf[1024]; | ||
| 436 | enet_address_get_host_ip(&event.peer->address, buf, sizeof(buf)); | ||
| 437 | std::cout << "[Connect] New Player: color=" << qPrintable(Util::colorToString(color)) | ||
| 438 | << ", address=" << buf << ':' << event.peer->address.port << std::endl; | ||
| 435 | } | 439 | } |
| 436 | break; | 440 | break; |
| 437 | case ENET_EVENT_TYPE_RECEIVE: | 441 | case ENET_EVENT_TYPE_RECEIVE: |
| 438 | keyPressUpdate(&event); | 442 | processClientUpdate(&event); |
| 439 | break; | 443 | break; |
| 440 | case ENET_EVENT_TYPE_DISCONNECT: | 444 | case ENET_EVENT_TYPE_DISCONNECT: |
| 441 | keyPressUpdate(&event); | 445 | processClientUpdate(&event); |
| 442 | break; | 446 | break; |
| 443 | default: | 447 | default: |
| 444 | break; | 448 | break; |
| @@ -626,28 +630,29 @@ void Server::botCalculate(Actor *actor) | |||
| 626 | m_actorMovements[actor->color()] = list.at(rand); | 630 | m_actorMovements[actor->color()] = list.at(rand); |
| 627 | } | 631 | } |
| 628 | 632 | ||
| 629 | void Server::keyPressUpdate() | 633 | void Server::readClientUpdates() |
| 630 | { | 634 | { |
| 631 | ProtoBuf::KeyPressUpdate packet; | ||
| 632 | ENetEvent event; | 635 | ENetEvent event; |
| 633 | while (enet_host_service(m_host, &event, 1) > 0) | 636 | while (enet_host_service(m_host, &event, 1) > 0) |
| 634 | keyPressUpdate(&event); | 637 | processClientUpdate(&event); |
| 635 | } | 638 | } |
| 636 | 639 | ||
| 637 | void Server::keyPressUpdate(ENetEvent *event) | 640 | void Server::processClientUpdate(ENetEvent *event) |
| 638 | { | 641 | { |
| 639 | ProtoBuf::KeyPressUpdate packet; | ||
| 640 | switch(event->type) | 642 | switch(event->type) |
| 641 | { | 643 | { |
| 642 | case ENET_EVENT_TYPE_RECEIVE: | 644 | case ENET_EVENT_TYPE_RECEIVE: |
| 643 | { | 645 | { |
| 644 | QSharedPointer<QByteArray> data = Util::receivePacket(event->packet); | 646 | QSharedPointer<QByteArray> data = Util::receivePacket(event->packet); |
| 645 | enet_packet_destroy(event->packet); | 647 | enet_packet_destroy(event->packet); |
| 646 | bool worked = packet.ParseFromArray(data->data(), data->size()); | 648 | ProtoBuf::ClientUpdate packet; |
| 647 | Q_ASSERT(worked); | ||
| 648 | Q_UNUSED(worked); | ||
| 649 | Transmission::field_t direction = packet.newkey(); | ||
| 650 | Color::Color color = m_clientConnections[event->peer]; | 649 | Color::Color color = m_clientConnections[event->peer]; |
| 650 | if (!packet.ParseFromArray(data->data(), data->size())) | ||
| 651 | { | ||
| 652 | qWarning() << "Invalid packet from client color=" << color; | ||
| 653 | return; | ||
| 654 | } | ||
| 655 | Transmission::field_t direction = packet.new_direction(); | ||
| 651 | qDebug() << "[KeyPress] actor=" << color << "direction=" << direction; | 656 | qDebug() << "[KeyPress] actor=" << color << "direction=" << direction; |
| 652 | m_actorMovements[color] = Util::transmissionMovementToActor(direction); | 657 | m_actorMovements[color] = Util::transmissionMovementToActor(direction); |
| 653 | } | 658 | } |
| @@ -760,7 +765,7 @@ void Server::initRoundMap() | |||
| 760 | 765 | ||
| 761 | disconnect(AudioManager::self()->audioPlayer(), NULL, this, NULL); | 766 | disconnect(AudioManager::self()->audioPlayer(), NULL, this, NULL); |
| 762 | connect(AudioManager::self()->audioPlayer(), SIGNAL(finished()), this, SLOT(startGame())); | 767 | connect(AudioManager::self()->audioPlayer(), SIGNAL(finished()), this, SLOT(startGame())); |
| 763 | AudioManager::self()->audioPlayer()->play(Sound::Intro); | 768 | AudioManager::self()->play(Sound::Intro); |
| 764 | m_tickTimer->start(); | 769 | m_tickTimer->start(); |
| 765 | } | 770 | } |
| 766 | 771 | ||
| @@ -782,7 +787,7 @@ void Server::stopGame(bool delay) | |||
| 782 | { | 787 | { |
| 783 | disconnect(AudioManager::self()->audioPlayer(), NULL, this, NULL); | 788 | disconnect(AudioManager::self()->audioPlayer(), NULL, this, NULL); |
| 784 | connect(AudioManager::self()->audioPlayer(), SIGNAL(finished()), this, SLOT(stopGame())); | 789 | connect(AudioManager::self()->audioPlayer(), SIGNAL(finished()), this, SLOT(stopGame())); |
| 785 | AudioManager::self()->audioPlayer()->play(Sound::Die); | 790 | AudioManager::self()->play(Sound::Die); |
| 786 | return; | 791 | return; |
| 787 | } | 792 | } |
| 788 | 793 | ||
