1 # Copyright 2009, Google Inc.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """Message related utilities.
33 Note: request.connection.write/read are used in this module, even though
34 mod_python document says that they should be used only in connection handlers.
35 Unfortunately, we have no other options. For example, request.write/read are
36 not suitable because they don't allow direct raw bytes writing/reading.
44 def send_message(request, message):
48 request: mod_python request.
49 message: unicode string to send.
52 request.connection.write('\x00' + message.encode('utf-8') + '\xff')
55 def receive_message(request):
56 """Receive a Web Socket frame and return its payload as unicode string.
59 request: mod_python request.
64 # mp_conn.read will block if no bytes are available.
65 # Timeout is controlled by TimeOut directive of Apache.
66 frame_type_str = request.connection.read(1)
67 frame_type = ord(frame_type_str[0])
68 if (frame_type & 0x80) == 0x80:
69 # The payload length is specified in the frame.
71 length = _payload_length(request)
72 _receive_bytes(request, length)
74 # The payload is delimited with \xff.
75 bytes = _read_until(request, '\xff')
76 message = bytes.decode('utf-8')
77 if frame_type == 0x00:
79 # Discard data of other types.
82 def _payload_length(request):
85 b_str = request.connection.read(1)
87 length = length * 128 + (b & 0x7f)
93 def _receive_bytes(request, length):
96 new_bytes = request.connection.read(length)
97 bytes.append(new_bytes)
98 length -= len(new_bytes)
102 def _read_until(request, delim_char):
105 ch = request.connection.read(1)
109 return ''.join(bytes)
112 class MessageReceiver(threading.Thread):
113 """This class receives messages from the client.
115 This class provides three ways to receive messages: blocking, non-blocking,
116 and via callback. Callback has the highest precedence.
118 Note: This class should not be used with the standalone server for wss
119 because pyOpenSSL used by the server raises a fatal error if the socket
120 is accessed from multiple threads.
122 def __init__(self, request, onmessage=None):
123 """Construct an instance.
126 request: mod_python request.
127 onmessage: a function to be called when a message is received.
128 May be None. If not None, the function is called on
129 another thread. In that case, MessageReceiver.receive
130 and MessageReceiver.receive_nowait are useless because
131 they will never return any messages.
133 threading.Thread.__init__(self)
134 self._request = request
135 self._queue = Queue.Queue()
136 self._onmessage = onmessage
137 self._stop_requested = False
142 while not self._stop_requested:
143 message = receive_message(self._request)
145 self._onmessage(message)
147 self._queue.put(message)
150 """ Receive a message from the channel, blocking.
153 message as a unicode string.
155 return self._queue.get()
157 def receive_nowait(self):
158 """ Receive a message from the channel, non-blocking.
161 message as a unicode string if available. None otherwise.
164 message = self._queue.get_nowait()
170 """Request to stop this instance.
172 The instance will be stopped after receiving the next message.
173 This method may not be very useful, but there is no clean way
174 in Python to forcefully stop a running thread.
176 self._stop_requested = True
179 class MessageSender(threading.Thread):
180 """This class sends messages to the client.
182 This class provides both synchronous and asynchronous ways to send
185 Note: This class should not be used with the standalone server for wss
186 because pyOpenSSL used by the server raises a fatal error if the socket
187 is accessed from multiple threads.
189 def __init__(self, request):
190 """Construct an instance.
193 request: mod_python request.
195 threading.Thread.__init__(self)
196 self._request = request
197 self._queue = Queue.Queue()
203 message, condition = self._queue.get()
205 send_message(self._request, message)
209 def send(self, message):
210 """Send a message, blocking."""
212 condition = threading.Condition()
214 self._queue.put((message, condition))
217 def send_nowait(self, message):
218 """Send a message, non-blocking."""
220 self._queue.put((message, threading.Condition()))