Skip to content

Instantly share code, notes, and snippets.

@bmwalters
Created November 20, 2016 06:49
Show Gist options
  • Save bmwalters/48dbdc2b61f42d7a1b2e1f94eb084ac1 to your computer and use it in GitHub Desktop.
Save bmwalters/48dbdc2b61f42d7a1b2e1f94eb084ac1 to your computer and use it in GitHub Desktop.
-- A net interface that doesn't write to the game's net buffer
-- until Send* is called. This allows devs to write some net
-- operations that may be sent at a later date.
local net = net
local unpack = unpack or table.unpack
local NETBUFFER = {}
NETBUFFER.__index = NETBUFFER
-- import all net.Write* functions
-- these will just add an operation to our buffer
-- in the form of { func, args }
for name, func in pairs(net) do
if string.sub(name, 1, 5) == "Write" then
NETBUFFER[name] = function(self, ...)
self._queue[#self._queue + 1] = { func, { ... } }
end
end
end
-- import all the send functions
-- these will start the message with the given name,
-- then perform all operations in the buffer and clear it
local funcs = {
"Send", "SendOmit", "SendPAS", "SendPVS",
"Broadcast", "SendToServer"
}
for _, name in pairs(funcs) do
NETBUFFER[name] = function(self, identifier, ...)
net.Start(identifier)
for _, operation in ipairs(self._queue) do
operation[1](unpack(operation[2]))
end
net[name](...)
self._queue = {}
end
end
-- constructor
function NetBuffer()
local instance = { _queue = {} }
return setmetatable(instance, NETBUFFER)
end
--[[ Usage ]]--
if CLIENT then
concommand.Add("test_netbuffer", function()
local buf = NetBuffer()
buf:WriteUInt(42, 8)
buf:WriteString("Hello World")
buf:WriteVector(Vector(1, 2, 3))
buf:WriteTable({ dog = "mammal", "axolotl", lizard = "reptile" })
buf:SendToServer("my_identifier")
end)
else
util.AddNetworkString("my_identifier")
net.Receive("my_identifier", function(_, sender)
print("Received message from sender", sender:GetName())
print("UInt:", net.ReadUInt(8))
print("String:", net.ReadString())
print("Vector:", tostring(net.ReadVector()))
print("Table:")
PrintTable(net.ReadTable())
end)
end
-- A net interface that doesn't write to the game's net buffer
-- until Send* is called. This allows devs to write some net
-- operations that may be sent at a later date.
local net = net
local unpack = unpack or table.unpack
local NETBUFFER = {}
NETBUFFER.__index = NETBUFFER
-- import all net.Write* functions
-- these will just add an operation to our buffer
-- in the form of { func, args }
for name, func in pairs(net) do
if string.sub(name, 1, 5) == "Write" then
NETBUFFER[name] = function(self, ...)
self._queue[#self._queue + 1] = { func, { ... } }
end
end
end
-- import all the send functions
-- these will start the message with the given name,
-- then perform all operations in the buffer and clear it
local funcs = {
"Send", "SendOmit", "SendPAS", "SendPVS",
"Broadcast", "SendToServer"
}
for _, name in pairs(funcs) do
NETBUFFER[name] = function(self, identifier, ...)
net.Start(identifier)
for _, operation in ipairs(self._queue) do
operation[1](unpack(operation[2]))
end
net[name](...)
self._queue = {}
end
end
-- constructor
function NetBuffer()
local instance = { _queue = {} }
return setmetatable(instance, NETBUFFER)
end
--[[ Usage ]]--
if CLIENT then
concommand.Add("test_netbuffer", function()
local buf = NetBuffer()
buf:WriteUInt(42, 8)
buf:WriteString("Hello World")
buf:WriteVector(Vector(1, 2, 3))
buf:WriteTable({ dog = "mammal", "axolotl", lizard = "reptile" })
buf:SendToServer("my_identifier")
end)
else
util.AddNetworkString("my_identifier")
net.Receive("my_identifier", function(_, sender)
print("Received message from sender", sender:GetName())
print("UInt:", net.ReadUInt(8))
print("String:", net.ReadString())
print("Vector:", tostring(net.ReadVector()))
print("Table:")
PrintTable(net.ReadTable())
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment