1 """Socket objects for the MUFF Engine"""
3 # Copyright (c) 2005 MUDpy, The Fungi <fungi@yuggoth.org>, all rights reserved.
4 # Licensed per terms in the LICENSE file distributed with this software.
6 # need socket.socket for new connection objects and the server's listener
9 # hack to load all modules in the muff package
12 for module in muff.__all__:
13 exec("import " + module)
15 def check_for_connection(newsocket):
16 """Check for a waiting connection and return a new user object."""
18 # try to accept a new connection
20 connection, address = newsocket.accept()
24 # note that we got one
25 muffmisc.log("Connection from " + address[0])
27 # disable blocking so we can proceed whether or not we can send/receive
28 connection.setblocking(0)
30 # create a new user object
31 user = muffuser.User()
33 # associate this connection with it
34 user.connection = connection
36 # set the user's ipa from the connection's ipa
37 user.address = address[0]
39 # return the new user object
42 def initialize_server_socket():
43 """Create and open the listening socket."""
45 # create a new ipv4 stream-type socket object
46 newsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
48 # set the socket options to allow existing open ones to be reused
49 # (fixes a bug where the server can't bind for a minute when restarting
51 newsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
53 # bind the socket to to our desired server ipa and port
54 newsocket.bind((muffuniv.universe.categories["internal"]["network"].get("host"), muffuniv.universe.categories["internal"]["network"].getint("port")))
56 # disable blocking so we can proceed whether or not we can send/receive
57 newsocket.setblocking(0)
59 # start listening on the socket
62 # note that we're now ready for user connections
63 muffmisc.log("Waiting for connection(s)...")
65 # store this in a globally-accessible place
66 muffvars.newsocket = newsocket