@@ -116,36 +116,37 @@ final class PluginHostConnection: MessageConnection {
116
116
}
117
117
}
118
118
119
- /// Send a serialized message data to the message channel.
119
+ /// Send a serialized message to the message channel.
120
120
private func sendMessageData( _ data: UnsafeBufferPointer < Int8 > ) throws {
121
121
// Write the header (a 64-bit length field in little endian byte order).
122
122
var header : UInt64 = UInt64 ( data. count) . littleEndian
123
123
let writtenSize = try Swift . withUnsafeBytes ( of: & header) { buffer in
124
- try self . write ( data : UnsafeRawBufferPointer ( buffer) )
124
+ try self . write ( buffer : UnsafeRawBufferPointer ( buffer) )
125
125
}
126
- guard writtenSize == 8 else {
126
+ guard writtenSize == MemoryLayout . size ( ofValue : header ) else {
127
127
throw PluginServerError ( message: " failed to write message header " )
128
128
}
129
129
130
130
// Write the body.
131
- guard try self . write ( data : UnsafeRawBufferPointer ( data) ) == data. count else {
132
- throw PluginServerError ( message: " failed to write message data " )
131
+ guard try self . write ( buffer : UnsafeRawBufferPointer ( data) ) == data. count else {
132
+ throw PluginServerError ( message: " failed to write message body " )
133
133
}
134
134
}
135
135
136
- /// Read a serialized message data from the message channel and call the 'body'
136
+ /// Read a serialized message from the message channel and call the 'body'
137
137
/// with the data.
138
138
private func withReadingMessageData< R> ( _ body: ( UnsafeBufferPointer < Int8 > ) throws -> R ) throws -> R ? {
139
- // Read the header.
139
+ // Read the header (a 64-bit length field in little endian byte order) .
140
140
var header : UInt64 = 0
141
141
let readSize = try Swift . withUnsafeMutableBytes ( of: & header) { buffer in
142
142
try self . read ( into: UnsafeMutableRawBufferPointer ( buffer) )
143
143
}
144
- if readSize == 0 {
145
- // The host closed the pipe.
146
- return nil
147
- }
148
- guard readSize == 8 else {
144
+ guard readSize == MemoryLayout . size ( ofValue: header) else {
145
+ if readSize == 0 {
146
+ // The host closed the pipe.
147
+ return nil
148
+ }
149
+ // Otherwise, some error happened.
149
150
throw PluginServerError ( message: " failed to read message header " )
150
151
}
151
152
@@ -154,18 +155,21 @@ final class PluginHostConnection: MessageConnection {
154
155
let data = UnsafeMutableBufferPointer< Int8> . allocate( capacity: count)
155
156
defer { data. deallocate ( ) }
156
157
guard try self . read ( into: UnsafeMutableRawBufferPointer ( data) ) == count else {
157
- throw PluginServerError ( message: " failed to read message data " )
158
+ throw PluginServerError ( message: " failed to read message body " )
158
159
}
159
160
160
161
// Invoke the handler.
161
162
return try body ( UnsafeBufferPointer ( data) )
162
163
}
163
164
164
- /// Write the 'data ' to the message channel.
165
+ /// Write the 'buffer ' to the message channel.
165
166
/// Returns the number of bytes succeeded to write.
166
- private func write( data: UnsafeRawBufferPointer ) throws -> Int {
167
- var bytesToWrite = data. count
168
- var ptr = data. baseAddress!
167
+ private func write( buffer: UnsafeRawBufferPointer ) throws -> Int {
168
+ var bytesToWrite = buffer. count
169
+ guard bytesToWrite > 0 else {
170
+ return 0
171
+ }
172
+ var ptr = buffer. baseAddress!
169
173
170
174
while ( bytesToWrite > 0 ) {
171
175
let writtenSize = PluginServer_write ( handle, ptr, bytesToWrite)
@@ -176,13 +180,16 @@ final class PluginHostConnection: MessageConnection {
176
180
ptr = ptr. advanced ( by: writtenSize)
177
181
bytesToWrite -= writtenSize
178
182
}
179
- return data . count - bytesToWrite
183
+ return buffer . count - bytesToWrite
180
184
}
181
185
182
186
/// Read data from the message channel into the 'buffer' up to 'buffer.count' bytes.
183
187
/// Returns the number of bytes succeeded to read.
184
188
private func read( into buffer: UnsafeMutableRawBufferPointer ) throws -> Int {
185
189
var bytesToRead = buffer. count
190
+ guard bytesToRead > 0 else {
191
+ return 0
192
+ }
186
193
var ptr = buffer. baseAddress!
187
194
188
195
while bytesToRead > 0 {
0 commit comments