pyCraft/NetworkManager.py

205 lines
9.1 KiB
Python
Raw Normal View History

2012-04-07 16:02:46 +02:00
import socket
import wx
2012-04-15 18:49:47 +02:00
import PacketListenerManager
2012-04-07 16:02:46 +02:00
import urllib2
import traceback
2012-04-10 21:14:32 +02:00
import threading
import struct
2012-04-07 16:02:46 +02:00
#Eclipse pyDev error fix
wx=wx
EntityID = 0
class ServerConnection(threading.Thread):
2012-04-07 16:02:46 +02:00
def __init__(self, window, username, password, sessionID, server, port):
threading.Thread.__init__(self)
2012-04-07 16:02:46 +02:00
self.username = username
self.password = password
self.sessionID = sessionID
self.server = server
self.port = port
if(window == None):
self.NoGUI = True
2012-04-07 16:02:46 +02:00
self.window = window
2012-04-15 18:49:47 +02:00
def grabSocket(self):
return self.socket
def run(self):
2012-04-07 16:02:46 +02:00
self.socket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
try:
self.socket.connect ( ( self.server, self.port ) )
2012-04-15 16:51:00 +02:00
self.FileObject = self.socket.makefile()
2012-04-15 18:49:47 +02:00
PacketListenerManager.sendHandshake(self.socket, self.username, self.server, self.port)
if(self.socket.recv(1) == "\x02"):
2012-04-15 18:49:47 +02:00
response = PacketListenerManager.handle02(self.FileObject)
else:
print "Server responded with a malformed packet"
pass
serverid = response
2012-04-07 16:02:46 +02:00
if(serverid != '-'):
url = "http://session.minecraft.net/game/joinserver.jsp?user=" + self.username + "&sessionId=" + self.sessionID + "&serverId=" + serverid
response = urllib2.urlopen(url).read()
if(response != "OK"):
if(self.NoGUI == False):
self.window.connectStatus.SetLabel("Response from sessions.minecraft.net wasn't OK")
else:
print "Response from sessions.minecraft.net wasn't OK, it was " + response
2012-04-07 16:02:46 +02:00
return False
2012-04-15 18:49:47 +02:00
PacketListenerManager.sendLoginRequest(self.socket, self.username)
PacketListener(self, self.window, self.socket, self.FileObject).start()
else:
print "Server is in offline mode"
2012-04-15 18:49:47 +02:00
PacketListenerManager.sendLoginRequest(self.socket, self.username)
2012-04-07 16:02:46 +02:00
except Exception, e:
if(self.NoGUI == False):
self.window.connectStatus.SetForegroundColour(wx.RED)
self.window.connectStatus.SetLabel("Connection to server failed")
else:
print "Connection to server failed"
2012-04-07 16:02:46 +02:00
traceback.print_exc()
return False
2012-04-15 18:49:47 +02:00
2012-04-10 21:14:32 +02:00
class PacketListener(threading.Thread):
2012-04-15 18:49:47 +02:00
def __init__(self, connection, window, socket, FileObject):
2012-04-10 21:14:32 +02:00
threading.Thread.__init__(self)
self.socket = socket
self.window = window
2012-04-15 16:51:00 +02:00
self.FileObject = FileObject
2012-04-15 18:49:47 +02:00
self.connection = connection
2012-04-10 21:14:32 +02:00
def run(self):
2012-04-15 16:51:00 +02:00
self.socket.setblocking(1)
2012-04-10 21:14:32 +02:00
while True:
try:
response = self.socket.recv(1)
2012-04-10 21:14:32 +02:00
except socket.timeout, e:
if(self.NoGUI == False):
self.window.connectStatus.SetLabel("Ping timeout")
else:
print "Ping timeout"
2012-04-10 21:14:32 +02:00
break
if(response[0] == "\x00"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle00(self.FileObject, self.socket)
if(response[0] == "\x01"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle01(self.FileObject)
if(response[0] == "\x03"):
print PacketListenerManager.handle03(self.FileObject).replace(u'\xa7', '&')
2012-04-12 00:21:16 +02:00
if(response[0] == "\x04"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle04(self.FileObject)
2012-04-12 00:21:16 +02:00
if(response[0] == "\x05"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle05(self.FileObject)
2012-04-12 00:21:16 +02:00
if(response[0] == "\x06"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle06(self.FileObject)
2012-04-12 00:21:16 +02:00
if(response[0] == "\x07"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle07(self.FileObject)
2012-04-12 00:21:16 +02:00
if(response[0] == "\x08"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle08(self.FileObject)
2012-04-12 00:21:16 +02:00
if(response[0] == "\x09"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle09(self.FileObject)
2012-04-12 00:21:16 +02:00
if(response[0] == "\x0D"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle0D(self.FileObject)
2012-04-12 00:21:16 +02:00
if(response[0] == "\x11"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle11(self.FileObject)
2012-04-12 00:21:16 +02:00
if(response[0] == "\x12"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle12(self.FileObject)
2012-04-12 00:21:16 +02:00
if(response[0] == "\x14"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle14(self.FileObject)
2012-04-12 12:07:25 +02:00
if(response[0] == "\x15"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle15(self.FileObject)
2012-04-12 12:07:25 +02:00
if(response[0] == "\x16"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle16(self.FileObject)
2012-04-12 12:07:25 +02:00
if(response[0] == "\x17"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle17(self.FileObject)
2012-04-12 12:07:25 +02:00
if(response[0] == "\x18"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle18(self.FileObject)
2012-04-15 16:51:00 +02:00
if(response[0] == "\x19"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle19(self.FileObject)
2012-04-12 12:07:25 +02:00
if(response[0] == "\x1A"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle1A(self.FileObject)
2012-04-12 12:07:25 +02:00
if(response[0] == "\x1C"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle1C(self.FileObject)
2012-04-12 12:07:25 +02:00
if(response[0] == "\x1D"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle1D(self.FileObject)
2012-04-12 12:07:25 +02:00
if(response[0] == "\x1E"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle1E(self.FileObject)
2012-04-15 16:51:00 +02:00
if(response[0] == "\x1F"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle1F(self.FileObject)
if(response[0] == "\x20"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle20(self.FileObject)
if(response[0] == "\x21"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle21(self.FileObject)
if(response[0] == "\x22"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle22(self.FileObject)
if(response[0] == "\x23"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle23(self.FileObject)
if(response[0] == "\x26"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle26(self.FileObject)
if(response[0] == "\x27"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle27(self.FileObject)
if(response[0] == "\x28"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle28(self.FileObject)
if(response[0] == "\x29"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle29(self.FileObject)
if(response[0] == "\x2A"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle2A(self.FileObject)
if(response[0] == "\x2B"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle2B(self.FileObject)
if(response[0] == "\x32"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle32(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x33"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle33(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x34"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle34(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x35"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle35(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x36"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle36(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x3C"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle3C(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x3D"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle3D(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x46"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle46(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x47"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle47(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x64"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle64(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x65"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle65(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x67"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle67(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x68"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle68(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x69"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle69(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x6A"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle6A(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x6B"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle6B(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x82"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle82(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x83"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle83(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\x84"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle84(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\xC8"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handleC8(self.FileObject)
2012-04-14 17:29:04 +02:00
if(response[0] == "\xC9"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handleC9(self.FileObject)
if(response[0] == "\xCA"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handleCA(self.FileObject)
if(response[0] == "\xFA"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handleFA(self.FileObject)
2012-04-10 21:14:32 +02:00
if(response[0] == "\xFF"):
2012-04-15 18:49:47 +02:00
DisconMessage = PacketListenerManager.handleFF(self.FileObject)
2012-04-14 17:29:04 +02:00
if(self.window == None):
print "Disconnected: " + DisconMessage
else:
self.window.connectStatus.SetLabel("Disconnected: " + DisconMessage)
2012-04-14 17:29:04 +02:00
break