https://github.com/unknown-gd/net-transfer
Object for conveniently sending and receiving large data/files via net.
https://github.com/unknown-gd/net-transfer
class filetransfer garrys-mod garrysmod garrysmod-addon gmod gmod-addon gmod-lua library network yuescript
Last synced: 15 days ago
JSON representation
Object for conveniently sending and receiving large data/files via net.
- Host: GitHub
- URL: https://github.com/unknown-gd/net-transfer
- Owner: unknown-gd
- License: mit
- Created: 2023-11-30T18:11:19.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-22T15:13:02.000Z (over 2 years ago)
- Last Synced: 2025-03-05T02:35:28.855Z (about 1 year ago)
- Topics: class, filetransfer, garrys-mod, garrysmod, garrysmod-addon, gmod, gmod-addon, gmod-lua, library, network, yuescript
- Homepage:
- Size: 41 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# net-transfer
Object for conveniently sending and receiving large data/files via [net](https://wiki.facepunch.com/gmod/net).
## Where is Lua code?
Written in [Yuescript](https://github.com/pigpigyyy/Yuescript), compiled Lua code can be found in [releases](https://github.com/PrikolMen/net-transfer/releases), or you can compile it yourself using compiled [Yuescript Compiler](https://github.com/pigpigyyy/Yuescript/releases/latest).
## Global Functions
- `NetTransferObject` NetTransfer( `string` transferName, `boolean` verifyChecksums, `boolean` unreliable ) - Creates a data transfer object with the specified parameters.
## `NetTransferObject:` Functions
- `number` NetTransfer:GetTransmissionSpeed() - Returns network messages per second.
- `nil` NetTransfer:SetTransmissionSpeed( `number` packet per second, `boolean` noCeil ) - Sets network messages per second.
- `boolean` NetTransfer:IsUnreliable() - https://wiki.facepunch.com/gmod/net.Start
- `nil` NetTransfer:SetUnreliable( `boolean` value ) - https://wiki.facepunch.com/gmod/net.Start
- `boolean` NetTransfer:IsVerifyChecksums() - Returns `true` if network message contains and verifies the checksum
- `nil` NetTransfer:SetVerifyChecksums( `boolean` value ) - Whether the network message must contain and verify the checksum.
- `any` NetTransfer:GetFilter() - Returns the filter of allowed players to send and receive.
- `nil` NetTransfer:SetFilter( `boolean/number/string/table/function/entity/vector/RecipientFilter` filter ) - Sets the filter of allowed players to send and receive
- `boolean` NetTransfer:IsAllowedPlayer( `Player` ply ) - Checks if the player is allowed by the filter.
- `boolean` NetTransfer:IsCompressedData() - returns `true` if the data is compressed.
- `string` NetTransfer:CompressData() - Compresses current data using LZMA.
- `string` NetTransfer:DecompressData() - Decompresses current data from LZMA.
- `string` NetTransfer:GetTransmittedData() - Returns the current data, if compressed returns the compressed data.
- `string` NetTransfer:SetTransmittedData( `string` transmittedData, `boolean` compress, `boolean` decompress ) - Sets data and compresses or decompresses it as needed.
- `boolean` NetTransfer:IsSending() - Returns `true` if data is being sent at the moment.
- `nil` NetTransfer:Send( `Player/RecipientFilter/table` target ) - Sends the current data to the selected target.
- `boolean` NetTransfer:IsReceiving() - Returns `true` if data receiving is in progress.
- `nil` NetTransfer:Receive( `function` func, `boolean` permanent ) - 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 to `true`.
- `nil` NetTransfer:OnProgress( `function` func ) - Sets the function that will receive the progress of data retrieval.
- `nil` NetTransfer:OnError( `function` func ) - Sets a function that will be executed if an error occurs and will receive the object and an error message.
- `number` NetTransfer:GetTimeout() - Returns the current timeout value.
- `nil` NetTransfer:SetTimeout( `number` int ) - 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.
- `nil` NetTransfer:Finish() - Forcibly terminates the data transfer.
- `nil` NetTransfer:Remove() - Removes an object from the list to receive data.
## Example Code ( very lazy example )
- SERVER
```lua
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
```lua
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 )
```


## Speeds that I got
### Reliable data transmission ( File size ~10 MB )

### Unreliable data transmission ( File size ~10 MB )
