[ad_1]
I’m writing an replace to a recreation I’ve written in python, and I’m at the moment including server help. I can not present you the primary code (for the shopper), as it’s lots of of strains lengthy. Nonetheless, what I can do is present you this snippet of code from strains 34 to 47:
s = ""
serverMode = "noServer"
def getserver(server_ip,server_num):
international s, serverMode
strive:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.settimeout(2)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
s.join((server_ip,server_num))
print "linked to " + str(server_ip)
serverMode = "activeServer"
besides:
easygui.msgbox("The server is just not working or doesn't exist.")
s = None
the ‘s’ variable is the place the server socket is saved. the ‘serverMode’ holds the data on wether the sport is linked to an lively server or not, in order that if that’s the case, it may be consistently checking for updates within the code. Right here is the server code, which is considerably shorter than the primary code:
import socket, sign
print "[SERVER INFO] hosted at " + str(socket.gethostname())
print "[SERVER INFO] loading server..."
def doStuff():
move
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
#socket.fork()
serversocket.bind((socket.gethostname(), 80))
serversocket.pay attention(5);
print "[SERVER INFO] server began."
purchasers = []
working = True
whereas working:
(clientsocket, deal with) = serversocket.settle for()
if not [clientsocket, address] in purchasers: purchasers.append([clientsocket, address])
information = clientsocket.recv(2048)
if not information: move
elif information.startswith("[SERVER INFO]"): print information
elif information == "disconnect":
move
elif information.startswith("[BLOCK PLACEMENT]"):
blocktype = information.break up(' ')[1]
loc = [data.split(' ')[2], information.break up(' ')[3]]
for shopper in purchasers:
strive: shopper.ship(bytes("[BLOCK] " + str(loc[0]) + "," + str(loc[1]) + "," + blocktype))
besides: move
elif information.startswith("[CHAT MESSAGE]"):
for shopper in purchasers:
strive: shopper.ship(bytes(information))
besides: move
clientsocket.sendall(bytes("0,0,wooden.pngn20,20,wooden.png"))
serversocket.shutdown(0)
print "[SERVER INFO] server shutdown appropriately."
raw_input()
As I discussed, the shopper is continually checking for server updates. It makes use of the recv perform in sockets. So due to this, it runs extraordinarily sluggish. I’ve checked out on-line tutorials on how you can repair related issues, however nothing appears to work for me. Nonetheless, right here is the code that checks for updates:
if serverMode == "activeServer":
#s.setsocketopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
strive:
recvbuff = 100
rx_bufftmp = s.recv(recvbuff)
rx_bufftmplen = len(rx_bufftmp)
recvbuff = max(recvbuff, rx_bufftmplen)
besides: information = False
if not information == False and information.startswith('[BLOCK]'):
blk = rx_bufftmp.break up(' ')[1].break up(',')
blx = blk[1]
bly = blk[2]
bln = blk[0]
bls.append(Block(bln, [int(blx), int(bly)]))
The code runs within the pygame mainloop. So, since I used to be not capable of finding any methods to optimize efficiency, might there be a means as a substitute to solely seize information if there may be incoming information? Possibly additionally another methods to optimize efficiency as nicely?
FYI:
I’m working a 32 bit model of python 2.7 on a 64 bit set up of Home windows 10.
[ad_2]