aboutsummaryrefslogtreecommitdiff
path: root/servert
blob: 3213373f184642bd0e969fd9b1c01c7ca1f09241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
import os
import sys
import socket
from threading import Thread
import importlib.machinery as imp

class conf:
	modulesFolder = 'modules'
	localip = '127.0.0.1'
	localport = 25565
	remoteip = '192.168.1.145'
	remoteport = 25566

# Load modules utils
utils = imp.SourceFileLoader("utils",
		conf.modulesFolder + '/utils.py').load_module()

__players__ = set()

class Packet:
	def __init__(self, sizeoffset, bsize, data):
		#print(sizeoffset)
		#print(bsize)
		pass

class Player:
	class Connection(Thread):
		def __init__(self, source, target, player, direction):
			Thread.__init__(self, name="PlayerConnection" + str(direction))
			self.source = source
			self.target = target
			self.player = player
		def run(self):
			while True:
				i = 0
				packet_size = 0
				nextbt = True
				data = self.source.recv(1)
				if not data: break
				while nextbt:
					bt = data[i]
					nextbt = bool(bt & (1 << 7))
					bt = bt & ~(1 << 7)
					packet_size = packet_size | (bt << (i * 7))
					if nextbt:
						data += self.source.recv(1)
						i += 1
				data += self.source.recv(packet_size)
				Packet(i, packet_size, data)
				self.target.send(data)
	def __init__(self, conn, addr):
		self.conn = conn
		self.addr = addr
		self.s = socket.socket()
		self.s.connect((conf.remoteip, conf.remoteport))
		self.t_left = Player.Connection(conn, self.s, self, 1)
		self.t_right = Player.Connection(self.s, conn, self, 2)
		self.t_left.start()
		self.t_right.start()

def fakeserver():
	s = socket.socket()
	s.bind((conf.localip, conf.localport))
	s.listen(2)
	#while True:
	conn, addr = s.accept()
	__players__.add(Player(conn, addr))

#################################################################################

if __name__ == '__main__':
	fakeserver()