Skip to content

Commit

Permalink
Sync with quark
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmes committed Aug 8, 2016
1 parent cdfd18c commit 3769475
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 127 deletions.
4 changes: 2 additions & 2 deletions Sources/AsyncConnection.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
public protocol AsyncConnection: AsyncStream {
func open(timingOut deadline: Double, completion: ((Void) throws -> AsyncConnection) -> Void) throws
func open(deadline: Double, completion: ((Void) throws -> AsyncConnection) -> Void) throws
}

extension AsyncConnection {
public func open(completion: ((Void) throws -> AsyncConnection) -> Void) throws {
try open(timingOut: .never, completion: completion)
try open(deadline: .never, completion: completion)
}
}
18 changes: 9 additions & 9 deletions Sources/AsyncDrain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public final class AsyncDrain: DataRepresentable, AsyncStream {
}

public convenience init() {
self.init(for: [])
self.init(buffer: [])
}

public init(for stream: AsyncReceivingStream, timingOut deadline: Double = .never, completion: @escaping ((Void) throws -> AsyncDrain) -> Void) {
public init(stream: AsyncInputStream, deadline: Double = .never, completion: @escaping ((Void) throws -> AsyncDrain) -> Void) {
var buffer: Data = []

if stream.closed {
Expand All @@ -24,7 +24,7 @@ public final class AsyncDrain: DataRepresentable, AsyncStream {
return
}

stream.receive(upTo: 1024, timingOut: deadline) { [unowned self] getData in
stream.read(upTo: 1024, deadline: deadline) { [unowned self] getData in
do {
let chunk = try getData()
buffer.bytes += chunk.bytes
Expand All @@ -43,12 +43,12 @@ public final class AsyncDrain: DataRepresentable, AsyncStream {
}
}

public init(for buffer: Data) {
public init(buffer: Data) {
self.buffer = buffer
}

public convenience init(for buffer: DataRepresentable) {
self.init(for: buffer.data)
public convenience init(buffer: DataRepresentable) {
self.init(buffer: buffer.data)
}

public func close() throws {
Expand All @@ -58,7 +58,7 @@ public final class AsyncDrain: DataRepresentable, AsyncStream {
closed = true
}

public func receive(upTo byteCount: Int, timingOut deadline: Double = .never, completion: ((Void) throws -> Data) -> Void) {
public func read(upTo byteCount: Int, deadline: Double = .never, completion: @escaping ((Void) throws -> Data) -> Void) {
if byteCount >= buffer.count {
completion { [unowned self] in
try self.close()
Expand All @@ -75,12 +75,12 @@ public final class AsyncDrain: DataRepresentable, AsyncStream {
}
}

public func send(_ data: Data, timingOut deadline: Double = .never, completion: ((Void) throws -> Void) -> Void) {
public func write(_ data: Data, deadline: Double = .never, completion: @escaping ((Void) throws -> Void) -> Void) {
buffer += data.bytes
completion {}
}

public func flush(timingOut deadline: Double = .never, completion: ((Void) throws -> Void) -> Void) {
public func flush(deadline: Double = .never, completion: @escaping ((Void) throws -> Void) -> Void) {
buffer = []
completion {}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/AsyncHost.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
public protocol AsyncHost {
func accept(timingOut deadline: Double, completion: ((Void) throws -> AsyncStream) -> Void)
func accept(deadline: Double, completion: ((Void) throws -> AsyncStream) -> Void)
}

extension AsyncHost {
public func accept(completion: ((Void) throws -> AsyncStream) -> Void) {
accept(timingOut: .never, completion: completion)
accept(deadline: .never, completion: completion)
}
}
41 changes: 23 additions & 18 deletions Sources/AsyncStream.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
public protocol AsyncSending {
func send(_ data: Data, timingOut deadline: Double, completion: ((Void) throws -> Void) -> Void)
func flush(timingOut deadline: Double, completion: ((Void) throws -> Void) -> Void)
public protocol AsyncWritable {
func write(_ data: Data, deadline: Double, completion: @escaping ((Void) throws -> Void) -> Void)
func flush(deadline: Double, completion: @escaping ((Void) throws -> Void) -> Void)
}

public protocol AsyncReceiving {
func receive(upTo byteCount: Int, timingOut deadline: Double, completion: ((Void) throws -> Data) -> Void)
}

public protocol AsyncSendingStream: Closable, AsyncSending {}
public protocol AsyncReceivingStream: Closable, AsyncReceiving {}
public protocol AsyncStream: AsyncSendingStream, AsyncReceivingStream {}
extension AsyncWritable {
public func write(_ data: Data, completion: @escaping ((Void) throws -> Void) -> Void) {
write(data, deadline: .never, completion: completion)
}

extension AsyncSending {
public func send(_ data: Data, completion: ((Void) throws -> Void) -> Void) {
send(data, timingOut: .never, completion: completion)
public func write(_ convertible: DataConvertible, deadline: Double = .never, completion: @escaping ((Void) throws -> Void) -> Void) {
write(convertible.data, deadline: deadline, completion: completion)
}
public func flush(completion: ((Void) throws -> Void) -> Void) {
flush(timingOut: .never, completion: completion)

public func flush(completion: @escaping ((Void) throws -> Void) -> Void) {
flush(deadline: .never, completion: completion)
}
}

extension AsyncReceiving {
public func receive(upTo byteCount: Int, completion: ((Void) throws -> Data) -> Void) {
receive(upTo: byteCount, timingOut: .never, completion: completion)
public protocol AsyncReadable {
func read(upTo byteCount: Int, deadline: Double, completion: @escaping ((Void) throws -> Data) -> Void)
}

extension AsyncReadable {
public func read(upTo byteCount: Int, completion: @escaping ((Void) throws -> Data) -> Void) {
read(upTo: byteCount, deadline: .never, completion: completion)
}
}

public protocol AsyncOutputStream: Closable, AsyncWritable {}
public protocol AsyncInputStream: Closable, AsyncReadable {}
public protocol AsyncStream: AsyncOutputStream, AsyncInputStream {}
2 changes: 1 addition & 1 deletion Sources/C7.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if swift(>=3.0)
#else
public typealias ErrorProtocol = ErrorType
public typealias Error = ErrorType
public typealias RangeReplaceableCollection = RangeReplaceableCollectionType
public typealias MutableCollection = MutableCollectionType
public typealias Sequence = SequenceType
Expand Down
8 changes: 0 additions & 8 deletions Sources/Closable.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Sources/Connection.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
public protocol Connection: Stream {
func open(timingOut deadline: Double) throws
func open(deadline: Double) throws
}

extension Connection {
public func open() throws {
try open(timingOut: .never)
try open(deadline: .never)
}
}
2 changes: 1 addition & 1 deletion Sources/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ extension String: DataConvertible {
}
#else
public init(data: Data) throws {
struct Error: ErrorProtocol {}
struct Error: Error {}
var string = ""
var decoder = UTF8()
var generator = data.generate()
Expand Down
27 changes: 11 additions & 16 deletions Sources/Drain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@ public final class Drain: DataRepresentable, Stream {
public var closed = false

public var data: Data {
if !closed {
return buffer
}
return []
return buffer
}

public convenience init() {
self.init(for: [])
self.init(buffer: [])
}

public init(for stream: ReceivingStream, timingOut deadline: Double = .never) {
public init(stream: InputStream, deadline: Double = .never) {
var buffer: Data = []

if stream.closed {
self.closed = true
}

while !stream.closed {
if let chunk = try? stream.receive(upTo: 1024, timingOut: deadline) {
if let chunk = try? stream.read(upTo: 1024, deadline: deadline) {
buffer.bytes += chunk.bytes
} else {
break
Expand All @@ -31,12 +28,12 @@ public final class Drain: DataRepresentable, Stream {
self.buffer = buffer
}

public init(for buffer: Data) {
public init(buffer: Data) {
self.buffer = buffer
}

public convenience init(for buffer: DataRepresentable) {
self.init(for: buffer.data)
public convenience init(buffer: DataRepresentable) {
self.init(buffer: buffer.data)
}

public func close() throws {
Expand All @@ -46,23 +43,21 @@ public final class Drain: DataRepresentable, Stream {
closed = true
}

public func receive(upTo byteCount: Int, timingOut deadline: Double = .never) throws -> Data {
public func read(upTo byteCount: Int, deadline: Double = .never) throws -> Data {
if byteCount >= buffer.count {
try close()
return buffer
}

let data = buffer[0..<byteCount]
let data = buffer.prefix(upTo: byteCount)
buffer.removeFirst(byteCount)

return Data(data)
}

public func send(_ data: Data, timingOut deadline: Double = .never) throws {
public func write(_ data: Data, deadline: Double = .never) throws {
buffer += data.bytes
}

public func flush(timingOut deadline: Double = .never) throws {
buffer = []
}
public func flush(deadline: Double = .never) throws {}
}
35 changes: 35 additions & 0 deletions Sources/File.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public enum FileMode {
case read
case createWrite
case truncateWrite
case appendWrite
case readWrite
case createReadWrite
case truncateReadWrite
case appendReadWrite
}

public protocol File : Stream {
init(path: String, mode: FileMode) throws
var fileExtension: String? { get }
static func changeWorkingDirectory(path: String) throws

func read(_ byteCount: Int, deadline: Double) throws -> Data
func readAll(deadline: Double) throws -> Data
}

extension File {
public func read(_ byteCount: Int) throws -> Data {
return try read(byteCount, deadline: .never)
}

public func readAll() throws -> Data {
return try readAll(deadline: .never)
}
}

extension File {
public init(path: String) throws {
try self.init(path: path, mode: .read)
}
}
6 changes: 3 additions & 3 deletions Sources/Host.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
public protocol Host {
func accept(timingOut deadline: Double) throws -> Stream
func accept(deadline: Double) throws -> Stream
}

extension Host {
func accept() throws -> Stream {
return try accept(timingOut: .never)
public func accept() throws -> Stream {
return try accept(deadline: .never)
}
}
65 changes: 39 additions & 26 deletions Sources/Stream.swift
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
public protocol Sending: AsyncSending {
func send(_ data: Data, timingOut deadline: Double) throws
func flush(timingOut deadline: Double) throws
public protocol Closable {
var closed: Bool { get }
func close() throws
}

public protocol Receiving: AsyncReceiving {
func receive(upTo byteCount: Int, timingOut deadline: Double) throws -> Data
public enum ClosableError: Error {
case alreadyClosed
}

public protocol SendingStream: Closable, Sending {}
public protocol ReceivingStream: Closable, Receiving {}
public protocol Stream: SendingStream, ReceivingStream {}
public protocol Writable: AsyncWritable {
func write(_ data: Data, deadline: Double) throws
func flush(deadline: Double) throws
}

extension Sending {
public func send(_ data: Data, timingOut deadline: Double, completion: ((Void) throws -> Void) -> Void) {
completion { try self.send(data, timingOut: deadline) }
extension Writable {
public func write(_ data: Data) throws {
try write(data, deadline: .never)
}

public func flush(timingOut deadline: Double, completion: ((Void) throws -> Void) -> Void) {
completion { try self.flush(timingOut: deadline) }
public func write(_ convertible: DataConvertible, deadline: Double = .never) throws {
try write(convertible.data, deadline: deadline)
}

public func flush() throws {
try flush(deadline: .never)
}
}

extension Sending {
public func send(_ data: Data) throws {
try send(data, timingOut: .never)
extension Writable {
public func write(_ data: Data, deadline: Double, completion: @escaping ((Void) throws -> Void) -> Void) {
completion { try self.write(data, deadline: deadline) }
}
public func flush() throws {
try flush(timingOut: .never)

public func flush(deadline: Double, completion: @escaping ((Void) throws -> Void) -> Void) {
completion { try self.flush(deadline: deadline) }
}
}

extension Receiving {
public func receive(upTo byteCount: Int, timingOut deadline: Double, completion: ((Void) throws -> Data) -> Void) {
completion { try self.receive(upTo: byteCount, timingOut: deadline) }
public protocol Readable: AsyncReadable {
func read(upTo byteCount: Int, deadline: Double) throws -> Data
}

extension Readable {
public func read(upTo byteCount: Int) throws -> Data {
return try read(upTo: byteCount, deadline: .never)
}
}

extension Receiving {
public func receive(upTo byteCount: Int) throws -> Data {
return try receive(upTo: byteCount, timingOut: .never)
extension Readable {
public func read(upTo byteCount: Int, deadline: Double, completion: @escaping ((Void) throws -> Data) -> Void) {
completion { try self.read(upTo: byteCount, deadline: deadline) }
}
}
}

public protocol OutputStream: Closable, Writable, AsyncOutputStream {}
public protocol InputStream: Closable, Readable, AsyncInputStream {}
public protocol Stream: OutputStream, InputStream, AsyncStream {}
Loading

0 comments on commit 3769475

Please # to comment.