VoxelServer/ServerCore/Server.py

57 lines
1.5 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-24 18:04:53 +01:00
import settings
from ConnectionWorker import ConnectionWorker
2018-11-15 10:38:38 +01:00
from WorldManager import WorldManager
import threading
2018-11-15 10:38:38 +01:00
2018-11-24 18:04:53 +01:00
class Server(threading.Thread):
2018-11-15 10:38:38 +01:00
def __init__(self, wm: 'WorldManager', hostname: str, port: int):
2018-11-15 10:38:38 +01:00
self.wm = wm
self.hostname = hostname
self.port = port
super(Server, self).__init__()
2018-11-24 18:04:53 +01:00
def runMainServerThread(self, hostname: str, port: int):
2018-11-15 10:38:38 +01:00
assert port >= 1024 and port <= 65535
2018-11-24 18:04:53 +01:00
# TODO: here will be the main thread listening for new connections
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((hostname, port))
s.listen()
while True:
conn, addr = s.accept()
# create a new worker thread for this connection
2018-11-27 16:21:31 +01:00
worker = ConnectionWorker(conn, addr, self.wm)
2018-11-24 18:04:53 +01:00
worker.start()
def run(self):
print("starting main server thread")
self.runMainServerThread(self.hostname, self.port)
2018-11-24 18:04:53 +01:00
def main():
2018-11-29 00:07:05 +01:00
raw_noise_cell_size = 32
2018-11-27 16:21:31 +01:00
raw_noise_chunk_size = 4
world_seed = 42
# create world:
wm = WorldManager(raw_noise_cell_size=raw_noise_cell_size,
raw_noise_chunk_size=raw_noise_chunk_size,
world_seed=world_seed)
2018-11-27 16:21:31 +01:00
wm.createEmptyChunk(0, 0, 0)
wm.applyPerlinToChunk(0, 0, 0)
server = Server(wm, settings.SERVER_HOST, settings.SERVER_PORT)
server.start()
2018-11-15 10:38:38 +01:00
2018-11-27 16:21:31 +01:00
2018-11-24 18:04:53 +01:00
if __name__ == "__main__":
main()