Skip to content

Commit

Permalink
Merge pull request #9 from briancroom/prevent-sigpipe
Browse files Browse the repository at this point in the history
Prevent crashing due to SIGPIPE
  • Loading branch information
kylef committed Dec 16, 2015
2 parents 5579c42 + d8ead8e commit faebd06
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Sources/Socket.swift
Expand Up @@ -60,6 +60,12 @@ class Socket {
guard setsockopt(descriptor, SOL_SOCKET, SO_REUSEADDR, &value, socklen_t(sizeof(Int32))) != -1 else {
throw SocketError(function: "setsockopt()")
}

#if !os(Linux)
guard setsockopt(descriptor, SOL_SOCKET, SO_NOSIGPIPE, &value, socklen_t(sizeof(Int32))) != -1 else {
throw SocketError(function: "setsockopt()")
}
#endif
}

init(descriptor: Descriptor) {
Expand Down Expand Up @@ -103,7 +109,12 @@ class Socket {

func send(output: String) {
output.withCString { bytes in
system_send(descriptor, bytes, Int(strlen(bytes)), 0)
#if os(Linux)
let flags = Int32(MSG_NOSIGNAL)
#else
let flags = Int32(0)
#endif
system_send(descriptor, bytes, Int(strlen(bytes)), flags)
}
}

Expand Down

0 comments on commit faebd06

Please sign in to comment.