VoxelServer/ServerCore/ConnectionWorker.py

97 lines
3.0 KiB
Python
Raw Permalink Normal View History

2018-11-15 10:38:38 +01:00
#!/usr/bin/env python3
import socket
import threading
2018-11-27 16:21:31 +01:00
import WorldManager as wm
from JSONStreamParser import JSONStreamParser
2018-11-30 11:08:02 +01:00
import json
2018-11-15 10:38:38 +01:00
2018-11-27 12:49:50 +01:00
SOCKETSIZE = 4096
2018-11-15 10:38:38 +01:00
2018-11-24 16:19:29 +01:00
class ConnectionWorker(threading.Thread):
2018-11-27 16:21:31 +01:00
def __init__(self,
clientsocket: 'socket',
address,
world: wm.WorldManager):
2018-11-15 10:38:38 +01:00
self.clientsocket = clientsocket
self.address = address
self.connectionEnabled = False
2018-11-27 16:21:31 +01:00
self.world = world
self.stream_parser = JSONStreamParser()
2018-11-24 18:04:53 +01:00
super(ConnectionWorker, self).__init__()
def run(self):
2018-11-27 16:21:31 +01:00
self._connect()
self._work()
self._close()
2018-11-24 18:04:53 +01:00
2018-11-27 16:21:31 +01:00
def _connect(self):
2018-11-15 10:38:38 +01:00
2018-11-27 16:21:31 +01:00
if not self._authenticate():
2018-11-24 18:04:53 +01:00
print("could not authenticate from " + str(self.address))
2018-11-15 10:38:38 +01:00
self.clientsocket.close()
return
2018-11-24 16:19:29 +01:00
2018-11-15 10:38:38 +01:00
self.connectionEnabled = True
# dummy action:
2018-11-24 18:04:53 +01:00
print("successfully established connection to " + str(self.address))
2018-11-15 10:38:38 +01:00
2018-11-27 16:21:31 +01:00
def _sendChunk(self, chunk_x: int, chunk_y: int, chunk_z: int) -> None:
2018-12-02 17:08:28 +01:00
# TODO: world generation should be done by another thread,
2018-11-29 00:07:05 +01:00
# just for testing purposes!!
if not self.world.isChunkKnown(chunk_x, chunk_y, chunk_z):
self.world.createEmptyChunk(chunk_x, chunk_y, chunk_z)
self.world.applyPerlinToChunk(chunk_x, chunk_y, chunk_z)
2018-11-27 16:21:31 +01:00
json = self.world.getChunkAsJson(chunk_x, chunk_y, chunk_z)
self.clientsocket.sendall(json.encode())
2018-11-30 11:08:02 +01:00
def _sendMessage(self, message: str) -> None:
msg_object = {}
msg_object['datatype'] = 'message'
msg_object['data'] = message
self.clientsocket.sendall(json.dumps(msg_object).encode())
2018-11-27 16:21:31 +01:00
def _processData(self, data: dict):
# check type:
print("processing\n:", data)
if 'datatype' not in data:
2018-12-02 17:07:16 +01:00
self._sendMessage('bad data')
2018-11-27 16:21:31 +01:00
raise Exception("bad data received")
if data['datatype'] == 'request_chunk':
chunk_x = int(data['position'][0])
chunk_y = int(data['position'][1])
chunk_z = int(data['position'][2])
self._sendChunk(chunk_x, chunk_y, chunk_z)
2018-12-02 17:07:16 +01:00
else:
self._sendMessage('unknown request')
2018-11-27 16:21:31 +01:00
def _work(self):
2018-11-27 12:49:50 +01:00
self.data = self.clientsocket.recv(SOCKETSIZE)
while len(self.data) > 0:
2018-11-27 16:21:31 +01:00
self.stream_parser.appendRawData(self.data)
while not self.stream_parser.isQueueEmpty():
parsed_data = self.stream_parser.popAsDictionary()
self._processData(parsed_data)
2018-11-27 12:49:50 +01:00
self.data = self.clientsocket.recv(SOCKETSIZE)
2018-11-15 10:38:38 +01:00
2018-11-27 16:21:31 +01:00
def _authenticate(self) -> bool:
2018-11-15 10:38:38 +01:00
# TODO
2018-11-24 18:04:53 +01:00
print("looking busy like i would check permissions of new ",
2018-11-27 12:49:50 +01:00
"client... beep... boop... beep... ")
2018-11-24 18:04:53 +01:00
print("new client accepted! [from ", str(self.address), " ]")
2018-11-15 10:38:38 +01:00
return True
2018-11-24 18:04:53 +01:00
2018-11-27 16:21:31 +01:00
def _close(self):
2018-11-24 18:04:53 +01:00
print("close connection to ", str(self.address))
self.clientsocket.close()