Skip to content

Commit e28d18b

Browse files
authored
Merge pull request #1695 from HaishinKit/feature/srt-socket-options
New SRTSocketOption api.
2 parents ec8254c + d88547a commit e28d18b

File tree

8 files changed

+522
-529
lines changed

8 files changed

+522
-529
lines changed

Examples/Examples.xcodeproj/xcuserdata/shogo.endo.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
99
<BreakpointContent
1010
uuid = "00193141-C09C-45D1-89AA-9219E1CB7CB6"
11-
shouldBeEnabled = "Yes"
11+
shouldBeEnabled = "No"
1212
ignoreCount = "0"
1313
continueAfterRunningActions = "No"
1414
filePath = "Examples/iOS/AppDelegate.swift"

SRTHaishinKit/Sources/SRT/SRTConnection.swift

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,52 +29,42 @@ public actor SRTConnection: NetworkConnection {
2929
}
3030
}
3131

32-
private var socket: SRTSocket? {
33-
didSet {
34-
Task {
35-
guard let socket else {
36-
return
37-
}
38-
let networkMonitor = await socket.makeNetworkMonitor()
39-
self.networkMonitor = networkMonitor
40-
await networkMonitor.startRunning()
41-
for await event in await networkMonitor.event {
42-
for stream in streams {
43-
await stream.dispatch(event)
44-
}
45-
}
46-
}
47-
Task {
48-
await oldValue?.stopRunning()
49-
}
50-
}
51-
}
32+
private var socket: SRTSocket?
5233
private var streams: [SRTStream] = []
53-
private var listener: SRTSocket? {
54-
didSet {
55-
Task {
56-
await oldValue?.stopRunning()
57-
}
58-
}
59-
}
60-
private var networkMonitor: NetworkMonitor? {
61-
didSet {
62-
Task {
63-
await oldValue?.stopRunning()
64-
}
65-
}
66-
}
34+
private var listener: SRTSocket?
35+
private var networkMonitor: NetworkMonitor?
6736

6837
/// Creates an object.
6938
public init() {
7039
srt_startup()
40+
socket = SRTSocket()
7141
}
7242

7343
deinit {
7444
streams.removeAll()
7545
srt_cleanup()
7646
}
7747

48+
/// Gets a SRTSocketOption.
49+
public func getSocketOption(_ name: SRTSocketOption.Name) async throws -> SRTSocketOption? {
50+
try await socket?.getSocketOption(name)
51+
}
52+
53+
/// Sets a SRTSocketOption.
54+
public func setSocketOption(_ option: SRTSocketOption) async throws {
55+
if connected {
56+
guard option.name.restriction == .post else {
57+
throw Error.invalidState
58+
}
59+
try await socket?.setSocketOption(option)
60+
} else {
61+
guard option.name.restriction == .pre else {
62+
throw Error.invalidState
63+
}
64+
try await socket?.setSocketOption(option)
65+
}
66+
}
67+
7868
/// Open a two-way connection to an application on SRT Server.
7969
@available(*, deprecated, renamed: "connect")
8070
public func open(_ uri: URL?, mode: SRTMode = .caller) async throws {
@@ -112,27 +102,40 @@ public actor SRTConnection: NetworkConnection {
112102
do {
113103
let options = SRTSocketOption.from(uri: uri)
114104
let addr = sockaddr_in(mode.host(host), port: UInt16(port))
115-
let socket = SRTSocket()
116105
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Swift.Error>) in
117106
Task {
118107
do {
119-
try await socket.open(addr, mode: mode, options: options)
108+
try await socket?.open(addr, mode: mode, options: options)
120109
self.uri = uri
121110
switch mode {
122111
case .caller:
123-
self.socket = socket
112+
break
124113
case .listener:
125-
self.listener = socket
126-
self.socket = try await socket.accept()
127-
self.listener = nil
114+
listener = socket
115+
socket = try await listener?.accept()
116+
await listener?.stopRunning()
117+
listener = nil
128118
}
129-
connected = await self.socket?.status == .connected
119+
connected = await socket?.status == .connected
130120
continuation.resume()
131121
} catch {
132122
continuation.resume(throwing: error)
133123
}
134124
}
135125
}
126+
Task {
127+
guard let socket else {
128+
return
129+
}
130+
let networkMonitor = await socket.makeNetworkMonitor()
131+
self.networkMonitor = networkMonitor
132+
await networkMonitor.startRunning()
133+
for await event in await networkMonitor.event {
134+
for stream in streams {
135+
await stream.dispatch(event)
136+
}
137+
}
138+
}
136139
} catch {
137140
throw error
138141
}
@@ -143,14 +146,18 @@ public actor SRTConnection: NetworkConnection {
143146
guard uri != nil else {
144147
return
145148
}
149+
await networkMonitor?.stopRunning()
146150
networkMonitor = nil
147151
for stream in streams {
148152
await stream.close()
149153
}
154+
await socket?.stopRunning()
150155
socket = nil
156+
await listener?.stopRunning()
151157
listener = nil
152158
uri = nil
153159
connected = false
160+
socket = SRTSocket()
154161
}
155162

156163
func send(_ data: Data) async {

SRTHaishinKit/Sources/SRT/SRTLogger.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public enum SRTLogLevel: Sendable {
2929
}
3030
}
3131

32-
/// Constants that indicate the addition to levels the logging system has functional areas .
32+
/// Constants that indicate the addition to levels the logging system has functional areas.
3333
public enum SRTLogFunctionalArea: Int32, Sendable {
3434
/// General uncategorized log, for serious issues only
3535
case general = 0

SRTHaishinKit/Sources/SRT/SRTSocket.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ final actor SRTSocket {
8383
private(set) var isRunning = false
8484
private var perf: CBytePerfMon = .init()
8585
private var socket: SRTSOCKET = SRT_INVALID_SOCK
86-
private var options: [SRTSocketOption: any Sendable] = [:]
86+
private var options: [SRTSocketOption] = []
8787
private var outputs: AsyncStream<Data>.Continuation? {
8888
didSet {
8989
oldValue?.finish()
@@ -96,6 +96,7 @@ final actor SRTSocket {
9696
private lazy var incomingBuffer: Data = .init(count: Int(windowSizeC))
9797

9898
init() {
99+
socket = srt_create_socket()
99100
}
100101

101102
init(socket: SRTSOCKET) async throws {
@@ -108,12 +109,15 @@ final actor SRTSocket {
108109
}
109110
}
110111

111-
func open(_ addr: sockaddr_in, mode: SRTMode, options: [SRTSocketOption: any Sendable] = [:]) async throws {
112-
guard socket == SRT_INVALID_SOCK else {
113-
return
114-
}
115-
// prepare socket
116-
socket = srt_create_socket()
112+
func getSocketOption(_ name: SRTSocketOption.Name) throws -> SRTSocketOption {
113+
return try SRTSocketOption(name: name, socket: socket)
114+
}
115+
116+
func setSocketOption(_ option: SRTSocketOption) throws {
117+
try option.setSockflag(socket)
118+
}
119+
120+
func open(_ addr: sockaddr_in, mode: SRTMode, options: [SRTSocketOption] = []) async throws {
117121
if socket == SRT_INVALID_SOCK {
118122
throw makeSocketError()
119123
}
@@ -176,8 +180,8 @@ final actor SRTSocket {
176180
}
177181
}
178182

179-
private func configure(_ binding: SRTSocketOption.Binding) -> Bool {
180-
let failures = SRTSocketOption.configure(socket, binding: binding, options: options)
183+
private func configure(_ binding: SRTSocketOption.Restriction) -> Bool {
184+
let failures = SRTSocketOption.configure(socket, restriction: binding, options: options)
181185
guard failures.isEmpty else {
182186
logger.error(failures)
183187
return false

0 commit comments

Comments
 (0)