This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
netstream.lua
159 lines (124 loc) · 3.63 KB
/
netstream.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
--[[
NetStream - 1.0.2
Alexander Grist-Hucker
http://www.revotech.org
Credits to:
Alexandru-Mihai Maftei aka Vercas for vON.
https://github.com/vercas/vON
--]]
local type, error, pcall, pairs, AddCSLuaFile, _player = type, error, pcall, pairs, AddCSLuaFile, player;
--[[
AddCSLuaFile("includes/modules/von.lua");
require("von");
--]]
if (!von) then
error("NetStream: Unable to find vON!");
end;
AddCSLuaFile();
netstream = {};
local stored = {};
-- A function to split data for a data stream.
local function split(data)
local index = 1;
local result = {};
local buffer = {};
for i = 0, string.len(data) do
buffer[#buffer + 1] = string.sub(data, i, i);
if (#buffer == 32768) then
result[#result + 1] = table.concat(buffer);
index = index + 1;
buffer = {};
end;
end;
result[#result + 1] = table.concat(buffer);
return result;
end;
-- A function to hook a data stream.
function netstream.Hook(name, Callback)
stored[name] = Callback;
end;
if (SERVER) then
util.AddNetworkString("NetStreamDS");
-- A function to start a net stream.
function netstream.Start(player, name, data)
local recipients = {};
local bShouldSend = false;
if (type(player) != "table") then
if (!player) then
player = _player.GetAll();
else
player = {player};
end;
end;
for k, v in pairs(player) do
if (type(v) == "Player") then
recipients[#recipients + 1] = v;
bShouldSend = true;
elseif (type(k) == "Player") then
recipients[#recipients + 1] = k;
bShouldSend = true;
end;
end;
local dataTable = {data = (data or 0)};
local vonData = von.serialize(dataTable);
if (vonData and #vonData > 0 and bShouldSend) then
net.Start("NetStreamDS");
net.WriteString(name);
net.WriteUInt(#vonData, 32);
net.WriteData(vonData, #vonData);
net.Send(recipients);
end;
end;
net.Receive("NetStreamDS", function(length, player)
local NS_DS_NAME = net.ReadString();
local NS_DS_LENGTH = net.ReadUInt(32);
local NS_DS_DATA = net.ReadData(NS_DS_LENGTH);
if (NS_DS_NAME and NS_DS_DATA and NS_DS_LENGTH) then
player.nsDataStreamName = NS_DS_NAME;
player.nsDataStreamData = "";
if (player.nsDataStreamName and player.nsDataStreamData) then
player.nsDataStreamData = NS_DS_DATA;
if (stored[player.nsDataStreamName]) then
local bStatus, value = pcall(von.deserialize, player.nsDataStreamData);
if (bStatus) then
stored[player.nsDataStreamName](player, value.data);
else
ErrorNoHalt("NetStream: '"..NS_DS_NAME.."'\n"..value.."\n");
end;
end;
player.nsDataStreamName = nil;
player.nsDataStreamData = nil;
end;
end;
NS_DS_NAME, NS_DS_DATA, NS_DS_LENGTH = nil, nil, nil;
end);
else
-- A function to start a net stream.
function netstream.Start(name, data)
local dataTable = {data = (data or 0)};
local vonData = von.serialize(dataTable);
if (vonData and #vonData > 0) then
net.Start("NetStreamDS");
net.WriteString(name);
net.WriteUInt(#vonData, 32);
net.WriteData(vonData, #vonData);
net.SendToServer();
end;
end;
net.Receive("NetStreamDS", function(length)
local NS_DS_NAME = net.ReadString();
local NS_DS_LENGTH = net.ReadUInt(32);
local NS_DS_DATA = net.ReadData(NS_DS_LENGTH);
if (NS_DS_NAME and NS_DS_DATA and NS_DS_LENGTH) then
if (stored[NS_DS_NAME]) then
local bStatus, value = pcall(von.deserialize, NS_DS_DATA);
if (bStatus) then
stored[NS_DS_NAME](value.data);
else
ErrorNoHalt("NetStream: '"..NS_DS_NAME.."'\n"..value.."\n");
end;
end;
end;
NS_DS_NAME, NS_DS_DATA, NS_DS_LENGTH = nil, nil, nil;
end);
end;