@@ -8,8 +8,8 @@ protocol PSQLChannelHandlerNotificationDelegate: AnyObject {
8
8
}
9
9
10
10
final class PSQLChannelHandler : ChannelDuplexHandler {
11
- typealias InboundIn = PSQLBackendMessage
12
11
typealias OutboundIn = PSQLTask
12
+ typealias InboundIn = ByteBuffer
13
13
typealias OutboundOut = PSQLFrontendMessage
14
14
15
15
private let logger : Logger
@@ -24,6 +24,7 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
24
24
/// The context is captured in `handlerAdded` and released` in `handlerRemoved`
25
25
private var handlerContext : ChannelHandlerContext !
26
26
private var rowStream : PSQLRowStream ?
27
+ private var decoder : NIOSingleStepByteToMessageProcessor < PSQLBackendMessageDecoder >
27
28
private let authentificationConfiguration : PSQLConnection . Configuration . Authentication ?
28
29
private let configureSSLCallback : ( ( Channel ) throws -> Void ) ?
29
30
@@ -38,6 +39,7 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
38
39
self . authentificationConfiguration = authentification
39
40
self . configureSSLCallback = configureSSLCallback
40
41
self . logger = logger
42
+ self . decoder = NIOSingleStepByteToMessageProcessor ( PSQLBackendMessageDecoder ( ) )
41
43
}
42
44
43
45
#if DEBUG
@@ -51,6 +53,7 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
51
53
self . authentificationConfiguration = authentification
52
54
self . configureSSLCallback = configureSSLCallback
53
55
self . logger = logger
56
+ self . decoder = NIOSingleStepByteToMessageProcessor ( PSQLBackendMessageDecoder ( ) )
54
57
}
55
58
#endif
56
59
@@ -91,54 +94,62 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
91
94
}
92
95
93
96
func channelRead( context: ChannelHandlerContext , data: NIOAny ) {
94
- let incomingMessage = self . unwrapInboundIn ( data)
97
+ let buffer = self . unwrapInboundIn ( data)
95
98
96
- self . logger. trace ( " Backend message received " , metadata: [ . message: " \( incomingMessage) " ] )
97
-
98
- let action : ConnectionStateMachine . ConnectionAction
99
-
100
- switch incomingMessage {
101
- case . authentication( let authentication) :
102
- action = self . state. authenticationMessageReceived ( authentication)
103
- case . backendKeyData( let keyData) :
104
- action = self . state. backendKeyDataReceived ( keyData)
105
- case . bindComplete:
106
- action = self . state. bindCompleteReceived ( )
107
- case . closeComplete:
108
- action = self . state. closeCompletedReceived ( )
109
- case . commandComplete( let commandTag) :
110
- action = self . state. commandCompletedReceived ( commandTag)
111
- case . dataRow( let dataRow) :
112
- action = self . state. dataRowReceived ( dataRow)
113
- case . emptyQueryResponse:
114
- action = self . state. emptyQueryResponseReceived ( )
115
- case . error( let errorResponse) :
116
- action = self . state. errorReceived ( errorResponse)
117
- case . noData:
118
- action = self . state. noDataReceived ( )
119
- case . notice( let noticeResponse) :
120
- action = self . state. noticeReceived ( noticeResponse)
121
- case . notification( let notification) :
122
- action = self . state. notificationReceived ( notification)
123
- case . parameterDescription( let parameterDescription) :
124
- action = self . state. parameterDescriptionReceived ( parameterDescription)
125
- case . parameterStatus( let parameterStatus) :
126
- action = self . state. parameterStatusReceived ( parameterStatus)
127
- case . parseComplete:
128
- action = self . state. parseCompleteReceived ( )
129
- case . portalSuspended:
130
- action = self . state. portalSuspendedReceived ( )
131
- case . readyForQuery( let transactionState) :
132
- action = self . state. readyForQueryReceived ( transactionState)
133
- case . rowDescription( let rowDescription) :
134
- action = self . state. rowDescriptionReceived ( rowDescription)
135
- case . sslSupported:
136
- action = self . state. sslSupportedReceived ( )
137
- case . sslUnsupported:
138
- action = self . state. sslUnsupportedReceived ( )
99
+ do {
100
+ try self . decoder. process ( buffer: buffer) { message in
101
+ self . logger. trace ( " Backend message received " , metadata: [ . message: " \( message) " ] )
102
+ let action : ConnectionStateMachine . ConnectionAction
103
+
104
+ switch message {
105
+ case . authentication( let authentication) :
106
+ action = self . state. authenticationMessageReceived ( authentication)
107
+ case . backendKeyData( let keyData) :
108
+ action = self . state. backendKeyDataReceived ( keyData)
109
+ case . bindComplete:
110
+ action = self . state. bindCompleteReceived ( )
111
+ case . closeComplete:
112
+ action = self . state. closeCompletedReceived ( )
113
+ case . commandComplete( let commandTag) :
114
+ action = self . state. commandCompletedReceived ( commandTag)
115
+ case . dataRow( let dataRow) :
116
+ action = self . state. dataRowReceived ( dataRow)
117
+ case . emptyQueryResponse:
118
+ action = self . state. emptyQueryResponseReceived ( )
119
+ case . error( let errorResponse) :
120
+ action = self . state. errorReceived ( errorResponse)
121
+ case . noData:
122
+ action = self . state. noDataReceived ( )
123
+ case . notice( let noticeResponse) :
124
+ action = self . state. noticeReceived ( noticeResponse)
125
+ case . notification( let notification) :
126
+ action = self . state. notificationReceived ( notification)
127
+ case . parameterDescription( let parameterDescription) :
128
+ action = self . state. parameterDescriptionReceived ( parameterDescription)
129
+ case . parameterStatus( let parameterStatus) :
130
+ action = self . state. parameterStatusReceived ( parameterStatus)
131
+ case . parseComplete:
132
+ action = self . state. parseCompleteReceived ( )
133
+ case . portalSuspended:
134
+ action = self . state. portalSuspendedReceived ( )
135
+ case . readyForQuery( let transactionState) :
136
+ action = self . state. readyForQueryReceived ( transactionState)
137
+ case . rowDescription( let rowDescription) :
138
+ action = self . state. rowDescriptionReceived ( rowDescription)
139
+ case . sslSupported:
140
+ action = self . state. sslSupportedReceived ( )
141
+ case . sslUnsupported:
142
+ action = self . state. sslUnsupportedReceived ( )
143
+ }
144
+
145
+ self . run ( action, with: context)
146
+ }
147
+ } catch let error as PSQLDecodingError {
148
+ let action = self . state. errorHappened ( . decoding( error) )
149
+ self . run ( action, with: context)
150
+ } catch {
151
+ preconditionFailure ( " Expected to only get PSQLDecodingErrors from the PSQLBackendMessageDecoder. " )
139
152
}
140
-
141
- self . run ( action, with: context)
142
153
}
143
154
144
155
func channelReadComplete( context: ChannelHandlerContext ) {
0 commit comments