This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KingofGamesYami's profile picture

iNet - Powerful & Simple Networking

Started by KingofGamesYami, 08 August 2014 - 08:49 PM
KingofGamesYami #1
Posted 08 August 2014 - 10:49 PM
API Functions (accessible through iNet.* )

local object = iNet.open( number channel, number replyChannel, string side )

Creates and returns an iNet object.

local tbl = { iNet.waitForAny( object iNet Object, … ) }

Waits for any of the object to receive a message, and returns the message values.

local tbl = { iNet.waitForAll( object iNet Object, … ) }

Waits for all objects to receive a message, and returns message values.

Object Functions (accessible through object:* )

object:send( string/table message )

Sends a message

local message, distance, sender = object:receive()

Receives messages. Sender is the computer label, if available, or the computer ID.

object:reply( message )

Sends a message to the computer that last communicated with the object.

object:repeat()

Repeater program. Duh.

object:setProctal( string proctal )

Sets the proctal filter. This will automatically be applied to send and receive. In 1.2, the spelling was updated to object:setProtocol

object:setEncryption( function encrypt )

An advanced setting for encrypting messages. The function specified will be given string message. Automatically encrypts object:send.

object:setDecryption( function decrypt )

The counterpart to object:setEncryption. This will automatically decrypt received messages.

object:removeEncryption()

Removes the encryption filter (if any)

object:removeDecryption()

Removes the decryption filter (if any)

object:sendFile( string fileName )

Sends a file. Be aware that some files may be corrupted when sent through the event filter. object:receive() will recognise it as a file and save it as the filename specified. If this overwrites something you had, well too bad for you. It will also return the filename, instead of the fileData. In 1.2, this should no longer be a concern, because all data is sent with base64 encoding

Downloads:

1.0 - pastebin get FWpbap6B
1.2 (beta) - pastebin get vuzF5t8p

--#sending script (run this SECOND)
os.loadAPI( "iNet" )
local channel = iNet.open( 255, 256, "top" )
channel:setProctal( "Examples" ) --#note: in 1.2, this was changed to channel:setProtocol( "Examples" )
channel:send( "Hello" )
local message, distance, sender = channel:receive()
print( "Recieved '" .. message .. "' from " .. sender .. ", " .. distance .. "m away" )
channel:reply( "Confirm" )
--#receiving script  (run this FIRST)
os.loadAPI( "iNet" )
local channel = iNet.open( 23, 255, "top" )
channel:setProctal( "Examples" ) --#see above note regarding this function and 1.2
local message, distance, sender = channel:receive()
print( "Recieved '" .. message .. "' from " .. sender .. ", " .. distance .. "m away" )
channel:send( "This will be ignored as it is on the wrong channel!" )
channel:reply( "Hello, " .. sender )
local message, distance, sender = channel:receive()
print( "Recieved '" .. message .. "' from " .. sender .. ", " .. distance .. "m away" )
Edited on 21 August 2014 - 02:12 AM
skwerlman #2
Posted 09 August 2014 - 06:41 AM
Neat API!
Could you add a B64 converter to avoid event corruption?
Edited on 09 August 2014 - 04:41 AM
KingofGamesYami #3
Posted 10 August 2014 - 08:25 PM
You could set B64 as the encrypt/decryption yourself. Example:

os.loadAPI( "iNet" )
local channel = iNet.open( 255, 256, "top" )
local function enc(data) --#found on google
	return ((data:gsub('.', function(x)
		local r,b='',x:byte()
		for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
		return r;
	end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
		if (#x < 6) then return '' end
		local c=0
		for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
		return b:sub(c+1,c+1)
	end)..({ '', '==', '=' })[#data%3+1])
end
channel:setEncryption( enc )

--#and on recieving;
--#bla bla bla set up apis &amp; such...

local function dec(data) --#also from google
	data = string.gsub(data, '[^'..b..'=]', '')
	return (data:gsub('.', function(x)
		if (x == '=') then return '' end
		local r,f='',(b:find(x)-1)
		for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
		return r;
	end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
		if (#x ~= 8) then return '' end
		local c=0
		for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
		return string.char(c)
	end))
end
channel:setDecryption( dec )

The B64 converter code I found here: http://lua-users.org...i/BaseSixtyFour
Edited on 10 August 2014 - 06:25 PM
KingofGamesYami #4
Posted 14 August 2014 - 02:19 AM
*bump* (added poll)
skwerlman #5
Posted 15 August 2014 - 12:02 PM
Regarding the poll: I think it should always convert, but also have an optional flag to prevent conversion if user so chooses.
SquidDev #6
Posted 19 August 2014 - 08:12 AM
I like it, rednet always bugs me so I will look into this. Just on a side note, encrypt and decrypt should probably pass the iNet connection object as a second argument - so you can implement 'handshakes' or whatever (Read Diffie-Hellman).

To be totally pedantic: Is protocol not spelt with an o?
Edited on 21 August 2014 - 05:51 AM
KingofGamesYami #7
Posted 19 August 2014 - 01:22 PM
I like it, rednet always bugs me so I will look into this. Just on a side note, encrypt and decrypt should probably pass the iNet connection object as a second argument - so you can implement 'handshakes' or whatever (Read Diffie-Hellman).
I have no idea what you just said, but ok.
To be totaly pedantic: Is protocol not spelt with an o?
Yes, yes it is… *facepalm* I'll fix the code later.
KingofGamesYami #8
Posted 19 August 2014 - 11:34 PM
iNet 1.2 (beta) released! I don't really have time to test it in game (since I don't have peripherals on the emulator I use), thus if you find any bugs/errors please post.

pastebin get vuzF5t8p

changelogAdded feature: Convert to b64 (not togglable)
Added feature: Now gives the object as a second argument to the encrypt and decrypt functions
Added feature: A fourth return has been added to iNet.waitForAny, the position of the object that returned.
Fix: Proctal changed to Proctol (blame SquidDev
Fix: Proctol changed to protocol (finally figured out the correct spelling)
Fix: Messages with proctol can no longer be received by computers with a nil or false proctol.
Fix: channel &amp; replyChannel cannot be the same
Edited on 21 August 2014 - 02:13 AM
skwerlman #9
Posted 21 August 2014 - 03:13 AM
To be totaly pedantic: Is protocol not spelt with an o?
To be totally pedantic, is totally not spelt with two l's?
KingofGamesYami #10
Posted 21 August 2014 - 04:14 AM
To be totaly pedantic: Is protocol not spelt with an o?
To be totally pedantic, is totally not spelt with two l's?
Argue with the official wiki: link
Edited on 23 August 2014 - 11:01 PM
SquidDev #11
Posted 21 August 2014 - 07:51 AM
To be totally pedantic: Is protocol not spelt with an o?
To be totally pedantic, is totally not spelt with two l's?
I don't see anything wrong. :P/>
DigitalMisha #12
Posted 20 November 2014 - 03:25 PM
inet:153: Expected string
when running inet:open function. Why? My code is:

local channel = inet:open(255, 256, "top")
KingofGamesYami #13
Posted 20 November 2014 - 04:04 PM
You are using incorrect syntax.


inet:open(255, 256, "top")
actually gives my code this:

inet.open( inet, 255, 256, "top" )

Instead of using the colin ( : ), use the period (.). the colin is reserved for object methods only.