Skip to content

Commit

Permalink
cleanup, add missing addnetworkstring
Browse files Browse the repository at this point in the history
  • Loading branch information
luttje committed Sep 4, 2024
1 parent 5376b3b commit 3bce523
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ if (GAMEUI) then
return
end

if (SERVER) then
Networks.AddNetworkString("EntityNetworkedVariable")
Networks.AddNetworkString("EntityNetworkedVariableSet")
end

local ENTITY_META = _R.Entity
local Hooks = require("hooks")

Expand Down Expand Up @@ -196,7 +201,7 @@ else
if (not IsValid(entity)) then
-- TODO: Should this happen? We should only be sending to players that are in the PVS of the entity
-- Erroring for debug, but perhaps we should just ignore it
error("Received networked variable for invalid entity")
debugPrint("Received networked variable for invalid entity")
return
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ if (GAMEUI) then
return
end

if (SERVER) then
Networks.AddNetworkString("GlobalNetworkedVariable")
Networks.AddNetworkString("GlobalNetworkedVariableSet")
end

local Hooks = require("hooks")

local debugPrint = function(...)
Expand Down
4 changes: 3 additions & 1 deletion game/experiment/scripts/lua/includes/extensions/networks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ local networkStrings = NetworkStringTables.FindTable("LuaNetworkStrings")

function Networks.AddNetworkString(name)
if (SERVER) then
networkStrings:AddString(true, name)
local networkId = networkStrings:AddString(true, name)

printDebug("Added network string: " .. name .. " with id " .. networkId)
end
end

Expand Down
10 changes: 4 additions & 6 deletions game/experiment/scripts/lua/utilities/test/networks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
local countReceived = 0

-- After registering the network strings we must wait a bit
local DELAY_BEFORE_START = 1
local DELAY_BEFORE_DONE = 2
local DELAY_BEFORE_START = 0.2
local DELAY_BEFORE_DONE = 1

print("Running net module tests...")

if (SERVER) then
Networks.AddNetworkString("TestMessage")
Expand All @@ -13,7 +15,6 @@ if (SERVER) then
Networks.AddNetworkString("CTestMessage2")

Networks.Receive("TestMessage", function(length, client)
print("TestMessage", length, client)
local message = Networks.ReadString()
local num = Networks.ReadFloat()

Expand All @@ -30,7 +31,6 @@ if (SERVER) then
end)

Networks.Receive("TestMessage2", function(length, client)
print("TestMessage2", length, client)
Networks.Start("CTestMessage2")
Networks.WriteString("Hello again, client!")
Networks.Send(client)
Expand All @@ -47,7 +47,6 @@ end

if (CLIENT) then
Networks.Receive("CTestMessage", function(length)
print("CTestMessage", length)
local num = Networks.ReadFloat()
local message = Networks.ReadString()
local position = Networks.ReadVector()
Expand All @@ -60,7 +59,6 @@ if (CLIENT) then
end)

Networks.Receive("CTestMessage2", function(length)
print("CTestMessage2", length)
local message = Networks.ReadString()

assert(message == "Hello again, client!", "Received incorrect message from server!")
Expand Down
5 changes: 2 additions & 3 deletions src/common/networksystem/isockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ class CNetPacket;
#define NETWORK_MESSAGE_HEADER_BITS ( NETWORK_MESSAGE_GROUP_BITS + NETWORK_MESSAGE_INDEX_BITS )

// The size of messages is sent first, so we know when a 'packet' is complete
#define NETWORK_MESSAGE_LENGTH_DATATYPE short // Max 64KB
#define NET_MAX_PAYLOAD 4000
#define NET_MAX_MESSAGE 4096
#define NETWORK_MESSAGE_LENGTH_DATATYPE uint16 // Max 64KB
#define NETWORK_MAX_MESSAGE_LENGTH ( 1 << ( sizeof( NETWORK_MESSAGE_LENGTH_DATATYPE ) * 8 ) )

enum
{
Expand Down
2 changes: 1 addition & 1 deletion src/game/shared/experiment/util/lnetworks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ LUA_BINDING_BEGIN( Networks, InternalSendToClients, "library", "Send a message t
message->SetBuffer( ( const char * )messageWriter.GetBasePointer(), messageWriter.GetNumBytesWritten() );

// If no clients are probided, broadcast the message
if ( lua_isnil( L, 3 ) )
if ( lua_isnoneornil( L, 3 ) )
{
g_pNetworkManager->BroadcastServerToClientMessage( message );
}
Expand Down
2 changes: 1 addition & 1 deletion src/networksystem/netpacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "netpacket.h"
#include "networksystem.h"

static CUtlMemoryPool s_PacketBufferAlloc( NET_MAX_MESSAGE, 8, CUtlMemoryPool::GROW_SLOW );
static CUtlMemoryPool s_PacketBufferAlloc( NETWORK_MAX_MESSAGE_LENGTH, 8, CUtlMemoryPool::GROW_SLOW );
DEFINE_FIXEDSIZE_ALLOCATOR( CNetPacket, 32, CUtlMemoryPool::GROW_SLOW );

CNetPacket::CNetPacket()
Expand Down
7 changes: 5 additions & 2 deletions src/networksystem/tcp_sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ int CTcpClientSocket::ReceiveMessageLength()
return -1;
}

return LittleLong( *( NETWORK_MESSAGE_LENGTH_DATATYPE * )buffer );
NETWORK_MESSAGE_LENGTH_DATATYPE length;
memcpy( &length, buffer, sizeof( length ) );
return LittleLong( length );
}

bool CTcpClientSocket::Receive( CUtlBuffer &data )
Expand Down Expand Up @@ -122,7 +124,8 @@ bool CTcpClientSocket::Send( const char *data, size_t dataLength )
{
// Send the length of the message first
char lengthBuffer[sizeof( NETWORK_MESSAGE_LENGTH_DATATYPE )];
*( NETWORK_MESSAGE_LENGTH_DATATYPE * )lengthBuffer = LittleLong( ( int )dataLength );
NETWORK_MESSAGE_LENGTH_DATATYPE length = LittleLong( ( int )dataLength );
memcpy( lengthBuffer, &length, sizeof( length ) );

int bytesSent = send( m_Socket, lengthBuffer, sizeof( lengthBuffer ), 0 );

Expand Down
4 changes: 2 additions & 2 deletions src/public/lnetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,8 @@ LUA_REGISTRATION_INIT( MessageWriters )

LUA_BINDING_BEGIN( MessageWriters, Create, "library", "Creates a new MessageWriter for writing network messages" )
{
void *buffer = malloc( NET_MAX_MESSAGE );
bf_write *writer = new bf_write( buffer, NET_MAX_MESSAGE );
void *buffer = malloc( NETWORK_MAX_MESSAGE_LENGTH );
bf_write *writer = new bf_write( buffer, NETWORK_MAX_MESSAGE_LENGTH );
lua_pushbf_write( L, writer );
return 1;
}
Expand Down

0 comments on commit 3bce523

Please sign in to comment.