mirror of
https://github.com/apple/swift-nio-extras.git
synced 2025-05-15 09:22:38 +08:00
Explicitly handle states
This commit is contained in:
parent
8d515b943e
commit
8a68b0ef56
@ -47,12 +47,6 @@ struct ServerStateMachine: Hashable {
|
|||||||
init() {
|
init() {
|
||||||
self.state = .inactive
|
self.state = .inactive
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate func guardState(_ expected: ServerState) throws {
|
|
||||||
guard expected == self.state else {
|
|
||||||
throw SOCKSError.InvalidServerState()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Inbound
|
// MARK: - Inbound
|
||||||
@ -109,17 +103,51 @@ extension ServerStateMachine {
|
|||||||
extension ServerStateMachine {
|
extension ServerStateMachine {
|
||||||
|
|
||||||
mutating func connectionEstablished() throws {
|
mutating func connectionEstablished() throws {
|
||||||
try self.guardState(.inactive)
|
switch self.state {
|
||||||
|
case .inactive:
|
||||||
|
()
|
||||||
|
case .authenticating,
|
||||||
|
.waitingForClientGreeting,
|
||||||
|
.waitingToSendAuthenticationMethod,
|
||||||
|
.waitingForClientRequest,
|
||||||
|
.waitingToSendResponse,
|
||||||
|
.active,
|
||||||
|
.error:
|
||||||
|
throw SOCKSError.InvalidServerState()
|
||||||
|
}
|
||||||
self.state = .waitingForClientGreeting
|
self.state = .waitingForClientGreeting
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func sendAuthenticationMethod(_ method: SelectedAuthenticationMethod) throws {
|
mutating func sendAuthenticationMethod(_ method: SelectedAuthenticationMethod) throws {
|
||||||
try self.guardState(.waitingToSendAuthenticationMethod)
|
switch self.state {
|
||||||
|
case .waitingToSendAuthenticationMethod:
|
||||||
|
()
|
||||||
|
case .inactive,
|
||||||
|
.waitingForClientGreeting,
|
||||||
|
.authenticating,
|
||||||
|
.waitingForClientRequest,
|
||||||
|
.waitingToSendResponse,
|
||||||
|
.active,
|
||||||
|
.error:
|
||||||
|
throw SOCKSError.InvalidServerState()
|
||||||
|
}
|
||||||
self.state = .authenticating
|
self.state = .authenticating
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func sendServerResponse(_ response: SOCKSResponse) throws {
|
mutating func sendServerResponse(_ response: SOCKSResponse) throws {
|
||||||
try self.guardState(.waitingToSendResponse)
|
switch self.state {
|
||||||
|
case .waitingToSendResponse:
|
||||||
|
()
|
||||||
|
case .inactive,
|
||||||
|
.waitingForClientGreeting,
|
||||||
|
.waitingToSendAuthenticationMethod,
|
||||||
|
.waitingForClientRequest,
|
||||||
|
.authenticating,
|
||||||
|
.active,
|
||||||
|
.error:
|
||||||
|
throw SOCKSError.InvalidServerState()
|
||||||
|
}
|
||||||
|
|
||||||
if response.reply == .succeeded {
|
if response.reply == .succeeded {
|
||||||
self.state = .active
|
self.state = .active
|
||||||
} else {
|
} else {
|
||||||
@ -128,11 +156,34 @@ extension ServerStateMachine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mutating func sendData() throws {
|
mutating func sendData() throws {
|
||||||
try self.guardState(.authenticating)
|
switch self.state {
|
||||||
|
case .authenticating:
|
||||||
|
()
|
||||||
|
case .inactive,
|
||||||
|
.waitingForClientGreeting,
|
||||||
|
.waitingToSendAuthenticationMethod,
|
||||||
|
.waitingForClientRequest,
|
||||||
|
.waitingToSendResponse,
|
||||||
|
.active,
|
||||||
|
.error:
|
||||||
|
throw SOCKSError.InvalidServerState()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func authenticationComplete() throws {
|
mutating func authenticationComplete() throws {
|
||||||
try self.guardState(.authenticating)
|
switch self.state {
|
||||||
|
case .authenticating:
|
||||||
|
()
|
||||||
|
case .inactive,
|
||||||
|
.waitingForClientGreeting,
|
||||||
|
.waitingToSendAuthenticationMethod,
|
||||||
|
.waitingForClientRequest,
|
||||||
|
.waitingToSendResponse,
|
||||||
|
.active,
|
||||||
|
.error:
|
||||||
|
throw SOCKSError.InvalidServerState()
|
||||||
|
}
|
||||||
|
|
||||||
self.state = .waitingForClientRequest
|
self.state = .waitingForClientRequest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user