@@ -56,6 +56,7 @@ class SecureServer extends EventEmitter implements ServerInterface
5656{
5757 private $ tcp ;
5858 private $ encryption ;
59+ private $ context ;
5960
6061 /**
6162 * Creates a secure TLS server and starts waiting for incoming connections
@@ -94,39 +95,36 @@ class SecureServer extends EventEmitter implements ServerInterface
9495 * and/or PHP version.
9596 * Passing unknown context options has no effect.
9697 *
97- * Advanced usage: Internally, the `SecureServer` has to set the required
98- * context options on the underlying stream resources.
99- * It should therefor be used with an unmodified `Server` instance as first
100- * parameter so that it can allocate an empty context resource which this
101- * class uses to set required TLS context options.
102- * Failing to do so may result in some hard to trace race conditions,
103- * because all stream resources will use a single, shared default context
104- * resource otherwise.
98+ * Advanced usage: Despite allowing any `ServerInterface` as first parameter,
99+ * you SHOULD pass an unmodified `Server` instance as first parameter, unless you
100+ * know what you're doing.
101+ * Internally, the `SecureServer` has to set the required TLS context options on
102+ * the underlying stream resources.
103+ * These resources are not exposed through any of the interfaces defined in this
104+ * package, but only through the `React\Stream\Stream` class.
105+ * The unmodified `Server` class is guaranteed to emit connections that implement
106+ * the `ConnectionInterface` and also extend the `Stream` class in order to
107+ * expose these underlying resources.
108+ * If you use a custom `ServerInterface` and its `connection` event does not
109+ * meet this requirement, the `SecureServer` will emit an `error` event and
110+ * then close the underlying connection.
105111 *
106- * @param Server $tcp
112+ * @param ServerInterface| Server $tcp
107113 * @param LoopInterface $loop
108114 * @param array $context
109- * @throws ConnectionException
110115 * @see Server
111116 * @link http://php.net/manual/en/context.ssl.php for TLS context options
112117 */
113- public function __construct (Server $ tcp , LoopInterface $ loop , array $ context )
118+ public function __construct (ServerInterface $ tcp , LoopInterface $ loop , array $ context )
114119 {
115- if (!is_resource ($ tcp ->master )) {
116- throw new ConnectionException ('TCP server already shut down ' );
117- }
118-
119120 // default to empty passphrase to surpress blocking passphrase prompt
120121 $ context += array (
121122 'passphrase ' => ''
122123 );
123124
124- foreach ($ context as $ name => $ value ) {
125- stream_context_set_option ($ tcp ->master , 'ssl ' , $ name , $ value );
126- }
127-
128125 $ this ->tcp = $ tcp ;
129126 $ this ->encryption = new StreamEncryption ($ loop );
127+ $ this ->context = $ context ;
130128
131129 $ that = $ this ;
132130 $ this ->tcp ->on ('connection ' , function ($ connection ) use ($ that ) {
@@ -156,6 +154,10 @@ public function handleConnection(ConnectionInterface $connection)
156154 return ;
157155 }
158156
157+ foreach ($ this ->context as $ name => $ value ) {
158+ stream_context_set_option ($ connection ->stream , 'ssl ' , $ name , $ value );
159+ }
160+
159161 $ that = $ this ;
160162
161163 $ this ->encryption ->enable ($ connection )->then (
0 commit comments