本文整理汇总了Python中panda3d.core.QueuedConnectionReader.isConnectionOk方法的典型用法代码示例。如果您正苦于以下问题:Python QueuedConnectionReader.isConnectionOk方法的具体用法?Python QueuedConnectionReader.isConnectionOk怎么用?Python QueuedConnectionReader.isConnectionOk使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类panda3d.core.QueuedConnectionReader
的用法示例。
在下文中一共展示了QueuedConnectionReader.isConnectionOk方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from panda3d.core import QueuedConnectionReader [as 别名]
# 或者: from panda3d.core.QueuedConnectionReader import isConnectionOk [as 别名]
class ConnectionManager:
def __init__(self, main):
self.main = main
self.cManager = QueuedConnectionManager()
self.cListener = QueuedConnectionListener(self.cManager, 0)
self.cReader = QueuedConnectionReader(self.cManager, 0)
self.cWriter = ConnectionWriter(self.cManager, 0)
self.rqTable = ServerRequestTable()
self.rsTable = ServerResponseTable()
self.connection = None
def startConnection(self):
"""Create a connection with the remote host.
If a connection can be created, create a task with a sort value of -39
to read packets from the socket.
"""
if self.connection == None:
self.connection = self.cManager.openTCPClientConnection(Constants.SERVER_IP, Constants.SERVER_PORT, 1000)
if self.connection:
self.cReader.addConnection(self.connection)
return True
return False
def initTasks(self):
"""Initialize tasks to check on connection and send heartbeat.
This must be done here because in `Main` we do not initialize Panda3D
until after a connection is started. Thus, `taskMgr` is not defined
when `startConnection()` is called. We rely on the callee to also run
`initTasks()` after a successful connection is created.
"""
if self.connection != None:
taskMgr.add(self.updateRoutine, "updateRoutine-Connection")
taskMgr.doMethodLater(5, self.checkConnection, "checkConnection")
taskMgr.doMethodLater(1.0 / Constants.TICKRATE, self.sendHeartbeat, "sendHeartbeat")
def closeConnection(self):
"""Close the current connection with the remote host.
If an existing connection is found, remove both the Main task, which
is responsible for the heartbeat, and the Connection task, which is
responsible for reading packets from the socket, then properly close
the existing connection.
"""
if self.connection != None:
taskMgr.remove("updateRoutine-Connection")
taskMgr.remove("checkConnection")
self.cManager.closeConnection(self.connection)
self.connection = None
def sendRequest(self, requestCode, args={}):
"""Prepare a request packet to be sent.
If the following request code exists, create an instance of this
specific request using any extra arguments, then properly send it to
the remote host.
"""
if self.connection != None:
request = self.rqTable.get(requestCode)
if request != None:
request.set(self.cWriter, self.connection)
request.send(args)
def handleResponse(self, responseCode, data):
"""Prepare a response packet to be processed.
If the following response code exists, create an instance of this
specific response using its data to be executed.
"""
response = self.rsTable.get(responseCode)
if response != None:
response.set(self.main)
response.execute(data)
def checkConnection(self, task):
if not self.cReader.isConnectionOk(self.connection):
self.closeConnection()
self.showDisconnected(0)
return task.done
return task.again
def updateRoutine(self, task):
"""A once-per-frame task used to read packets from the socket."""
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from panda3d.core import QueuedConnectionReader [as 别名]
# 或者: from panda3d.core.QueuedConnectionReader import isConnectionOk [as 别名]
#.........这里部分代码省略.........
except:
pass
self.loginHandler()
#send packet
myPyDatagram = PyDatagram()
myPyDatagram.addUint8(1)
myPyDatagram.addString(self.username)
myPyDatagram.addString('_')
myPyDatagram.addString(self.password)
self.cWriter.send(myPyDatagram,self.connection)
#receive packet
datagram = NetDatagram()
if self.cReader.getData(datagram):
myProcessDataFunction(datagram)
return False
def loginHandler(self):
self.username = raw_input("Enter username: ")
self.password = raw_input("Enter password: ")
def myProcessDataFunction(netDatagram):
myIterator = PyDatagramIterator(netDatagram)
msgID = myIterator.getUnit8()
if msgID == 1:
msgToPrint = myIterator.getString()
print msgToPrint
def closeConnection(self):
if self.connection != None:
taskMgr.remove('updateRoutine-Main')
taskMgr.remove('updateRoutine-Connection')
taskMgr.remove('checkConnection')
self.cManager.closeConnection(self.connection)
self.connection = None
def sendRequest(self, requestCode, args = {}):
if self.connection != None:
request = ServerRequestTable.get(requestCode)
if request != None:
request.set(self.cWriter, self.connection)
request.send(args)
def handleResponse(self, responseCode, data):
#Prepare a response packet to be processed.
#If the following response code exists, create an instance of this
#specific response using its data to be executed.
response = ServerResponseTable.get(responseCode)
if response != None:
response.set(main)
response.execute(data)
def checkConnection(self, task):
if not self.cReader.isConnectionOk(self.connection):
self.closeConnection()
self.showDisconnected(0)
return task.done
return task.again
def showDisconnected(self, status):
if status == 0:
result = DatabaseHelper.dbSelectRowByID('msg', 'msg_id', 43)
main.createMessageBox(0, result['msg_text'], main.switchEnvironment, ['Login'])
elif status == 1:
result = DatabaseHelper.dbSelectRowByID('msg', 'msg_id', 44)
main.createMessageBox(0, result['msg_text'], main.switchEnvironment, ['Login'])
def updateRoutine(self, task):
#A once-per-frame task used to read packets from the socket.
while self.cReader.dataAvailable():
# Create a datagram to store all necessary data.
datagram = NetDatagram()
# Retrieve the contents of the datagram.
if self.cReader.getData(datagram):
# Prepare the datagram to be iterated.
data = PyDatagramIterator(datagram)
# Retrieve a "short" that contains the response code.
responseCode = data.getUint16()
# Pass into another method to execute the response.
if responseCode != 0:
self.handleResponse(responseCode, data)
return task.cont
示例3: __init__
# 需要导入模块: from panda3d.core import QueuedConnectionReader [as 别名]
# 或者: from panda3d.core.QueuedConnectionReader import isConnectionOk [as 别名]
class ConnectionManager:
def __init__(self, main):
#self.world = world
self.cManager = QueuedConnectionManager()
self.cListener = QueuedConnectionListener(self.cManager, 0)
self.cReader = QueuedConnectionReader(self.cManager, 0)
self.cWriter = ConnectionWriter(self.cManager, 0)
self.main = main
self.rqTable = ServerRequestTable()
self.rsTable = ServerResponseTable()
self.connection = None
def startConnection(self):
"""Create a connection with the remote host.
If a connection can be created, create a task with a sort value of -39
to read packets from the socket.
"""
try:
if self.connection == None:
self.connection = self.cManager.openTCPClientConnection(Constants.SERVER_IP,
Constants.SERVER_PORT,
1000)
if self.connection:
self.cReader.addConnection(self.connection)
taskMgr.add(self.updateRoutine, 'updateRoutine-Connection', -39)
taskMgr.doMethodLater(5, self.checkConnection, 'checkConnection')
return True
except:
pass
return False
def closeConnection(self):
"""Close the current connection with the remote host.
If an existing connection is found, remove both the Main task, which
is responsible for the heartbeat, and the Connection task, which is
responsible for reading packets from the socket, then properly close
the existing connection.
"""
if self.connection != None:
taskMgr.remove('updateRoutine-Main')
taskMgr.remove('updateRoutine-Connection')
taskMgr.remove('checkConnection')
self.cManager.closeConnection(self.connection)
self.connection = None
def sendRequest(self, requestCode, args = {}):
"""Prepare a request packet to be sent.
If the following request code exists, create an instance of this
specific request using any extra arguments, then properly send it to
the remote host.
"""
if self.connection != None:
request = self.rqTable.get(requestCode)
if request != None:
request.set(self.cWriter, self.connection)
#print('requestCode:'+str(requestCode))
#print('args:'+str(args))
request.send(args)
def handleResponse(self, responseCode, data):
"""Prepare a response packet to be processed.
If the following response code exists, create an instance of this
specific response using its data to be executed.
"""
response = self.rsTable.get(responseCode)
if response != None:
response.set(self.main)
response.execute(data)
def checkConnection(self, task):
if not self.cReader.isConnectionOk(self.connection):
self.closeConnection()
#self.showDisconnected(0)
return task.done
return task.again
def updateRoutine(self, task):
"""A once-per-frame task used to read packets from the socket."""
while self.cReader.dataAvailable():
#.........这里部分代码省略.........
示例4: login
# 需要导入模块: from panda3d.core import QueuedConnectionReader [as 别名]
# 或者: from panda3d.core.QueuedConnectionReader import isConnectionOk [as 别名]
#.........这里部分代码省略.........
self.getInt(data)
elif responseCode == 201: #login code
if (self.getString(data)=="Unsuccessful login"): #appears if login unsuccessful
print " "
print "Unsuccessful login"
print " "
self.createLoginWindow()
else:
print "Your are logged in" #appear if login successful
self.createLoginWindow()#appears the login options
self.showRalph()
elif responseCode == 203: #register code
if (self.getString(data)=="Registration successful"): #appear if registration was successful
print "You are now registered"
print "Please login" #user must login
print " "
self.createLoginWindow()
else:
print " "#appear if registration wasn't successful
print "Registration was unsuccessful. Pick a different username and please try again "
print " "
self.createLoginWindow()#user must attempt to register again
elif responseCode == 4:
self.getFloat(data)
else:
print "nothing found"
def updateRoutine(self,task):
self.check()
return task.again;
def checkConnection(self, task):
if not self.cReader.isConnectionOk(self.connection):
self.closeConnection()
self.showDisconnected(0)
return task.done
return task.again
def closeConnection(self):
"""Close the current connection with the remote host.
If an existing connection is found, remove both the Main task, which
is responsible for the heartbeat, and the Connection task, which is
responsible for reading packets from the socket, then properly close
the existing connection.
"""
if self.connection != None:
taskMgr.remove('updateRoutine-Main')
taskMgr.remove('updateRoutine-Connection')
taskMgr.remove('checkConnection')
self.cManager.closeConnection(self.connection)
self.connection = None
#function that unpackage the message from server
def getString(self, data):
msg = data.getString()
return msg
#package login request
def loginRequest(self, login_info):
pkg = PyDatagram()