From: <Blu...@us...> - 2009-12-18 19:32:28
|
Revision: 325 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=325&view=rev Author: BlueWolf_ Date: 2009-12-18 19:32:18 +0000 (Fri, 18 Dec 2009) Log Message: ----------- You can now close the server with server.close. This will disconnect all users after sending the reason Modified Paths: -------------- trunk/server/core/server.py Modified: trunk/server/core/server.py =================================================================== --- trunk/server/core/server.py 2009-12-18 19:07:07 UTC (rev 324) +++ trunk/server/core/server.py 2009-12-18 19:32:18 UTC (rev 325) @@ -130,7 +130,10 @@ #Infinite loop that will wait for incomming connections while 1: - sock, addr = self.__sock.accept() + try: sock, addr = self.__sock.accept() + except: + # Server has gone offline + return if self.__config['max_connections'] and \ len(self.clients) >= \ @@ -173,9 +176,26 @@ continue self.clients[cid]['con'].start() - + + def exit(self, reason = "gone offline"): + """ + Will shutdown the server and sends an 'gone offline' message to + all connected clients and then closes their connection. Use this + when you're about to shutdown your server. + This will call the callback.connection_closed for all clients! + Note that you'll not be able to start the server again + """ + try: self.__sock.shutdown(0) + except: pass + self.__sock.close() + self.__sock = None + for client in self.clients.values(): + client['con'].close_msg(reason) + + + class Client(threading.Thread): """ This class manages the socket for the connection to clients. @@ -262,15 +282,12 @@ self.__parser_crash() # Kick connection - self.send("disconnect", - {"reason":"crash"}) - self.close("crash") + self.close_msg("crash") def close(self, reason = "manual"): """ Closes the connection for this client and kills the socket. """ - if self.is_online == False: return self.is_online = False @@ -281,7 +298,26 @@ self.__call.connection_closed(self.__cid, reason) del self.__clients[self.__cid] + + def close_msg(self, reason): + """ + Same as Client.close, but this will send the reason to the + client before closing + """ + if self.is_online == False: return + self.is_online = False + + self.send("disconnect", {"reason": reason}) + + try: self.__sock.shutdown(0); + except: pass + self.__sock.close() + + self.__call.connection_closed(self.__cid, reason) + del self.__clients[self.__cid] + + def send(self, data_header, data_body = {}): """ Sends `data_body` of type `data_header` to the client. It will This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |