File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99import Foundation
1010import Dispatch
1111
12+ private let sourceReadQ = DispatchQueue ( label: " prorsum.net.source-read.queue " )
13+
14+ private let tcpIncommingRequestHandleQ = DispatchQueue ( label: " prorsum.tcp.concurrent-queue " , attributes: . concurrent)
15+
1216public final class TCPServer {
1317 let stream : TCPStream
1418
@@ -45,19 +49,21 @@ public final class TCPServer {
4549 try stream. socket. bind ( host: host, port: port)
4650 }
4751
52+ /**
53+ * Kitura HTTPServerのように、acceptはループで行い、read/writeを非同期で
54+ * DispatchWorkItemを用いている模様
55+ */
4856 public func listen( backlog: Int = 1024 ) throws {
4957 try stream. socket. listen ( backlog: backlog)
5058
51- watcher = DispatchSource . makeReadSource ( fileDescriptor: stream. socket. fd, queue: . main )
59+ watcher = DispatchSource . makeReadSource ( fileDescriptor: stream. socket. fd, queue: sourceReadQ )
5260
5361 watcher? . setEventHandler { [ unowned self] in
54- do {
55- let client = try TCPStream ( socket : self . stream . socket . accept ( ) )
56- go {
62+ tcpIncommingRequestHandleQ . async {
63+ do {
64+ let client = try TCPStream ( socket : self . stream . socket . accept ( ) )
5765 self . handler ( client)
58- }
59- } catch {
60- go {
66+ } catch {
6167 self . onError ? ( error)
6268 }
6369 }
You can’t perform that action at this time.
0 commit comments