summaryrefslogtreecommitdiffstats
path: root/src/ChannelManager.h
blob: 874588d2993ef897d7c1db554edbed15d3114fa4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once

#include <unordered_map>
#include <set>

#include "StringUtils.h"


class cClientHandle;
class cCriticalSection;
class cPluginLua;
class cWorld;
class cChannelCallback;
class cByteBuffer;

// tolua_begin
class cChannelManager
{
// tolua_end
public:
	typedef SharedPtr<cChannelCallback> cChannelCallbackPtr;
	typedef std::unordered_map<AString, cChannelCallbackPtr> CallbackMap;

	/** Adds a client to a specific channel. */
	void AddClientToChannel      (cClientHandle & a_Client, const AString & a_Channel);  // tolua_export

	/** Adds a client to multiple channels at once. */
	void AddClientToChannels     (cClientHandle & a_Client, const AString & a_Channels);

	/** Removes a client from a specific channel. */
	void RemoveClientFromChannel (cClientHandle & a_Client, const AString & a_Channel);  // tolua_export

	/** Removes a client from multiple channels at once. */
	void RemoveClientFromChannels(cClientHandle & a_Client, const AString & a_Channels);

	/** Returns true if the client is registered for the specified channel. */
	bool ClientHasChannel        (cClientHandle & a_Client, const AString & a_Channel);

	/** Broadcasts a channel message to all clients.
	If a_World is provided, broadcast is limited to that world. */
	void BroadcastChannelMessage (const AString & a_Channel, cByteBuffer & a_Data, cWorld * a_World = nullptr);  // tolua_export

	/** Registers a handler for a specific channel.
	Returns true if registration was successful. */
	bool RegisterChannel         (const AString & a_Channel, cChannelCallbackPtr a_Callback);  // Exported in manual bindings

	/** Removes the handler for the specified channel.
	Returns true if the handler was removed. */
	bool RemoveChannel           (const AString & a_Channel);  // tolua_export


	void HandleChannelMessage    (cClientHandle & a_Client, const AString & a_Channel, cByteBuffer & a_Data);


	void HandlePluginUnloading   (const cPluginLua * a_Plugin);

private:

	AStringVector BreakApartChannels(const AString & a_Channels);

	/** This protects m_ChannelCallbacks */
	cCriticalSection m_CSCallbacks;

	CallbackMap m_ChannelCallbacks;

};  // tolua_export