summaryrefslogtreecommitdiff
path: root/src/netcomputer.cpp
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2006-08-25 19:47:33 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2006-08-25 19:47:33 +0000
commit1221231dc656368bf1595b7827ad3a48f38b33b7 (patch)
treed0b5691a3a027d46d57364f86727baaecce5f4e0 /src/netcomputer.cpp
parent87eecbfc7c7ac72cb1a57ba83e967c655213d116 (diff)
downloadmanaserv-1221231dc656368bf1595b7827ad3a48f38b33b7.tar.gz
manaserv-1221231dc656368bf1595b7827ad3a48f38b33b7.tar.bz2
manaserv-1221231dc656368bf1595b7827ad3a48f38b33b7.tar.xz
manaserv-1221231dc656368bf1595b7827ad3a48f38b33b7.zip
Applied patch by Rogier, addressing TODOs in MessageOut and NetComputer.
Diffstat (limited to 'src/netcomputer.cpp')
-rw-r--r--src/netcomputer.cpp66
1 files changed, 46 insertions, 20 deletions
diff --git a/src/netcomputer.cpp b/src/netcomputer.cpp
index 1c3c50b6..fd9bec5e 100644
--- a/src/netcomputer.cpp
+++ b/src/netcomputer.cpp
@@ -23,39 +23,65 @@
#include "netcomputer.h"
-#include "chatchannelmanager.h"
-#include "connectionhandler.h"
+#include <iosfwd>
+#include <queue>
+#include <iostream>
+
+#include <enet/enet.h>
+
+#include "defines.h"
#include "messageout.h"
-#include "state.h"
+#include "connectionhandler.h"
#include "utils/logger.h"
-NetComputer::NetComputer(ConnectionHandler *handler, ENetPeer *peer):
- mHandler(handler),
+NetComputer::NetComputer(ENetPeer *peer):
mPeer(peer)
{
}
-void NetComputer::disconnect(const std::string &reason)
+bool
+NetComputer::isConnected()
+{
+ return (mPeer->state == ENET_PEER_STATE_CONNECTED);
+}
+
+void
+NetComputer::disconnect(const MessageOut &msg)
{
- // TODO: Send a disconnect message containing the reason, and somehow only
- // TODO: really disconnect the client after waiting for the client to get
- // TODO: the message (or likely got it).
+ if (isConnected())
+ {
+ /* ChannelID 0xFF is the channel used by enet_peer_disconnect.
+ * If a reliable packet is send over this channel ENet guaranties
+ * that the message is recieved before the disconnect request.
+ */
+ send(msg, ENET_PACKET_FLAG_RELIABLE, 0xFF);
- // ENet should generate a disconnect event (notifying the connection
- // handler)
- enet_peer_disconnect(mPeer, 0);
+ /* ENet generates a disconnect event
+ * (notifying the connection handler).
+ */
+ enet_peer_disconnect(mPeer, 0);
+ }
}
-void NetComputer::send(const MessageOut &msg)
+void
+NetComputer::send(const MessageOut &msg, bool reliable,
+ unsigned int channel)
{
- LOG_INFO("Sending packet of length " << msg.getDataSize() << " to "
- << ip4ToString(mPeer->address.host), 2);
+ LOG_INFO("Sending packet of length " << msg.getLength() << " to "
+ << ip4ToString(mPeer->address.host), 2);
- // Create a reliable packet.
- ENetPacket *packet = enet_packet_create(msg.getData(), msg.getDataSize(),
- ENET_PACKET_FLAG_RELIABLE);
+ ENetPacket *packet;
+ packet = enet_packet_create(msg.getData(),
+ msg.getLength(),
+ reliable ? ENET_PACKET_FLAG_RELIABLE : 0);
- // Send the packet to the peer over channel id 0.
- enet_peer_send(mPeer, 0, packet);
+ if (packet)
+ {
+ enet_peer_send(mPeer, channel, packet);
+ }
+ else
+ {
+ LOG_WARN("Failure to create packet!", 0);
+ }
}