-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathserver.py
79 lines (65 loc) · 2.28 KB
/
server.py
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
74
75
76
77
78
79
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [https://neo4j.com]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
from socketserver import (
StreamRequestHandler,
TCPServer,
)
from ._async.backend import AsyncBackend
from ._sync.backend import Backend
class Server(TCPServer):
allow_reuse_address = True
def __init__(self, address):
class Handler(StreamRequestHandler):
# undocumented config but unbearably slow without
wbufsize = 2**16 # 64 KiB
def handle(self):
backend = Backend(self.rfile, self.wfile)
try:
while backend.process_request():
pass
finally:
backend.close()
print("Disconnected")
super().__init__(address, Handler)
class AsyncServer:
def __init__(self, address):
self._address = address
self._server = None
@staticmethod
async def _handler(reader, writer):
backend = AsyncBackend(reader, writer)
try:
while await backend.process_request():
pass
finally:
writer.close()
await backend.close()
print("Disconnected")
async def start(self):
self._server = await asyncio.start_server(
self._handler,
host=self._address[0],
port=self._address[1],
limit=float("inf"), # this is dirty but works (for now)
)
async def serve_forever(self):
if not self._server:
raise RuntimeError("Server not started")
await self._server.serve_forever()
def stop(self):
if not self._server:
raise RuntimeError("Try starting the server before stopping it ;)")
self._server.close()