0001 /* 0002 The MIT License (MIT) 0003 0004 Copyright (c) 2015 Shun Takebayashi 0005 0006 Permission is hereby granted, free of charge, to any person obtaining a copy 0007 of this software and associated documentation files (the "Software"), to deal 0008 in the Software without restriction, including without limitation the rights 0009 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0010 copies of the Software, and to permit persons to whom the Software is 0011 furnished to do so, subject to the following conditions: 0012 0013 The above copyright notice and this permission notice shall be included in all 0014 copies or substantial portions of the Software. 0015 0016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0019 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0020 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0021 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 0022 SOFTWARE. 0023 */ 0024 0025 #if os(OSX) 0026 import Darwin 0027 #elseif os(Linux) 0028 import Glibc 0029 #endif 0030 0031 public struct Socket{ 0032 0033 static let defaultDomain
HTTPServer.swift:37 var socket: SocketHTTPServer.swift:40 public init?(socket: Socket, addr: SocketAddress) {HTTPServer.swift:73 let reader = SocketReader(socket: Socket(raw: client))Reader.swift:49 let socket: SocketReader.swift:51 init(socket: Socket) {Socket.swift:42 public init?(domain: Int32 = defaultDomain, type: Int32 = defaultType, proto: Int32 = 0) {Socket.swift:42 public init?(domain: Int32 = defaultDomain, type: Int32 = defaultType, proto: Int32 = 0) {: Int32 = AF_INET 0034 #if os(OSX) 0035 static let defaultType
Socket.swift:42 public init?(domain: Int32 = defaultDomain, type: Int32 = defaultType, proto: Int32 = 0) {: Int32 = SOCK_STREAM 0036 #else 0037 static let defaultType: Int32 = Int32(SOCK_STREAM.rawValue) 0038 #endif 0039 0040 var raw
Socket.swift:42 public init?(domain: Int32 = defaultDomain, type: Int32 = defaultType, proto: Int32 = 0) {: Int32 0041 0042 public init?(domain: Int32 = defaultDomain, type: Int32 = defaultType, proto: Int32 = 0) { 0043 raw = socket(domain, Int32(type), proto) 0044 if raw <= 0 { 0045 return nil 0046 } 0047 } 0048 0049 public init
HTTPServer.swift:65 if (listen(socket.raw, 100) != 0) {HTTPServer.swift:68 let client = accept(socket.raw, nil, nil)Reader.swift:62 let size = recv(socket.raw, buffer, maxLength, 0)Socket.swift:43 raw = socket(domain, Int32(type), proto)Socket.swift:44 if raw <= 0 {Socket.swift:50 self.raw = rawSocket.swift:54 return bind(raw, UnsafeMutablePointer<sockaddr>(address), length) == 0Socket.swift:59 setsockopt(raw, SOL_SOCKET, option, &val, socklen_t(sizeof(Int32)))(raw: Int32) { 0050 self.raw = raw 0051 } 0052 0053 public func bindAddress
HTTPServer.swift:73 let reader = SocketReader(socket: Socket(raw: client))(address: UnsafeMutablePointer<Void>, length: socklen_t) -> Bool { 0054 return bind(raw, UnsafeMutablePointer<sockaddr>(address), length) == 0 0055 } 0056 0057 public func setOption
HTTPServer.swift:49 if !socket.bindAddress(&address.underlying, length: socklen_t(UInt8(sizeof(sockaddr_in)))) {(option: Int32, value: Int32) { 0058 var val = value 0059 setsockopt(raw, SOL_SOCKET, option, &val, socklen_t(sizeof(Int32))) 0060 } 0061 } 0062 0063 public struct SocketAddress
HTTPServer.swift:44 socket.setOption(SO_REUSEADDR, value: 1)HTTPServer.swift:46 socket.setOption(SO_NOSIGPIPE, value: 1){ 0064 0065 static let defaultDomain
HTTPServer.swift:38 var address: SocketAddressHTTPServer.swift:40 public init?(socket: Socket, addr: SocketAddress) {Socket.swift:73 public init(port: UInt16, domain: Int32 = defaultDomain) {Socket.swift:78 sin_port: SocketAddress.htons(port),= AF_INET 0066 0067 var underlying
Socket.swift:73 public init(port: UInt16, domain: Int32 = defaultDomain) {: sockaddr_in 0068 0069 static func htons
HTTPServer.swift:49 if !socket.bindAddress(&address.underlying, length: socklen_t(UInt8(sizeof(sockaddr_in)))) {Socket.swift:75 underlying = sockaddr_in((value: CUnsignedShort) -> CUnsignedShort { 0070 return value.bigEndian 0071 } 0072 0073 public init(port: UInt16, domain: Int32 = defaultDomain) { 0074 #if os(OSX) 0075 underlying = sockaddr_in( 0076 sin_len: __uint8_t(sizeof(sockaddr_in)), 0077 sin_family: sa_family_t(AF_INET), 0078 sin_port: SocketAddress.htons(port), 0079 sin_addr: in_addr(s_addr: in_addr_t(0)), 0080 sin_zero: (0, 0, 0, 0, 0, 0, 0, 0) 0081 ) 0082 #else 0083 underlying = sockaddr_in( 0084 sin_family: sa_family_t(AF_INET), 0085 sin_port: SocketAddress.htons(port), 0086 sin_addr: in_addr(s_addr: in_addr_t(0)), 0087 sin_zero: (0, 0, 0, 0, 0, 0, 0, 0) 0088 ) 0089 #endif 0090 } 0091 0092 } 0093
Socket.swift:78 sin_port: SocketAddress.htons(port),