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
|
|
|
|
|
2012-04-11 22:22:58 +02:00
|
|
|
EntityID = 0
|
|
|
|
|
|
|
|
class ServerConnection(threading.Thread):
|
2012-04-07 16:02:46 +02:00
|
|
|
|
|
|
|
def __init__(self, window, username, password, sessionID, server, port):
|
2012-04-11 22:22:58 +02:00
|
|
|
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
|
2012-04-11 22:22:58 +02:00
|
|
|
if(window == None):
|
|
|
|
self.NoGUI = True
|
2012-04-07 16:02:46 +02:00
|
|
|
self.window = window
|
|
|
|
|
2012-04-11 22:22:58 +02:00
|
|
|
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-11 22:22:58 +02:00
|
|
|
PacketManager.sendString("\x02", self.username + ";" + self.server + ":" + str(self.port), self.socket)
|
|
|
|
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"):
|
2012-04-11 22:22:58 +02:00
|
|
|
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()
|
2012-04-11 22:22:58 +02:00
|
|
|
else:
|
|
|
|
print "Server is in offline mode"
|
|
|
|
PacketManager.sendLoginRequest(self.socket, self.username)
|
2012-04-07 16:02:46 +02:00
|
|
|
except Exception, e:
|
2012-04-11 22:22:58 +02:00
|
|
|
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:
|
2012-04-11 22:22:58 +02:00
|
|
|
response = self.socket.recv(1)
|
2012-04-10 21:14:32 +02:00
|
|
|
except socket.timeout, e:
|
2012-04-11 22:22:58 +02:00
|
|
|
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)
|
2012-04-11 22:22:58 +02:00
|
|
|
if(response[0] == "\x01"):
|
|
|
|
PacketManager.handle01(self.socket)
|
|
|
|
if(response[0] == "\x03"):
|
|
|
|
PacketManager.handle03(self.socket)
|
2012-04-10 21:14:32 +02:00
|
|
|
if(response[0] == "\xFF"):
|
2012-04-11 22:22:58 +02:00
|
|
|
DisconMessage = PacketManager.handleFF(self.socket, response)
|
|
|
|
if(self.NoGUI == False):
|
|
|
|
"Disconnected: " + DisconMessage
|
|
|
|
else:
|
|
|
|
self.window.connectStatus.SetLabel("Disconnected: " + DisconMessage)
|
|
|
|
|