-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced Ktor with SwiftNIO, Removed all Ktor references
- Loading branch information
Showing
19 changed files
with
639 additions
and
357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
iosApp/iosApp.xcworkspace/xcshareddata/swiftpm/Package.resolved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "swift-atomics", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-atomics.git", | ||
"state" : { | ||
"revision" : "cd142fd2f64be2100422d658e7411e39489da985", | ||
"version" : "1.2.0" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-collections", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-collections.git", | ||
"state" : { | ||
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb", | ||
"version" : "1.1.0" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-nio", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-nio.git", | ||
"state" : { | ||
"revision" : "fc63f0cf4e55a4597407a9fc95b16a2bc44b4982", | ||
"version" : "2.64.0" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-system", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-system.git", | ||
"state" : { | ||
"revision" : "025bcb1165deab2e20d4eaba79967ce73013f496", | ||
"version" : "1.2.1" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import Foundation | ||
import NIO | ||
import NIOFoundationCompat | ||
import shared | ||
|
||
class SpProtocolApple: SyncplayProtocol { | ||
private var channel: Channel? | ||
private var eventLoopGroup: EventLoopGroup? | ||
|
||
|
||
override func connectSocket() { | ||
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount) | ||
eventLoopGroup = group | ||
|
||
print("Bootstrapping...") | ||
|
||
let bootstrap = ClientBootstrap(group: group) | ||
.connectTimeout(TimeAmount.seconds(10)) | ||
.channelInitializer { channel in | ||
channel.pipeline.addHandler(Reader(p: self)) | ||
} | ||
|
||
let host = session.serverHost | ||
let port = Int(session.serverPort) | ||
|
||
do { | ||
channel = try bootstrap.connect(host: host, port: port).wait() | ||
} catch { | ||
print(error) | ||
syncplayCallback?.onConnectionFailed() | ||
return | ||
} | ||
|
||
if channel != nil { | ||
print("Connected!") | ||
syncplayCallback?.onConnected() | ||
} | ||
} | ||
|
||
|
||
override func isSocketValid() -> Bool { | ||
return channel?.isActive ?? false | ||
} | ||
|
||
override func endConnection(terminating: Bool) { | ||
try? channel?.close().wait() | ||
try? eventLoopGroup?.syncShutdownGracefully() | ||
|
||
if terminating { | ||
terminateScope() | ||
} | ||
} | ||
|
||
override func writeActualString(s: String) { | ||
guard let channel = channel else { | ||
syncplayCallback?.onDisconnected() | ||
return | ||
} | ||
|
||
let data = s.data(using: .utf8)! | ||
let buffer = channel.allocator.buffer(bytes: data) | ||
|
||
channel.writeAndFlush(buffer).whenComplete { result in | ||
do { | ||
try result.get() | ||
} catch { | ||
self.syncplayCallback?.onDisconnected() | ||
print("Error writing to channel: \(error)") | ||
} | ||
} | ||
} | ||
|
||
override func upgradeTls() { | ||
// TLS setup for SwiftNIO | ||
} | ||
} | ||
|
||
class Reader: ChannelInboundHandler { | ||
typealias InboundIn = ByteBuffer | ||
let p: SpProtocolApple | ||
|
||
init(p: SpProtocolApple) { | ||
self.p = p | ||
} | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
var buffer = unwrapInboundIn(data) | ||
let readableBytes = buffer.readableBytes | ||
|
||
if let received = buffer.readString(length: readableBytes) { | ||
self.p.routeInScope() { | ||
JsonHandlerKt.handleJson(protocol: self.p, json: received) | ||
} | ||
} | ||
} | ||
|
||
func channelReadComplete(context: ChannelHandlerContext) { | ||
context.flush() | ||
} | ||
|
||
func errorCaught(context: ChannelHandlerContext, error: Error) { | ||
print("Reader exception: \(error)") | ||
p.syncplayCallback?.onDisconnected() | ||
context.close(promise: nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.