diff --git a/network/connection.py b/network/connection.py index 8569a0b..64dd91b 100644 --- a/network/connection.py +++ b/network/connection.py @@ -68,8 +68,7 @@ class Connection: #to read the number of bytes specified, the socket itself will mostly be #used to write data upstream to the server self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.socket.settimeout(20.0) - self.socket.connect(( self.address, self.port )) + self.socket.connect((self.address, self.port)) self.file_object = self.socket.makefile() def _handshake(self, next_state=2): @@ -79,7 +78,7 @@ class Connection: handshake.server_port = self.port handshake.next_state = next_state - handshake.write(self.socket) + self.write_packet(handshake) class NetworkingThread(threading.Thread): @@ -126,15 +125,15 @@ class PacketReactor: state_name = None clientbound_packets = None - TIME_OUT = 0.5 + TIME_OUT = 1 def __init__(self, connection): self.connection = connection def read_packet(self, stream): - ready = select.select([self.connection.socket], [], [], 0.5) - if ready[0]: - length = VarInt.read(stream) + ready = select.select([self.connection.socket], [], [], self.TIME_OUT) + if self.connection.socket in ready[0]: + length = VarInt.read_socket(self.connection.socket) packet_id = VarInt.read(stream) if packet_id in self.clientbound_packets: diff --git a/network/packets.py b/network/packets.py index 5e9e3b5..b33c56f 100644 --- a/network/packets.py +++ b/network/packets.py @@ -4,7 +4,9 @@ from types import * class PacketBuffer(object): - b = BytesIO() + + def __init__(self): + self.b = BytesIO() def send(self, value): self.b.write(value) diff --git a/network/types.py b/network/types.py index 432cb58..9b92c06 100644 --- a/network/types.py +++ b/network/types.py @@ -5,7 +5,7 @@ These definitions and methods are used by the packet definitions import struct -class Type: +class Type(object): @staticmethod def read(file_object): pass @@ -73,7 +73,10 @@ class VarInt(Type): def read_socket(s): d = 0 for i in range(5): - b = ord(s.recv(1)) + b = s.recv(1) + if b == "": + raise RuntimeError("Socket disconnected") + b = ord(b) d |= (b & 0x7F) << 7 * i if not b & 0x80: break @@ -117,7 +120,7 @@ class Float(Type): return struct.unpack('>f', file_object.read(4))[0] @staticmethod - def send(svalue, socket): + def send(value, socket): socket.send(struct.pack('>f', value)) diff --git a/requirements.txt b/requirements.txt index dc76301..c9cd909 100755 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -pycrypto==2.5 \ No newline at end of file +pycrypto>=2.5 \ No newline at end of file