Ind.ie is now Small Technology Foundation.
Commit f912da9d authored by Stefan van den Oord's avatar Stefan van den Oord

Moved SocketConnection into separate file

parent 49386aa1
......@@ -114,6 +114,7 @@
F898AD801B98D7CB00644CA5 /* GlobalDiscoveryServerStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = F898AD7F1B98D7CB00644CA5 /* GlobalDiscoveryServerStub.swift */; };
F8A1437C1B979D3000F2D1CF /* QueryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8A1437B1B979D3000F2D1CF /* QueryTests.swift */; };
F8A1437E1B979D9200F2D1CF /* Query.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8A1437D1B979D9200F2D1CF /* Query.swift */; };
F8A692D71D3FB04C0095EFB1 /* SocketConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8A692D61D3FB04C0095EFB1 /* SocketConnection.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
......@@ -250,6 +251,7 @@
F898AD7F1B98D7CB00644CA5 /* GlobalDiscoveryServerStub.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlobalDiscoveryServerStub.swift; sourceTree = "<group>"; };
F8A1437B1B979D3000F2D1CF /* QueryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryTests.swift; sourceTree = "<group>"; };
F8A1437D1B979D9200F2D1CF /* Query.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Query.swift; sourceTree = "<group>"; };
F8A692D61D3FB04C0095EFB1 /* SocketConnection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketConnection.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -449,6 +451,7 @@
5CA368F51B9237F60081F0F6 /* TLS.swift */,
C016451023B4710BBC9D9B60 /* RemoteDevice.swift */,
C016434FECCC131B93021CAC /* Connection.swift */,
F8A692D61D3FB04C0095EFB1 /* SocketConnection.swift */,
);
path = synchronization;
sourceTree = "<group>";
......@@ -831,6 +834,7 @@
4D90356E19F91965009C9A34 /* IndexUpdate.swift in Sources */,
4D90356A19F70CE8009C9A34 /* FileInfo.swift in Sources */,
4D11A2C719EACC33002C6861 /* Folder.swift in Sources */,
F8A692D71D3FB04C0095EFB1 /* SocketConnection.swift in Sources */,
F8A1437E1B979D9200F2D1CF /* Query.swift in Sources */,
4D9D5A5B19F533F70058D1B8 /* Options.swift in Sources */,
4D6A477019E860CF005D7C23 /* Ping.swift in Sources */,
......
import Foundation
import SwiftBytes
import CocoaAsyncSocket
public class LocalDevice: NSObject, GCDAsyncSocketDelegate {
......@@ -47,74 +46,3 @@ public class LocalDevice: NSObject, GCDAsyncSocketDelegate {
var tls: TLS = TLS()
var queue = dispatch_get_main_queue()
}
class SocketConnection: NSObject, GCDAsyncSocketDelegate, Connection {
let socket: GCDAsyncSocket
var incomingMessageBytes: [UInt8] = []
var onMessage: (Message) -> ()
var onError: (NSError) -> ()
init(socket: GCDAsyncSocket, onMessage: (Message) -> (), onError: (NSError) -> ()) {
self.socket = socket
self.onMessage = onMessage
self.onError = onError
super.init()
self.socket.synchronouslySetDelegate(self)
readHeader()
}
func send(message: Message) {
let data = NSData(bytes: message.encode().serialize())
self.socket.writeData(data, withTimeout:-1, tag: 0)
}
func disconnect() {
self.socket.disconnect()
}
func socket(socket: GCDAsyncSocket!, didReadData data: NSData!, withTag tag: Int) {
incomingMessageBytes += data.bytes
if tag == readingHeaderTag {
onHeaderRead(incomingMessageBytes)
}
if tag == readingContentsTag {
onContentsRead(incomingMessageBytes)
}
}
private func readHeader() {
socket.readDataToLength(8, withTimeout: timeout, tag: readingHeaderTag)
}
private func readContents(length length: UInt32) {
socket.readDataToLength(UInt(length), withTimeout: timeout, tag: readingContentsTag)
}
private func onHeaderRead(headerBytes: [UInt8]) {
let length = concatenateBytes(headerBytes[4], headerBytes[5], headerBytes[6], headerBytes[7])
if length > 0 {
readContents(length: length)
} else {
onContentsRead(headerBytes)
}
}
private func onContentsRead(messageBytes: [UInt8]) {
incomingMessageBytes = []
if let encodedMessage = EncodedMessage.deserialize(messageBytes) {
let message = Message.decode(encodedMessage)!
onMessage(message)
readHeader()
}
else {
disconnect()
onError(NSError(domain:PulseNSError.domain, code:PulseNSError.protocolError, userInfo:nil))
}
}
let readingHeaderTag = 0;
let readingContentsTag = 1;
let timeout: NSTimeInterval = 30;
}
\ No newline at end of file
import Foundation
import SwiftBytes
import CocoaAsyncSocket
class SocketConnection: NSObject, GCDAsyncSocketDelegate, Connection {
let socket: GCDAsyncSocket
var incomingMessageBytes: [UInt8] = []
var onMessage: (Message) -> ()
var onError: (NSError) -> ()
init(socket: GCDAsyncSocket, onMessage: (Message) -> (), onError: (NSError) -> ()) {
self.socket = socket
self.onMessage = onMessage
self.onError = onError
super.init()
self.socket.synchronouslySetDelegate(self)
readHeader()
}
func send(message: Message) {
let data = NSData(bytes: message.encode().serialize())
self.socket.writeData(data, withTimeout:-1, tag: 0)
}
func disconnect() {
self.socket.disconnect()
}
func socket(socket: GCDAsyncSocket!, didReadData data: NSData!, withTag tag: Int) {
incomingMessageBytes += data.bytes
if tag == readingHeaderTag {
onHeaderRead(incomingMessageBytes)
}
if tag == readingContentsTag {
onContentsRead(incomingMessageBytes)
}
}
private func readHeader() {
socket.readDataToLength(8, withTimeout: timeout, tag: readingHeaderTag)
}
private func readContents(length length: UInt32) {
socket.readDataToLength(UInt(length), withTimeout: timeout, tag: readingContentsTag)
}
private func onHeaderRead(headerBytes: [UInt8]) {
let length = concatenateBytes(headerBytes[4], headerBytes[5], headerBytes[6], headerBytes[7])
if length > 0 {
readContents(length: length)
} else {
onContentsRead(headerBytes)
}
}
private func onContentsRead(messageBytes: [UInt8]) {
incomingMessageBytes = []
if let encodedMessage = EncodedMessage.deserialize(messageBytes) {
let message = Message.decode(encodedMessage)!
onMessage(message)
readHeader()
}
else {
disconnect()
onError(NSError(domain:PulseNSError.domain, code:PulseNSError.protocolError, userInfo:nil))
}
}
let readingHeaderTag = 0;
let readingContentsTag = 1;
let timeout: NSTimeInterval = 30;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment