mirror of
https://github.com/luanti-org/luanti.git
synced 2025-07-02 16:38:41 +00:00
Implement mod communication channels (#6351)
Implement network communication for channels * Implement ModChannel manager server side to route incoming messages from clients to other clients * Add signal handler switch on client & ModChannelMgr on client to handle channels * Add Lua API bindings + client packet sending + unittests * Implement server message sending * Add callback from received message handler to Lua API using registration method
This commit is contained in:
parent
6df312a608
commit
6f1c907204
37 changed files with 1206 additions and 39 deletions
|
@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "map.h"
|
||||
#include "mapsector.h"
|
||||
#include "minimap.h"
|
||||
#include "modchannels.h"
|
||||
#include "nodedef.h"
|
||||
#include "serialization.h"
|
||||
#include "server.h"
|
||||
|
@ -1330,3 +1331,95 @@ void Client::handleCommand_CSMFlavourLimits(NetworkPacket *pkt)
|
|||
{
|
||||
*pkt >> m_csm_flavour_limits >> m_csm_noderange_limit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Mod channels
|
||||
*/
|
||||
|
||||
void Client::handleCommand_ModChannelMsg(NetworkPacket *pkt)
|
||||
{
|
||||
std::string channel_name, sender, channel_msg;
|
||||
*pkt >> channel_name >> sender >> channel_msg;
|
||||
|
||||
verbosestream << "Mod channel message received from server " << pkt->getPeerId()
|
||||
<< " on channel " << channel_name << ". sender: `" << sender << "`, message: "
|
||||
<< channel_msg << std::endl;
|
||||
|
||||
if (!m_modchannel_mgr->channelRegistered(channel_name)) {
|
||||
verbosestream << "Server sent us messages on unregistered channel "
|
||||
<< channel_name << ", ignoring." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
m_script->on_modchannel_message(channel_name, sender, channel_msg);
|
||||
}
|
||||
|
||||
void Client::handleCommand_ModChannelSignal(NetworkPacket *pkt)
|
||||
{
|
||||
u8 signal_tmp;
|
||||
ModChannelSignal signal;
|
||||
std::string channel;
|
||||
|
||||
*pkt >> signal_tmp >> channel;
|
||||
|
||||
signal = (ModChannelSignal)signal_tmp;
|
||||
|
||||
bool valid_signal = true;
|
||||
// @TODO: send Signal to Lua API
|
||||
switch (signal) {
|
||||
case MODCHANNEL_SIGNAL_JOIN_OK:
|
||||
m_modchannel_mgr->setChannelState(channel, MODCHANNEL_STATE_READ_WRITE);
|
||||
infostream << "Server ack our mod channel join on channel `" << channel
|
||||
<< "`, joining." << std::endl;
|
||||
break;
|
||||
case MODCHANNEL_SIGNAL_JOIN_FAILURE:
|
||||
// Unable to join, remove channel
|
||||
m_modchannel_mgr->leaveChannel(channel, 0);
|
||||
infostream << "Server refused our mod channel join on channel `" << channel
|
||||
<< "`" << std::endl;
|
||||
break;
|
||||
case MODCHANNEL_SIGNAL_LEAVE_OK:
|
||||
#ifndef NDEBUG
|
||||
infostream << "Server ack our mod channel leave on channel " << channel
|
||||
<< "`, leaving." << std::endl;
|
||||
#endif
|
||||
break;
|
||||
case MODCHANNEL_SIGNAL_LEAVE_FAILURE:
|
||||
infostream << "Server refused our mod channel leave on channel `" << channel
|
||||
<< "`" << std::endl;
|
||||
break;
|
||||
case MODCHANNEL_SIGNAL_CHANNEL_NOT_REGISTERED:
|
||||
#ifndef NDEBUG
|
||||
// Generally unused, but ensure we don't do an implementation error
|
||||
infostream << "Server tells us we sent a message on channel `" << channel
|
||||
<< "` but we are not registered. Message was dropped." << std::endl;
|
||||
#endif
|
||||
break;
|
||||
case MODCHANNEL_SIGNAL_SET_STATE: {
|
||||
u8 state;
|
||||
*pkt >> state;
|
||||
|
||||
if (state == MODCHANNEL_STATE_INIT || state >= MODCHANNEL_STATE_MAX) {
|
||||
infostream << "Received wrong channel state " << state
|
||||
<< ", ignoring." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
m_modchannel_mgr->setChannelState(channel, (ModChannelState) state);
|
||||
infostream << "Server sets mod channel `" << channel
|
||||
<< "` in read-only mode." << std::endl;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
#ifndef NDEBUG
|
||||
warningstream << "Received unhandled mod channel signal ID "
|
||||
<< signal << ", ignoring." << std::endl;
|
||||
#endif
|
||||
valid_signal = false;
|
||||
break;
|
||||
}
|
||||
|
||||
// If signal is valid, forward it to client side mods
|
||||
if (valid_signal)
|
||||
m_script->on_modchannel_signal(channel, signal);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue