Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Sources/Hobbits/TCP/TCPConnection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Foundation

class TCPConnection {

// @todo not sure if this is the way to go yet
fileprivate var input: InputStream
fileprivate var output: OutputStream

init(input: InputStream, output: OutputStream) {
self.input = input
self.output = output
}

func read(length: Int) -> [UInt8] {
if length == 0 {
return [UInt8]()
}

var buffer = [UInt8](repeating: 0, count: length)
input?.read(&buffer, maxLength: length)
return buffer
}

func write(_ response: Response) throws {
let data = String(describing: response)
let bytes = data.bytes

output?.write(bytes, maxLength: bytes.count)
}

func close() {

}

}
29 changes: 29 additions & 0 deletions Sources/Hobbits/TCP/TCPServer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Foundation

class TCPServer: NSObject {

func start() {
let service = NetService(domain: "", type: "", name: "")
service.publish(options: [NetService.Options.listenForConnections])
}

}

extension TCPServer: NetServiceDelegate {

public func netService(
_ sender: NetService,
didAcceptConnectionWith inputStream: InputStream,
outputStream: OutputStream
) {
let connection = TCPConnection(input: inputStream, output: outputStream)
// @todo handle connection
}
}

extension TCPServer {

func handle(connection: TCPConnection) {

}
}