Add some client logic, status to function state

This commit is contained in:
Ammar Askar committed 2014-11-10 20:25:30 +05:00
1 parent 1982716f73
commit f68206140b
4 files changed
+16 -12

No files matched your search

+6 -7
View File
@@ -68,8 +68,7 @@ class Connection:
#to read the number of bytes specified, the socket itself will mostly be #to read the number of bytes specified, the socket itself will mostly be
#used to write data upstream to the server #used to write data upstream to the server
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 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() self.file_object = self.socket.makefile()
def _handshake(self, next_state=2): def _handshake(self, next_state=2):
@@ -79,7 +78,7 @@ class Connection:
handshake.server_port = self.port handshake.server_port = self.port
handshake.next_state = next_state handshake.next_state = next_state
handshake.write(self.socket) self.write_packet(handshake)
class NetworkingThread(threading.Thread): class NetworkingThread(threading.Thread):
@@ -126,15 +125,15 @@ class PacketReactor:
state_name = None state_name = None
clientbound_packets = None clientbound_packets = None
TIME_OUT = 0.5 TIME_OUT = 1
def __init__(self, connection): def __init__(self, connection):
self.connection = connection self.connection = connection
def read_packet(self, stream): def read_packet(self, stream):
ready = select.select([self.connection.socket], [], [], 0.5) ready = select.select([self.connection.socket], [], [], self.TIME_OUT)
if ready[0]: if self.connection.socket in ready[0]:
length = VarInt.read(stream) length = VarInt.read_socket(self.connection.socket)
packet_id = VarInt.read(stream) packet_id = VarInt.read(stream)
if packet_id in self.clientbound_packets: if packet_id in self.clientbound_packets:
+3 -1
View File
@@ -4,7 +4,9 @@ from types import *
class PacketBuffer(object): class PacketBuffer(object):
b = BytesIO()
def __init__(self):
self.b = BytesIO()
def send(self, value): def send(self, value):
self.b.write(value) self.b.write(value)
+6 -3
View File
@@ -5,7 +5,7 @@ These definitions and methods are used by the packet definitions
import struct import struct
class Type: class Type(object):
@staticmethod @staticmethod
def read(file_object): def read(file_object):
pass pass
@@ -73,7 +73,10 @@ class VarInt(Type):
def read_socket(s): def read_socket(s):
d = 0 d = 0
for i in range(5): 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 d |= (b & 0x7F) << 7 * i
if not b & 0x80: if not b & 0x80:
break break
@@ -117,7 +120,7 @@ class Float(Type):
return struct.unpack('>f', file_object.read(4))[0] return struct.unpack('>f', file_object.read(4))[0]
@staticmethod @staticmethod
def send(svalue, socket): def send(value, socket):
socket.send(struct.pack('>f', value)) socket.send(struct.pack('>f', value))
+1 -1
View File
@@ -1 +1 @@
pycrypto==2.5 pycrypto>=2.5