@@ -13,7 +13,7 @@ internal final class SocketManager {
1313
1414 // MARK: - Properties
1515
16- let storage = Storage ( )
16+ private let storage = Storage ( )
1717
1818 // MARK: - Initialization
1919
@@ -100,35 +100,42 @@ internal final class SocketManager {
100100
101101 @discardableResult
102102 func write( _ data: Data , for fileDescriptor: SocketDescriptor ) async throws -> Int {
103- try await wait ( for: . write, fileDescriptor: fileDescriptor)
104- . write ( data)
103+ try await wait ( for: . write, fileDescriptor: fileDescriptor) {
104+ try $0. write ( data)
105+ }
106+
105107 }
106108
107109 @discardableResult
108110 func sendMessage( _ data: Data , for fileDescriptor: SocketDescriptor ) async throws -> Int {
109- try await wait ( for: . write, fileDescriptor: fileDescriptor)
110- . sendMessage ( data)
111+ try await wait ( for: . write, fileDescriptor: fileDescriptor) {
112+ try $0. sendMessage ( data)
113+ }
111114 }
112115
113116 @discardableResult
114117 func sendMessage< Address: SocketAddress > ( _ data: Data , to address: Address , for fileDescriptor: SocketDescriptor ) async throws -> Int {
115- try await wait ( for: . write, fileDescriptor: fileDescriptor)
116- . sendMessage ( data, to: address)
118+ try await wait ( for: . write, fileDescriptor: fileDescriptor) {
119+ try $0. sendMessage ( data, to: address)
120+ }
117121 }
118122
119123 func read( _ length: Int , for fileDescriptor: SocketDescriptor ) async throws -> Data {
120- try await wait ( for: . read, fileDescriptor: fileDescriptor)
121- . read ( length)
124+ try await wait ( for: . read, fileDescriptor: fileDescriptor) {
125+ try $0. read ( length)
126+ }
122127 }
123128
124129 func receiveMessage( _ length: Int , for fileDescriptor: SocketDescriptor ) async throws -> Data {
125- try await wait ( for: . read, fileDescriptor: fileDescriptor)
126- . receiveMessage ( length)
130+ try await wait ( for: . read, fileDescriptor: fileDescriptor) {
131+ try $0. receiveMessage ( length)
132+ }
127133 }
128134
129135 func receiveMessage< Address: SocketAddress > ( _ length: Int , fromAddressOf addressType: Address . Type = Address . self, for fileDescriptor: SocketDescriptor ) async throws -> ( Data , Address ) {
130- try await wait ( for: . read, fileDescriptor: fileDescriptor)
131- . receiveMessage ( length, fromAddressOf: addressType)
136+ try await wait ( for: . read, fileDescriptor: fileDescriptor) {
137+ try $0. receiveMessage ( length, fromAddressOf: addressType)
138+ }
132139 }
133140
134141 // MARK: - Private Methods
@@ -140,14 +147,20 @@ internal final class SocketManager {
140147 return socket
141148 }
142149
143- private func wait( for event: FileEvents , fileDescriptor: SocketDescriptor ) async throws -> SocketState {
150+ private func wait< T> (
151+ for event: FileEvents ,
152+ fileDescriptor: SocketDescriptor ,
153+ _ block: ( SocketState ) throws -> ( T )
154+ ) async throws -> T {
144155 // try to poll immediately and not wait
145156 let pendingEvent : Bool = try await self . storage. update { ( state: inout SocketManager . ManagerState ) throws -> ( Bool ) in
146157 try state. poll ( )
147158 return try state. events ( for: fileDescriptor) . contains ( event) == false
148159 }
160+ // try to execute immediately
149161 guard pendingEvent else {
150- return try await socket ( for: fileDescriptor)
162+ let socket = try await socket ( for: fileDescriptor)
163+ return try block ( socket)
151164 }
152165 // store continuation to resume when event is polled
153166 try await withThrowingContinuation ( for: fileDescriptor) { ( continuation: SocketContinuation < ( ) , Swift . Error > ) -> ( ) in
@@ -162,7 +175,9 @@ internal final class SocketManager {
162175 }
163176 }
164177 }
165- return try await socket ( for: fileDescriptor)
178+ // execute after waiting
179+ let socket = try await socket ( for: fileDescriptor)
180+ return try block ( socket)
166181 }
167182}
168183
@@ -207,6 +222,7 @@ extension SocketManager.ManagerState {
207222 }
208223
209224 mutating func poll( ) throws {
225+ // build poll descriptor array
210226 let sockets = self . sockets
211227 . lazy
212228 . sorted ( by: { $0. key. rawValue < $1. key. rawValue } )
@@ -217,6 +233,7 @@ extension SocketManager.ManagerState {
217233 pollDescriptors. append ( poll)
218234 }
219235 assert ( pollDescriptors. count == sockets. count)
236+ // poll sockets
220237 do {
221238 try pollDescriptors. poll ( )
222239 }
0 commit comments