本文整理汇总了Python中panda3d.core.QueuedConnectionManager.closeConnection方法的典型用法代码示例。如果您正苦于以下问题:Python QueuedConnectionManager.closeConnection方法的具体用法?Python QueuedConnectionManager.closeConnection怎么用?Python QueuedConnectionManager.closeConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类panda3d.core.QueuedConnectionManager
的用法示例。
在下文中一共展示了QueuedConnectionManager.closeConnection方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from panda3d.core import QueuedConnectionManager [as 别名]
# 或者: from panda3d.core.QueuedConnectionManager import closeConnection [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 QueuedConnectionManager [as 别名]
# 或者: from panda3d.core.QueuedConnectionManager import closeConnection [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():
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from panda3d.core import QueuedConnectionManager [as 别名]
# 或者: from panda3d.core.QueuedConnectionManager import closeConnection [as 别名]
class NetworkManager:
def __init__(self):
print "Network Manager Started"
def connection_open(self):
self.cManager = QueuedConnectionManager()
self.cReader = QueuedConnectionReader(self.cManager, 0)
self.cWriter = ConnectionWriter(self.cManager,0)
self.activeConnections=[] # We'll want to keep track of these later
self.cListener = QueuedConnectionListener(self.cManager, 0)
port_address=9099 #No-other TCP/IP services are using this port
backlog=1000 #If we ignore 1,000 connection attempts, something is wrong!
self.tcpSocket = self.cManager.openTCPServerRendezvous(port_address,backlog)
self.cListener.addConnection(self.tcpSocket)
print "Network Connection Opened"
taskMgr.add(self.tskListenerPolling,"Poll the connection listener",-39)
taskMgr.add(self.tskReaderPolling,"Poll the connection reader",-40)
def connection_close(self):
for aClient in self.activeConnections:
self.cReader.removeConnection(aClient)
self.activeConnections=[]
# close down our listener
self.cManager.closeConnection(self.tcpSocket)
print "Network Connection Closed"
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
return Task.cont
def tskReaderPolling(self, taskdata):
if self.cReader.dataAvailable():
datagram=NetDatagram() # catch the incoming data in this instance
# Check the return value; if we were threaded, someone else could have
# snagged this data before we did
if self.cReader.getData(datagram):
if base.client == True:
self.client_processing(datagram)
else:
self.server_processing(datagram)
return Task.cont
def server_messager(self,msg,args=[]):
if msg == "map_set":
order = PyDatagram()
order.addUint16(MAP_SET)
order.addInt32(args[0])
self.send_package(order)
elif msg == "client_update":
order = PyDatagram()
order.addUint16(CLIENT_INIT_UPDATE)
order.addString(args[0])
order.addString(args[1])
order.addInt32(args[2])
order.addInt32(args[3])
self.send_package(order)
elif msg == "chat_send":
r = args[0][0]
g = args[0][1]
b = args[0][2]
order = PyDatagram()
order.addUint16(SERVER_CHAT)
order.addInt32(r)
order.addInt32(g)
order.addInt32(b)
order.addString(args[1])
self.send_package(order)
base.menu_manager.menus["mp-game"].chat_add((r,g,b,1),args[1])
elif msg == "ready_button":
order = PyDatagram()
order.addUint16(SERVER_READY)
order.addInt32(args[0])
order.addInt32(args[1])
self.send_package(order)
base.menu_manager.menus["mp-game"].obj_list[args[0]]["indicatorValue"]=args[1]
base.menu_manager.menus["mp-game"].start_game_check()
elif msg == "server_loaded":
order = PyDatagram()
order.addUint16(SERVER_LOADED)
self.send_package(order)
elif msg == "all_loaded":
order = PyDatagram()
order.addUint16(ALL_LOADED)
self.send_package(order)
elif msg == "game_start":
order = PyDatagram()
order.addUint16(GAME_START)
self.send_package(order)
elif msg == "army_kill":
order = PyDatagram()
#.........这里部分代码省略.........
示例4: __init__
# 需要导入模块: from panda3d.core import QueuedConnectionManager [as 别名]
# 或者: from panda3d.core.QueuedConnectionManager import closeConnection [as 别名]
class client:
def __init__(self):
self.cManager = QueuedConnectionManager()
self.cListener = QueuedConnectionListener(self.cManager, 0)
self.cReader = QueuedConnectionReader(self.cManager, 0)
self.cWriter = ConnectionWriter(self.cManager, 0)
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('localhost',
9090,
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
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:
#.........这里部分代码省略.........
示例5: login
# 需要导入模块: from panda3d.core import QueuedConnectionManager [as 别名]
# 或者: from panda3d.core.QueuedConnectionManager import closeConnection [as 别名]
#.........这里部分代码省略.........
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()
pkg.addUint16(101)