local REQUEST_CHANNEL
= string.byte(
"z")
function BuildNetChannel(target, status)
local bf
= bitbuf.
CreateWriteBuffer(
64)
bf:
WriteLong(
-1)
bf:
WriteByte(REQUEST_CHANNEL)
bf:
WriteByte(status
or 0)
gameserver.
SendConnectionlessPacket(bf, target)
end
function IncomingNetMessage(channel, bf, length)
print(
"Received a message at " .. tostring(channel), bf, length)
end
netChannels
= netChannels
or {}
hook.
Add(
"HolyLib:ProcessConnectionlessPacket",
"ProcessResponse",
function(bf, ip)
local header
= bf:
ReadByte()
if header
!= REQUEST_CHANNEL
then return end
local status
= bf:
ReadByte()
local netChan
= gameserver.
CreateNetChannel(ip)
netChan:
SetMessageCallback(
function(bf, length)
IncomingNetMessage(netChan, bf, length)
end)
table.insert(netChannels, netChan)
if status
== 0 then
print(
"Created a requested net channel to " .. ip)
BuildNetChannel(ip,
1)
elseif status
== 1 then
print(
"Created our net channel to " .. ip)
end
<br
>
return true
end)
function SendNetMessage(target, bf, reliable)
for _, netChan
in ipairs(netChannels)
do
if not netChan:
IsValid()
then continue end
if netChan:
GetName()
!= target
then continue end
return netChan:
SendMessage(bf, reliable)
end
return false
end
hook.
Add(
"Think",
"UpdateNetChannels",
function()
for _, netChan
in ipairs(netChannels)
do
if not netChan:
IsValid()
then continue end
netChan:
ProcessStream()
netChan:
Transmit()
end
end)
BuildNetChannel(
"127.0.0.1:27015")