Swimat formats the following code:
import Foundation
public final class Server {
private var sock: Int32
private var addr: sockaddr_in?
private var bufferSize = 1024
public init?(_ port: UInt16) {
sock = socket(AF_INET, Int32(SOCK_STREAM), 0)
if sock < 0 {
addr = nil
return nil
}
// Set the option to reuse the address on the server socket
var reuseOn: Int32 = 1
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseOn, socklen_t(sizeof(Int32)))
// Set the receive buffer size on the server socket
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &bufferSize, socklen_t(sizeof(Int)))
// Bind to any available interface
addr = sockaddr_in(
sin_len: __uint8_t(sizeof(sockaddr_in)),
sin_family: sa_family_t(AF_INET),
sin_port: htons(port),
sin_addr: in_addr(s_addr: in_addr_t(0)),
sin_zero: (0, 0, 0, 0, 0, 0, 0, 0)
)
let serverBind = bind(sock, sockaddr_cast(&addr), socklen_t(sizeof(sockaddr_in)))
if serverBind >= 0 {
print("Server started at port \(port)")
}
}
public func listenAndServe() {
if listen(sock, 10) < 0 {
exit(1)
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
while true {
let client = accept(self.sock, nil, nil)
if client > 0 {
// This prevents crashes when blocking calls are pending and the app is backgrounded
var no_sig_pipe: Int32 = 1;
setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &no_sig_pipe, socklen_t(sizeof(Int32)));
var buffer = [UInt8](count: self.bufferSize, repeatedValue: 0)
let readBytes = read(client, &buffer, self.bufferSize)
let data = NSData(bytes: buffer, length: readBytes)
if let dataString = String(data: data, encoding: NSUTF8StringEncoding) {
print(dataString)
// TODO: Parse the request
}
let msg = "Hello World"
let contentLength = msg.utf8.count
self.echo(client, "HTTP/1.1 200 OK\n")
self.echo(client, "Server: Swift Web Server\n")
self.echo(client, "Content-length: \(contentLength)\n")
self.echo(client, "Content-type: text-plain\n")
self.echo(client, "\r\n")
self.echo(client, msg)
print("Response sent: '\(msg)' - Length: \(contentLength)")
shutdown(client, SHUT_RDWR)
close(client)
}
}
}
}
private func echo(socket: Int32, _ output: String) {
output.withCString { (bytes) in
send(socket, bytes, Int(strlen(bytes)), 0)
}
}
}
like this:
import Foundation
public final class Server {
private var sock: Int32
private var addr: sockaddr_in?
private var bufferSize = 1024
public init?(_ port: UInt16) {
sock = socket(AF_INET, Int32(SOCK_STREAM), 0)
if sock <0{
addr=nil
returnnil
}
//Settheoptiontoreusetheaddressontheserversocket
varreuseOn:Int32=1
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseOn, socklen_t(sizeof(Int32)))
//Setthereceivebuffersizeontheserversocket
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &bufferSize, socklen_t(sizeof(Int)))
//Bindtoanyavailableinterface
addr=sockaddr_in(
sin_len:__uint8_t(sizeof(sockaddr_in)),
sin_family:sa_family_t(AF_INET),
sin_port:htons(port),
sin_addr:in_addr(s_addr:in_addr_t(0)),
sin_zero:(0, 0, 0, 0, 0, 0, 0, 0)
)
letserverBind=bind(sock, sockaddr_cast(&addr), socklen_t(sizeof(sockaddr_in)))
ifserverBind> = 0 {
print("Server started at port \(port)")
}
}
public func listenAndServe() {
if listen(sock, 10) <0{
exit(1)
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)){
whiletrue{
letclient=accept(self.sock, nil, nil)
ifclient> 0 {
// This prevents crashes when blocking calls are pending and the app is backgrounded
var no_sig_pipe: Int32 = 1;
setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &no_sig_pipe, socklen_t(sizeof(Int32))) ;
var buffer = [UInt8] (count: self.bufferSize, repeatedValue: 0)
let readBytes = read(client, &buffer, self.bufferSize)
let data = NSData(bytes: buffer, length: readBytes)
if let dataString = String(data: data, encoding: NSUTF8StringEncoding) {
print(dataString)
// TODO: Parse the request
}
let msg = "Hello World"
let contentLength = msg.utf8.count
self.echo(client, "HTTP/1.1 200 OK\n")
self.echo(client, "Server: Swift Web Server\n")
self.echo(client, "Content-length: \(contentLength)\n")
self.echo(client, "Content-type: text-plain\n")
self.echo(client, "\r\n")
self.echo(client, msg)
print("Response sent: '\(msg)' - Length: \(contentLength)")
shutdown(client, SHUT_RDWR)
close(client)
}
}
}
}
private func echo(socket: Int32, _ output: String) {
output.withCString {(bytes) in
send(socket, bytes, Int(strlen(bytes)), 0)
}
}
}
It seems to be an issue related to the lack of parentheses around the condition in the first if
statement in the initializer.
I'm using Xcode 7.1.1 and installed Swimat via Alcatraz.