-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
1,375 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
Oops, something went wrong.