本文整理汇总了Python中panda3d.core.QueuedConnectionManager.openTCPServerRendezvous方法的典型用法代码示例。如果您正苦于以下问题:Python QueuedConnectionManager.openTCPServerRendezvous方法的具体用法?Python QueuedConnectionManager.openTCPServerRendezvous怎么用?Python QueuedConnectionManager.openTCPServerRendezvous使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类panda3d.core.QueuedConnectionManager
的用法示例。
在下文中一共展示了QueuedConnectionManager.openTCPServerRendezvous方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TCP
# 需要导入模块: from panda3d.core import QueuedConnectionManager [as 别名]
# 或者: from panda3d.core.QueuedConnectionManager import openTCPServerRendezvous [as 别名]
class TCP():
def __init__(self, _core):
print ("TCP Protocol Startup...")
self.core = _core
self.config = self.core.server.config
def start(self):
self.setupTCP()
self.startTCPTasks()
def setupTCP(self):
self.tcpManager = QueuedConnectionManager()
self.tcpReader = QueuedConnectionReader(self.tcpManager, 0)
self.tcpWriter = ConnectionWriter(self.tcpManager, 0)
self.tcpListener = QueuedConnectionListener(self.tcpManager, 0)
self.tcpSocket = self.tcpManager.openTCPServerRendezvous(self.config.TCPPORT, self.config.BACKLOG)
self.tcpListener.addConnection(self.tcpSocket)
print ("Started Server on: ", self.config.HOSTNAME, self.config.TCPPORT)
def startTCPTasks(self):
taskMgr.add(self.tcpListenerTask, "tcpListenerTask", 0)
print ("TCP Listener Started")
taskMgr.add(self.tcpReaderTask, "tcpReaderTask", -10)
print ("TCP Reader Started")
taskMgr.add(self.tcpDisconnectionHandler, "tcpDisconnectionHandler", 20)
print ("TCP Disconnection Handler Started")
# TCP Listener Task
def tcpListenerTask(self, task):
"""
Accept new incoming connection from clients, related to TCP
"""
# Handle new connection
if self.tcpListener.newConnectionAvailable():
rendezvous = PointerToConnection()
netAddress = NetAddress()
newConnection = PointerToConnection()
if self.tcpListener.getNewConnection(rendezvous, netAddress, newConnection):
newConnection = newConnection.p()
# Tell the reader about the new TCP connection
self.tcpReader.addConnection(newConnection)
# Handle the connection depending on persistent or not
if self.core.server.isPersistent:
self.core.handleConnection(generateUUID(), newConnection, netAddress)
else:
self.core.createPlayerObject(generateUUID(),newConnection, netAddress)
print ("Server: New Connection from -", str(netAddress.getIpString()))
else:
print ("Server: Connection Failed from -", str(netAddress.getIpString()))
return Task.cont
def tcpReaderTask(self, task):
"""
Handle any data from clients by sending it to the Handlers.
"""
while 1:
(datagram, data, opcode) = self.tcpNonBlockingRead(self.tcpReader)
if opcode is MSG_NONE:
# Do nothing or use it as some 'keep_alive' thing.
break
else:
# Handle it
self.core.packetManager.handlePacket(opcode, data, datagram.getAddress())
return Task.cont
# TCP NonBlockingRead??
def tcpNonBlockingRead(self, qcr):
"""
Return a datagram collection and type if data is available on
the queued connection udpReader
"""
if self.tcpReader.dataAvailable():
datagram = NetDatagram()
if self.tcpReader.getData(datagram):
data = DatagramIterator(datagram)
opcode = data.getUint8()
else:
data = None
opcode = MSG_NONE
else:
datagram = None
data = None
opcode = MSG_NONE
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from panda3d.core import QueuedConnectionManager [as 别名]
# 或者: from panda3d.core.QueuedConnectionManager import openTCPServerRendezvous [as 别名]
class Server:
def __init__(self):
self.activeConnections = [] # lists all connections
self.players = {} # keys are the players logins, values are the players datagram connections
self.parties = {} # keys are the parties names, values are dicts representing parties data
self.sessions = {} # keys are the datagram connections, values are dicts storing the characters of the player and its party
self.playersinlobby = [] # lists players in the party screen
self.charid = 0 # used for random team generation
self.chars = [] # lists of dicts representing characters data
self.cManager = QueuedConnectionManager()
self.cListener = QueuedConnectionListener(self.cManager, 0)
self.cReader = QueuedConnectionReader(self.cManager, 0)
self.cReader.setTcpHeaderSize(4)
self.send = Send(self.cManager)
port = 3001
if len(sys.argv) > 1:
port = sys.argv[1]
self.tcpSocket = self.cManager.openTCPServerRendezvous(port, 10)
self.cListener.addConnection(self.tcpSocket)
print("Server listening on port", port)
taskMgr.add(self.tskListenerPolling, "Poll the connection listener", -39)
taskMgr.add(self.tskReaderPolling, "Poll the connection reader", -40)
def processData(self, datagram):
iterator = PyDatagramIterator(datagram)
source = datagram.getConnection()
callback = iterator.getString()
getattr(globals()[callback], 'execute')(self, iterator, source)
def updateAllPartyLists(self):
parties = deepcopy(self.parties)
for party in list(parties.values()):
del party['map']['tiles']
for player in self.playersinlobby:
self.send.UPDATE_PARTY_LIST(parties, player)
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)
self.cReader.addConnection(newConnection)
print('A new client is connected', newConnection)
return Task.cont
def tskReaderPolling(self, taskdata):
if self.cReader.dataAvailable():
datagram=NetDatagram()
if self.cReader.getData(datagram):
self.processData(datagram)
return Task.cont
示例3: __init__
# 需要导入模块: from panda3d.core import QueuedConnectionManager [as 别名]
# 或者: from panda3d.core.QueuedConnectionManager import openTCPServerRendezvous [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: SocketTCP
# 需要导入模块: from panda3d.core import QueuedConnectionManager [as 别名]
# 或者: from panda3d.core.QueuedConnectionManager import openTCPServerRendezvous [as 别名]
class SocketTCP(ShowBase):
def __init__(self, _parent=None):
ShowBase.__init__(self, windowType = 'none')
print ("TCP Protocol Startup...")
#self.parent = _parent
#self.config = self.parent.server.config
# tmp config
self.tcpport = 6000
self.backlog = 10
self.hostname = '127.0.0.1'
self.sendPacketQueue = None
def startAll(self):
self.setupTCP()
self.startTCPTasks()
def setupTCP(self):
self.tcpManager = QueuedConnectionManager()
self.tcpReader = QueuedConnectionReader(self.tcpManager, 0)
self.tcpWriter = ConnectionWriter(self.tcpManager, 0)
self.tcpListener = QueuedConnectionListener(self.tcpManager, 0)
self.tcpSocket = self.tcpManager.openTCPServerRendezvous(self.tcpport, self.backlog)
self.tcpListener.addConnection(self.tcpSocket)
print ("Started Server on: ", self.hostname, self.tcpport)
def startTCPTasks(self):
taskMgr.add(self.tcpListenerTask, "tcpListenerTask", 0)
print ("TCP Listener Started")
taskMgr.add(self.tcpReaderTask, "tcpReaderTask", -10)
print ("TCP Reader Started")
taskMgr.add(self.tcpDisconnectionHandler, "tcpDisconnectionHandler", 20)
print ("TCP Disconnection Handler Started")
taskMgr.add(self.tcpQueuedSendHandlerTask, "tcpQueuedSendhandlerTask", -11)
print ("TCP Queued Send Handler Started")
# TCP Listener Task
def tcpListenerTask(self, task):
"""
Accept new incoming connection from clients, related to TCP
"""
# Handle new connection
if self.tcpListener.newConnectionAvailable():
rendezvous = PointerToConnection()
netAddress = NetAddress()
newConnection = PointerToConnection()
if self.tcpListener.getNewConnection(rendezvous, netAddress, newConnection):
newConnection = newConnection.p()
# Tell the reader about the new TCP connection
self.tcpReader.addConnection(newConnection)
#self.core.createPlayerObject(generateUUID(),newConnection, netAddress)
print ("Server: " + str(generateUUID), str(netAddress.getIpString()))
else:
print ("Server: Connection Failed from -", str(netAddress.getIpString()))
return Task.cont
def tcpReaderTask(self, task):
"""
Handle any data from clients by sending it to the Handlers.
"""
while 1:
(datagram, data, opcode) = self.tcpNonBlockingRead(self.tcpReader)
if opcode is MSG_NONE:
# Do nothing or use it as some 'keep_alive' thing.
break
else:
# Handle it
#self.core.packetManager.handlePacket(opcode, data, datagram.getAddress())
print("Set Packet Handler to handle packets!")
return Task.cont
# TCP NonBlockingRead??
def tcpNonBlockingRead(self, qcr):
"""
Return a datagram collection and type if data is available on
the queued connection udpReader
"""
if self.tcpReader.dataAvailable():
datagram = NetDatagram()
if self.tcpReader.getData(datagram):
data = DatagramIterator(datagram)
opcode = data.getUint8()
else:
data = None
#.........这里部分代码省略.........