diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2008-10-26 20:32:38 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2008-10-26 20:32:38 +0000 |
commit | ff9438ab94d19f0957a264ff79def01f9855b707 (patch) | |
tree | 81f9a6e2fd54b33548b7b60c7e3906b270f60c39 /src/net/connection.cpp | |
parent | cfabe4237b9298e63912972666a2ab25948ce2e9 (diff) | |
download | manaserv-ff9438ab94d19f0957a264ff79def01f9855b707.tar.gz manaserv-ff9438ab94d19f0957a264ff79def01f9855b707.tar.bz2 manaserv-ff9438ab94d19f0957a264ff79def01f9855b707.tar.xz manaserv-ff9438ab94d19f0957a264ff79def01f9855b707.zip |
Some cleanup and an unsuccesful attempt at fixing a crash on map server exit
when there is no connection the account server (a different one surfaced).
Diffstat (limited to 'src/net/connection.cpp')
-rw-r--r-- | src/net/connection.cpp | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/src/net/connection.cpp b/src/net/connection.cpp index faff533c..38ecd1bf 100644 --- a/src/net/connection.cpp +++ b/src/net/connection.cpp @@ -26,16 +26,22 @@ #include "net/messageout.hpp" #include "utils/logger.h" +Connection::Connection(): + mRemote(0), + mLocal(0) +{ +} + bool Connection::start(std::string const &address, int port) { ENetAddress enetAddress; enet_address_set_host(&enetAddress, address.c_str()); enetAddress.port = port; - mLocal = enet_host_create(NULL /* create a client host */, - 1 /* allow one outgoing connection */, - 0 /* assume any amount of incoming bandwidth */, - 0 /* assume any amount of outgoing bandwidth */); + mLocal = enet_host_create(NULL /* create a client host */, + 1 /* allow one outgoing connection */, + 0 /* assume any amount of incoming bandwidth */, + 0 /* assume any amount of outgoing bandwidth */); if (!mLocal) return false; @@ -54,11 +60,17 @@ bool Connection::start(std::string const &address, int port) void Connection::stop() { - enet_peer_disconnect(mRemote, 0); - enet_host_flush(mLocal); - enet_peer_reset(mRemote); - enet_host_destroy(mLocal); - mRemote = NULL; + if (mRemote) + enet_peer_disconnect(mRemote, 0); + if (mLocal) + enet_host_flush(mLocal); + if (mRemote) + enet_peer_reset(mRemote); + if (mLocal) + enet_host_destroy(mLocal); + + mRemote = 0; + mLocal = 0; } bool Connection::isConnected() const @@ -68,19 +80,20 @@ bool Connection::isConnected() const void Connection::send(MessageOut const &msg, bool reliable, unsigned channel) { + if (!mRemote) { + LOG_WARN("Can't send message to unconnected host! (" << msg << ")"); + return; + } + ENetPacket *packet; packet = enet_packet_create(msg.getData(), msg.getLength(), reliable ? ENET_PACKET_FLAG_RELIABLE : 0); if (packet) - { enet_peer_send(mRemote, channel, packet); - } else - { LOG_ERROR("Failure to create packet!"); - } } void Connection::process() |