diff options
author | truebrain <truebrain@openttd.org> | 2011-12-19 21:00:32 +0000 |
---|---|---|
committer | truebrain <truebrain@openttd.org> | 2011-12-19 21:00:32 +0000 |
commit | 3a535690d4e9aaa896a062e7b5b5454b1b07ac47 (patch) | |
tree | 600859d25db8391542b9cc0da9e063c7bdc6f4fc /src/network/network_admin.cpp | |
parent | 77b7366c2947a3f2545d5542021be1cc203a74e8 (diff) | |
download | openttd-3a535690d4e9aaa896a062e7b5b5454b1b07ac47.tar.xz |
(svn r23623) -Add: allow bi-directional communication with the AdminPort and GameScript
Diffstat (limited to 'src/network/network_admin.cpp')
-rw-r--r-- | src/network/network_admin.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
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. |