@@ -38,7 +38,7 @@ final class SwiftPluginServer {
3838
3939 /// @main entry point.
4040 static func main( ) throws {
41- let connection = try PluginHostConnection ( )
41+ let connection = try StandardIOMessageConnection ( )
4242 let listener = CompilerPluginMessageListener (
4343 connection: connection,
4444 provider: self . init ( )
@@ -98,125 +98,6 @@ extension SwiftPluginServer: PluginProvider {
9898 }
9999}
100100
101- final class PluginHostConnection : MessageConnection {
102- let handle : UnsafeRawPointer
103- init ( ) throws {
104- var errorMessage : UnsafePointer < CChar > ? = nil
105- guard let handle = PluginServer_createConnection ( & errorMessage) else {
106- throw PluginServerError ( message: String ( cString: errorMessage!) )
107- }
108- self . handle = handle
109- }
110-
111- deinit {
112- PluginServer_destroyConnection ( self . handle)
113- }
114-
115- func sendMessage< TX: Encodable > ( _ message: TX ) throws {
116- try JSON . encode ( message) . withUnsafeBufferPointer { buffer in
117- try buffer. withMemoryRebound ( to: Int8 . self) { buffer in
118- try self . sendMessageData ( buffer)
119- }
120- }
121- }
122-
123- func waitForNextMessage< RX: Decodable > ( _ type: RX . Type ) throws -> RX ? {
124- return try self . withReadingMessageData { jsonData in
125- try jsonData. withMemoryRebound ( to: UInt8 . self) { buffer in
126- try JSON . decode ( RX . self, from: buffer)
127- }
128- }
129- }
130-
131- /// Send a serialized message to the message channel.
132- private func sendMessageData( _ data: UnsafeBufferPointer < Int8 > ) throws {
133- // Write the header (a 64-bit length field in little endian byte order).
134- var header : UInt64 = UInt64 ( data. count) . littleEndian
135- let writtenSize = try Swift . withUnsafeBytes ( of: & header) { buffer in
136- try self . write ( buffer: UnsafeRawBufferPointer ( buffer) )
137- }
138- guard writtenSize == MemoryLayout . size ( ofValue: header) else {
139- throw PluginServerError ( message: " failed to write message header " )
140- }
141-
142- // Write the body.
143- guard try self . write ( buffer: UnsafeRawBufferPointer ( data) ) == data. count else {
144- throw PluginServerError ( message: " failed to write message body " )
145- }
146- }
147-
148- /// Read a serialized message from the message channel and call the 'body'
149- /// with the data.
150- private func withReadingMessageData< R> ( _ body: ( UnsafeBufferPointer < Int8 > ) throws -> R ) throws -> R ? {
151- // Read the header (a 64-bit length field in little endian byte order).
152- var header : UInt64 = 0
153- let readSize = try Swift . withUnsafeMutableBytes ( of: & header) { buffer in
154- try self . read ( into: UnsafeMutableRawBufferPointer ( buffer) )
155- }
156- guard readSize == MemoryLayout . size ( ofValue: header) else {
157- if readSize == 0 {
158- // The host closed the pipe.
159- return nil
160- }
161- // Otherwise, some error happened.
162- throw PluginServerError ( message: " failed to read message header " )
163- }
164-
165- // Read the body.
166- let count = Int ( UInt64 ( littleEndian: header) )
167- let data = UnsafeMutableBufferPointer< Int8> . allocate( capacity: count)
168- defer { data. deallocate ( ) }
169- guard try self . read ( into: UnsafeMutableRawBufferPointer ( data) ) == count else {
170- throw PluginServerError ( message: " failed to read message body " )
171- }
172-
173- // Invoke the handler.
174- return try body ( UnsafeBufferPointer ( data) )
175- }
176-
177- /// Write the 'buffer' to the message channel.
178- /// Returns the number of bytes succeeded to write.
179- private func write( buffer: UnsafeRawBufferPointer ) throws -> Int {
180- var bytesToWrite = buffer. count
181- guard bytesToWrite > 0 else {
182- return 0
183- }
184- var ptr = buffer. baseAddress!
185-
186- while ( bytesToWrite > 0 ) {
187- let writtenSize = PluginServer_write ( handle, ptr, bytesToWrite)
188- if ( writtenSize <= 0 ) {
189- // error e.g. broken pipe.
190- break
191- }
192- ptr = ptr. advanced ( by: writtenSize)
193- bytesToWrite -= Int ( writtenSize)
194- }
195- return buffer. count - bytesToWrite
196- }
197-
198- /// Read data from the message channel into the 'buffer' up to 'buffer.count' bytes.
199- /// Returns the number of bytes succeeded to read.
200- private func read( into buffer: UnsafeMutableRawBufferPointer ) throws -> Int {
201- var bytesToRead = buffer. count
202- guard bytesToRead > 0 else {
203- return 0
204- }
205- var ptr = buffer. baseAddress!
206-
207- while bytesToRead > 0 {
208- let readSize = PluginServer_read ( handle, ptr, bytesToRead)
209- if ( readSize <= 0 ) {
210- // 0: EOF (the host closed), -1: Broken pipe (the host crashed?)
211- break ;
212- }
213- ptr = ptr. advanced ( by: readSize)
214- bytesToRead -= readSize
215- }
216- return buffer. count - bytesToRead
217- }
218- }
219-
220101struct PluginServerError : Error , CustomStringConvertible {
221102 var description : String
222103 init ( message: String ) {
0 commit comments