Frame rate is crashing when i using socket


Hello, i am ridanlue, a japanese BlenderGE user.
I tried make network-TPS.
However, Frame rate is crashing when i using socket.(python3.3 standard module)

try -> problem:
Client sends object’s worldPosition every frame -> The game run faster(100+ FPS), and logic uses 5-10% time
Client sends object’s worldPosition if (frame%8 == 0) -> The game run 60FPS, BUT logic uses 60%+ time

If you found problems in this Part of code, Please tell me that illegal code.
m(_ _)m

(Code is moved to #3. the reason is I forgot to paste CODE tag)

please use code tags when posting code snippets. This preserves the very important indentation.

As far as I remember such issue poped up some time ago (1…2 years ago). I suggest to search this forum. Beside that I can’t help, sorry.

Thanks, modelator Monster!
I searching that thread…

This is socketing.py , includes socket processing and issues.

serverProc() and clientProc() is calling by every frame.
setupServerSocket() and setupClientSocket is called once.


import bge
import socket
import pickle
import os
import wrapscene

sock = None
isServer = None
clients = []

ip = "localhost"
port =  52526
bufsize = 4096
maxClient = 8
currentClientID = 1
clientID = 0

def setupServerSocket():
	global sock,isServer,clients
	wrapscene.addToConsole("Server opening...")
	sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
	sock.settimeout(0.01)
	sock.setblocking(0)
	sock.bind(("",port))
	isServer = True
	clients = []
	wrapscene.addToConsole("Server opened! Host name:",os.linesep)

def setupClientSocket():
	global sock,isServer
	sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
	sock.settimeout(0.01)
	sock.setblocking(0)
	sock.connect((ip,port))
	isServer = False
	dic = { "Command" : "NewConnect" }
	send(dic)
	wrapscene.addToConsole("Client is connected! Your info:",os.linesep,ip,port)

def closeSocket():
	global sock,isServer
	if sock != None:
		sock.close()
		sock = None
		isServer = None
		wrapscene.addToConsole("Socket is closed.")
	else:
		wrapscene.addToConsole("Err : socket isn't opened.")

def send(data):
	if type(data).__name__ != "bytes":
		data = pickle.dumps(data,-1)
	if isServer:
		for addr in clients:
			sock.sendto(data,addr)
	else:
		sock.send(data)

def recv():
	global sock,isServer
	try:
		string,addr = sock.recvfrom(bufsize)
	except socket.timeout:
		return False
	
	command(string,addr)
	return string,addr

def command(string,addr):
	global currentClientID
	global clientID
	
	data = pickle.loads(string)
	if not isServer:
		if data["Command"] == "Movement":
			scene = bge.logic.getCurrentScene()
			obj = scene.objects[data["Object"]]
			obj.worldPosition = (data["Value"])
			
		if data["Command"] == "SetClientID":
			clientID = data["ClientID"]
			wrapscene.addToConsole("My Client ID is ",clientID)
			
	else:
		if data["Command"] == "NewConnect":
			clients.append(addr)
			
			dic = {"Command" : "SetClientID",
			"ClientID" : currentClientID}
			send(dic)
			currentClientID += 1
			wrapscene.addToConsole("New Connection!",addr)


def serverProc():
	
	r = recv()
	if not r == False:
		string,addr = r
		send(string)


def clientProc():
	
	recv()

I checked “Use frame rate” in render tab, and the issue is SOLVED!