0

I'm creating a game in the Blender Game Engine. And I have coded an IRC script which works fine on OS X and Linux distros. The output is similar to this:

Logging in...
LOGIN_ERROR
LOGIN_ERROR
LOGIN_ERROR
LOGIN_ERROR
LOGIN_ERROR
LOGIN_ERROR
<name> has joined.
Logged in!

And then I can call my sendmsg() function to send messages to the IRC channel.

This is the error I get when I try to run on Windows 7: stack overflow python sockets error

My python IRC code: http://pastebin.com/aG6TwTir

Ignore the "bge" references. Those variables and such are filled from the game engine.

In the game engine, I call login() once, and it spits out "LOGIN_ERROR" so I know it's trying to connect, and then it will connect, therefore not throwing an exception and ending the function.

In OS X and Linux, it runs perfectly and seemlessly in the background while the player can continue to play as it connects.

In windows 7, it throws that error.

So I guess what needs to happen is a way to wait for the script to connect to the server. Then once connected, I can send the login information and join the channel.

So how do I wait for the connection?

FYI: I have the sockets non-blocking, since the script needs to run on the same thread as the game engine, on every frame. Main() is run every frame, not the whole script. At the menu, it executes the script and calls login(). Then once in the game, it will call Main() every frame. Oh and I'm using Python 3.3.

Any help is greatly apreciated! ^_^

EDIT: How do I handle this exception? irc exception

3
  • I assume this has nothing to do with sockets by the way. Commented Mar 6, 2014 at 19:48
  • Well if I can figure out when it has made a connection, then I can pause/block as it tries to connect. Then I can check for if it made the connection to the server, then send login information. ...I guess. Lol! Thanks for helping! Idk what to do =( Commented Mar 6, 2014 at 20:15
  • Solved! Thanks Erik Allik! ^_^ Commented Mar 6, 2014 at 21:44

2 Answers 2

1

This code:

def login():
    ...
    try:
        ...
    except:
        ...
        login()  #  <===

recursively calls itself; given a high enough number of login failures, depending on the stack size limit (which depends on platform I guess), you'll get a stack overflow.

See also: Setting stacksize in a python script

Although I would always just avoid recursion and use looping instead, unless I know in advance that the recursion depth will never be more than ~100:

while True:
    try:
        do_login()
    except:  # NOTE: USE A SPECIFIC EXCEPTION CLASS HERE, BTW
        continue
    else:
        break
Sign up to request clarification or add additional context in comments.

9 Comments

Oh ohkay I understand that issue now! Thanks ^_^ Though the only problem in doing a loop, is that it will block the thread that the game engine it running on until the loop ends.
@user3001105: recursion has the exact same semantics with regards to threading/blocking as looping; if you want non-blocking behavior, you need to either use a separate thread, or use something like gevent.
Ohkay so I have to use looping... or threading. But in both cases I need to have a way of knowing when the connection has been made (or failed) so that I can break the loop/thread to continue gameplay.
How do I check for the exception in the picture? That error does not come up in OSX or Linux. So if I could check for that exception, then it will work as I could handle how long it will block. (the picture is edited into the original question)
hmm, do you have any prior experience with non-blocking sockets or non-blocking IO in general? because by the things you're saying (e.g. "so I have to use looping or threading") I'd suggest trying to tackle a simpler problem first...
|
0

You have recursion happening in your error handling

def login():
    #print('login')
    # Bind the socket
    try:
        s.connect((HOST, PORT))
        # Send login info
        s.send(bytes('NICK %s\r\n' % NICK, 'UTF-8'))
        s.send(bytes('USER %s %s bla :%s\r\n' % (IDENT, HOST, REALNAME), 'UTF-8'))
        s.send(bytes('JOIN %s\r\n' % CHAN, 'UTF-8'));
        print('Logging in...')
        chatlog('Logging in...')
    except:
        print('LOGIN_ERROR')
        login()

So in your function login() you have a try, then in the except you call login() again. This will just loop over and over again if the login fails.

1 Comment

Yes, which is what I want: 'Loop' and try logging in, but without blocking the thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.