A simple GCD based socket library for Swift.
SwiftSockets is kind of a demo on how to integrate Swift with raw C APIs. More for stealing Swift coding ideas than for actually using the code in a real world project. In most real world Swift apps you have access to Cocoa, use it.
It also comes with a great Echo daemon as a demo, it's always there if you need a chat.
Note: This is my first Swift project. Any suggestions on how to improve the code are welcome. I expect lots and lots :-)
###Targets
Updated to use Swift v0.2.0 (aka Xcode 6.3).
The project includes three targets:
- ARISockets
- ARIEchoServer
- ARIFetch
I suggest you start out looking at the ARIEchoServer.
####ARISockets
A framework containing the socket classes and relevant extensions. It takes a bit of inspiration from the SOPE NGStreams library.
Server Sample:
let socket = PassiveSocket<sockaddr_in>(address: sockaddr_in(port: 4242))
.listen(dispatch_get_global_queue(0, 0), backlog: 5) {
println("Wait, someone is attempting to talk to me!")
$0.close()
println("All good, go ahead!")
}
Client Sample:
let socket = ActiveSocket<sockaddr_in>()
.onRead {
let (count, block, errno) = $0.read()
if count < 1 {
println("EOF, or great error handling \(errno).")
return
}
println("Answer to ring,ring is: \(count) bytes: \(block)")
}
.connect("127.0.0.1:80") {
socket.write("Ring, ring!\r\n")
}
####ARIEchoServer
Great echo server. This is actually a Cocoa app. Compile it, run it, then
connect to it in the Terminal.app via telnet 1337
.
####ARIFetch
Connects a socket to some end point, sends an HTTP/1.0 GET request with some awesome headers, then shows the results the server sends. Cocoa app.
Why HTTP/1.0? Avoids redirects on www.apple.com :-)
###Goals
- Max line length: 80 characters
- Great error handling
- PS style great error handling
- println() error handling
- Real error handling
- Twisted (no blocking reads or writes)
- Async reads and writes
- Never block on reads
- Never block on listen
- Async connect()
- Async reads and writes
- Support all types of Unix sockets & addresses
- IPv4
- IPv6 (I guess this should work too)
- Unix domain sockets
- Datagram sockets
- No NS'ism
- Use as many language features Swift provides
- Generics
- Generic function
- typealias
- Closures
- weak self
- trailing closures
- implicit parameters
- Unowned
- Extensions on structs
- Extensions to organize classes
- Protocols on structs
- Tuples, with labels
- Trailing closures
- @Lazy
- Pure Swift weak delegates via @class
- Optionals
- Convenience initializers
- Class variables on structs
- CConstPointer, CConstVoidPointer
- withCString {}
- UnsafePointer
- sizeof()
- Standard Protocols
- Printable
- BooleanType (aka LogicValue)
- OutputStreamType
- Equatable
- Equatable on Enums with Associated Values
- Hashable
- SequenceType (GeneratorOf)
- Literal Convertibles
- StringLiteralConvertible
- IntegerLiteralConvertible
- Left shift AND right shift
- Enums on steroids
- Dynamic type system, reflection
- Operator overloading
- UCS-4 identifiers (🐔🐔🐔)
-
RTF source code with images and code sections in different fonts - Nested classes/types
- Patterns
- Use wildcard pattern to ignore value
- Literal Convertibles
- @autoclosure
- unsafeBitCast (was reinterpretCast)
- final
- Nil coalescing operator
- dynamic
- Generics
###Why?!
This is an experiment to get acquainted with Swift. To check whether something real can be implemented in 'pure' Swift. Meaning, without using any Objective-C Cocoa classes (no NS'ism). Or in other words: Can you use Swift without writing all the 'real' code in wrapped Objective-C? :-)
###Contact