1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2025-09-19 20:16:10 +08:00

Update INetworkMessages & add CNetMessagePB (#258)

Co-authored-by: GAMMACASE <31375974+GAMMACASE@users.noreply.github.com>
This commit is contained in:
Alex
2024-06-12 13:41:47 -04:00
committed by GitHub
parent 4202f1c864
commit 6eeeb874c4
6 changed files with 114 additions and 85 deletions

View File

@ -8,6 +8,7 @@
#include <eiface.h>
#include <networksystem/inetworkserializer.h>
#include <networksystem/netmessage.h>
#include <tier1/utldelegate.h>
#include <tier1/utlsymbol.h>
#include <irecipientfilter.h>
@ -21,26 +22,26 @@ class CUtlSlot;
abstract_class IGameEventSystem : public IAppSystem
{
public:
virtual void RegisterGameEvent( INetworkSerializable *pEvent ) = 0;
virtual void RegisterGameEvent( INetworkMessageInternal *pEvent ) = 0;
virtual void RegisterGameEventHandlerAbstract( CUtlSlot *nSlot, const CUtlAbstractDelegate &delegate, INetworkSerializable *pEvent, int nPriority ) = 0;
virtual void UnregisterGameEventHandlerAbstract( CUtlSlot *nSlot, const CUtlAbstractDelegate &delegate, INetworkSerializable *pEvent ) = 0;
virtual void RegisterGameEventHandlerAbstract( CUtlSlot *nSlot, const CUtlAbstractDelegate &delegate, INetworkMessageInternal *pEvent, int nPriority ) = 0;
virtual void UnregisterGameEventHandlerAbstract( CUtlSlot *nSlot, const CUtlAbstractDelegate &delegate, INetworkMessageInternal *pEvent ) = 0;
// Providing nSize has no effect and is unused.
virtual void PostEventAbstract_Local( CSplitScreenSlot nSlot, INetworkSerializable *pEvent, const void *pData, unsigned long nSize ) = 0;
virtual void PostEventAbstract_Local( CSplitScreenSlot nSlot, INetworkMessageInternal *pEvent, const CNetMessage *pData, unsigned long nSize ) = 0;
// Providing nSlot as -1 would select 0nth slot.
// clients pointer is a masked uint64 value where (client index - 1) is mapped to each bit.
// Providing nClientCount as -1 and clients pointer as NULL would post event to all available clients.
// Providing nSize has no effect and is unused.
virtual void PostEventAbstract( CSplitScreenSlot nSlot, bool bLocalOnly, int nClientCount, const uint64 *clients,
INetworkSerializable *pEvent, const void *pData, unsigned long nSize, NetChannelBufType_t bufType ) = 0;
INetworkMessageInternal *pEvent, const CNetMessage *pData, unsigned long nSize, NetChannelBufType_t bufType ) = 0;
virtual void PostEventAbstract( CSplitScreenSlot nSlot, bool bLocalOnly, IRecipientFilter *pFilter,
INetworkSerializable *pEvent, const void *pData, unsigned long nSize ) = 0;
INetworkMessageInternal *pEvent, const CNetMessage *pData, unsigned long nSize ) = 0;
// Posts the event to all clients, even tho the function name tells otherwise
// Providing nSize has no effect and is unused.
virtual void PostEntityEventAbstract( const CBaseHandle &hndl, INetworkSerializable *pEvent, const void *pData, unsigned long nSize, NetChannelBufType_t bufType ) = 0;
virtual void PostEntityEventAbstract( const CBaseHandle &hndl, INetworkMessageInternal *pEvent, const CNetMessage *pData, unsigned long nSize, NetChannelBufType_t bufType ) = 0;
virtual void ProcessQueuedEvents() = 0;
virtual CEntityIndex GetEventSource() const = 0;

View File

@ -20,13 +20,13 @@
class IDemoRecorderBase;
class IInstantReplayIntercept;
class INetMessage;
class CNetMessage;
class INetChannelHandler;
class INetChannel;
class INetChannelInfo;
class INetMessageBinder;
class INetworkMessageProcessingPreFilter;
class INetworkSerializable;
class INetworkMessageInternal;
class INetMessageDispatcher;
class InstantReplayMessage_t;
class CUtlSlot;
@ -59,7 +59,7 @@ abstract_class INetworkMessageProcessingPreFilter
{
public:
// Filter incoming messages from the netchan, return true to filter out (block) the further processing of the message
virtual bool FilterMessage( INetworkSerializable *pNetMessage, const void *pData, INetChannel *pChannel ) = 0;
virtual bool FilterMessage( INetworkMessageInternal *pNetMessage, const CNetMessage *pData, INetChannel *pChannel ) = 0;
};
abstract_class INetChannel : public INetChannelInfo
@ -73,7 +73,7 @@ public:
virtual HSteamNetConnection GetSteamNetConnection( void ) const = 0;
virtual bool SendNetMessage( INetworkSerializable *pNetMessage, const void *pData, NetChannelBufType_t bufType ) = 0;
virtual bool SendNetMessage( INetworkMessageInternal *pNetMessage, const CNetMessage *pData, NetChannelBufType_t bufType ) = 0;
virtual bool SendData( bf_write &msg, NetChannelBufType_t bufferType ) = 0;
virtual int Transmit( const char *pDebugName, bf_write *data ) = 0;
virtual void SetBitsToSend( void ) = 0;
@ -112,8 +112,8 @@ public:
virtual void StartRegisteringMessageHandlers( void ) = 0;
virtual void FinishRegisteringMessageHandlers( void ) = 0;
virtual void RegisterNetMessageHandlerAbstract( CUtlSlot *nSlot, const CUtlAbstractDelegate &delegate, int nParamCount, INetworkSerializable *pNetMessage, int nPriority ) = 0;
virtual void UnregisterNetMessageHandlerAbstract( CUtlSlot *nSlot, const CUtlAbstractDelegate &delegate, INetworkSerializable *pNetMessage ) = 0;
virtual void RegisterNetMessageHandlerAbstract( CUtlSlot *nSlot, const CUtlAbstractDelegate &delegate, int nParamCount, INetworkMessageInternal *pNetMessage, int nPriority ) = 0;
virtual void UnregisterNetMessageHandlerAbstract( CUtlSlot *nSlot, const CUtlAbstractDelegate &delegate, INetworkMessageInternal *pNetMessage ) = 0;
virtual void SetChallengeNr( unsigned int challenge ) = 0;
virtual int GetNumBitsWritten( NetChannelBufType_t bufferType ) const = 0;
@ -126,7 +126,7 @@ public:
virtual void InstallMessageFilter( INetworkMessageProcessingPreFilter *pFilter ) = 0;
virtual void UninstallMessageFilter( INetworkMessageProcessingPreFilter *pFilter ) = 0;
virtual void PostReceivedNetMessage( INetworkSerializable *pNetMessage, const void *pData, const NetChannelBufType_t *pBufType, int nBits, int nInSequenceNr ) = 0;
virtual void PostReceivedNetMessage( INetworkMessageInternal *pNetMessage, const CNetMessage *pData, const NetChannelBufType_t *pBufType, int nBits, int nInSequenceNr ) = 0;
virtual void InsertReplayMessage( InstantReplayMessage_t &msg ) = 0;
virtual bool HasQueuedNetMessages( int nMessageId ) const = 0;

View File

@ -1,48 +0,0 @@
//========= Copyright <20> 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: INetMessage interface
//
// $NoKeywords: $
//=============================================================================//
#ifndef INETMESSAGE_H
#define INETMESSAGE_H
#include "tier1/bitbuf.h"
class INetMsgHandler;
class INetMessage;
class INetChannel;
// typedef bool (INetMsgHandler::*PROCESSFUNCPTR)(INetMessage*);
// #define CASTPROCPTR( fn ) static_cast <bool (INetMsgHandler::*)(INetMessage*)> (fn)
class INetMessage
{
public:
virtual ~INetMessage() {};
// Use these to setup who can hear whose voice.
// Pass in client indices (which are their ent indices - 1).
virtual void SetNetChannel(INetChannel * netchan) = 0; // netchannel this message is from/for
virtual void SetReliable( bool state ) = 0; // set to true if it's a reliable message
virtual bool Process( void ) = 0; // calls the recently set handler to process this message
virtual bool ReadFromBuffer( bf_read &buffer ) = 0; // returns true if parsing was OK
virtual bool WriteToBuffer( bf_write &buffer ) = 0; // returns true if writing was OK
virtual bool IsReliable( void ) const = 0; // true, if message needs reliable handling
virtual int GetType( void ) const = 0; // returns module specific header tag eg svc_serverinfo
virtual int GetGroup( void ) const = 0; // returns net message group of this message
virtual const char *GetName( void ) const = 0; // returns network message name, eg "svc_serverinfo"
virtual INetChannel *GetNetChannel( void ) const = 0;
virtual const char *ToString( void ) const = 0; // returns a human readable string about message content
virtual size_t GetSize() const = 0;
};
#endif

View File

@ -8,6 +8,7 @@
#include <eiface.h>
#include <networksystem/inetworkserializer.h>
#include <networksystem/netmessage.h>
#include <tier1/bitbuf.h>
#include <tier1/utlstring.h>
#include <tier1/utlsymbol.h>
@ -60,31 +61,36 @@ abstract_class INetworkMessages
public:
virtual void RegisterNetworkCategory(NetworkCategoryId nCategoryId, char const *szDebugName) = 0;
virtual void AssociateNetMessageWithChannelCategoryAbstract(INetworkSerializable *pNetMessage, NetworkCategoryId nCategoryId, bool) = 0;
virtual void AssociateNetMessageWithChannelCategoryAbstract(INetworkMessageInternal *pNetMessage, NetworkCategoryId nCategoryId, bool) = 0;
// Passing nMessasgeId as -1 would auto-assign the id even if bAutoAssignId is false based on the message name hash.
virtual INetworkSerializable *FindOrCreateNetMessage(int nMessageId, IProtobufBinding const *pProtoBinding, uint nMessageSize, INetworkSerializerBindingBuildFilter *pUnused, bool bCreateIfNotFound = true, bool bAutoAssignId = false) = 0;
virtual INetworkMessageInternal *FindOrCreateNetMessage(int nMessageId, IProtobufBinding const *pProtoBinding, uint nMessageSize, INetworkSerializerBindingBuildFilter *pUnused, bool bCreateIfNotFound = true, bool bAutoAssignId = false) = 0;
virtual bool SerializeAbstract(bf_write &pBuf, INetworkSerializable *pNetMessage, void const *pData) = 0;
virtual bool Serialize(bf_write &pBuf, const CNetMessage *pData) = 0;
virtual bool UnserializeMessageInternal( bf_read &pBuf, CNetMessage *pData ) = 0;
virtual bool SerializeMessageInternal( bf_write &pBuf, const CNetMessage *pData ) = 0;
virtual bool SerializeAbstract(bf_write &pBuf, INetworkMessageInternal *pNetMessage, const CNetMessage *pData) = 0;
// Returns true if the buffer is fully read, false if there's still data left
virtual bool UnserializeAbstract(bf_read &pBuf, INetworkSerializable *pNetMessage, void *pData) = 0;
virtual bool UnserializeAbstract(bf_read &pBuf, INetworkSerializable **pNetMessage, void **pData) = 0;
virtual bool UnserializeAbstract(bf_read &pBuf, INetworkMessageInternal *pNetMessage, CNetMessage *pData) = 0;
virtual bool UnserializeAbstract(bf_read &pBuf, INetworkMessageInternal **pNetMessage, CNetMessage **pData) = 0;
virtual void *AllocateNetMessageAbstract(INetworkSerializable *pNetMessage) = 0;
virtual void *AllocateAndCopyConstructNetMessageAbstract(INetworkSerializable *pNetMessage, void const *pFrom) = 0;
virtual CNetMessage *AllocateNetMessageAbstract(INetworkMessageInternal *pNetMessage) = 0;
virtual CNetMessage *AllocateAndCopyConstructNetMessageAbstract(INetworkMessageInternal *pNetMessage, const CNetMessage *pFrom) = 0;
virtual void DeallocateNetMessageAbstract(INetworkSerializable *pNetMessage, void *pData) = 0;
virtual void DeallocateNetMessageAbstract(INetworkMessageInternal *pNetMessage, CNetMessage *pData) = 0;
virtual void *RegisterNetworkFieldSerializer(char const *, NetworkSerializationMode_t, NetworkableDataType_t, int, NetworkFieldSerializeCB, NetworkFieldUnserializeCB, NetworkFieldInfoCB,
NetworkFieldMetaInfoCB, NetworkableDataCB, NetworkUnkCB001, NetworkFieldSerializeCB, NetworkFieldUnserializeCB) = 0;
virtual void *RegisterNetworkArrayFieldSerializer(char const *, NetworkSerializationMode_t, NetworkFieldSerializeBufferCB, NetworkFieldUnserializeBufferCB, NetworkFieldInfoCB,
NetworkFieldMetaInfoCB, NetworkFieldSerializeBufferCB, NetworkFieldUnserializeBufferCB) = 0;
virtual NetMessageInfo_t *GetNetMessageInfo(INetworkSerializable *pNetMessage) = 0;
virtual NetMessageInfo_t *GetNetMessageInfo(INetworkMessageInternal *pNetMessage) = 0;
virtual INetworkSerializable* FindNetworkMessage(char const *szName) = 0;
virtual INetworkSerializable* FindNetworkMessagePartial(char const *szPartialName) = 0;
virtual INetworkMessageInternal* FindNetworkMessage(char const *szName) = 0;
virtual INetworkMessageInternal* FindNetworkMessagePartial(char const *szPartialName) = 0;
virtual NetworkGroupId FindNetworkGroup(char const *szGroup, bool bCreateIfNotFound = false) = 0;
virtual int GetNetworkGroupCount() = 0;
@ -98,7 +104,8 @@ public:
virtual void SetNetworkSerializationContextData(char const *szContext, NetworkSerializationMode_t, void *) = 0;
virtual void *GetNetworkSerializationContextData(char const *szContext) = 0;
virtual void unk001() = 0;
virtual void unk101() = 0;
virtual void unk102() = 0;
// Doesn't support duplicated callbacks per field
virtual void RegisterNetworkFieldChangeCallbackInternal(char const *szFieldName, uint64, NetworkFieldChangedDelegateType_t fieldType, CUtlAbstractDelegate pCallback, NetworkFieldChangeCallbackPerformType_t cbPerformType, int unkflag ) = 0;
@ -111,7 +118,7 @@ public:
virtual void RegisterFieldChangeCallbackPriority(int nPriority) = 0;
virtual INetworkSerializable *FindNetworkMessageById(int nMessageId) = 0;
virtual INetworkMessageInternal *FindNetworkMessageById(int nMessageId) = 0;
virtual void SetIsForServer(bool bIsForServer) = 0;
virtual bool GetIsForServer() = 0;

View File

@ -12,6 +12,8 @@
#include <tier1/bitbuf.h>
#include "Color.h"
class CNetMessage;
enum NetworkValidationMode_t
{
@ -49,10 +51,10 @@ struct NetMessageInfo_t
bool m_bOkayToRedispatch;
};
abstract_class INetworkSerializable
abstract_class INetworkMessageInternal
{
public:
virtual ~INetworkSerializable() = 0;
virtual ~INetworkMessageInternal() = 0;
virtual const char *GetUnscopedName() = 0;
virtual NetMessageInfo_t *GetNetMessageInfo() = 0;
@ -63,12 +65,14 @@ public:
virtual void SwitchMode( NetworkValidationMode_t nMode ) = 0;
virtual void *AllocateMessage() = 0;
virtual void DeallocateMessage(void *pMsg) = 0;
virtual void *AllocateAndCopyConstructNetMessage(void const *pOther) = 0;
virtual CNetMessage *AllocateMessage() = 0;
virtual void DeallocateMessage( CNetMessage *pMsg ) = 0;
virtual CNetMessage *AllocateAndCopyConstructNetMessage( const CNetMessage *pOther ) = 0;
virtual bool Serialize(bf_write &pBuf, void const *pData, NetworkSerializationMode_t unused) = 0;
virtual bool Unserialize(bf_read &pBuf, void *pData, NetworkSerializationMode_t unused) = 0;
// Calls to INetworkMessages::SerializeMessageInternal
virtual bool Serialize( bf_write &pBuf, const CNetMessage *pData ) = 0;
// Calls to INetworkMessages::UnserializeMessageInternal
virtual bool Unserialize( bf_read &pBuf, CNetMessage *pData ) = 0;
};
#endif /* NETWORKSERIALIZER_H */

View File

@ -0,0 +1,65 @@
#ifndef NETMESSAGE_H
#define NETMESSAGE_H
#ifdef _WIN32
#pragma once
#endif
#include "networksystem/inetworkserializer.h"
template<typename PROTO_TYPE>
class CNetMessagePB;
class CNetMessage
{
public:
virtual ~CNetMessage() {}
// Returns the underlying proto object
virtual void *AsProto() const = 0;
virtual void *AsProto2() const = 0;
virtual INetworkMessageInternal *GetNetMessage() const
{
// This shouldn't ever be called and the game provided vtable should be called instead!
Assert(0);
return nullptr;
}
// Helper function to cast up the abstract message to a concrete CNetMessagePB<T> type.
// Doesn't do any validity checks itself!
template<typename T>
const CNetMessagePB<T> *ToPB() const
{
return static_cast<const CNetMessagePB<T> *>(this);
}
template<typename T>
CNetMessagePB<T> *ToPB()
{
return static_cast<CNetMessagePB<T> *>(this);
}
};
// AMNOTE: This is a stub class over real CNetMessagePB!
// This is mainly to access the game constructed objects, and not for direct initialization of them
// since this misses the CNetMessage implementation which requires supplying other proto related info like
// proto binding object, proto msg id/group, etc.
// So to allocate the message yourself use INetworkMessageInternal::AllocateMessage() or INetworkMessages::AllocateNetMessageAbstract()
// functions instead of direct initialization (they both are equivalent)!
// Example usage:
// CNetMessagePB<ProtoClass> *msg = INetworkMessageInternal::AllocateMessage()->ToPB<ProtoClass>();
// msg->field1( 2 );
// msg->field2( 3 );
// IGameEventSystem::PostEventAbstract( ..., msg, ... );
template<typename PROTO_TYPE>
class CNetMessagePB : public CNetMessage, public PROTO_TYPE
{
private:
// Prevent manual construction of this object as per the comment above
CNetMessagePB() { };
CNetMessagePB( const CNetMessagePB & ) { };
};
#endif // NETMESSAGE_H