Jump to content

[solved] Coroutine might be yielding early?

networking lua help

3 replies to this topic

#1 zxci

  • Members
  • 17 posts

Posted 27 November 2015 - 01:45 PM

I'm coding in my modemShark scanner functionality, and I've been stuck in the same place for two hours now, with zero progress made.

This code resides in a function(no args, for reasons). pMax, pMin, and modemSide are supplied externally. It uses pMin and pMax to send traffic over channels that are opened elsewhere in the program. For the sake of working with a normal CraftOS repeat program, traffic is being sent over 65533, so assume pMin and pMax are both 65533.

What ends up happening is that the program sends a packet. A repeater then sends the same packet back (twice, incidentally because the rednet's repeat() program wasn't designed to handle an incoming packet on 65533, headed for 65533 and not transmit it twice). I've verified with another computer that indeed the packets are being transmitted both ways. The program then SHOULD be waiting for a response, but I have no way of telling if it's actually doing that. All of the code up until:
local _, _, _, _, mMsg, _ = os.pullEvent("modem_message")
will execute flawlessly, and then it just dies. It definitely IS getting a response(two, infact) and they are on open modem channels, so I don't know what could be happening here.

Note that when I say it just dies, the program itself keeps running. The coroutine itself is what ceases to progress. It doesn't throw any errors, and any statements after that line of code above simply doesn't execute. However, the loop that created and resumed the coroutines will progress, and code after the loop is fine too.

Wat do?

Edited by zxci, 27 November 2015 - 03:27 PM.

#2 Lupus590

  • Members
  • 2,007 posts
  • LocationUK

Posted 27 November 2015 - 02:35 PM

rednet captures your modem_message before you get it and converts it. you should be waiting for a rednet_message

Edited by Lupus590, 27 November 2015 - 02:35 PM.

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 27 November 2015 - 02:40 PM

From bios.lua:


os.pullEvent("modem_message") is hence much the same as coroutine.yield("modem_message"). You never resume the coroutines with the data of a modem_message event, and so the function you built your coroutines out of will never progress past the line where it requested one. Frankly I can't see why you're attempting to use coroutines here are all (BB's first rule of coroutines: No matter what you might think, you probably don't need to use coroutines).

Here's an untested re-write; please provide further details on your end goals if it doesn't suit your purposes:


View PostLupus590, on 27 November 2015 - 02:35 PM, said:

rednet captures your modem_message before you get it and converts it. you should be waiting for a rednet_message

That in no way prevents you from capturing the modem_message event yourself, as the function that performs this task - rednet.run() - executes in a coroutine separate to the shell.

#4 zxci

  • Members
  • 17 posts

Posted 27 November 2015 - 02:49 PM

I'll give that a try after I get some sleep Bomb. That missing bracket was a result of me selecting too much when I was removing a print statement last-minute lol

My idea behind using coroutines (note: I am very fuzzy on coroutine knowledge) was that they would be able to run for indeterminate amounts of time without bogging down the program and then some part or another of my brain said "hey you can even use coroutines.yield() to pass flags!" and that led to me making a coroutine lol

Also, ya, Lupus I'm explicitly using modem_message to access the messageID. You can use os.pullEvent("modem_message") just fine and there are certain tasks only it can do as opposed to "rednet_message"


edit: I got impatient.
That worked the first try! It seems to do what I was trying to accomplish with my convoluted(I actually put a comment in there that I took out prior to posting that pointed out to myself how convoluted it was) method and it's easy on the eyes too LOL. I appreciate it.

Edited by zxci, 27 November 2015 - 03:26 PM.

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users