Object for conveniently sending and receiving large data/files via net.
Written in Yuescript, compiled Lua code can be found in releases, or you can compile it yourself using compiled Yuescript Compiler.
NetTransferObjectNetTransfer(stringtransferName,booleanverifyChecksums,booleanunreliable ) - Creates a data transfer object with the specified parameters.
numberNetTransfer:GetTransmissionSpeed() - Returns network messages per second.nilNetTransfer:SetTransmissionSpeed(numberpacket per second,booleannoCeil ) - Sets network messages per second.booleanNetTransfer:IsUnreliable() - https://wiki.facepunch.com/gmod/net.StartnilNetTransfer:SetUnreliable(booleanvalue ) - https://wiki.facepunch.com/gmod/net.StartbooleanNetTransfer:IsVerifyChecksums() - Returnstrueif network message contains and verifies the checksumnilNetTransfer:SetVerifyChecksums(booleanvalue ) - Whether the network message must contain and verify the checksum.anyNetTransfer:GetFilter() - Returns the filter of allowed players to send and receive.nilNetTransfer:SetFilter(boolean/number/string/table/function/entity/vector/RecipientFilterfilter ) - Sets the filter of allowed players to send and receivebooleanNetTransfer:IsAllowedPlayer(Playerply ) - Checks if the player is allowed by the filter.booleanNetTransfer:IsCompressedData() - returnstrueif the data is compressed.stringNetTransfer:CompressData() - Compresses current data using LZMA.stringNetTransfer:DecompressData() - Decompresses current data from LZMA.stringNetTransfer:GetTransmittedData() - Returns the current data, if compressed returns the compressed data.stringNetTransfer:SetTransmittedData(stringtransmittedData,booleancompress,booleandecompress ) - Sets data and compresses or decompresses it as needed.booleanNetTransfer:IsSending() - Returnstrueif data is being sent at the moment.nilNetTransfer:Send(Player/RecipientFilter/tabletarget ) - Sends the current data to the selected target.booleanNetTransfer:IsReceiving() - Returnstrueif data receiving is in progress.nilNetTransfer:Receive(functionfunc,booleanpermanent ) - As soon as the data is completely retrieved it will execute the specified function and return the data in it (if the data was compressed it will decompress it), by default the function is executed once after it is deleted unless permanent is set totrue.nilNetTransfer:OnProgress(functionfunc ) - Sets the function that will receive the progress of data retrieval.nilNetTransfer:OnError(functionfunc ) - Sets a function that will be executed if an error occurs and will receive the object and an error message.numberNetTransfer:GetTimeout() - Returns the current timeout value.nilNetTransfer:SetTimeout(numberint ) - Sets the time for receiving a response from the sender, default is 10 seconds, used mostly for unreliable data transmission because messages may be lost during transmission.nilNetTransfer:Finish() - Forcibly terminates the data transfer.nilNetTransfer:Remove() - Removes an object from the list to receive data.
- SERVER
concommand.Add( "fs_test_req", function( ply, _, args )
path = args[1]
if #path == 0 then
return
end
local fs = NetTransfer( "example" )
fs:SetTransmittedData( file.Read( path, "GAME" ), true, false )
fs:OnProgress( function( _, fraction, bytes )
print( string.format( "Net transfer: %d%% (%d Bytes)", fraction * 100, bytes ) )
end )
fs:Send( ply )
end )- CLIENT
concommand.Add( "fs_test", function()
local frame = vgui.Create( "DFrame" )
frame:SetSize( 256, 128 )
frame:Center()
frame:MakePopup()
frame:SetTitle( "Net transfer example" )
local pbar = frame:Add( "DProgress" )
pbar:Dock( BOTTOM )
local button = frame:Add( "DButton" )
button:SetText( "Start" )
button:Dock( BOTTOM )
local text = frame:Add( "DTextEntry" )
text:Dock( BOTTOM )
local pbarText = frame:Add( "DLabel" )
pbarText:Dock( FILL )
local nt = NetTransfer( "example" )
local startTime = 0
nt:Receive( function( _, data )
pbarText:SetText( string.format( "Total received: %d KB, took %f seconds.", #data / 1024, SysTime() - startTime ) )
button:SetText( "Start" )
button:SetEnabled( true )
end )
nt:OnProgress( function( _, fraction, bytes )
if fraction < 1 then
button:SetText( "Stop" )
button:SetEnabled( true )
end
print( string.format( "Net transfer: %d%% (%d Bytes)", fraction * 100, bytes ), SysTime() - startTime )
pbarText:SetText( string.format( "Downloaded: %d%%, took %d seconds.", fraction * 100, SysTime() - startTime ) )
pbar:SetFraction( fraction )
end )
function button:DoClick()
if nt:IsReceiving() then
self:SetText( "Start" )
self:SetEnabled( true )
nt:Finish()
else
RunConsoleCommand( "fs_test_req", text:GetValue() )
self:SetEnabled( false )
startTime = SysTime()
end
end
end )


