Add some client logic, status to function state

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

View File

@ -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:

View File

@ -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)

View File

@ -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))

View File

@ -1 +1 @@
pycrypto==2.5
pycrypto>=2.5