rcon: code style fixes
This commit is contained in:
@@ -14,10 +14,10 @@
|
|||||||
# with the Overviewer. If not, see <http://www.gnu.org/licenses/>.
|
# with the Overviewer. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
#from enum import Enum
|
|
||||||
import struct
|
import struct
|
||||||
import select
|
import select
|
||||||
|
|
||||||
|
|
||||||
class RConException(Exception):
|
class RConException(Exception):
|
||||||
def __init__(self, request_id, reason):
|
def __init__(self, request_id, reason):
|
||||||
self.request_id = request_id
|
self.request_id = request_id
|
||||||
@@ -27,24 +27,6 @@ class RConException(Exception):
|
|||||||
return ("Failed RCon request with request ID %d, reason %s" %
|
return ("Failed RCon request with request ID %d, reason %s" %
|
||||||
(self.request_id, self.reason))
|
(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():
|
class RConConnection():
|
||||||
rid = 0
|
rid = 0
|
||||||
@@ -56,13 +38,11 @@ class RConConnection():
|
|||||||
def send(self, t, payload):
|
def send(self, t, payload):
|
||||||
self.rid = self.rid + 1
|
self.rid = self.rid + 1
|
||||||
header = struct.pack("<iii",
|
header = struct.pack("<iii",
|
||||||
len(payload) + 4 + 4 + 2, # rid, type and padding
|
len(payload) + 4 + 4 + 2, # rid, type and padding
|
||||||
self.rid,
|
self.rid, t)
|
||||||
t)
|
|
||||||
data = header + payload + '\x00\x00'
|
data = header + payload + '\x00\x00'
|
||||||
self.sock.send(data)
|
self.sock.send(data)
|
||||||
|
|
||||||
|
|
||||||
toread = select.select([self.sock], [], [], 30)
|
toread = select.select([self.sock], [], [], 30)
|
||||||
|
|
||||||
if not toread:
|
if not toread:
|
||||||
@@ -73,29 +53,27 @@ class RConConnection():
|
|||||||
struct.unpack("<iii", self.sock.recv(12, socket.MSG_WAITALL))
|
struct.unpack("<iii", self.sock.recv(12, socket.MSG_WAITALL))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RConException(self.rid,
|
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)
|
"talking to the RCon port? Error: %s" % e)
|
||||||
res_data = self.sock.recv(res_len - 4 - 4)
|
res_data = self.sock.recv(res_len - 4 - 4)
|
||||||
res_data = res_data[:-2]
|
res_data = res_data[:-2]
|
||||||
|
|
||||||
if res_id == -1:
|
if res_id == -1:
|
||||||
if t == 3:
|
if t == 3:
|
||||||
raise RConException(self.rid, "Login failed.")
|
raise RConException(self.rid, "Login failed.")
|
||||||
else:
|
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:
|
elif res_id != self.rid:
|
||||||
raise RConException(self.rid,
|
raise RConException(self.rid, "Received unexpected response "
|
||||||
"Received unexpected response number: %d" % res_id)
|
"number: %d" % res_id)
|
||||||
|
|
||||||
return res_data
|
return res_data
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def login(self, password):
|
def login(self, password):
|
||||||
self.send(3, password)
|
self.send(3, password)
|
||||||
|
|
||||||
def command(self, com, args):
|
def command(self, com, args):
|
||||||
self.send(2, com + " " + args)
|
self.send(2, com + " " + args)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.sock.close()
|
self.sock.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user