attached this time ;) was Re: [ENet-discuss]pyenet: how to send data
from server to client.
Rene Dudfield
illumen at yahoo.com
Tue Jun 24 13:21:19 PDT 2003
oops, the test files are attached this time :)
-------------- next part --------------
"""
purpose: to test out the enet networking.
"""
import sys, time
import networking_enet
def client():
"""
"""
print "RUNNING: client"
apeer = networking_enet.Peer("127.0.0.1",
7779,
"",
"somenickclient")
en = networking_enet.EnetNet(is_server = 0)
en.connect(apeer)
en.update()
en.put("hi")
en.update()
#en.host.flush()
en.peer.disconnect()
en.update()
while en.isconnected:
time.sleep(0.5)
en.update()
def evil_client():
""" this causes the server to run out of peer slots.
"""
print "RUNNING: evil_client"
apeer = networking_enet.Peer("127.0.0.1",
7779,
"",
"somenickclient")
en = networking_enet.EnetNet(is_server = 0)
en.connect(apeer)
en.update()
en.put("hi")
en.update()
#en.host.flush()
en.peer.disconnect()
def bench_client():
""" this sends lots of messages.
"""
print "RUNNING: bench_client"
apeer = networking_enet.Peer("127.0.0.1",
7779,
"",
"somenickclient")
en = networking_enet.EnetNet(is_server = 0)
en.connect(apeer)
en.update()
t1 = time.time()
for xx in xrange(1000):
tt1 = time.time()
en.put("hi")
en.update()
tt2 = time.time()
print "time for 1 message:", tt2 - tt1
#en.host.flush()
time.sleep(1/60.)
t2 = time.time()
print "time for 1000 messages:", t2 - t1
#en.host.flush()
en.update()
en.peer.disconnect()
en.update()
while en.isconnected:
time.sleep(0.5)
en.update()
def server():
"""
"""
print "RUNNING: server"
apeer = networking_enet.Peer("127.0.0.1",
7779,
"",
"somenickserver")
en = networking_enet.EnetNet(is_server = 1)
en.connect(apeer)
while 1:
en.update()
a = en.get()
if a != None:
print a
#time.sleep(1/ 60.)
#en.host.flush()
if __name__ == "__main__":
if "server" in sys.argv:
server()
elif "client" in sys.argv:
client()
elif "evil_client" in sys.argv:
evil_client()
elif "bench_client" in sys.argv:
bench_client()
else:
print "use client, evil_client, bench_client, or server as args"
-------------- next part --------------
"""
file: networking_enet.py
purpose: to use enet for the networking.
"""
# Amount of time to block for. 20 is a pretty good time as its
# less than 1/60th of a second.
#
BLOCK_TIME = 10
#PEER_COUNT = 16
PEER_COUNT = 6
import enet
class NetworkError(IOError):
pass
class Peer:
def __init__(self, ip_address,
port,
hostname,
nick):
""" nick - name of player.
"""
self.ip_address = ip_address
self.port = port
self.hostname = hostname
self.nick = nick
class EnetNet:
def __init__(self, is_server):
"""
"""
self.is_server = is_server
self.events = []
self.isconnected = 0
def get_peer_list(self):
""" Returns a list of peers to play with.
"""
#TODO: make this get the list from:
# local subnet on network, internet
raise NotImplementedError
def connect(self, a_peer):
""" a_peer is a Peer object which to connect to.
Non blocking call.
"""
if self.is_server:
ip_address, port = a_peer.ip_address, a_peer.port
# (addr, port), peercount, incomingbandwidth, outgoingbandwidth
self.host = enet.host((ip_address, port),
PEER_COUNT,
56 * 1024 *1000,
14 * 1024 *1000)
else:
# peercount, incomingbandwidth, outgoingbandwidth
self.host = enet.host(1, 28 * 1024 *1000, 9 * 1024 *1000)
#peer = self.host.connect(('127.0.0.1', 5000), 2)
#TODO: see if the ip address is "" then lookup the hostnames ip.
ip_address, port = a_peer.ip_address, a_peer.port
# (ip,port), num_channels
self.peer = self.host.connect((ip_address, port), 2)
def update(self):
""" call this often ( 10 times a second would be good :)
Non blocking call.
"""
result, event = self.host.service(int(BLOCK_TIME / 4) )
#result, event = self.host.service(int(60) )
#print result, event
if result == -1:
raise NetworkError("service returned -1")
# no events to get. return.
if result == 0:
return
# loop up to 3 more times.
count = 1
while (result == 1) and (count < 4):
#TODO: handle the event.
self.handle_event(event)
result, event = self.host.service(int(BLOCK_TIME / 4) )
if result == -1:
raise NetworkError("service returned -1")
count += 1
def handle_event(self, event):
""" event - an event passed into. adds event to list of events.
"""
if event.type == enet.EVENT_RECEIVE:
self.events.append(event)
elif event.type == enet.EVENT_CONNECT:
#TODO: someone has connected.
print "connected"
self.isconnected = 1
self.connect_event = event
# note: crashes.
if self.is_server:
print dir(event.peer)
pass
elif event.type == enet.EVENT_DISCONNECT:
#TODO: we have lost connection with the peer.
print "disconnected"
self.isconnected = 0
self.connect_event = None
pass
else:
raise NotImplementedError("event type not implemented :%s:",
event.type)
def put(self, a_str):
"""
"""
#TODO: figure out how to specify a specific peer.
# sending on channel 0.
self.host.broadcast(0,a_str, enet.FLAG_RELIABLE)
#self.peer.send(0,a_str, enet.FLAG_RELIABLE)
#self.peer.send(0,a_str, 0)
def get(self):
""" returns an a string from an event if there are any.
If there aren't any events, it returns None.
"""
# get the first event put in there.
event = self.events[:1]
if event:
del self.events[0]
return event[0].data
else:
return None
More information about the ENet-discuss
mailing list