Skip to content

Commit

Permalink
Merge pull request #7 from Zewo/migrate_to_data
Browse files Browse the repository at this point in the history
Migrating from [int8] to data
  • Loading branch information
paulofaria committed Mar 15, 2016
2 parents 4f528ab + 45b1088 commit df4a2b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PackageDescription
let package = Package(
name: "SwiftZMQ",
dependencies: [
.Package(url: "https://github.com/Zewo/CZeroMQ.git", majorVersion: 1)
.Package(url: "https://github.com/Zewo/CZeroMQ.git", majorVersion: 1),
.Package(url: "https://github.com/Zewo/Data.git", majorVersion: 0, minor: 2)
]
)
28 changes: 12 additions & 16 deletions Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// SOFTWARE.

import CZeroMQ
import Data

public struct SendMode : OptionSetType {
public let rawValue: Int
Expand Down Expand Up @@ -107,6 +108,10 @@ public final class Socket {

return true
}
public func send(data: Data, mode: SendMode = []) throws -> Bool {
var data = data
return try self.send(&data.bytes, length: data.count, mode: mode)
}

func sendImmutable(buffer: UnsafePointer<Void>, length: Int, mode: SendMode = []) throws -> Bool {
let result = zmq_send_const(socket, buffer, length, Int32(mode.rawValue))
Expand Down Expand Up @@ -137,20 +142,18 @@ public final class Socket {
return message
}

public func receive(bufferSize bufferSize: Int = 1024, mode: ReceiveMode = []) throws -> [Int8]? {
var buffer = [Int8](count: bufferSize, repeatedValue: 0)
let result = zmq_recv(socket, &buffer, bufferSize, Int32(mode.rawValue))

public func receive(bufferSize bufferSize: Int = 1024, mode: ReceiveMode = []) throws -> Data? {
var data = Data.bufferWithSize(bufferSize)
let result = zmq_recv(socket, &data.bytes, bufferSize, Int32(mode.rawValue))
if result == -1 && zmq_errno() == EAGAIN {
return nil
}

if result == -1 {
throw Error.lastError
}

let bufferEnd = min(Int(result), bufferSize)
return Array(buffer[0 ..< bufferEnd])
return data[0 ..< bufferEnd]
}

public func close() throws {
Expand Down Expand Up @@ -857,21 +860,14 @@ extension SecurityMechanism {
}

extension Socket {
public func send(buffer: [Int8], mode: SendMode = []) throws -> Bool {
var buffer = buffer
return try send(&buffer, length: buffer.count, mode: mode)
}

public func sendString(string: String, mode: SendMode = []) throws -> Bool {
var buffer = string.utf8.map { Int8($0) }
return try send(&buffer, length: buffer.count, mode: mode)
return try send(Data(string), mode: mode)
}

public func receiveString(mode: ReceiveMode = []) throws -> String? {
guard var buffer = try receive(mode: mode) else {
guard let buffer = try receive(mode: mode) else {
return nil
}
buffer.append(0)
return String.fromCString(buffer)
return try? String(data: buffer)
}
}

0 comments on commit df4a2b2

Please sign in to comment.