--- /dev/null
+"""Socket objects for the MUFF Engine"""
+
+# Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>, all rights reserved.
+# Licensed per terms in the LICENSE file distributed with this software.
+
+# need socket.socket for new connection objects and the server's listener
+import socket
+
+# hack to load all modules in the muff package
+import muff
+import muffuniv
+for module in muff.__all__:
+ exec("import " + module)
+
+def check_for_connection(newsocket):
+ """Check for a waiting connection and return a new user object."""
+
+ # try to accept a new connection
+ try:
+ connection, address = newsocket.accept()
+ except:
+ return None
+
+ # note that we got one
+ muffmisc.log("Connection from " + address[0])
+
+ # disable blocking so we can proceed whether or not we can send/receive
+ connection.setblocking(0)
+
+ # create a new user object
+ user = muffuser.User()
+
+ # associate this connection with it
+ user.connection = connection
+
+ # set the user's ipa from the connection's ipa
+ user.address = address[0]
+
+ # return the new user object
+ return user
+
+def initialize_server_socket():
+ """Create and open the listening socket."""
+
+ # create a new ipv4 stream-type socket object
+ newsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+
+ # set the socket options to allow existing open ones to be reused
+ # (fixes a bug where the server can't bind for a minute when restarting
+ # on linux systems)
+ newsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+
+ # bind the socket to to our desired server ipa and port
+ newsocket.bind((muffuniv.universe.categories["internal"]["network"].get("host"), muffuniv.universe.categories["internal"]["network"].getint("port")))
+
+ # disable blocking so we can proceed whether or not we can send/receive
+ newsocket.setblocking(0)
+
+ # start listening on the socket
+ newsocket.listen(1)
+
+ # note that we're now ready for user connections
+ muffmisc.log("Waiting for connection(s)...")
+
+ # store this in a globally-accessible place
+ muffvars.newsocket = newsocket
+