@@ -75,7 +75,7 @@ private[net] trait SocketGroupCompanionPlatform { self: SocketGroup.type =>
7575 }
7676 }
7777
78- setup.flatMap (ch => Resource .eval( connect(ch))).flatMap( Socket .forAsync(_ ))
78+ setup.evalMap (ch => connect(ch) *> Socket .forAsync(ch ))
7979 }
8080
8181 def serverResource (
@@ -108,32 +108,34 @@ private[net] trait SocketGroupCompanionPlatform { self: SocketGroup.type =>
108108 sch : AsynchronousServerSocketChannel
109109 ): Stream [F , Socket [F ]] = {
110110 def go : Stream [F , Socket [F ]] = {
111- def acceptChannel : F [AsynchronousSocketChannel ] =
112- Async [F ].async[AsynchronousSocketChannel ] { cb =>
113- Async [F ]
114- .delay {
115- sch.accept(
116- null ,
117- new CompletionHandler [AsynchronousSocketChannel , Void ] {
118- def completed (ch : AsynchronousSocketChannel , attachment : Void ): Unit =
119- cb(Right (ch))
120- def failed (rsn : Throwable , attachment : Void ): Unit =
121- cb(Left (rsn))
122- }
123- )
124- }
125- .as(Some (Async [F ].delay(sch.close())))
111+ def acceptChannel = Resource .makeFull[F , AsynchronousSocketChannel ] { poll =>
112+ poll {
113+ Async [F ].async[AsynchronousSocketChannel ] { cb =>
114+ Async [F ]
115+ .delay {
116+ sch.accept(
117+ null ,
118+ new CompletionHandler [AsynchronousSocketChannel , Void ] {
119+ def completed (ch : AsynchronousSocketChannel , attachment : Void ): Unit =
120+ cb(Right (ch))
121+ def failed (rsn : Throwable , attachment : Void ): Unit =
122+ cb(Left (rsn))
123+ }
124+ )
125+ }
126+ .as(Some (Async [F ].delay(sch.close())))
127+ }
126128 }
129+ }(ch => Async [F ].delay(if (ch.isOpen) ch.close else ()))
127130
128131 def setOpts (ch : AsynchronousSocketChannel ) =
129132 Async [F ].delay {
130133 options.foreach(o => ch.setOption(o.key, o.value))
131134 }
132135
133- Stream .eval(acceptChannel.attempt).flatMap {
134- case Left (_) => Stream .empty[F ]
135- case Right (accepted) =>
136- Stream .resource(Socket .forAsync(accepted).evalTap(_ => setOpts(accepted)))
136+ Stream .resource(acceptChannel.attempt).flatMap {
137+ case Left (_) => Stream .empty[F ]
138+ case Right (accepted) => Stream .eval(setOpts(accepted) *> Socket .forAsync(accepted))
137139 } ++ go
138140 }
139141
0 commit comments