0

rcon: code style fixes

This commit is contained in:
Nicolas F
2017-03-16 18:25:55 +01:00
parent 3551518bbd
commit 94d6772266

View File

@@ -14,10 +14,10 @@
# with the Overviewer. If not, see <http://www.gnu.org/licenses/>.
import socket
#from enum import Enum
import struct
import select
class RConException(Exception):
def __init__(self, request_id, reason):
self.request_id = request_id
@@ -27,24 +27,6 @@ class RConException(Exception):
return ("Failed RCon request with request ID %d, reason %s" %
(self.request_id, self.reason))
# In D, enums are just that, enums. They're a group of named constants,
# sometimes with a tag, sometimes anonymous.
# In Python, Enums use the same syntax as class objects that derive from
# the "Enum" base class, even though they are not normal python classes
# and work as singletons anyway, but instead of using a different syntax,
# Python instead decided to have a chapter in their docs about how Enums
# are different from regular classes while looking exactly the same.
# You can look at said document of failure right here:
# https://docs.python.org/3/library/enum.html#how-are-enums-different
#
# "D has too much shit going on for me" -- agrif, 2014
#
# Fortunately, we're not allowed to use Enums in Python 2.
#class RConType(Enum):
# command = 2
# login = 3
class RConConnection():
rid = 0
@@ -56,13 +38,11 @@ class RConConnection():
def send(self, t, payload):
self.rid = self.rid + 1
header = struct.pack("<iii",
len(payload) + 4 + 4 + 2, # rid, type and padding
self.rid,
t)
len(payload) + 4 + 4 + 2, # rid, type and padding
self.rid, t)
data = header + payload + '\x00\x00'
self.sock.send(data)
toread = select.select([self.sock], [], [], 30)
if not toread:
@@ -73,29 +53,27 @@ class RConConnection():
struct.unpack("<iii", self.sock.recv(12, socket.MSG_WAITALL))
except Exception as e:
raise RConException(self.rid,
"RCon protocol error. Are you sure you're " + \
"RCon protocol error. Are you sure you're "
"talking to the RCon port? Error: %s" % e)
res_data = self.sock.recv(res_len - 4 - 4)
res_data = res_data[:-2]
if res_id == -1:
if t == 3:
raise RConException(self.rid, "Login failed.")
else:
raise RConException(self.rid, "Request failed due to invalid login.")
raise RConException(self.rid,
"Request failed due to invalid login.")
elif res_id != self.rid:
raise RConException(self.rid,
"Received unexpected response number: %d" % res_id)
raise RConException(self.rid, "Received unexpected response "
"number: %d" % res_id)
return res_data
def login(self, password):
self.send(3, password)
def command(self, com, args):
self.send(2, com + " " + args)
def close(self):
self.sock.close()