コード例 #1
0
def tcphandler():
	s = socketlibevent.socket()
	print b"listening..."
	s.bind((b'', 64738))
	s.listen(5)
	while True:
		client_socket, client_address = s.accept()
		ssl_socket = socketlibevent.ssl(client_socket, b"server.key", b"server.pem", True)
		Connection(ssl_socket, client_address)
		stackless.schedule()
コード例 #2
0
def udphandler():
	s = socketlibevent.socket(socket.AF_INET, socket.SOCK_DGRAM)
	print b"starting udp handler..."
	s.bind((b'', 64738))
	while True:
		(buf, addr) = s.recvfrom(4096)
		if len(buf) == 12:
			r = struct.unpack(b"!iQ", buf)
			if r[0] != 0: continue
			r = struct.pack(b"!iQiii", (1 << 16 | 2 << 8 | 2 & 0xFF), r[1], len(connections), -1, 240000)
			s.sendto(r, 0, addr)
		else:
			if addr in udpAddrToUser:
				if not u.cs or not u.cs.isValid():
					continue
				u = udpAddrToUser[addr]
#				print "UDP: packet from known user %d" % (u.session)
				buf = u.cs.decrypt(buf)
				if buf != False:
					handle_udp_message(u, buf)
			else:
				u = None
				for usr in connections:
					if usr.cs and usr.cs.isValid():
						r = usr.cs.decrypt(buf)
						if r == False:
							continue

#						print "UDP: found user %d" % (usr.session)
						usr.udpSocket = s
						usr.udpAddr = addr
						udpAddrToUser[addr] = usr
						u = usr
						buf = r
						break

				if not u:
					print addr
					print binascii.hexlify(buf)
				else:
					handle_udp_message(u, buf)