Maintain reference to QueryClientConnection while processing packet

This commit is contained in:
Tommy van der Vorst 2017-11-24 12:23:38 +01:00
parent db9edd5ede
commit 55b8e6e08d
1 changed files with 48 additions and 46 deletions

View File

@ -643,39 +643,40 @@ public final class QueryClientConnection<PreparedStatementType: PreparedStatemen
let queue = DispatchQueue.global(qos: .default) let queue = DispatchQueue.global(qos: .default)
// Create the run loop work item and dispatch to the default priority global queue... // Create the run loop work item and dispatch to the default priority global queue...
queue.async { [unowned self] in queue.async { [weak self] in
if let s = self {
var shouldKeepRunning = true var shouldKeepRunning = true
do { do {
switch self.state { switch s.state {
case .new: case .new:
if let len = self.readInt32(), let msg = self.readInt32() { if let len = s.readInt32(), let msg = s.readInt32() {
if len == 8 && msg == 80877103 { if len == 8 && msg == 80877103 {
// No SSL, thank you // No SSL, thank you
try self.socket.write(from: "N") try s.socket.write(from: "N")
} }
else if len > UInt32(8) { else if len > UInt32(8) {
// Read client version number // Read client version number
self.majorVersion = UInt16(msg >> 16) s.majorVersion = UInt16(msg >> 16)
self.minorVersion = UInt16(msg & 0xFFFF) s.minorVersion = UInt16(msg & 0xFFFF)
// Read parameters // Read parameters
if let p = try self.readParameters(length: len - UInt32(8)) { if let p = try s.readParameters(length: len - UInt32(8)) {
self.username = p["user"] s.username = p["user"]
// Send authentication request // Send authentication request
let buf = Data(bytes: [UInt8(Character("R").codePoint), 0, 0, 0, 8, 0, 0, 0, 3]) let buf = Data(bytes: [UInt8(Character("R").codePoint), 0, 0, 0, 8, 0, 0, 0, 3])
try self.socket.write(from: buf) try s.socket.write(from: buf)
// Read authentication // Read authentication
if let pw = try self.readAuthentication() { if let pw = try s.readAuthentication() {
self.password = pw s.password = pw
// Send authentication success // Send authentication success
let buf = Data(bytes: [UInt8(Character("R").codePoint), 0, 0, 0, 8, 0, 0, 0, 0]) let buf = Data(bytes: [UInt8(Character("R").codePoint), 0, 0, 0, 8, 0, 0, 0, 0])
try self.socket.write(from: buf) try s.socket.write(from: buf)
self.state = .ready s.state = .ready
try self.sendReadyForQuery() try s.sendReadyForQuery()
} }
else { else {
shouldKeepRunning = false shouldKeepRunning = false
@ -691,7 +692,7 @@ public final class QueryClientConnection<PreparedStatementType: PreparedStatemen
} }
case .ready, .querying: case .ready, .querying:
if try !self.readPacket() { if try !s.readPacket() {
shouldKeepRunning = false shouldKeepRunning = false
} }
@ -700,15 +701,16 @@ public final class QueryClientConnection<PreparedStatementType: PreparedStatemen
} }
} }
catch { catch {
try? self.send(error: error.localizedDescription) try? s.send(error: error.localizedDescription)
shouldKeepRunning = false shouldKeepRunning = false
} }
if shouldKeepRunning { if shouldKeepRunning {
self.run() s.run()
} }
else { else {
self.close() s.close()
}
} }
} }
} }