diff options
Diffstat (limited to 'pacman-c++/server.cpp')
| -rw-r--r-- | pacman-c++/server.cpp | 104 |
1 files changed, 92 insertions, 12 deletions
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index 8539410..8f9e42d 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp | |||
| @@ -1,34 +1,39 @@ | |||
| 1 | #include "server.h" | 1 | #include "server.h" |
| 2 | 2 | ||
| 3 | #include <QtNetwork/QTcpServer> | ||
| 4 | #include <QtNetwork/QTcpSocket> | ||
| 3 | #include "util.h" | 5 | #include "util.h" |
| 4 | |||
| 5 | #include "pacman.pb.h" | 6 | #include "pacman.pb.h" |
| 6 | 7 | ||
| 7 | Server::Server(QWidget *parent) | 8 | Server::Server(QWidget *parent) |
| 8 | : SceneHolder(parent) | 9 | : SceneHolder(parent) |
| 9 | { | 10 | { |
| 11 | qDebug() << "waiting for clients"; | ||
| 12 | waitForClientConnections(); | ||
| 13 | qDebug() << "clients connected"; | ||
| 14 | |||
| 10 | updateMap(Util::createDummyMap()); | 15 | updateMap(Util::createDummyMap()); |
| 11 | 16 | ||
| 12 | Transmission::map_t map = calculateUpdates(); | 17 | QTimer *timer = new QTimer(this); |
| 13 | for (int i=0; i<10; ++i) { | 18 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); |
| 14 | qDebug() << "doing srv update"; | 19 | timer->start(500); |
| 15 | updateMap(map); | ||
| 16 | } | ||
| 17 | } | 20 | } |
| 18 | 21 | ||
| 19 | QMap< Color::Color, Actor::Movement > Server::getActorDirections() | 22 | void Server::tick() |
| 20 | { | 23 | { |
| 21 | QMap<Color::Color, Actor::Movement> directions; | 24 | qDebug() << "doing srv update"; |
| 22 | directions[Color::red] = Actor::Down; | 25 | Transmission::map_t map = calculateUpdates(); |
| 23 | return directions; | 26 | updateMap(map); |
| 27 | |||
| 28 | QSharedPointer<ProtoBuf::MapUpdate> packet = createUpdatePacket(map); | ||
| 29 | sendUpdate(packet); | ||
| 24 | } | 30 | } |
| 25 | 31 | ||
| 26 | Transmission::map_t Server::calculateUpdates() | 32 | Transmission::map_t Server::calculateUpdates() |
| 27 | { | 33 | { |
| 28 | Transmission::map_t map = Util::createEmptyMap(); | 34 | Transmission::map_t map = Util::createEmptyMap(); |
| 29 | 35 | ||
| 30 | QMap<Color::Color, Actor::Movement> directions = getActorDirections(); | 36 | QMapIterator<Color::Color, Actor::Movement> i(m_actorMovements); |
| 31 | QMapIterator<Color::Color, Actor::Movement> i(directions); | ||
| 32 | while (i.hasNext()) { | 37 | while (i.hasNext()) { |
| 33 | i.next(); | 38 | i.next(); |
| 34 | Actor *actor = m_actors.value( i.key() ); | 39 | Actor *actor = m_actors.value( i.key() ); |
| @@ -52,8 +57,83 @@ Transmission::map_t Server::calculateUpdates() | |||
| 52 | return map; | 57 | return map; |
| 53 | } | 58 | } |
| 54 | 59 | ||
| 60 | QSharedPointer< ProtoBuf::MapUpdate > Server::createUpdatePacket(Transmission::map_t map) | ||
| 61 | { | ||
| 62 | QSharedPointer<ProtoBuf::MapUpdate> updatePacket = | ||
| 63 | QSharedPointer<ProtoBuf::MapUpdate>(new ProtoBuf::MapUpdate); | ||
| 64 | |||
| 65 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) { | ||
| 66 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) { | ||
| 67 | updatePacket->add_field(map[x][y]); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | //qDebug() << "field sz "<< updatePacket->field_size(); | ||
| 71 | return updatePacket; | ||
| 72 | } | ||
| 73 | |||
| 74 | void Server::waitForClientConnections() | ||
| 75 | { | ||
| 76 | QTcpServer *tcpSrv = new QTcpServer(this); | ||
| 77 | // server must stay alive as long as sockets (qt parent mem mechanism) | ||
| 78 | tcpSrv->listen(QHostAddress::Any, Constants::port); | ||
| 79 | |||
| 80 | //for (unsigned int i=0; i<Color::max; ++i) { | ||
| 81 | for (unsigned int i=0; i<1; ++i) { | ||
| 82 | bool connectionAvailable = tcpSrv->waitForNewConnection(-1); | ||
| 83 | Q_ASSERT(connectionAvailable); | ||
| 84 | QTcpSocket *socket = tcpSrv->nextPendingConnection(); | ||
| 85 | // TODO: color assignment | ||
| 86 | m_clientConnections[Color::red] = socket; | ||
| 87 | connect(socket, SIGNAL(readyRead()), this, SLOT(keyPressUpdate())); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | void Server::sendUpdate(QSharedPointer< ProtoBuf::MapUpdate > packet) | ||
| 92 | { | ||
| 93 | std::string dataStr = packet->SerializeAsString(); | ||
| 94 | const char *data = dataStr.c_str(); | ||
| 95 | foreach(QTcpSocket *socket, m_clientConnections) { | ||
| 96 | //qDebug() << "sending str len: " << dataStr.length(); | ||
| 97 | int bytesWritten = socket->write(data, dataStr.length()); | ||
| 98 | Q_ASSERT(bytesWritten == dataStr.length()); | ||
| 99 | } | ||
| 100 | foreach(QTcpSocket *socket, m_clientConnections) { | ||
| 101 | socket->flush(); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | void Server::keyPressUpdate() | ||
| 106 | { | ||
| 107 | qDebug() << "kpress"; | ||
| 108 | QMapIterator<Color::Color, QTcpSocket*> i(m_clientConnections); | ||
| 109 | while (i.hasNext()) { | ||
| 110 | i.next(); | ||
| 111 | Color::Color color = i.key(); | ||
| 112 | QTcpSocket *socket = i.value(); | ||
| 113 | qDebug() << "data?"; | ||
| 114 | if (socket->bytesAvailable() > 0) { | ||
| 115 | qDebug() << "data!"; | ||
| 116 | QByteArray data = socket->readAll(); | ||
| 117 | // see mainwidget.cpp:153 | ||
| 118 | std::string dataStr; | ||
| 119 | for (int i = 0; i < data.size(); ++i) { | ||
| 120 | dataStr += data[i]; | ||
| 121 | } | ||
| 122 | ProtoBuf::KeyPressUpdate packet; | ||
| 123 | packet.ParseFromString(dataStr); | ||
| 124 | Transmission::field_t direction = packet.newkey(); | ||
| 125 | qDebug() << "data:" << direction; | ||
| 126 | m_actorMovements[ color ] = Util::transmissionMovementToActor(direction); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 55 | int main(int argc, char ** argv) | 133 | int main(int argc, char ** argv) |
| 56 | { | 134 | { |
| 135 | GOOGLE_PROTOBUF_VERIFY_VERSION; | ||
| 136 | |||
| 57 | QApplication app(argc, argv); | 137 | QApplication app(argc, argv); |
| 58 | app.setApplicationName("Pacman Server"); | 138 | app.setApplicationName("Pacman Server"); |
| 59 | app.setWindowIcon(QIcon(":/appicon")); | 139 | app.setWindowIcon(QIcon(":/appicon")); |
