Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronous websocket write #385

Merged
merged 1 commit into from Feb 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion rosbridge_server/src/rosbridge_server/websocket_handler.py
Expand Up @@ -34,10 +34,12 @@

from rosauth.srv import Authentication

import threading
from functools import partial

from tornado.ioloop import IOLoop
from tornado.websocket import WebSocketHandler
from tornado.gen import coroutine

from rosbridge_library.rosbridge_protocol import RosbridgeProtocol
from rosbridge_library.util import json, bson
Expand Down Expand Up @@ -72,6 +74,7 @@ def open(self):
self.protocol.outgoing = self.send_message
self.set_nodelay(True)
self.authenticated = False
self._write_lock = threading.RLock()
cls.client_id_seed += 1
cls.clients_connected += 1
if cls.client_count_pub:
Expand Down Expand Up @@ -130,7 +133,13 @@ def send_message(self, message):
else:
binary = False

IOLoop.instance().add_callback(partial(self.write_message, message, binary))
with self._write_lock:
IOLoop.instance().add_callback(partial(self.prewrite_message, message, binary))

@coroutine
def prewrite_message(self, message, binary):
with self._write_lock:
yield self.write_message(message, binary)

def check_origin(self, origin):
return True
Expand Down