diff options
Diffstat (limited to 'bitbake/lib/bb/asyncrpc/serv.py')
-rw-r--r-- | bitbake/lib/bb/asyncrpc/serv.py | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/bitbake/lib/bb/asyncrpc/serv.py b/bitbake/lib/bb/asyncrpc/serv.py index 3e0d0632cb..dfb0377380 100644 --- a/bitbake/lib/bb/asyncrpc/serv.py +++ b/bitbake/lib/bb/asyncrpc/serv.py | |||
@@ -12,7 +12,7 @@ import signal | |||
12 | import socket | 12 | import socket |
13 | import sys | 13 | import sys |
14 | import multiprocessing | 14 | import multiprocessing |
15 | from .connection import StreamConnection | 15 | from .connection import StreamConnection, WebsocketConnection |
16 | from .exceptions import ClientError, ServerError, ConnectionClosedError | 16 | from .exceptions import ClientError, ServerError, ConnectionClosedError |
17 | 17 | ||
18 | 18 | ||
@@ -178,6 +178,54 @@ class UnixStreamServer(StreamServer): | |||
178 | os.unlink(self.path) | 178 | os.unlink(self.path) |
179 | 179 | ||
180 | 180 | ||
181 | class WebsocketsServer(object): | ||
182 | def __init__(self, host, port, handler, logger): | ||
183 | self.host = host | ||
184 | self.port = port | ||
185 | self.handler = handler | ||
186 | self.logger = logger | ||
187 | |||
188 | def start(self, loop): | ||
189 | import websockets.server | ||
190 | |||
191 | self.server = loop.run_until_complete( | ||
192 | websockets.server.serve( | ||
193 | self.client_handler, | ||
194 | self.host, | ||
195 | self.port, | ||
196 | ping_interval=None, | ||
197 | ) | ||
198 | ) | ||
199 | |||
200 | for s in self.server.sockets: | ||
201 | self.logger.debug("Listening on %r" % (s.getsockname(),)) | ||
202 | |||
203 | # Enable keep alives. This prevents broken client connections | ||
204 | # from persisting on the server for long periods of time. | ||
205 | s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) | ||
206 | s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 30) | ||
207 | s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 15) | ||
208 | s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 4) | ||
209 | |||
210 | name = self.server.sockets[0].getsockname() | ||
211 | if self.server.sockets[0].family == socket.AF_INET6: | ||
212 | self.address = "ws://[%s]:%d" % (name[0], name[1]) | ||
213 | else: | ||
214 | self.address = "ws://%s:%d" % (name[0], name[1]) | ||
215 | |||
216 | return [self.server.wait_closed()] | ||
217 | |||
218 | async def stop(self): | ||
219 | self.server.close() | ||
220 | |||
221 | def cleanup(self): | ||
222 | pass | ||
223 | |||
224 | async def client_handler(self, websocket): | ||
225 | socket = WebsocketConnection(websocket, -1) | ||
226 | await self.handler(socket) | ||
227 | |||
228 | |||
181 | class AsyncServer(object): | 229 | class AsyncServer(object): |
182 | def __init__(self, logger): | 230 | def __init__(self, logger): |
183 | self.logger = logger | 231 | self.logger = logger |
@@ -190,6 +238,9 @@ class AsyncServer(object): | |||
190 | def start_unix_server(self, path): | 238 | def start_unix_server(self, path): |
191 | self.server = UnixStreamServer(path, self._client_handler, self.logger) | 239 | self.server = UnixStreamServer(path, self._client_handler, self.logger) |
192 | 240 | ||
241 | def start_websocket_server(self, host, port): | ||
242 | self.server = WebsocketsServer(host, port, self._client_handler, self.logger) | ||
243 | |||
193 | async def _client_handler(self, socket): | 244 | async def _client_handler(self, socket): |
194 | try: | 245 | try: |
195 | client = self.accept_client(socket) | 246 | client = self.accept_client(socket) |