Skip to content

Instantly share code, notes, and snippets.

@Srlion
Last active April 11, 2019 08:02
Show Gist options
  • Save Srlion/87f85a0ea4d94c20381bb95d407e59c0 to your computer and use it in GitHub Desktop.
Save Srlion/87f85a0ea4d94c20381bb95d407e59c0 to your computer and use it in GitHub Desktop.
--[[
NetStream - 2.0.1
https://github.com/alexgrist/NetStream/blob/master/netstream2.lua
Alexander Grist-Hucker
http://www.revotech.org
Credits to:
thelastpenguin for pON.
https://github.com/thelastpenguin/gLUA-Library/tree/master/pON
]]--
--[[
if (SERVER) then
netstream.Hook("Hi", function(ply, ...) -- Third argument is called to check if the player has permission to send the net message before decoding
print(...)
end, function(ply)
if (!ply:IsAdmin()) then
return false
end
end)
-- OR
netstream.Hook("Hi", function(ply, ...)
print(...)
end)
netstream.Start(Entity(1), "Hi", "a", 1, {}, true, false, nil, "!") -- First argument player or table of players or any other argument to send to all players
netstream.Start({Entity(1), Entity(2)}, "Hi", "a", 1, {}, true, false, nil, "!")
netstream.Start(nil, "Hi", "a", 1, {}, true, false, nil, "!")
end
if (CLIENT) then
netstream.Hook("Hi", function(...)
print(...)
end)
netstream.Start("Hi", "a", 1, {}, true, false, nil, "!")
end
]]--
-- Config
local addonName = "TestAddon"
local mainTable = _G -- _G.netstream = netstream
local compress = true -- compress data before sending
local pon = include("pon.lua")
--
local type = type
local pcall = pcall
local unpack = unpack
local net = net
local table_maxn = table.maxn
local uCompress, uDecomress = util.Compress, util.Decompress
local netStreamSend = addonName .. ".NetStreamDS.Sending"
local netstream = {}
if (istable(mainTable)) then
mainTable.netstream = netstream
end
local checks = {}
local receivers = {}
local str_len = string.len
local function compressData(data)
local length = str_len(data)
if (!compress) then return data, length, false end
local compressed = uCompress(data)
local length2 = str_len(compressed)
if (length2 < length) then
return compressed, length2, true
else
return data, length, false
end
end
if (SERVER) then
util.AddNetworkString(netStreamSend)
-- local str_sub = string.sub
-- local str_len = string.len
-- local function Split(str, buffer, result)
-- if (!result) then
-- result = {}
-- end
-- if (!buffer) then
-- buffer = 32768
-- end
-- local len = str_len(str)
-- if (len >= buffer) then
-- result[#result + 1] = str_sub(str, 1, buffer - 1)
-- str = str_sub(str, buffer, len)
-- else
-- result[#result + 1] = str
-- return result
-- end
-- return Split(str, buffer, result)
-- end
local player_GetAll = player.GetAll
function netstream.Start(ply, name, ...)
local plyType = type(ply)
if (plyType != "Player" && plyType != "table") then
ply = player_GetAll()
elseif (plyType == "table" && #ply == 0) then
return
end
local encodedData, compressed, length
encodedData = pon.encode({...})
encodedData, length, compressed = compressData(encodedData)
net.Start(netStreamSend)
net.WriteString(name)
net.WriteUInt(length, 32)
net.WriteData(encodedData, length)
net.WriteBool(compressed)
net.Send(ply)
end
function netstream.Hook(name, callback, check)
receivers[name] = callback
if (type(check) == "function") then
checks[name] = check
end
end
net.Receive(netStreamSend, function(_, ply)
local name = net.ReadString()
local callback = receivers[name]
if (!callback) then return end
local length = net.ReadUInt(32)
local check = checks[name]
if (check && check(ply, length) == false) then return end
local data = net.ReadData(length)
local compressed = net.ReadBool()
if (compressed) then
data = uDecomress(data)
if (data == nil) then return end
end
local status
status, data = pcall(pon.decode, data)
if (!status) then return end
callback(ply, unpack(data, 1, table_maxn(data)))
end)
else
checks = nil
function netstream.Start(name, ...)
local encodedData, compressed, length
encodedData = pon.encode({...})
encodedData, length, compressed = compressData(encodedData)
net.Start(netStreamSend)
net.WriteString(name)
net.WriteUInt(length, 32)
net.WriteData(encodedData, length)
net.WriteBool(compressed)
net.SendToServer()
end
function netstream.Hook(name, callback)
receivers[name] = callback
end
net.Receive(netStreamSend, function()
local callback = receivers[net.ReadString()]
if (!callback) then return end
local length = net.ReadUInt(32)
local data = net.ReadData(length)
local compressed = net.ReadBool()
if (compressed == true) then
data = uDecomress(data)
end
data = pon.decode(data)
callback(unpack(data, 1, table_maxn(data)))
end)
end
return netstream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment