Skip to content

Instantly share code, notes, and snippets.

@carlbennett
Created November 17, 2011 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlbennett/1372138 to your computer and use it in GitHub Desktop.
Save carlbennett/1372138 to your computer and use it in GitHub Desktop.
diff --git a/src/app.cpp b/src/app.cpp
index a6adf14..1c82aa3 100644
--- a/src/app.cpp
+++ b/src/app.cpp
@@ -38,6 +38,7 @@
#include <mineserver/network/protocol/notch/protocol.h>
#include <mineserver/network/server.h>
+#include <mineserver/watcher/keepalive.h>
#include <mineserver/watcher/login.h>
#include <mineserver/watcher/handshake.h>
#include <mineserver/watcher/serverlistping.h>
@@ -50,6 +51,7 @@ int main()
Mineserver::Network_Protocol::pointer_t protocol = boost::make_shared<Mineserver::Network_Protocol_Notch_Protocol>();
Mineserver::Network_Server::pointer_t server = boost::make_shared<Mineserver::Network_Server>(game, protocol, &service);
+ game->addMessageWatcher(0x00, Mineserver::Watcher_KeepAlive());
game->addMessageWatcher(0x01, Mineserver::Watcher_Login());
game->addMessageWatcher(0x02, Mineserver::Watcher_Handshake());
game->addMessageWatcher(0xFE, Mineserver::Watcher_ServerListPing());
diff --git a/src/mineserver/game.cpp b/src/mineserver/game.cpp
index a004649..02e7f65 100644
--- a/src/mineserver/game.cpp
+++ b/src/mineserver/game.cpp
@@ -49,6 +49,15 @@ void Mineserver::Game::run()
continue;
}
+ // 1200 in-game ticks = timed out, inactive ticks = ticks past since last keep-alive:
+// if (client->inactiveTicks() > 1200) {
+// std::cout << "Client timed-out." << std::endl;
+// client->timedOut();
+// if (!client || client->alive() = false) {
+// continue;
+// }
+// }
+
std::cout << "There are " << client->incoming().size() << " messages." << std::endl;
for (std::vector<Mineserver::Network_Message::pointer_t>::iterator message_it=client->incoming().begin();message_it!=client->incoming().end();++message_it) {
@@ -60,6 +69,10 @@ void Mineserver::Game::run()
client->incoming().clear();
+ // +1 in-game tick, and anything else:
+ // possibly send keep-alive?
+// client->run();
+
client->write();
}
diff --git a/src/mineserver/network/client.cpp b/src/mineserver/network/client.cpp
index 01eb5a7..e684f24 100644
--- a/src/mineserver/network/client.cpp
+++ b/src/mineserver/network/client.cpp
@@ -37,6 +37,27 @@
#include <mineserver/byteorder.h>
#include <mineserver/network/client.h>
+#include <mineserver/network/message/kick.h>
+
+void Mineserver::Network_Client::run()
+{
+ m_inactiveTicks++;
+}
+
+void Mineserver::Network_Client::resetInactiveTicks()
+{
+ m_inactiveTicks = 0;
+}
+
+void Mineserver::Network_Client::timedOut()
+{
+ boost::shared_ptr<Mineserver::Network_Message_Kick> responseMessage(new Mineserver::Network_Message_Kick);
+ responseMessage->mid = 0xFF;
+ responseMessage->reason = "Timed-out";
+ outgoing().push_back(responseMessage);
+ stop();
+ // Jailout2000: Does the kick message get sent before closing the socket? I guess it doesn't *really* matter...
+}
void Mineserver::Network_Client::start()
{
diff --git a/src/mineserver/network/client.h b/src/mineserver/network/client.h
index 43e5939..6b7d89d 100644
--- a/src/mineserver/network/client.h
+++ b/src/mineserver/network/client.h
@@ -56,10 +56,12 @@ namespace Mineserver
std::vector<Mineserver::Network_Message::pointer_t> m_incoming;
std::vector<Mineserver::Network_Message::pointer_t> m_outgoing;
bool m_alive;
+ int m_inactiveTicks;
public:
Network_Client(boost::asio::io_service* service, Mineserver::Network_Protocol::pointer_t protocol) : m_socket(*service),m_protocol(protocol),m_alive(true)
{
+ m_inactiveTicks = 0;
}
boost::asio::ip::tcp::socket& socket()
@@ -77,12 +79,20 @@ namespace Mineserver
return m_alive;
}
+ int inactiveTicks()
+ {
+ return m_inactiveTicks;
+ }
+
std::vector<Mineserver::Network_Message::pointer_t>& incoming() { return m_incoming; }
std::vector<Mineserver::Network_Message::pointer_t>& outgoing() { return m_outgoing; }
+ void read();
+ void resetInactiveTicks();
+ void run();
void start();
void stop();
- void read();
+ void timedOut();
void write();
private:
diff --git a/src/mineserver/network/message/0x00.h b/src/mineserver/network/message/0x00.h
index 0bbc099..34a362c 100644
--- a/src/mineserver/network/message/0x00.h
+++ b/src/mineserver/network/message/0x00.h
@@ -35,7 +35,7 @@ namespace Mineserver
{
struct Network_Message_0x00 : public Mineserver::Network_Message
{
- int32_t id;
+ int32_t keepalive_id;
};
}
diff --git a/src/mineserver/network/message/keepalive.h b/src/mineserver/network/message/keepalive.h
new file mode 100644
index 0000000..5f7dabb
--- /dev/null
+++ b/src/mineserver/network/message/keepalive.h
@@ -0,0 +1,44 @@
+/*
+ Copyright (c) 2011, The Mineserver Project
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the The Mineserver Project nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef MINESERVER_NETWORK_PACKET_KEEPALIVE_H
+#define MINESERVER_NETWORK_PACKET_KEEPALIVE_H
+
+#include <string>
+
+#include <mineserver/byteorder.h>
+#include <mineserver/network/message.h>
+
+namespace Mineserver
+{
+ struct Network_Message_KeepAlive : public Mineserver::Network_Message
+ {
+ int32_t keepalive_id;
+ };
+}
+
+#endif
diff --git a/src/mineserver/watcher/keepalive.h b/src/mineserver/watcher/keepalive.h
new file mode 100644
index 0000000..6c167fc
--- /dev/null
+++ b/src/mineserver/watcher/keepalive.h
@@ -0,0 +1,51 @@
+/*
+ Copyright (c) 2011, The Mineserver Project
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the The Mineserver Project nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef MINESERVER_WATCHER_KEEPALIVE_H
+#define MINESERVER_WATCHER_KEEPALIVE_H
+
+#include <boost/shared_ptr.hpp>
+
+#include <mineserver/game.h>
+#include <mineserver/network/client.h>
+#include <mineserver/network/message.h>
+#include <mineserver/network/message/keepalive.h>
+
+namespace Mineserver
+{
+ struct Watcher_KeepAlive
+ {
+ void operator()(Mineserver::Game::pointer_t game, Mineserver::Network_Client::pointer_t client, Mineserver::Network_Message::pointer_t message) const
+ {
+ std::cout << "Keep-alive watcher called!" << std::endl;
+
+ client->resetInactiveTicks();
+ }
+ };
+}
+
+#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment