pyCraft/NetworkManager.py

129 lines
5.0 KiB
Python
Raw Normal View History

2012-04-07 16:02:46 +02:00
import socket
import wx
import PacketManager
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
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-12 12:07:25 +02:00
PacketManager.sendHandshake(self.socket, self.username, self.server, self.port)
if(self.socket.recv(1) == "\x02"):
response = PacketManager.handle02(self.socket)
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
PacketManager.sendLoginRequest(self.socket, self.username)
2012-04-10 21:14:32 +02:00
PacketListener(self.window, self.socket).start()
else:
print "Server is in offline mode"
PacketManager.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-10 21:14:32 +02:00
class PacketListener(threading.Thread):
def __init__(self, window, socket):
threading.Thread.__init__(self)
self.socket = socket
self.window = window
def run(self):
self.socket.settimeout(60)
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
print hex(ord(response[0]))
if(response[0] == "\x00"):
PacketManager.handle00(self.socket)
if(response[0] == "\x01"):
PacketManager.handle01(self.socket)
if(response[0] == "\x03"):
PacketManager.handle03(self.socket)
2012-04-12 00:21:16 +02:00
if(response[0] == "\x04"):
PacketManager.handle04(self.socket)
if(response[0] == "\x05"):
PacketManager.handle05(self.socket)
if(response[0] == "\x06"):
PacketManager.handle06(self.socket)
if(response[0] == "\x07"):
PacketManager.handle07(self.socket)
if(response[0] == "\x08"):
PacketManager.handle08(self.socket)
if(response[0] == "\x09"):
PacketManager.handle09(self.socket)
if(response[0] == "\x0D"):
PacketManager.handle0D(self.socket)
if(response[0] == "\x11"):
PacketManager.handle11(self.socket)
if(response[0] == "\x12"):
PacketManager.handle12(self.socket)
if(response[0] == "\x14"):
PacketManager.handle14(self.socket)
2012-04-12 12:07:25 +02:00
if(response[0] == "\x15"):
PacketManager.handle15(self.socket)
if(response[0] == "\x16"):
PacketManager.handle16(self.socket)
if(response[0] == "\x17"):
PacketManager.handle17(self.socket)
if(response[0] == "\x18"):
print PacketManager.handle18(self.socket)
if(response[0] == "\x1A"):
PacketManager.handle1A(self.socket)
if(response[0] == "\x1C"):
PacketManager.handle1C(self.socket)
if(response[0] == "\x1D"):
PacketManager.handle1D(self.socket)
if(response[0] == "\x1E"):
PacketManager.handle1E(self.socket)
2012-04-10 21:14:32 +02:00
if(response[0] == "\xFF"):
DisconMessage = PacketManager.handleFF(self.socket, response)
if(self.NoGUI == False):
"Disconnected: " + DisconMessage
else:
self.window.connectStatus.SetLabel("Disconnected: " + DisconMessage)