I’ve got my code to the point where I’m shifting the connection data into a mysql database, but I’ve run into something interesting:
When I connect to the server I get 00A4EFB0 as the object memory location, but when I disconnect the server thinks my object was at 00A4ED88, which totally buggers the code I was trying to put in that would track a bunch of things in the database based on the user’s connection object.
When a user connects it adds their connection object, ip address, and connection time to a table called client_sessions:
In the image above you can see that it recorded my connection. So now I’m trying to delete that row when I disconnect, but the server thinks I have a different connection object when disconnecting which makes this impossible.
Here’s the relevant codebits:
Listening for client connections
def tskListenerPolling(self, taskdata):
if self.cListener.newConnectionAvailable():
rendezvous = PointerToConnection()
netAddress = NetAddress()
newConnection = PointerToConnection()
if self.cListener.getNewConnection(rendezvous,netAddress,newConnection):
newConnection = newConnection.p()
self.activeConnections.append(newConnection) # Remember connection
self.cReader.addConnection(newConnection) # Begin reading connection
print str(newConnection) + " connected from " + str(newConnection.getAddress())
self.activeConnectionsCounter += 1
query = """INSERT INTO client_sessions(ipaddress, connection) VALUES ('"""
query += str(newConnection.getAddress()) + """', '"""
query += str(newConnection) + """')"""
self.mysqlInsertQuery(query)
return Task.cont
Disconnect code:
def taskCheckConnections(self, task):
if(self.cManager.resetConnectionAvailable()):
connectionPointer = PointerToConnection()
self.cManager.getResetConnection(connectionPointer)
lostConnection = connectionPointer.p()
print str(lostConnection) + " disconnected"
query = "DELETE FROM `client_sessions` WHERE (`connection`='" + str(lostConnection) + "')"
print "\n" + str(query)
self.mysqlDeleteQuery(query)
self.cManager.closeConnection(lostConnection)
self.activeConnections.remove(lostConnection)
self.activeConnectionsCounter -= 1
#print "Current connections: " + str(self.activeConnections)
for i in self.activeConnections:
print i
return Task.again
The mysqlInsertQuery and mysqlDeleteQuery functions do exactly as they say. Essentially they both execute a sql query and then commit it. I’ve tested both functions and they work with no issue.
The output of the print statements in these two tasks is shown in the first screenshot at the top of this post.
Any ideas on how to correct this so I can actually delete the connection out of the database?
Basically what I’m trying to do is have some kind of identifier on a given user, that I can use for various database functions and delete it when they disconnect. I can’t seem to do that with the connection object, so I need a way of linking a connection object to something like an integer.
I can’t use their IP address because they need to be able to establish multiple connections from the same IP address.