blob: 27b4b5521f1f1089b9057fbf352426930fb7a08a [file] [log] [blame]
Chris Simpsona9b6c702018-04-08 07:11:37 -04001/*
2* Licensed to the Apache Software Foundation (ASF) under one
3* or more contributor license agreements. See the NOTICE file
4* distributed with this work for additional information
5* regarding copyright ownership. The ASF licenses this file
6* to you under the Apache License, Version 2.0 (the
7* "License"); you may not use this file except in compliance
8* with the License. You may obtain a copy of the License at
9*
10* http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing,
13* software distributed under the License is distributed on an
14* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15* KIND, either express or implied. See the License for the
16* specific language governing permissions and limitations
17* under the License.
18*/
19
20#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
21 import Darwin
22#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android)
23 import Glibc
24 import Dispatch
25#endif
26
27import Foundation
28import CoreFoundation
29
30public let TSocketServerClientConnectionFinished = "TSocketServerClientConnectionFinished"
31public let TSocketServerProcessorKey = "TSocketServerProcessor"
32public let TSocketServerTransportKey = "TSocketServerTransport"
33
Jano Svitok1edf3292020-02-28 12:44:59 +010034open class TSocketServer<InProtocol: TProtocol, OutProtocol: TProtocol, Processor: TProcessor> {
Chris Simpsona9b6c702018-04-08 07:11:37 -040035 var socketFileHandle: FileHandle
36 var processingQueue = DispatchQueue(label: "TSocketServer.processing",
37 qos: .background,
38 attributes: .concurrent)
Alexander Edgea89036c2020-02-05 17:03:53 +000039 let processor: Processor
Chris Simpsona9b6c702018-04-08 07:11:37 -040040
41 public init(port: Int,
Chris Simpsona9b6c702018-04-08 07:11:37 -040042 inProtocol: InProtocol.Type,
43 outProtocol: OutProtocol.Type,
Alexander Edgea89036c2020-02-05 17:03:53 +000044 processor: Processor) throws {
Alexander Edgea89036c2020-02-05 17:03:53 +000045 self.processor = processor
Chris Simpsona9b6c702018-04-08 07:11:37 -040046
47 // create a socket
48 var fd: Int32 = -1
49 #if os(Linux)
50 let sock = CFSocketCreate(kCFAllocatorDefault, PF_INET, Int32(SOCK_STREAM.rawValue), Int32(IPPROTO_TCP), 0, nil, nil)
51 #else
52 let sock = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, 0, nil, nil)
53 #endif
54 if sock != nil {
Jano Svitok3127e4a2020-04-27 09:12:42 +020055 CFSocketSetSocketFlags(sock, CFSocketGetSocketFlags(sock) & ~CFOptionFlags(kCFSocketCloseOnInvalidate))
Chris Simpsona9b6c702018-04-08 07:11:37 -040056
57 fd = CFSocketGetNative(sock)
58 var yes = 1
59 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, UInt32(MemoryLayout<Int>.size))
Jano Svitok1edf3292020-02-28 12:44:59 +010060 let inPort = in_port_t(UInt16(truncatingIfNeeded: port).bigEndian)
Chris Simpsona9b6c702018-04-08 07:11:37 -040061 #if os(Linux)
62 var addr = sockaddr_in(sin_family: sa_family_t(AF_INET),
Jano Svitok1edf3292020-02-28 12:44:59 +010063 sin_port: inPort,
Chris Simpsona9b6c702018-04-08 07:11:37 -040064 sin_addr: in_addr(s_addr: in_addr_t(0)),
65 sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
66 #else
67 var addr = sockaddr_in(sin_len: UInt8(MemoryLayout<sockaddr_in>.size),
68 sin_family: sa_family_t(AF_INET),
Jano Svitok1edf3292020-02-28 12:44:59 +010069 sin_port: inPort,
Chris Simpsona9b6c702018-04-08 07:11:37 -040070 sin_addr: in_addr(s_addr: in_addr_t(0)),
71 sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
72 #endif
73
74 let ptr = withUnsafePointer(to: &addr) {
75 return UnsafePointer<UInt8>(OpaquePointer($0))
76 }
77
78 let address = Data(bytes: ptr, count: MemoryLayout<sockaddr_in>.size)
79
80 let cfaddr = address.withUnsafeBytes {
Jano Svitok1edf3292020-02-28 12:44:59 +010081 CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, $0.bindMemory(to: UInt8.self).baseAddress!, address.count, kCFAllocatorNull)
Chris Simpsona9b6c702018-04-08 07:11:37 -040082 }
83 if CFSocketSetAddress(sock, cfaddr) != CFSocketError.success { //kCFSocketSuccess {
84 CFSocketInvalidate(sock)
85 print("TSocketServer: Could not bind to address")
86 throw TTransportError(error: .notOpen, message: "Could not bind to address")
87 }
88
89 } else {
90 print("TSocketServer: No server socket")
91 throw TTransportError(error: .notOpen, message: "Could not create socket")
92 }
93
94 // wrap it in a file handle so we can get messages from it
95 socketFileHandle = FileHandle(fileDescriptor: fd, closeOnDealloc: true)
96
97 // throw away our socket
98 CFSocketInvalidate(sock)
Kino Roya9da9eb2022-10-07 23:13:01 -070099
Chris Simpsona9b6c702018-04-08 07:11:37 -0400100 print("TSocketServer: Listening on TCP port \(port)")
Kino Roya9da9eb2022-10-07 23:13:01 -0700101
102 // tell socket to listen
103 acceptConnectionInBackgroundAndNotify(handle: socketFileHandle)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400104 }
Kino Roy29d87732023-02-20 22:32:43 -0800105
106 public init(path: String,
107 inProtocol: InProtocol.Type,
108 outProtocol: OutProtocol.Type,
109 processor: Processor) throws {
110 self.processor = processor
111 // create a socket
112 let socket = UnixSocket(path: path)
113 let fd = socket.fd
114
115 if fd == -1 {
116 print("TSocketServer: No server socket")
117 throw TTransportError(error: .notOpen, message: "Could not create socket")
118 }
119
120 // wrap it in a file handle so we can get messages from it
121 socketFileHandle = FileHandle(fileDescriptor: fd, closeOnDealloc: true)
122
123 // register for notifications of accepted incoming connections
124 _ = NotificationCenter.default.addObserver(forName: .NSFileHandleConnectionAccepted,
125 object: nil, queue: nil) {
126 [weak self] notification in
127 guard let strongSelf = self else { return }
128 guard let clientSocket = notification.userInfo?[NSFileHandleNotificationFileHandleItem] as? FileHandle else { return }
129 strongSelf.connectionAccepted(clientSocket)
130 }
131
132 let bindRes = socket.bind()
133 guard bindRes == 0 else {
134 print("TServerSocket: bind failed")
135 throw TTransportError(error: .notOpen, message: "Could not create socket")
136 }
137 let listenRes = listen(fd, 1024)
138 guard listenRes == 0 else {
139 print("TServerSocket: listen failed")
140 throw TTransportError(error: .notOpen, message: "Could not create socket")
141 }
142
143 // tell socket to listen
144 acceptConnectionInBackgroundAndNotify(handle: socketFileHandle)
145
146 print("TSocketServer: Listening on unix path \(path)")
147 }
Kino Roya9da9eb2022-10-07 23:13:01 -0700148
149 private func acceptConnectionInBackgroundAndNotify(handle: FileHandle) {
150 DispatchQueue(label: "TSocketServer.connectionAccept").async {
151 let acceptedFD = accept(handle.fileDescriptor, nil, nil)
152 DispatchQueue.main.async {
153 self.connectionAccepted(FileHandle(fileDescriptor: acceptedFD))
154 }
155 }
Chris Simpsona9b6c702018-04-08 07:11:37 -0400156 }
Kino Roy29d87732023-02-20 22:32:43 -0800157
Jano Svitok1edf3292020-02-28 12:44:59 +0100158 func connectionAccepted(_ clientSocket: FileHandle) {
Chris Simpsona9b6c702018-04-08 07:11:37 -0400159 // Now that we have a client connected, handle the request on queue
160 processingQueue.async {
Jano Svitok1edf3292020-02-28 12:44:59 +0100161 self.handleClientConnection(clientSocket)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400162 }
Jano Svitok1edf3292020-02-28 12:44:59 +0100163
164 // continue accepting connections
Kino Roya9da9eb2022-10-07 23:13:01 -0700165 acceptConnectionInBackgroundAndNotify(handle: socketFileHandle)
Jano Svitok1edf3292020-02-28 12:44:59 +0100166 }
167
168 open func createTransport(fileHandle: FileHandle) -> TTransport {
169 return TFileHandleTransport(fileHandle: fileHandle)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400170 }
171
172 func handleClientConnection(_ clientSocket: FileHandle) {
Jano Svitok1edf3292020-02-28 12:44:59 +0100173 let transport = createTransport(fileHandle: clientSocket)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400174 let inProtocol = InProtocol(on: transport)
175 let outProtocol = OutProtocol(on: transport)
176
177 do {
Jano Svitok1edf3292020-02-28 12:44:59 +0100178 while true {
179 try processor.process(on: inProtocol, outProtocol: outProtocol)
180 }
Chris Simpsona9b6c702018-04-08 07:11:37 -0400181 } catch let error {
Kevin Wojniak6a7278b2021-10-06 11:43:56 -0700182 print("Error processing request: \(error)")
Chris Simpsona9b6c702018-04-08 07:11:37 -0400183 }
184 DispatchQueue.main.async {
185 NotificationCenter.default
186 .post(name: Notification.Name(rawValue: TSocketServerClientConnectionFinished),
Kino Roya9da9eb2022-10-07 23:13:01 -0700187 object: nil,
Alexander Edgea89036c2020-02-05 17:03:53 +0000188 userInfo: [TSocketServerProcessorKey: self.processor,
Chris Simpsona9b6c702018-04-08 07:11:37 -0400189 TSocketServerTransportKey: transport])
190 }
191 }
192}
Jano Svitok1edf3292020-02-28 12:44:59 +0100193
194public class TFramedSocketServer<InProtocol: TProtocol, OutProtocol: TProtocol, Processor: TProcessor>: TSocketServer<InProtocol, OutProtocol, Processor> {
195 open override func createTransport(fileHandle: FileHandle) -> TTransport {
196 return TFramedTransport(transport: super.createTransport(fileHandle: fileHandle))
197 }
198}