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
/
netstream2.lua
153 lines (120 loc) · 3.64 KB
/
netstream2.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
--[[
NetStream - 2.0.0
Alexander Grist-Hucker
http://www.revotech.org
Credits to:
thelastpenguin for pON.
https://github.com/thelastpenguin/gLUA-Library/tree/master/pON
--]]
local type, error, pcall, pairs, _player = type, error, pcall, pairs, player;
if (!pon) then
error("NetStream: Unable to find pON!");
end;
AddCSLuaFile();
netstream = netstream or {};
netstream.stored = netstream.stored or {};
-- A function to split data for a data stream.
function netstream.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)
netstream.stored[name] = Callback;
end;
if (SERVER) then
util.AddNetworkString("NetStreamDS");
-- A function to start a net stream.
function netstream.Start(player, name, ...)
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 = {...};
local encodedData = pon.encode(dataTable);
if (encodedData and #encodedData > 0 and bShouldSend) then
net.Start("NetStreamDS");
net.WriteString(name);
net.WriteUInt(#encodedData, 32);
net.WriteData(encodedData, #encodedData);
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 (netstream.stored[player.nsDataStreamName]) then
local bStatus, value = pcall(pon.decode, player.nsDataStreamData);
if (bStatus) then
netstream.stored[player.nsDataStreamName](player, unpack(value));
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, ...)
local dataTable = {...};
local encodedData = pon.encode(dataTable);
if (encodedData and #encodedData > 0) then
net.Start("NetStreamDS");
net.WriteString(name);
net.WriteUInt(#encodedData, 32);
net.WriteData(encodedData, #encodedData);
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 (netstream.stored[NS_DS_NAME]) then
local bStatus, value = pcall(pon.decode, NS_DS_DATA);
if (bStatus) then
netstream.stored[NS_DS_NAME](unpack(value));
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;