ISource2ModNetworkMessages

Interface for manipulating and sending Source2 network (protobuf) messages.

Provides functionality for:

  • Allocating and deallocating network messages by ID or partial name
  • Reading and writing protobuf fields via reflection (scalar, repeated, nested)
  • Sending messages to individual players or a set of players
  • Hooking outgoing server/client network messages

Interface for working with Source2 network messages.

All field access is done via protobuf reflection using field names. The @p msg pointer is an opaque handle to an allocated protobuf message; always deallocate with DeallocateNetMessage() when done.

Interface id ISource2ModNetworkMessages002 (SOURCE2MOD_NETWORKMESSAGES_INTERFACE) · Global g_pSource2ModNetworkMessages · Header source2mod/ISource2ModNetworkMessages.h

Types

Source2ModNetMessageHookId

using Source2ModNetMessageHookId = Source2ModHookId;

A net message hook (HookServerMessage / HookClientMessage / HookServerInternalMessage).

NetMessageServerHook

using NetMessageServerHook = Source2ModCallback<Action(uint64_t* clients, int messageid, void* msg)>;

Hook called when the server sends a message to a set of clients.

NetMessageClientHook

using NetMessageClientHook = Source2ModCallback<Action(CPlayerSlot slot, int messageid, void* msg)>;

Hook called when a client sends a message to the server, or for internal server sends.

Methods

AllocateNetMessageByID

virtual void* AllocateNetMessageByID(int msgid) = 0;

Allocates a network message by its numeric ID.

ParameterTypeDescription
msgidintMessage ID.

Returns void* — Opaque message pointer, or nullptr on failure.

AllocateNetMessageByPartialName

virtual void* AllocateNetMessageByPartialName(const char* name) = 0;

Allocates a network message by a partial name match.

ParameterTypeDescription
nameconst char*Partial name string (e.g. "CSVCMsg_UserMessage").

Returns void* — Opaque message pointer, or nullptr on failure.

DeallocateNetMessage

virtual void DeallocateNetMessage(void* msg) = 0;

Deallocates a previously allocated network message.

ParameterTypeDescription
msgvoid*Message pointer returned by Allocate*.

HasField

virtual bool HasField(void* msg, const char* fieldName) = 0;

Returns true if a singular field is set on the message.

ParameterType
msgvoid*
fieldNameconst char*

GetInt32

virtual int GetInt32(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedInt32

virtual int GetRepeatedInt32(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetInt32

virtual void SetInt32(void* msg, const char* fieldName, int value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueint

SetRepeatedInt32

virtual void SetRepeatedInt32(void* msg, const char* fieldName, int index, int value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valueint

AddInt32

virtual void AddInt32(void* msg, const char* fieldName, int value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueint

GetInt64

virtual int64_t GetInt64(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedInt64

virtual int64_t GetRepeatedInt64(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetInt64

virtual void SetInt64(void* msg, const char* fieldName, int64_t value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueint64_t

SetRepeatedInt64

virtual void SetRepeatedInt64(void* msg, const char* fieldName, int index, int64_t value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valueint64_t

AddInt64

virtual void AddInt64(void* msg, const char* fieldName, int64_t value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueint64_t

GetUInt32

virtual uint32_t GetUInt32(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedUInt32

virtual uint32_t GetRepeatedUInt32(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetUInt32

virtual void SetUInt32(void* msg, const char* fieldName, uint32_t value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueuint32_t

SetRepeatedUInt32

virtual void SetRepeatedUInt32(void* msg, const char* fieldName, int index, uint32_t value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valueuint32_t

AddUInt32

virtual void AddUInt32(void* msg, const char* fieldName, uint32_t value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueuint32_t

GetUInt64

virtual uint64_t GetUInt64(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedUInt64

virtual uint64_t GetRepeatedUInt64(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetUInt64

virtual void SetUInt64(void* msg, const char* fieldName, uint64_t value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueuint64_t

SetRepeatedUInt64

virtual void SetRepeatedUInt64(void* msg, const char* fieldName, int index, uint64_t value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valueuint64_t

AddUInt64

virtual void AddUInt64(void* msg, const char* fieldName, uint64_t value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueuint64_t

GetBool

virtual bool GetBool(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedBool

virtual bool GetRepeatedBool(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetBool

virtual void SetBool(void* msg, const char* fieldName, bool value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valuebool

SetRepeatedBool

virtual void SetRepeatedBool(void* msg, const char* fieldName, int index, bool value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valuebool

AddBool

virtual void AddBool(void* msg, const char* fieldName, bool value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valuebool

GetFloat

virtual float GetFloat(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedFloat

virtual float GetRepeatedFloat(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetFloat

virtual void SetFloat(void* msg, const char* fieldName, float value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valuefloat

SetRepeatedFloat

virtual void SetRepeatedFloat(void* msg, const char* fieldName, int index, float value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valuefloat

AddFloat

virtual void AddFloat(void* msg, const char* fieldName, float value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valuefloat

GetDouble

virtual double GetDouble(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedDouble

virtual double GetRepeatedDouble(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetDouble

virtual void SetDouble(void* msg, const char* fieldName, double value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valuedouble

SetRepeatedDouble

virtual void SetRepeatedDouble(void* msg, const char* fieldName, int index, double value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valuedouble

AddDouble

virtual void AddDouble(void* msg, const char* fieldName, double value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valuedouble

GetString

virtual std::string GetString(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedString

virtual std::string GetRepeatedString(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetString

virtual void SetString(void* msg, const char* fieldName, const char* value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueconst char*

SetRepeatedString

virtual void SetRepeatedString(void* msg, const char* fieldName, int index, const char* value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valueconst char*

AddString

virtual void AddString(void* msg, const char* fieldName, const char* value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueconst char*

GetVector2D

virtual Vector2D GetVector2D(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedVector2D

virtual Vector2D GetRepeatedVector2D(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetVector2D

virtual void SetVector2D(void* msg, const char* fieldName, Vector2D value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueVector2D

SetRepeatedVector2D

virtual void SetRepeatedVector2D(void* msg, const char* fieldName, int index, Vector2D value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valueVector2D

AddVector2D

virtual void AddVector2D(void* msg, const char* fieldName, Vector2D value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueVector2D

GetVector

virtual Vector GetVector(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedVector

virtual Vector GetRepeatedVector(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetVector

virtual void SetVector(void* msg, const char* fieldName, Vector value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueVector

SetRepeatedVector

virtual void SetRepeatedVector(void* msg, const char* fieldName, int index, Vector value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valueVector

AddVector

virtual void AddVector(void* msg, const char* fieldName, Vector value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueVector

GetColor

virtual Color GetColor(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedColor

virtual Color GetRepeatedColor(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetColor

virtual void SetColor(void* msg, const char* fieldName, Color value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueColor

SetRepeatedColor

virtual void SetRepeatedColor(void* msg, const char* fieldName, int index, Color value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valueColor

AddColor

virtual void AddColor(void* msg, const char* fieldName, Color value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueColor

GetQAngle

virtual QAngle GetQAngle(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedQAngle

virtual QAngle GetRepeatedQAngle(void* msg, const char* fieldName, int index) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint

SetQAngle

virtual void SetQAngle(void* msg, const char* fieldName, QAngle value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueQAngle

SetRepeatedQAngle

virtual void SetRepeatedQAngle(void* msg, const char* fieldName, int index, QAngle value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valueQAngle

AddQAngle

virtual void AddQAngle(void* msg, const char* fieldName, QAngle value) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueQAngle

GetBytes

virtual int GetBytes(uint8_t* out, void* msg, const char* fieldName) = 0;

Copies a bytes field into @p out.

Pass @p out as nullptr to query the size without copying.

ParameterType
outuint8_t*
msgvoid*
fieldNameconst char*

Returns int — Number of bytes in the field.

GetRepeatedBytes

virtual int GetRepeatedBytes(uint8_t* out, void* msg, const char* fieldName, int index) = 0;
ParameterType
outuint8_t*
msgvoid*
fieldNameconst char*
indexint

SetBytes

virtual void SetBytes(void* msg, const char* fieldName, const char* value, int valueLength) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueconst char*
valueLengthint

SetRepeatedBytes

virtual void SetRepeatedBytes(void* msg, const char* fieldName, int index, const char* value, int valueLength) = 0;
ParameterType
msgvoid*
fieldNameconst char*
indexint
valueconst char*
valueLengthint

AddBytes

virtual void AddBytes(void* msg, const char* fieldName, const char* value, int valueLength) = 0;
ParameterType
msgvoid*
fieldNameconst char*
valueconst char*
valueLengthint

GetNestedMessage

virtual void* GetNestedMessage(void* msg, const char* fieldName) = 0;

Returns a mutable pointer to a singular nested message field.

ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedNestedMessage

virtual void* GetRepeatedNestedMessage(void* msg, const char* fieldName, int index) = 0;

Returns a mutable pointer to a repeated nested message element.

ParameterType
msgvoid*
fieldNameconst char*
indexint

AddNestedMessage

virtual void* AddNestedMessage(void* msg, const char* fieldName) = 0;

Appends a new element to a repeated nested message field and returns it.

ParameterType
msgvoid*
fieldNameconst char*

GetRepeatedFieldSize

virtual int GetRepeatedFieldSize(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

ClearRepeatedField

virtual void ClearRepeatedField(void* msg, const char* fieldName) = 0;
ParameterType
msgvoid*
fieldNameconst char*

Clear

virtual void Clear(void* msg) = 0;

Clears all fields on the message.

ParameterType
msgvoid*

SendMessage

virtual void SendMessage(void* msg, int msgid, CPlayerSlot slot) = 0;

Sends a network message to a single player.

ParameterTypeDescription
msgvoid*Allocated message to send (not freed by this call).
msgidintMessage ID matching the allocation.
slotCPlayerSlot

SendMessageToPlayers

virtual void SendMessageToPlayers(void* msg, int msgid, uint64_t playermask) = 0;

Sends a network message to a set of players.

ParameterTypeDescription
msgvoid*Allocated message to send (not freed by this call).
msgidintMessage ID matching the allocation.
playermaskuint64_t64-bit bitmask; bit N = slot N.

HookServerMessage

virtual Source2ModNetMessageHookId HookServerMessage(NetMessageServerHook handler) = 0;

Hooks outgoing server-to-client messages.

A plugin may hook as often as it likes; each handler runs. The source2mod drops what a plugin still holds when it unloads, so a plugin that forgets to unhook does not leave the engine calling into a closed library. Whose a hook is, the core reads off the handler (see Source2ModCallback).

ParameterTypeDescription
handlerNetMessageServerHookA function, an object and a method, or a lambda

Returns Source2ModNetMessageHookId — The id UnhookServerMessage(id) takes

UnhookServerMessage

virtual bool UnhookServerMessage(const NetMessageServerHook& handler) = 0;
virtual bool UnhookServerMessage(Source2ModNetMessageHookId id) = 0;

Drops the hook with this handler -- a function or an object and a method; a lambda goes by its id.

Drops the hook HookServerMessage() returned the id for.

Returns bool — true when one was found

HookClientMessage

virtual Source2ModNetMessageHookId HookClientMessage(NetMessageClientHook handler) = 0;

Hooks incoming client-to-server messages (as HookServerMessage()).

ParameterType
handlerNetMessageClientHook

Returns Source2ModNetMessageHookId — The id UnhookClientMessage(id) takes

UnhookClientMessage

virtual bool UnhookClientMessage(const NetMessageClientHook& handler) = 0;
virtual bool UnhookClientMessage(Source2ModNetMessageHookId id) = 0;

Drops the hook with this handler -- a function or an object and a method; a lambda goes by its id.

Drops the hook HookClientMessage() returned the id for.

Returns bool — true when one was found

HookServerInternalMessage

virtual Source2ModNetMessageHookId HookServerInternalMessage(NetMessageClientHook handler) = 0;

Hooks internal server-side message sends, per client (as HookServerMessage()).

ParameterType
handlerNetMessageClientHook

Returns Source2ModNetMessageHookId — The id UnhookServerInternalMessage(id) takes

UnhookServerInternalMessage

virtual bool UnhookServerInternalMessage(const NetMessageClientHook& handler) = 0;
virtual bool UnhookServerInternalMessage(Source2ModNetMessageHookId id) = 0;

Drops the hook with this handler -- a function or an object and a method; a lambda goes by its id.

Drops the hook HookServerInternalMessage() returned the id for.

Returns bool — true when one was found

Macros

NET_MSG_ALLOC_BY_ID

#define NET_MSG_ALLOC_BY_ID(id) g_pSource2ModNetworkMessages->AllocateNetMessageByID(id)

Shorthand accessors via g_Source2ModAPI.

HOOK_SERVER_MESSAGE

#define HOOK_SERVER_MESSAGE(handler) g_pSource2ModNetworkMessages->HookServerMessage(handler)

Hook macros, matching HOOK_GAME_EVENT and HOOK_CONVAR_CHANGE.

Other macros

MacroExpands to
NET_MSG_ALLOC_BY_NAME(name)g_pSource2ModNetworkMessages->AllocateNetMessageByPartialName(name)
NET_MSG_FREE(msg)g_pSource2ModNetworkMessages->DeallocateNetMessage(msg)
NET_MSG_SEND(msg, id, slot)g_pSource2ModNetworkMessages->SendMessage(msg, id, slot)
NET_MSG_SEND_PLAYERS(msg, id, mask)g_pSource2ModNetworkMessages->SendMessageToPlayers(msg, id, mask)
UNHOOK_SERVER_MESSAGE(handler)g_pSource2ModNetworkMessages->UnhookServerMessage(handler)
HOOK_CLIENT_MESSAGE(handler)g_pSource2ModNetworkMessages->HookClientMessage(handler)
UNHOOK_CLIENT_MESSAGE(handler)g_pSource2ModNetworkMessages->UnhookClientMessage(handler)
HOOK_SERVER_INTERNAL_MESSAGE(handler)g_pSource2ModNetworkMessages->HookServerInternalMessage(handler)
UNHOOK_SERVER_INTERNAL_MESSAGE(handler)g_pSource2ModNetworkMessages->UnhookServerInternalMessage(handler)

On this page