Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Mar 24, 2017
2 parents 8edb46b + 9f4913c commit 7a9feca
Show file tree
Hide file tree
Showing 42 changed files with 1,375 additions and 138 deletions.
20 changes: 20 additions & 0 deletions .travis.d/before-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

if [[ "$TRAVIS_OS_NAME" == "Linux" ]]; then
sudo apt-get install -y wget \
clang-3.6 libc6-dev make git libicu52 libicu-dev \
autoconf libtool pkg-config \
libblocksruntime-dev \
libkqueue-dev \
libpthread-workqueue-dev \
systemtap-sdt-dev \
libbsd-dev libbsd0 libbsd0-dbg \
curl libcurl4-openssl-dev \
libedit-dev \
python2.7 python2.7-dev

sudo update-alternatives --quiet \
--install /usr/bin/clang clang /usr/bin/clang-3.6 100
sudo update-alternatives --quiet \
--install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100
fi
File renamed without changes.
File renamed without changes.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ matrix:
dist: trusty
env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-3.0.2-release/ubuntu1404/swift-3.0.2-RELEASE/swift-3.0.2-RELEASE-ubuntu14.04.tar.gz"
sudo: required
- os: Linux
dist: trusty
env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-3.1-branch/ubuntu1404/swift-3.1-DEVELOPMENT-SNAPSHOT-2017-03-23-a/swift-3.1-DEVELOPMENT-SNAPSHOT-2017-03-23-a-ubuntu14.04.tar.gz"
sudo: required
- os: osx
osx_image: xcode8.2

before_install:
- ./xcconfig/before-install.sh
- ./.travis.d/before-install.sh

install:
- ./xcconfig/install.sh
- ./.travis.d/install.sh

script:
- export PATH="$HOME/usr/bin:$PATH"
Expand Down
401 changes: 400 additions & 1 deletion Noze.io.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ let package = Package(
.Target(name: "fs"),
.Target(name: "dns")
]),
Target(name: "dgram",
dependencies: [
.Target(name: "net"),
]),
Target(name: "process",
dependencies: [
.Target(name: "core"),
Expand Down
16 changes: 16 additions & 0 deletions Samples/udpd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control?
#
# Pods/

.DS_Store
xcuserdata
.build
Packages
*~
build
apidox

7 changes: 7 additions & 0 deletions Samples/udpd/GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# GNUmakefile

NOZE_DIR=../..
PACKAGE=$(notdir $(shell pwd))
$(PACKAGE)_SWIFT_MODULES = xsys core events streams net fs dns

include $(NOZE_DIR)/xcconfig/rules.make
9 changes: 9 additions & 0 deletions Samples/udpd/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import PackageDescription

let package = Package(
name: "udpd",
dependencies: [
.Package(url: "../..",
majorVersion: 0, minor: 5)
]
)
31 changes: 31 additions & 0 deletions Samples/udpd/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Noze.io UDP server
// - to compile in Swift 3 invoke: swift build
// - to run result: .build/debug/udpd
// - to try it:
// - Linux: nc.openbsd -u <interface> 10000
// - macOS: nc -vu4 localhost 10000

import Foundation // for String/Data
import dgram
import console

let sock = dgram.createSocket()
sock
.onListening { address in console.info ("dgram: bound to:", address) }
.onError { err in console.error("error:", err) }
.onMessage { (msg, from) in
console.log("received: \(msg) from \(from)")

guard let decoded = String(data: Data(msg), encoding: .utf8) else {
console.info("could not decode packet: \(msg)")
return
}

console.log(" decoded:",
decoded.replacingOccurrences(of: "\n", with: "\\n"))

let packet = [UInt8](decoded.uppercased().utf8)
console.log(" calling send on \(sock) with:", packet)
sock.send(packet, to: from)
}
.bind(10000)
2 changes: 1 addition & 1 deletion Sources/child_process/ChildProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class ChildProcess : ErrorEmitter {
if rc == -1 {
// TODO
let error = POSIXErrorCode(rawValue: xsys.errno)
print("ERROR: waitpid error: \(error)")
print("ERROR: waitpid error: \(error as Optional)")

if error?.rawValue == ECHILD {
print(" child gone already?")
Expand Down
4 changes: 2 additions & 2 deletions Sources/console/Console.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public extension ConsoleType { // Actual logging funcs

public func dir(_ obj: Any?) {
// TODO: implement more
log("\(obj)")
log("\(obj as Optional)")
}
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func writeValues<T: GWritableStreamType>(to t: T, _ values : [ Any? ])
_ = t.write(v)
}
else {
_ = t.write("\(v)")
_ = t.write("\(v as Optional)")
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/core/NozeCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class NozeCore : NozeModule {
workCount += 1

if debugRetain {
let hash = "\(filename)"
let hash = "\(filename as Optional)"
let old = retainDebugMap[hash] ?? 0
retainDebugMap[hash] = old + 1

Expand All @@ -63,7 +63,7 @@ public class NozeCore : NozeModule {
function: String? = #function)
{
if debugRetain {
let hash = "\(filename)"
let hash = "\(filename as Optional)"
let old = retainDebugMap[hash] ?? 0
assert(old > 0)
if old == 1 {
Expand All @@ -79,7 +79,7 @@ public class NozeCore : NozeModule {
workCount -= 1
if workCount == 0 {
if debugRetain {
print("TERMINATE[\(workCount): \(filename):\(line) \(function)")
print("TERMINATE[\(workCount): \(filename as Optional):\(line as Optional) \(function as Optional)")
}
maybeTerminate()
}
Expand Down
55 changes: 55 additions & 0 deletions Sources/dgram/Internals.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Module.swift
// Noze.io
//
// Created by https://github.com/lichtblau
//

import xsys
import fs

func recvfrom<AT: SocketAddress>(_ fd: FileDescriptor,
count: Int = 65535)
-> ( Error?, [ UInt8 ]?, AT?)
{
// TODO: inefficient init. Also: reuse buffers.
var buf = [ UInt8 ](repeating: 0, count: count)

// synchronous

var address = AT()
var addrlen = socklen_t(address.len)
let readCount = withUnsafeMutablePointer(to: &address) { ptr in
ptr.withMemoryRebound(to: xsys_sockaddr.self, capacity: 1) { bptr in
return xsys.recvfrom(fd.fd, &buf, count, 0, bptr, &addrlen)
}
}

guard readCount >= 0 else {
return ( POSIXErrorCode(rawValue: xsys.errno)!, nil, nil )
}

// TODO: super inefficient. how to declare sth which works with either?
buf = Array(buf[0..<readCount]) // TODO: slice to array, lame
return ( nil, buf, address )
}

func sendto(_ fd: FileDescriptor, data: [UInt8], to: SocketAddress) -> Error? {
// synchronous

var data = data
var toAddress = to
let addrlen = socklen_t(toAddress.len)
let writtenCount = withUnsafePointer(to: &toAddress) { ptr in
ptr.withMemoryRebound(to: xsys_sockaddr.self, capacity: 1) {
bptr in
return xsys.sendto(fd.fd, &data, data.count, 0, bptr, addrlen)
}
}

guard writtenCount >= 0 else {
return POSIXErrorCode(rawValue: xsys.errno)!
}

return nil
}
49 changes: 49 additions & 0 deletions Sources/dgram/Module.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Module.swift
// Noze.io
//
// Created by Helge Heß on 4/10/16.
// Changed by https://github.com/lichtblau
// Copyright © 2016 ZeeZide GmbH and Contributors. All rights reserved.
//

@_exported import core
import xsys

public class NozeDgram : NozeModule {
}
public let module = NozeDgram()


open class CreateOptions {
/// Version of IP stack (IPv4)
public var family : sa_family_t = sa_family_t(xsys.AF_INET)

public init() {}
}

/// Creates a new `dgram.Socket` object.
///
/// Optional onMessage block.
///
/// Sample:
///
/// let server = dgram.createSocket { sock in
/// print("connected")
/// }
/// .onError { error in
/// print("error: \(error)")
/// }
/// .bind(...) {
/// print("Server is listening on \($0.address)")
/// }
///
@discardableResult
public func createSocket(options : CreateOptions = CreateOptions(),
onMessage : MessageCB? = nil) -> Socket
{
// TODO: support options
let sock = Socket()
if let cb = onMessage { _ = sock.onMessage(handler: cb) }
return sock
}
44 changes: 44 additions & 0 deletions Sources/dgram/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Noze.io UDP / Datagram module

An UDP/datagram socket module modelled after the builtin Node
[dgram module](https://nodejs.org/api/dgram.html).

### Example

Small UDP server which echos back packets it receives:

```Swift
import dgram

sock = dgram.createSocket()
sock
.onMessage { (msg, from) in
sock.send(msg, to: from) // echo back
}
.bind(1337)
```

You can test that on Linux using

nc.openbsd -u localhost 1337

and on macOS via

nc -vu4 localhost 1337


### TODO

- [ ] make the Datagram socket a proper stream (e.g. a
`Duplex<Datagram,Datagram>`)
- [ ] sends are blocking and not queued

### Who

Noze.io is brought to you by
[The Always Right Institute](http://www.alwaysrightinstitute.com)
and
[ZeeZide](http://zeezide.de).

The `dgram` module was contributed by
[David Lichteblau](https://github.com/lichtblau).
Loading

0 comments on commit 7a9feca

Please sign in to comment.