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

[Question] Does rednet usually cause slowdowns?

Started by Naoticon, 25 August 2012 - 07:39 PM
Naoticon #1
Posted 25 August 2012 - 09:39 PM
Hey guys, I've been programming for a little while now and I've run into some (what I think is) weird behavior. I'm writing a code that's basically a login server/credential manager. I'll post the code if needed but my only problem seems to be that somewhere between the proccess of the server recieving the login information and sending a response, my entire minecraft client (and my computer, for that matter) experiances massive slowdown. I haven't used rednet much before, so I don't know if the problem is Rednet, my computer's hardware (it's not an amazing computer, but I usually can get about 20 fps on Tekkit), or my script. Any help would be greatly appreciated!
immibis #2
Posted 26 August 2012 - 12:32 PM
Post the script, in case it is.
Naoticon #3
Posted 26 August 2012 - 03:11 PM
Alright, here is the script:
function authS()
 repeat
 rednet.open( ""..pSide )
 printC(1, "Waiting for login request...")
 local sID, sString, sWTF = rednet.receive()
 term.clearLine(1)
 printC(1, "Request recieved, proccessing...")
 rednet.close( ""..pSide )
 local tPasswd={}
 local file = fs.open("/accounts.lst","r")
 local sLine = file:readLine()
 while sLine do
  for k, v, a in string.gmatch(sLine, "(%w+)%s(%w+)%s(%w+)") do
   tPasswd[k]=v
  end
  sLine = file:readLine()
 end
 file:close()
 while sString do
  for usern, passw in string.gmatch(sString, "(%w+)%s(%w+)") do
  end
 end
 if (tPasswd[usern]==passw) then
  if a == 1 then
   rednet.send(sID, "1")
  elseif a == 2 then
   rednet.send(sID, "2")
  elseif a == 3 then
   rednet.send(sID, "3")
  elseif a == 4 then
   rednet.send(sID, "4")
  elseif a == 5 then
   rednet.send(sID, "5")
  else
   rednet.send(sID, "-1")
  end
 else
  rednet.send(sID, "0")
 end
 usern = nil
 passw = nil
 until auth == 1
end
It runs this function later on in the script.
TCGM #4
Posted 26 August 2012 - 10:22 PM
Try sticking an os.pullEvent(); at the start of each loop. Might solve your slowdown.
Naoticon #5
Posted 27 August 2012 - 02:15 AM
Try sticking an os.pullEvent(); at the start of each loop. Might solve your slowdown.
Ok forgive me I'm a little confused… I put os.pullEvent() one line before each while and if loop, that just made the computer freeze at those points until I pushed a key.
Xfel #6
Posted 27 August 2012 - 09:01 AM
You should rather use sleep(0) to cause a tick delay. But another question, how many computers with active modems do you have on your map? that could cause lag, too.
makerimages #7
Posted 27 August 2012 - 09:47 AM
Try sticking an os.pullEvent(); at the start of each loop. Might solve your slowdown.
Ok forgive me I'm a little confused… I put os.pullEvent() one line before each while and if loop, that just made the computer freeze at those points until I pushed a key.

THATS EXACTLY what should happen, the os.pullEvent() stops everything, and waits for something to happen-in this case your keypress, and then lets the program continue from where it was stopped

and also-do you have SPC? type /killall it kills all animals that could also cause lag
Naoticon #8
Posted 27 August 2012 - 10:58 PM
You should rather use sleep(0) to cause a tick delay. But another question, how many computers with active modems do you have on your map? that could cause lag, too.
Ok I will try this. And there is only that one… I have another that temporarily opens to send the login information though.
Try sticking an os.pullEvent(); at the start of each loop. Might solve your slowdown.
Ok forgive me I'm a little confused… I put os.pullEvent() one line before each while and if loop, that just made the computer freeze at those points until I pushed a key.

THATS EXACTLY what should happen, the os.pullEvent() stops everything, and waits for something to happen-in this case your keypress, and then lets the program continue from where it was stopped

and also-do you have SPC? type /killall it kills all animals that could also cause lag
Well I need it to run by itself, the purpose is to logon remotely so the credentials aren't stored on the client computer. But thanks for the idea! :D/>/> I don't have SPC, but I use the most minimal graphic settings.

*EDIT* adding os.sleep(0) in various locations doesn't seem to do anything.
TCGM #9
Posted 28 August 2012 - 12:33 AM
You should rather use sleep(0) to cause a tick delay. But another question, how many computers with active modems do you have on your map? that could cause lag, too.
Ok I will try this. And there is only that one… I have another that temporarily opens to send the login information though.
Try sticking an os.pullEvent(); at the start of each loop. Might solve your slowdown.
Ok forgive me I'm a little confused… I put os.pullEvent() one line before each while and if loop, that just made the computer freeze at those points until I pushed a key.

THATS EXACTLY what should happen, the os.pullEvent() stops everything, and waits for something to happen-in this case your keypress, and then lets the program continue from where it was stopped

and also-do you have SPC? type /killall it kills all animals that could also cause lag
Well I need it to run by itself, the purpose is to logon remotely so the credentials aren't stored on the client computer. But thanks for the idea! :D/>/> I don't have SPC, but I use the most minimal graphic settings.

os.pullEvent() halts the program and waits for an incoming event. These events can be a rednet message, a key stroke, a redstone update, or several other things. It returns the event type, and 6 parameters. For instance, a rednet_message event returns:

event type ("rednet_message"), param1 (id of sending computer), param2 (message), and 4 other params which are useless. You can capture these by putting
event,p1,p2,p3,p4,p5,p6 = os.pullEvent()
in the beginning of a loop. You can then check if the event was a rednet message like this:

if event == "rednet_message" then
code here
end

You can get the content of that message by examining p2.

So, instead of running the loop constantly, which we do NOT want, this will wait until it receives input of the rednet_message type. You should have this in EVERY networking interface application, and in fact, in anything using rednet. It is DESIGNED to stall the computer's program until it receives an event. This is good, as it stops lag.
makerimages #10
Posted 28 August 2012 - 03:07 PM
its noy os.sleep() but sleep()