Skip to content

Latest commit

 

History

History
100 lines (71 loc) · 3.7 KB

networking.md

File metadata and controls

100 lines (71 loc) · 3.7 KB

Networking

Computers talk over wireless modems. Place one against a computer, or equip one on a turtle. A modem sends and hears messages on numbered channels. Rednet builds addressed messages on top of that, and GPS uses it to work out where a computer is.

Modems

A wireless modem reaches 64 blocks. A message gets through when either end is within its range of the other, in the same dimension.

Method
open(channel), close(channel), closeAll() Channels are 0 to 65535. A modem can have 128 open.
isOpen(channel)
transmit(channel, replyChannel, message) Sends message to every modem in range with channel open.
isWireless() Always true.

A message can be any value that isn't a function: numbers, strings, booleans, or tables of them. It arrives as a modem_message event:

local modem = peripheral.find("modem")
modem.open(42)
modem.transmit(42, 42, { hello = "world" })

local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")

distance is how far away the sender was, in blocks.

Rednet

Rednet sends messages to a computer by its ID, filters them by protocol, and drops duplicates. It's the same protocol as ComputerCraft's, and the Rust SDK speaks it too, so Lua and Rust programs can talk to each other.

rednet.open("top")                       -- the modem's side
rednet.send(7, "hello", "greetings")     -- to computer 7
rednet.broadcast({ x = 1 }, "status")    -- to everyone in range

local sender, message, protocol = rednet.receive("greetings", 5)   -- wait up to 5 seconds
Function
rednet.open(side), rednet.close([side]), rednet.isOpen([side])
rednet.send(id, message [, protocol])
rednet.broadcast(message [, protocol])
rednet.receive([protocol] [, timeout]) Returns the sender's ID, the message and its protocol, or nil after timeout.
rednet.host(protocol, hostname), rednet.unhost(protocol) Answer lookups for a protocol under a name.
rednet.lookup(protocol [, hostname]) The IDs of computers hosting protocol, or the one called hostname.

Rednet runs alongside the shell, so a rednet_message event arrives whenever a message does, whatever program is running.

Under the hood

A message to computer n goes out on channel n mod 65500. Broadcasts use channel 65535. Each message is a table with a random message ID, the sender, the recipient, the message and the protocol. Every message is also sent on channel 65533, for repeaters.

Repeaters

A computer running repeat passes on every rednet message it hears, so messages reach computers beyond the sender's range. Put repeaters where two areas overlap.

Chat

chat <name> is a chat room for every computer in range: type a line and press Enter.

GPS

GPS finds a computer's position from the distances to computers that know theirs.

Set up four hosts. Place four computers with wireless modems, spread out and at different heights, so they aren't all in one plane. On each, put this in startup.lua, with that computer's own coordinates (press F3 and look at the block to read them):

shell.run("gps", "host", 120, 72, -40)

Locate. Any computer or turtle with a modem within range of the hosts can then run:

> gps locate

or, in a program:

local x, y, z = gps.locate(2)    -- wait up to 2 seconds
if not x then print("No GPS fix") end

Three hosts give two possible positions; the fourth decides between them. With fewer than four in range, gps.locate can return nil. Turtles use GPS to navigate and report where they are; the fleet example does both.