1 # Author: Trevor Perrin
2 # See the LICENSE file for legal information regarding use of this file.
5 A state machine for using TLS Lite with asynchronous I/O.
8 class AsyncStateMachine:
10 This is an abstract class that's used to integrate TLS Lite with
13 This class signals wantsReadsEvent() and wantsWriteEvent(). When
14 the underlying socket has become readable or writeable, the event
15 should be passed to this class by calling inReadEvent() or
16 inWriteEvent(). This class will then try to read or write through
17 the socket, and will update its state appropriately.
19 This class will forward higher-level events to its subclass. For
20 example, when a complete TLS record has been received,
21 outReadEvent() will be called with the decrypted data.
28 #These store the various asynchronous operations (i.e.
29 #generators). Only one of them, at most, is ever active at a
31 self.handshaker = None
36 #This stores the result from the last call to the
37 #currently active operation. If 0 it indicates that the
38 #operation wants to read, if 1 it indicates that the
39 #operation wants to write. If None, there is no active
43 def _checkAssert(self, maxActive=1):
44 #This checks that only one operation, at most, is
45 #active, and that self.result is set appropriately.
56 if self.result == None:
58 raise AssertionError()
59 elif self.result in (0,1):
61 raise AssertionError()
63 raise AssertionError()
64 if activeOps > maxActive:
65 raise AssertionError()
67 def wantsReadEvent(self):
68 """If the state machine wants to read.
70 If an operation is active, this returns whether or not the
71 operation wants to read from the socket. If an operation is
72 not active, this returns None.
75 @return: If the state machine wants to read.
77 if self.result != None:
78 return self.result == 0
81 def wantsWriteEvent(self):
82 """If the state machine wants to write.
84 If an operation is active, this returns whether or not the
85 operation wants to write to the socket. If an operation is
86 not active, this returns None.
89 @return: If the state machine wants to write.
91 if self.result != None:
92 return self.result == 1
95 def outConnectEvent(self):
96 """Called when a handshake operation completes.
98 May be overridden in subclass.
102 def outCloseEvent(self):
103 """Called when a close operation completes.
105 May be overridden in subclass.
109 def outReadEvent(self, readBuffer):
110 """Called when a read operation completes.
112 May be overridden in subclass."""
115 def outWriteEvent(self):
116 """Called when a write operation completes.
118 May be overridden in subclass."""
121 def inReadEvent(self):
122 """Tell the state machine it can read from the socket."""
126 self._doHandshakeOp()
134 self.reader = self.tlsConnection.readAsync(16384)
140 def inWriteEvent(self):
141 """Tell the state machine it can write to the socket."""
145 self._doHandshakeOp()
158 def _doHandshakeOp(self):
160 self.result = self.handshaker.next()
161 except StopIteration:
162 self.handshaker = None
164 self.outConnectEvent()
166 def _doCloseOp(self):
168 self.result = self.closer.next()
169 except StopIteration:
175 self.result = self.reader.next()
176 if not self.result in (0,1):
177 readBuffer = self.result
180 self.outReadEvent(readBuffer)
182 def _doWriteOp(self):
184 self.result = self.writer.next()
185 except StopIteration:
189 def setHandshakeOp(self, handshaker):
190 """Start a handshake operation.
192 @type handshaker: generator
193 @param handshaker: A generator created by using one of the
194 asynchronous handshake functions (i.e. handshakeServerAsync, or
195 handshakeClientxxx(..., async=True).
199 self.handshaker = handshaker
200 self._doHandshakeOp()
205 def setServerHandshakeOp(self, **args):
206 """Start a handshake operation.
208 The arguments passed to this function will be forwarded to
209 L{tlslite.tlsconnection.TLSConnection.handshakeServerAsync}.
211 handshaker = self.tlsConnection.handshakeServerAsync(**args)
212 self.setHandshakeOp(handshaker)
214 def setCloseOp(self):
215 """Start a close operation.
219 self.closer = self.tlsConnection.closeAsync()
225 def setWriteOp(self, writeBuffer):
226 """Start a write operation.
228 @type writeBuffer: str
229 @param writeBuffer: The string to transmit.
233 self.writer = self.tlsConnection.writeAsync(writeBuffer)