Skip to content

Repository files navigation

AIO

Asynchronous non-blocking io with syncronous API. No callbacks.

Package.swift

.package(url:"https://github.com/swiftstack/aio.git",.branch("dev"))

Network

Zero-cost socket abstraction designed for cooperative multitasking.

Usage

You can find this code and more in examples.

Sync

letsocket=trySocket()

Async

import Async
import Network
async{letsocket=trySocket()
// use non-blocking api
}
loop.run()
lethello=[UInt8]("Hello, World!".utf8)letempty=[UInt8](repeating:0, count: hello.count +1)

TCP

async{letsocket=trySocket().bind(to:"127.0.0.1", port:1111).listen()letclient=try socket.accept()
_ =try client.send(bytes: hello)}async{letsocket=trySocket().connect(to:"127.0.0.1", port:1111)varbuffer= empty
_ =try socket.receive(to:&buffer)}

UDP

letudpServerAddress=trySocket.Address("127.0.0.1", port:2222)async{letsocket=trySocket(type:.datagram).bind(to: udpServerAddress)varbuffer= empty
varclient:Socket.Address?=nil
_ =try socket.receive(to:&buffer, from:&client)
_ =try socket.send(bytes: hello, to: client!)}async{letsocket=trySocket(type:.datagram)varbuffer= empty
_ =try socket.send(bytes: hello, to: udpServerAddress)
_ =try socket.receive(to:&buffer)}

TCP IPv6

async{letsocket=trySocket(family:.inet6).bind(to:"::1", port:3333).listen()letclient=try socket.accept()
_ =try client.send(bytes: hello)}async{letsocket=trySocket(family:.inet6).connect(to:"::1", port:3333)varbuffer= empty
_ =try socket.receive(to:&buffer)}

UNIX

#if os(Linux)lettype:Socket.SocketType=.sequenced
#elselettype:Socket.SocketType=.stream
#endifunlink("/tmp/socketexample.sock")async{letsocket=trySocket(family:.unix, type: type).bind(to:"/tmp/socketexample.sock").listen()letclient=try socket.accept()
_ =try client.send(bytes: hello)}async{letsocket=trySocket(family:.unix, type: type).connect(to:"/tmp/socketexample.sock")varbuffer= empty
_ =try socket.receive(to:&buffer)}

Socket API

finalclass Socket {enumFamily{case inet, inet6, unspecified, unix
}enumSocketType{case stream, datagram, sequenced
}enumAddress{init(_:String, port:UInt16?=nil)throwsinit(ip4:String, port:UInt16)throwsinit(ip6:String, port:UInt16)throwsinit(unix:String)throws}init(family:Family=.tcp, type:SocketType=.stream)throwsfunc bind(to:Address)throws-> Socket
func listen()throws-> Socket
func accept(deadline:Time=.distantFuture)throws->Socketfunc connect(to:Address, deadline:Time=.distantFuture)throws->Socketfunc close()throwsfunc send(bytes:UnsafeRawPointer, count:Int, deadline:Time=.distantFuture)throws->Intfunc send(bytes:UnsafeRawPointer, count:Int, to:Address, deadline:Time=.distantFuture)throws->Intfunc receive(to:UnsafeMutableRawPointer, count:Int, deadline:Time=.distantFuture)throws->Intfunc receive(to:UnsafeMutableRawPointer, count:Int, from:inoutAddress?, deadline:Time=.distantFuture)throws->Int}extension Socket {func bind(to:String, port:UInt16)throws-> Socket
func bind(to:String)throws-> Socket
func connect(to:String, port:UInt16, deadline:Time=.distantFuture)throws->Socketfunc connect(to:String, deadline:Time=.distantFuture)throws->Socketfunc send(bytes:[UInt8], deadline:Time=.distantFuture)throws->Intfunc send(bytes:[UInt8], to:Address, deadline:Time=.distantFuture)throws->Intfunc receive(to:inout[UInt8], deadline:Time=.distantFuture)throws->Intfunc receive(to:inout[UInt8], from:inoutAddress?, deadline:Time=.distantFuture)throws->Int}

About

Non-blocking Socket, Server, Client, DNS

Topics

Resources

Stars

15 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages