blob: a3c63894a794f901e22e1652213c4ae2233cdbe4 [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
Alexander Edgea89036c2020-02-05 17:03:53 +000034class TSocketServer<InProtocol: TProtocol, OutProtocol: TProtocol, Processor: TProcessor, Service> {
Chris Simpsona9b6c702018-04-08 07:11:37 -040035 var socketFileHandle: FileHandle
36 var processingQueue = DispatchQueue(label: "TSocketServer.processing",
37 qos: .background,
38 attributes: .concurrent)
39 var serviceHandler: Service
Alexander Edgea89036c2020-02-05 17:03:53 +000040 let processor: Processor
Chris Simpsona9b6c702018-04-08 07:11:37 -040041
42 public init(port: Int,
43 service: Service,
44 inProtocol: InProtocol.Type,
45 outProtocol: OutProtocol.Type,
Alexander Edgea89036c2020-02-05 17:03:53 +000046 processor: Processor) throws {
Chris Simpsona9b6c702018-04-08 07:11:37 -040047 // set service handler
48 self.serviceHandler = service
Alexander Edgea89036c2020-02-05 17:03:53 +000049 self.processor = processor
Chris Simpsona9b6c702018-04-08 07:11:37 -040050
51 // create a socket
52 var fd: Int32 = -1
53 #if os(Linux)
54 let sock = CFSocketCreate(kCFAllocatorDefault, PF_INET, Int32(SOCK_STREAM.rawValue), Int32(IPPROTO_TCP), 0, nil, nil)
55 #else
56 let sock = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, 0, nil, nil)
57 #endif
58 if sock != nil {
59 CFSocketSetSocketFlags(sock, CFSocketGetSocketFlags(sock) & ~kCFSocketCloseOnInvalidate)
60
61 fd = CFSocketGetNative(sock)
62 var yes = 1
63 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, UInt32(MemoryLayout<Int>.size))
64
65 #if os(Linux)
66 var addr = sockaddr_in(sin_family: sa_family_t(AF_INET),
67 sin_port: in_port_t(port.bigEndian),
68 sin_addr: in_addr(s_addr: in_addr_t(0)),
69 sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
70 #else
71 var addr = sockaddr_in(sin_len: UInt8(MemoryLayout<sockaddr_in>.size),
72 sin_family: sa_family_t(AF_INET),
73 sin_port: in_port_t(port.bigEndian),
74 sin_addr: in_addr(s_addr: in_addr_t(0)),
75 sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
76 #endif
77
78 let ptr = withUnsafePointer(to: &addr) {
79 return UnsafePointer<UInt8>(OpaquePointer($0))
80 }
81
82 let address = Data(bytes: ptr, count: MemoryLayout<sockaddr_in>.size)
83
84 let cfaddr = address.withUnsafeBytes {
85 CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, $0, address.count, nil)
86 }
87 if CFSocketSetAddress(sock, cfaddr) != CFSocketError.success { //kCFSocketSuccess {
88 CFSocketInvalidate(sock)
89 print("TSocketServer: Could not bind to address")
90 throw TTransportError(error: .notOpen, message: "Could not bind to address")
91 }
92
93 } else {
94 print("TSocketServer: No server socket")
95 throw TTransportError(error: .notOpen, message: "Could not create socket")
96 }
97
98 // wrap it in a file handle so we can get messages from it
99 socketFileHandle = FileHandle(fileDescriptor: fd, closeOnDealloc: true)
100
101 // throw away our socket
102 CFSocketInvalidate(sock)
103
104 // register for notifications of accepted incoming connections
105 _ = NotificationCenter.default.addObserver(forName: .NSFileHandleConnectionAccepted,
106 object: nil, queue: nil) {
107 [weak self] notification in
108 guard let strongSelf = self else { return }
109 strongSelf.connectionAccepted(strongSelf.socketFileHandle)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400110 }
111
112 // tell socket to listen
113 socketFileHandle.acceptConnectionInBackgroundAndNotify()
114
115 print("TSocketServer: Listening on TCP port \(port)")
116 }
117
118 deinit {
119 NotificationCenter.default.removeObserver(self)
120 }
121
122 func connectionAccepted(_ socket: FileHandle) {
123 // Now that we have a client connected, handle the request on queue
124 processingQueue.async {
125 self.handleClientConnection(socket)
126 }
127 }
128
129 func handleClientConnection(_ clientSocket: FileHandle) {
130
131 let transport = TFileHandleTransport(fileHandle: clientSocket)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400132
133 let inProtocol = InProtocol(on: transport)
134 let outProtocol = OutProtocol(on: transport)
135
136 do {
137 try processor.process(on: inProtocol, outProtocol: outProtocol)
138 } catch let error {
139 print("Error processign request: \(error)")
140 }
141 DispatchQueue.main.async {
142 NotificationCenter.default
143 .post(name: Notification.Name(rawValue: TSocketServerClientConnectionFinished),
144 object: self,
Alexander Edgea89036c2020-02-05 17:03:53 +0000145 userInfo: [TSocketServerProcessorKey: self.processor,
Chris Simpsona9b6c702018-04-08 07:11:37 -0400146 TSocketServerTransportKey: transport])
147 }
148 }
149}