|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2015 Zewo |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +public enum CloseCode : Equatable { |
| 24 | + case normal |
| 25 | + case goingAway |
| 26 | + case protocolError |
| 27 | + case unsupported |
| 28 | + case noStatus |
| 29 | + case abnormal |
| 30 | + case unsupportedData |
| 31 | + case policyViolation |
| 32 | + case tooLarge |
| 33 | + case missingExtension |
| 34 | + case internalError |
| 35 | + case serviceRestart |
| 36 | + case tryAgainLater |
| 37 | + case tlsHandshake |
| 38 | + case raw(UInt16) |
| 39 | + |
| 40 | + init(code: UInt16) { |
| 41 | + switch code { |
| 42 | + case 1000: self = .normal |
| 43 | + case 1001: self = .goingAway |
| 44 | + case 1002: self = .protocolError |
| 45 | + case 1003: self = .unsupported |
| 46 | + case 1005: self = .noStatus |
| 47 | + case 1006: self = .abnormal |
| 48 | + case 1007: self = .unsupportedData |
| 49 | + case 1008: self = .policyViolation |
| 50 | + case 1009: self = .tooLarge |
| 51 | + case 1010: self = .missingExtension |
| 52 | + case 1011: self = .internalError |
| 53 | + case 1012: self = .serviceRestart |
| 54 | + case 1013: self = .tryAgainLater |
| 55 | + case 1015: self = .tlsHandshake |
| 56 | + default: self = .raw(UInt16(code)) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + var code: UInt16 { |
| 61 | + switch self { |
| 62 | + case .normal: return 1000 |
| 63 | + case .goingAway: return 1001 |
| 64 | + case .protocolError: return 1002 |
| 65 | + case .unsupported: return 1003 |
| 66 | + case .noStatus: return 1005 |
| 67 | + case .abnormal: return 1006 |
| 68 | + case .unsupportedData: return 1007 |
| 69 | + case .policyViolation: return 1008 |
| 70 | + case .tooLarge: return 1009 |
| 71 | + case .missingExtension: return 1010 |
| 72 | + case .internalError: return 1011 |
| 73 | + case .serviceRestart: return 1012 |
| 74 | + case .tryAgainLater: return 1013 |
| 75 | + case .tlsHandshake: return 1015 |
| 76 | + case .raw(let code): return code |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + var isValid: Bool { |
| 81 | + let code = self.code |
| 82 | + |
| 83 | + if code >= 1000 && code <= 5000 { |
| 84 | + return code != 1004 && code != 1005 && code != 1006 && code != 1014 && code != 1015 |
| 85 | + && code != 1016 && code != 1100 && code != 2000 && code != 2999 |
| 86 | + } |
| 87 | + |
| 88 | + return false |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +public func == (lhs: CloseCode, rhs: CloseCode) -> Bool { |
| 93 | + return lhs.code == rhs.code |
| 94 | +} |
0 commit comments