HolyLib Wiki

CreateNetChannel

  CNetChan gameserver.CreateNetChannel( string ip, boolean useDNS, number protocolVersion = 1, number socket = NS_SERVER )

Description

Creates a net channel for the given ip.

Recently Changed

This was recently changed in version (0.8 - DEV).

The protocolVersion and socket arguments were added.

Arguments

1string ip
The IP of the target, format ip:port
2boolean useDNS
If true it will try to resolve the IP
3number protocolVersion = 1
The protocol version to use
4number socket = NS_SERVER
The socket to use for the creation

Returns

1CNetChan channel
Returns the channel or nil on failure

Example

Example implementation of creating a working connection between two servers
local REQUEST_CHANNEL = string.byte("z") function BuildNetChannel(target, status) -- status should not be set when called local bf = bitbuf.CreateWriteBuffer(64) bf:WriteLong(-1) -- CONNECTIONLESS_HEADER bf:WriteByte(REQUEST_CHANNEL) -- Our header bf:WriteByte(status or 0) -- 0 = We requested it. 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) -- Respond to the sender to confirm creation. elseif status == 1 then print("Created our net channel to " .. ip) end
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() -- process any incomming messages netChan:Transmit() -- Transmit out a update. end end) -- Install the script on two servers. -- call BuildNetChannel with the target on one of the servers and on both servers a net channel is created BuildNetChannel("127.0.0.1:27015")