diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-08-20 11:56:23 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-08-20 11:56:23 +0000 |
commit | ae45030b2ebd5b07f8cfd234696fd024e6de9e66 (patch) | |
tree | 465affc2bd7df675edb428d7f003e5dbbba84c0e /src/net/network.cpp | |
parent | 01591924a4f33d5a5e4a86db6c256c8ce797a820 (diff) | |
download | mana-ae45030b2ebd5b07f8cfd234696fd024e6de9e66.tar.gz mana-ae45030b2ebd5b07f8cfd234696fd024e6de9e66.tar.bz2 mana-ae45030b2ebd5b07f8cfd234696fd024e6de9e66.tar.xz mana-ae45030b2ebd5b07f8cfd234696fd024e6de9e66.zip |
Added documentation and did a bit of cleanup.
Diffstat (limited to 'src/net/network.cpp')
-rw-r--r-- | src/net/network.cpp | 89 |
1 files changed, 25 insertions, 64 deletions
diff --git a/src/net/network.cpp b/src/net/network.cpp index 23d9e291..911353b1 100644 --- a/src/net/network.cpp +++ b/src/net/network.cpp @@ -32,11 +32,12 @@ Network *network; Network::Network(): - mAccountServer(NULL), - mGameServer(NULL), - mChatServer(NULL), mState(NET_OK) { + // Initialize server peers + for (int i = 0; i < 3; ++i) + mServers[i] = NULL; + mClient = enet_host_create(NULL, 3, 0, 0); if (!mClient) @@ -68,15 +69,7 @@ Network::connect(Server server, const std::string &address, short port) return false; } - ENetPeer *peer = NULL; - - switch (server) { - case ACCOUNT: peer = mAccountServer; break; - case GAME: peer = mGameServer; break; - case CHAT: peer = mChatServer; break; - } - - if (peer != NULL) + if (mServers[server] != NULL) { logger->log("Network::connect() already connected (or connecting) to " "this server!"); @@ -89,46 +82,28 @@ Network::connect(Server server, const std::string &address, short port) enetAddress.port = port; // Initiate the connection, allocating channel 0. - peer = enet_host_connect(mClient, &enetAddress, 1); + mServers[server] = enet_host_connect(mClient, &enetAddress, 1); - if (peer == NULL) + if (mServers[server] == NULL) { logger->log("Unable to initiate connection to the server."); mState = NET_ERROR; return false; } - switch (server) { - case ACCOUNT: mAccountServer = peer; break; - case GAME: mGameServer = peer; break; - case CHAT: mChatServer = peer; break; - } - return true; } void Network::disconnect(Server server) { - ENetPeer *peer = NULL; - - switch (server) { - case ACCOUNT: peer = mAccountServer; break; - case GAME: peer = mGameServer; break; - case CHAT: peer = mChatServer; break; - } - - if (peer) + if (mServers[server]) { - enet_peer_disconnect(peer, 0); + enet_peer_disconnect(mServers[server], 0); enet_host_flush(mClient); - enet_peer_reset(peer); + enet_peer_reset(mServers[server]); - switch (server) { - case ACCOUNT: mAccountServer = NULL; break; - case GAME: mGameServer = NULL; break; - case CHAT: mChatServer = NULL; break; - } + mServers[server] = NULL; } } @@ -171,15 +146,8 @@ Network::clearHandlers() bool Network::isConnected(Server server) const { - ENetPeer *peer = NULL; - - switch (server) { - case ACCOUNT: peer = mAccountServer; break; - case GAME: peer = mGameServer; break; - case CHAT: peer = mChatServer; break; - } - - return peer->state == ENET_PEER_STATE_CONNECTED; + return mServers[server] != NULL && + mServers[server]->state == ENET_PEER_STATE_CONNECTED; } void @@ -253,27 +221,20 @@ void Network::send(Server server, const MessageOut &msg) "ready."); return; } - - ENetPeer *peer = NULL; - - switch (server) { - case ACCOUNT: peer = mAccountServer; break; - case GAME: peer = mGameServer; break; - case CHAT: peer = mChatServer; break; - } - - if (peer) + else if (!isConnected(server)) { - logger->log("Sending message of size %d to server %d...", - msg.getDataSize(), server); - - // Directly send away the packet (TODO: check what ENet does in case - // this is done before connection is ready) - ENetPacket *packet = enet_packet_create(msg.getData(), - msg.getDataSize(), - ENET_PACKET_FLAG_RELIABLE); - enet_peer_send(peer, 0, packet); + logger->log("Warning: cannot send message to not connected server %d!", + server); + return; } + + logger->log("Sending message of size %d to server %d...", + msg.getDataSize(), server); + + ENetPacket *packet = enet_packet_create(msg.getData(), + msg.getDataSize(), + ENET_PACKET_FLAG_RELIABLE); + enet_peer_send(mServers[server], 0, packet); } char *iptostring(int address) |