blob: d70020ec31b5d86fdd58457787a5c3dd511f27b9 [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)
99
100 // register for notifications of accepted incoming connections
101 _ = NotificationCenter.default.addObserver(forName: .NSFileHandleConnectionAccepted,
102 object: nil, queue: nil) {
103 [weak self] notification in
104 guard let strongSelf = self else { return }
Jano Svitok1edf3292020-02-28 12:44:59 +0100105 guard let clientSocket = notification.userInfo?[NSFileHandleNotificationFileHandleItem] as? FileHandle else { return }
106 strongSelf.connectionAccepted(clientSocket)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400107 }
108
109 // tell socket to listen
110 socketFileHandle.acceptConnectionInBackgroundAndNotify()
111
112 print("TSocketServer: Listening on TCP port \(port)")
113 }
114
115 deinit {
116 NotificationCenter.default.removeObserver(self)
117 }
118
Jano Svitok1edf3292020-02-28 12:44:59 +0100119 func connectionAccepted(_ clientSocket: FileHandle) {
Chris Simpsona9b6c702018-04-08 07:11:37 -0400120 // Now that we have a client connected, handle the request on queue
121 processingQueue.async {
Jano Svitok1edf3292020-02-28 12:44:59 +0100122 self.handleClientConnection(clientSocket)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400123 }
Jano Svitok1edf3292020-02-28 12:44:59 +0100124
125 // continue accepting connections
126 socketFileHandle.acceptConnectionInBackgroundAndNotify()
127 }
128
129 open func createTransport(fileHandle: FileHandle) -> TTransport {
130 return TFileHandleTransport(fileHandle: fileHandle)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400131 }
132
133 func handleClientConnection(_ clientSocket: FileHandle) {
Jano Svitok1edf3292020-02-28 12:44:59 +0100134 let transport = createTransport(fileHandle: clientSocket)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400135 let inProtocol = InProtocol(on: transport)
136 let outProtocol = OutProtocol(on: transport)
137
138 do {
Jano Svitok1edf3292020-02-28 12:44:59 +0100139 while true {
140 try processor.process(on: inProtocol, outProtocol: outProtocol)
141 }
Chris Simpsona9b6c702018-04-08 07:11:37 -0400142 } catch let error {
143 print("Error processign request: \(error)")
144 }
145 DispatchQueue.main.async {
146 NotificationCenter.default
147 .post(name: Notification.Name(rawValue: TSocketServerClientConnectionFinished),
148 object: self,
Alexander Edgea89036c2020-02-05 17:03:53 +0000149 userInfo: [TSocketServerProcessorKey: self.processor,
Chris Simpsona9b6c702018-04-08 07:11:37 -0400150 TSocketServerTransportKey: transport])
151 }
152 }
153}
Jano Svitok1edf3292020-02-28 12:44:59 +0100154
155public class TFramedSocketServer<InProtocol: TProtocol, OutProtocol: TProtocol, Processor: TProcessor>: TSocketServer<InProtocol, OutProtocol, Processor> {
156 open override func createTransport(fileHandle: FileHandle) -> TTransport {
157 return TFramedTransport(transport: super.createTransport(fileHandle: fileHandle))
158 }
159}