summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authortruebrain <truebrain@openttd.org>2011-12-19 21:00:32 +0000
committertruebrain <truebrain@openttd.org>2011-12-19 21:00:32 +0000
commit3a535690d4e9aaa896a062e7b5b5454b1b07ac47 (patch)
tree600859d25db8391542b9cc0da9e063c7bdc6f4fc /src/network
parent77b7366c2947a3f2545d5542021be1cc203a74e8 (diff)
downloadopenttd-3a535690d4e9aaa896a062e7b5b5454b1b07ac47.tar.xz
(svn r23623) -Add: allow bi-directional communication with the AdminPort and GameScript
Diffstat (limited to 'src/network')
-rw-r--r--src/network/core/config.h1
-rw-r--r--src/network/core/tcp_admin.cpp2
-rw-r--r--src/network/core/tcp_admin.h11
-rw-r--r--src/network/network_admin.cpp49
-rw-r--r--src/network/network_admin.h3
5 files changed, 66 insertions, 0 deletions
diff --git a/src/network/core/config.h b/src/network/core/config.h
index 98d5711e6..cbe4f5d74 100644
--- a/src/network/core/config.h
+++ b/src/network/core/config.h
@@ -48,6 +48,7 @@ static const uint NETWORK_PASSWORD_LENGTH = 33; ///< The maximum lengt
static const uint NETWORK_CLIENTS_LENGTH = 200; ///< The maximum length for the list of clients that controls a company, in bytes including '\0'
static const uint NETWORK_CLIENT_NAME_LENGTH = 25; ///< The maximum length of a client's name, in bytes including '\0'
static const uint NETWORK_RCONCOMMAND_LENGTH = 500; ///< The maximum length of a rconsole command, in bytes including '\0'
+static const uint NETWORK_GAMESCRIPT_JSON_LENGTH = 1450; ///< The maximum length of a gamescript json string, in bytes including '\0'
static const uint NETWORK_CHAT_LENGTH = 900; ///< The maximum length of a chat message, in bytes including '\0'
static const uint NETWORK_GRF_NAME_LENGTH = 80; ///< Maximum length of the name of a GRF
diff --git a/src/network/core/tcp_admin.cpp b/src/network/core/tcp_admin.cpp
index fbf9ac580..027159883 100644
--- a/src/network/core/tcp_admin.cpp
+++ b/src/network/core/tcp_admin.cpp
@@ -60,6 +60,7 @@ NetworkRecvStatus NetworkAdminSocketHandler::HandlePacket(Packet *p)
case ADMIN_PACKET_ADMIN_POLL: return this->Receive_ADMIN_POLL(p);
case ADMIN_PACKET_ADMIN_CHAT: return this->Receive_ADMIN_CHAT(p);
case ADMIN_PACKET_ADMIN_RCON: return this->Receive_ADMIN_RCON(p);
+ case ADMIN_PACKET_ADMIN_GAMESCRIPT: return this->Receive_ADMIN_GAMESCRIPT(p);
case ADMIN_PACKET_SERVER_FULL: return this->Receive_SERVER_FULL(p);
case ADMIN_PACKET_SERVER_BANNED: return this->Receive_SERVER_BANNED(p);
@@ -134,6 +135,7 @@ NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_UPDATE_FREQUENCY(Pack
NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_POLL(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_POLL); }
NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_CHAT(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_CHAT); }
NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_RCON(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_RCON); }
+NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_GAMESCRIPT(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_GAMESCRIPT); }
NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_FULL(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_FULL); }
NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_BANNED(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_BANNED); }
diff --git a/src/network/core/tcp_admin.h b/src/network/core/tcp_admin.h
index df6e55397..f950f1a97 100644
--- a/src/network/core/tcp_admin.h
+++ b/src/network/core/tcp_admin.h
@@ -32,6 +32,7 @@ enum PacketAdminType {
ADMIN_PACKET_ADMIN_POLL, ///< The admin explicitly polls for a piece of information.
ADMIN_PACKET_ADMIN_CHAT, ///< The admin sends a chat message to be distributed.
ADMIN_PACKET_ADMIN_RCON, ///< The admin sends a remote console command.
+ ADMIN_PACKET_ADMIN_GAMESCRIPT, ///< The admin sends a JSON string for the GameScript.
ADMIN_PACKET_SERVER_FULL = 100, ///< The server tells the admin it cannot accept the admin.
ADMIN_PACKET_SERVER_BANNED, ///< The server tells the admin it is banned.
@@ -58,6 +59,7 @@ enum PacketAdminType {
ADMIN_PACKET_SERVER_CONSOLE, ///< The server gives the admin the data that got printed to its console.
ADMIN_PACKET_SERVER_CMD_NAMES, ///< The server sends out the names of the DoCommands to the admins.
ADMIN_PACKET_SERVER_CMD_LOGGING, ///< The server gives the admin copies of incoming command packets.
+ ADMIN_PACKET_SERVER_GAMESCRIPT, ///< The server gives the admin information from the GameScript in JSON.
INVALID_ADMIN_PACKET = 0xFF, ///< An invalid marker for admin packets.
};
@@ -80,6 +82,7 @@ enum AdminUpdateType {
ADMIN_UPDATE_CONSOLE, ///< The admin would like to have console messages.
ADMIN_UPDATE_CMD_NAMES, ///< The admin would like a list of all DoCommand names.
ADMIN_UPDATE_CMD_LOGGING, ///< The admin would like to have DoCommand information.
+ ADMIN_UPDATE_GAMESCRIPT, ///< The admin would like to have gamescript messages.
ADMIN_UPDATE_END, ///< Must ALWAYS be on the end of this list!! (period)
};
@@ -170,6 +173,14 @@ protected:
virtual NetworkRecvStatus Receive_ADMIN_RCON(Packet *p);
/**
+ * Send a JSON string to the current active GameScript.
+ * json JSON string for the GameScript.
+ * @param p The packet that was just received.
+ * @return The state the network should have.
+ */
+ virtual NetworkRecvStatus Receive_ADMIN_GAMESCRIPT(Packet *p);
+
+ /**
* The server is full (connection gets closed).
* @param p The packet that was just received.
* @return The state the network should have.
diff --git a/src/network/network_admin.cpp b/src/network/network_admin.cpp
index ecf94ab0d..592e26b3a 100644
--- a/src/network/network_admin.cpp
+++ b/src/network/network_admin.cpp
@@ -23,6 +23,7 @@
#include "../core/pool_func.hpp"
#include "../map_func.h"
#include "../rev.h"
+#include "../game/game.hpp"
/* This file handles all the admin network commands. */
@@ -52,6 +53,7 @@ static const AdminUpdateFrequency _admin_update_type_frequencies[] = {
ADMIN_FREQUENCY_AUTOMATIC, ///< ADMIN_UPDATE_CONSOLE
ADMIN_FREQUENCY_POLL, ///< ADMIN_UPDATE_CMD_NAMES
ADMIN_FREQUENCY_AUTOMATIC, ///< ADMIN_UPDATE_CMD_LOGGING
+ ADMIN_FREQUENCY_AUTOMATIC, ///< ADMIN_UPDATE_GAMESCRIPT
};
/** Sanity check. */
assert_compile(lengthof(_admin_update_type_frequencies) == ADMIN_UPDATE_END);
@@ -510,6 +512,20 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_RCON(Packet *p)
return NETWORK_RECV_STATUS_OKAY;
}
+NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_GAMESCRIPT(Packet *p)
+{
+ if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
+
+ char json[NETWORK_GAMESCRIPT_JSON_LENGTH];
+
+ p->Recv_string(json, sizeof(json));
+
+ DEBUG(net, 2, "[admin] GameScript JSON from '%s' (%s): '%s'", this->admin_name, this->admin_version, json);
+
+ Game::NewEvent(new ScriptEventAdminPort(json));
+ return NETWORK_RECV_STATUS_OKAY;
+}
+
/**
* Send console output of other clients.
* @param origin The origin of the string.
@@ -532,6 +548,25 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendConsole(const char *origi
return NETWORK_RECV_STATUS_OKAY;
}
+/**
+ * Send GameScript JSON output.
+ * @param json The JSON string.
+ */
+NetworkRecvStatus ServerNetworkAdminSocketHandler::SendGameScript(const char *json)
+{
+ /* At the moment we cannot transmit anything larger than MTU. So the string
+ * has to be no longer than the length of the json + '\0' + 3 bytes of the
+ * packet header. */
+ if (strlen(json) + 1 + 3 >= SEND_MTU) return NETWORK_RECV_STATUS_OKAY;
+
+ Packet *p = new Packet(ADMIN_PACKET_SERVER_GAMESCRIPT);
+
+ p->Send_string(json);
+ this->SendPacket(p);
+
+ return NETWORK_RECV_STATUS_OKAY;
+}
+
/** Send the names of the commands. */
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCmdNames()
{
@@ -896,6 +931,20 @@ void NetworkAdminConsole(const char *origin, const char *string)
}
/**
+ * Send GameScript JSON to the admin network (if they did opt in for the respective update).
+ * @param json The JSON data as received from the GameScript.
+ */
+void NetworkAdminGameScript(const char *json)
+{
+ ServerNetworkAdminSocketHandler *as;
+ FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
+ if (as->update_frequency[ADMIN_UPDATE_GAMESCRIPT] & ADMIN_FREQUENCY_AUTOMATIC) {
+ as->SendGameScript(json);
+ }
+ }
+}
+
+/**
* Distribute CommandPacket details over the admin network for logging purposes.
* @param owner The owner of the CommandPacket (who sent us the CommandPacket).
* @param cp The CommandPacket to be distributed.
diff --git a/src/network/network_admin.h b/src/network/network_admin.h
index 5a3afc084..c8241cabf 100644
--- a/src/network/network_admin.h
+++ b/src/network/network_admin.h
@@ -34,6 +34,7 @@ protected:
virtual NetworkRecvStatus Receive_ADMIN_POLL(Packet *p);
virtual NetworkRecvStatus Receive_ADMIN_CHAT(Packet *p);
virtual NetworkRecvStatus Receive_ADMIN_RCON(Packet *p);
+ virtual NetworkRecvStatus Receive_ADMIN_GAMESCRIPT(Packet *p);
NetworkRecvStatus SendProtocol();
public:
@@ -65,6 +66,7 @@ public:
NetworkRecvStatus SendChat(NetworkAction action, DestType desttype, ClientID client_id, const char *msg, int64 data);
NetworkRecvStatus SendRcon(uint16 colour, const char *command);
NetworkRecvStatus SendConsole(const char *origin, const char *command);
+ NetworkRecvStatus SendGameScript(const char *json);
NetworkRecvStatus SendCmdNames();
NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket *cp);
@@ -116,6 +118,7 @@ void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_i
void NetworkAdminUpdate(AdminUpdateFrequency freq);
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const char *string);
void NetworkAdminConsole(const char *origin, const char *string);
+void NetworkAdminGameScript(const char *json);
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp);
#endif /* ENABLE_NETWORK */