@@ -56,37 +56,38 @@ module DAP.Adaptor
56
56
import Control.Concurrent.Lifted ( fork , killThread )
57
57
import Control.Exception ( throwIO )
58
58
import Control.Concurrent.STM ( atomically , readTVarIO , modifyTVar' )
59
- import Control.Monad ( when , unless )
59
+ import Control.Monad ( when , unless , void )
60
60
import Control.Monad.Except ( runExceptT , throwError )
61
61
import Control.Monad.State ( runStateT , gets , gets , modify' )
62
- import Control.Monad.Reader
62
+ import Control.Monad.IO.Class ( liftIO )
63
+ import Control.Monad.Reader ( asks , ask , runReaderT )
63
64
import Data.Aeson ( FromJSON , Result (.. ), fromJSON )
64
65
import Data.Aeson.Encode.Pretty ( encodePretty )
65
66
import Data.Aeson.Types ( object , Key , KeyValue ((.=) ), ToJSON )
67
+ import Data.IORef ( readIORef , writeIORef )
66
68
import Data.Text ( unpack , pack )
67
69
import Network.Socket ( SockAddr )
68
70
import System.IO ( Handle )
69
71
import qualified Data.ByteString.Lazy.Char8 as BL8
70
72
import qualified Data.ByteString.Char8 as BS
71
73
import qualified Data.HashMap.Strict as H
72
- import Data.IORef
73
74
----------------------------------------------------------------------------
74
75
import DAP.Types
75
76
import DAP.Utils
76
77
import DAP.Internal
77
78
----------------------------------------------------------------------------
78
- logWarn :: BL8. ByteString -> Adaptor app r ()
79
+ logWarn :: BL8. ByteString -> Adaptor app request ()
79
80
logWarn msg = logWithAddr WARN Nothing (withBraces msg)
80
81
----------------------------------------------------------------------------
81
- logError :: BL8. ByteString -> Adaptor app r ()
82
+ logError :: BL8. ByteString -> Adaptor app request ()
82
83
logError msg = logWithAddr ERROR Nothing (withBraces msg)
83
84
----------------------------------------------------------------------------
84
- logInfo :: BL8. ByteString -> Adaptor app r ()
85
+ logInfo :: BL8. ByteString -> Adaptor app request ()
85
86
logInfo msg = logWithAddr INFO Nothing (withBraces msg)
86
87
----------------------------------------------------------------------------
87
88
-- | Meant for internal consumption, used to signify a message has been
88
89
-- SENT from the server
89
- debugMessage :: BL8. ByteString -> Adaptor app r ()
90
+ debugMessage :: BL8. ByteString -> Adaptor app request ()
90
91
debugMessage msg = do
91
92
shouldLog <- getDebugLogging
92
93
addr <- getAddress
@@ -95,7 +96,7 @@ debugMessage msg = do
95
96
$ logger DEBUG addr (Just SENT ) msg
96
97
----------------------------------------------------------------------------
97
98
-- | Meant for external consumption
98
- logWithAddr :: Level -> Maybe DebugStatus -> BL8. ByteString -> Adaptor app r ()
99
+ logWithAddr :: Level -> Maybe DebugStatus -> BL8. ByteString -> Adaptor app request ()
99
100
logWithAddr level status msg = do
100
101
addr <- getAddress
101
102
liftIO (logger level addr status msg)
@@ -115,22 +116,22 @@ logger level addr maybeDebug msg = do
115
116
, msg
116
117
]
117
118
----------------------------------------------------------------------------
118
- getDebugLogging :: Adaptor app r Bool
119
+ getDebugLogging :: Adaptor app request Bool
119
120
getDebugLogging = asks (debugLogging . serverConfig)
120
121
----------------------------------------------------------------------------
121
- getServerCapabilities :: Adaptor app r Capabilities
122
+ getServerCapabilities :: Adaptor app request Capabilities
122
123
getServerCapabilities = asks (serverCapabilities . serverConfig)
123
124
----------------------------------------------------------------------------
124
- getAddress :: Adaptor app r SockAddr
125
+ getAddress :: Adaptor app request SockAddr
125
126
getAddress = asks address
126
127
----------------------------------------------------------------------------
127
- getHandle :: Adaptor app r Handle
128
+ getHandle :: Adaptor app request Handle
128
129
getHandle = asks handle
129
130
----------------------------------------------------------------------------
130
131
getRequestSeqNum :: Adaptor app Request Seq
131
132
getRequestSeqNum = asks (requestSeqNum . request)
132
133
----------------------------------------------------------------------------
133
- getDebugSessionId :: Adaptor app r SessionId
134
+ getDebugSessionId :: Adaptor app request SessionId
134
135
getDebugSessionId = do
135
136
var <- asks (sessionId)
136
137
res <- liftIO $ readIORef var
@@ -142,7 +143,7 @@ getDebugSessionId = do
142
143
let err = " No Debug Session has started"
143
144
sendError (ErrorMessage (pack err)) Nothing
144
145
----------------------------------------------------------------------------
145
- setDebugSessionId :: SessionId -> Adaptor app r ()
146
+ setDebugSessionId :: SessionId -> Adaptor app request ()
146
147
setDebugSessionId session = do
147
148
var <- asks sessionId
148
149
liftIO $ writeIORef var (Just session)
@@ -167,21 +168,21 @@ registerNewDebugSession
167
168
-- > withAdaptor $ sendOutputEvent defaultOutputEvent { outputEventOutput = output }
168
169
-- > ]
169
170
--
170
- -> Adaptor app r ()
171
+ -> Adaptor app request ()
171
172
registerNewDebugSession k v debuggerConcurrentActions = do
172
173
store <- asks appStore
173
174
lcl <- ask
174
175
let lcl' = lcl { request = () }
175
176
let emptyState = AdaptorState MessageTypeEvent []
176
177
debuggerThreadState <- liftIO $
177
178
DebuggerThreadState
178
- <$> sequence [fork $ action (runAdaptorWith lcl' emptyState " s " ) | action <- debuggerConcurrentActions]
179
+ <$> sequence [fork $ action (runAdaptorWith lcl' emptyState) | action <- debuggerConcurrentActions]
179
180
liftIO . atomically $ modifyTVar' store (H. insert k (debuggerThreadState, v))
180
181
logInfo $ BL8. pack $ " Registered new debug session: " <> unpack k
181
182
setDebugSessionId k
182
183
183
184
----------------------------------------------------------------------------
184
- updateDebugSession :: (app -> app ) -> Adaptor app r ()
185
+ updateDebugSession :: (app -> app ) -> Adaptor app request ()
185
186
updateDebugSession updateFun = do
186
187
sessionId <- getDebugSessionId
187
188
store <- asks appStore
@@ -192,7 +193,7 @@ getDebugSession = do
192
193
(_, _, app) <- getDebugSessionWithThreadIdAndSessionId
193
194
pure app
194
195
----------------------------------------------------------------------------
195
- getDebugSessionWithThreadIdAndSessionId :: Adaptor app r (SessionId , DebuggerThreadState , app )
196
+ getDebugSessionWithThreadIdAndSessionId :: Adaptor app request (SessionId , DebuggerThreadState , app )
196
197
getDebugSessionWithThreadIdAndSessionId = do
197
198
sessionId <- getDebugSessionId
198
199
appStore <- liftIO . readTVarIO =<< getAppStore
@@ -212,7 +213,7 @@ getDebugSessionWithThreadIdAndSessionId = do
212
213
-- | Whenever a debug Session ends (cleanly or otherwise) this function
213
214
-- will remove the local debugger communication state from the global state
214
215
----------------------------------------------------------------------------
215
- destroyDebugSession :: Adaptor app r ()
216
+ destroyDebugSession :: Adaptor app request ()
216
217
destroyDebugSession = do
217
218
(sessionId, DebuggerThreadState {.. }, _) <- getDebugSessionWithThreadIdAndSessionId
218
219
store <- getAppStore
@@ -221,7 +222,7 @@ destroyDebugSession = do
221
222
atomically $ modifyTVar' store (H. delete sessionId)
222
223
logInfo $ BL8. pack $ " SessionId " <> unpack sessionId <> " ended"
223
224
----------------------------------------------------------------------------
224
- getAppStore :: Adaptor app r (AppStore app )
225
+ getAppStore :: Adaptor app request (AppStore app )
225
226
getAppStore = asks appStore
226
227
----------------------------------------------------------------------------
227
228
getCommand :: Adaptor app Request Command
@@ -231,7 +232,7 @@ getCommand = command <$> asks request
231
232
-- Sends a raw JSON payload to the editor. No "seq", "type" or "command" fields are set.
232
233
-- The message is still encoded with the ProtocolMessage Header, byte count, and CRLF.
233
234
--
234
- sendRaw :: ToJSON value => value -> Adaptor app r ()
235
+ sendRaw :: ToJSON value => value -> Adaptor app request ()
235
236
sendRaw value = do
236
237
handle <- getHandle
237
238
address <- getAddress
@@ -259,36 +260,40 @@ send action = do
259
260
260
261
-- "seq" and "type" must be set for all protocol messages
261
262
setField " type" messageType
262
- unless (messageType == MessageTypeEvent ) $
263
- setField " seq" seqNum
263
+ unless (messageType == MessageTypeEvent ) (setField " seq" seqNum)
264
264
265
265
-- Once all fields are set, fetch the payload for sending
266
266
payload <- object <$> gets payload
267
267
268
268
-- Send payload to client from debug adaptor
269
269
writeToHandle address handle payload
270
270
resetAdaptorStatePayload
271
-
272
- sendEvent :: Adaptor app r () -> Adaptor app r ()
271
+ ----------------------------------------------------------------------------
272
+ -- | Write event to Handle
273
+ sendEvent
274
+ :: Adaptor app request ()
275
+ -> Adaptor app request ()
273
276
sendEvent action = do
274
- () <- action
277
+ () <- action
275
278
handle <- getHandle
276
279
messageType <- gets messageType
277
280
address <- getAddress
281
+ let errorMsg =
282
+ " Use 'send' function when responding to a DAP request, 'sendEvent'\
283
+ \ is for responding to events"
278
284
case messageType of
279
- MessageTypeResponse -> error " use send"
280
- MessageTypeRequest -> error " use send"
281
- MessageTypeEvent -> do
282
- address <- getAddress
285
+ MessageTypeResponse ->
286
+ sendError (ErrorMessage errorMsg) Nothing
287
+ MessageTypeRequest ->
288
+ sendError (ErrorMessage errorMsg) Nothing
289
+ MessageTypeEvent ->
283
290
setField " type" messageType
284
291
285
292
-- Once all fields are set, fetch the payload for sending
286
293
payload <- object <$> gets payload
287
294
-- Send payload to client from debug adaptor
288
295
writeToHandle address handle payload
289
296
resetAdaptorStatePayload
290
-
291
-
292
297
----------------------------------------------------------------------------
293
298
-- | Writes payload to the given 'Handle' using the local connection lock
294
299
----------------------------------------------------------------------------
@@ -297,15 +302,15 @@ writeToHandle
297
302
=> SockAddr
298
303
-> Handle
299
304
-> event
300
- -> Adaptor app r ()
305
+ -> Adaptor app request ()
301
306
writeToHandle _ handle evt = do
302
307
let msg = encodeBaseProtocolMessage evt
303
308
debugMessage (" \n " <> encodePretty evt)
304
309
withConnectionLock (BS. hPutStr handle msg)
305
310
----------------------------------------------------------------------------
306
311
-- | Resets Adaptor's payload
307
312
----------------------------------------------------------------------------
308
- resetAdaptorStatePayload :: Adaptor app r ()
313
+ resetAdaptorStatePayload :: Adaptor app request ()
309
314
resetAdaptorStatePayload = modify' $ \ s -> s { payload = [] }
310
315
----------------------------------------------------------------------------
311
316
sendSuccesfulResponse :: Adaptor app Request () -> Adaptor app Request ()
@@ -319,7 +324,10 @@ sendSuccesfulEmptyResponse :: Adaptor app Request ()
319
324
sendSuccesfulEmptyResponse = sendSuccesfulResponse (pure () )
320
325
----------------------------------------------------------------------------
321
326
-- | Sends successful event
322
- sendSuccesfulEvent :: EventType -> Adaptor app r () -> Adaptor app r ()
327
+ sendSuccesfulEvent
328
+ :: EventType
329
+ -> Adaptor app request ()
330
+ -> Adaptor app request ()
323
331
sendSuccesfulEvent event action = do
324
332
sendEvent $ do
325
333
setEvent event
@@ -333,7 +341,7 @@ sendSuccesfulEvent event action = do
333
341
sendError
334
342
:: ErrorMessage
335
343
-> Maybe Message
336
- -> Adaptor app r a
344
+ -> Adaptor app request a
337
345
sendError errorMessage maybeMessage = do
338
346
throwError (errorMessage, maybeMessage)
339
347
----------------------------------------------------------------------------
@@ -352,24 +360,24 @@ sendErrorResponse errorMessage maybeMessage = do
352
360
----------------------------------------------------------------------------
353
361
setErrorMessage
354
362
:: ErrorMessage
355
- -> Adaptor app r ()
363
+ -> Adaptor app request ()
356
364
setErrorMessage v = setField " message" v
357
365
----------------------------------------------------------------------------
358
366
-- | Sends successful event
359
367
setSuccess
360
368
:: Bool
361
- -> Adaptor app r ()
369
+ -> Adaptor app request ()
362
370
setSuccess = setField " success"
363
371
----------------------------------------------------------------------------
364
372
setBody
365
373
:: ToJSON value
366
374
=> value
367
- -> Adaptor app r ()
375
+ -> Adaptor app request ()
368
376
setBody value = setField " body" value
369
377
----------------------------------------------------------------------------
370
378
setType
371
379
:: MessageType
372
- -> Adaptor app r ()
380
+ -> Adaptor app request ()
373
381
setType messageType = do
374
382
modify' $ \ adaptorState ->
375
383
adaptorState
@@ -378,14 +386,14 @@ setType messageType = do
378
386
----------------------------------------------------------------------------
379
387
setEvent
380
388
:: EventType
381
- -> Adaptor app r ()
389
+ -> Adaptor app request ()
382
390
setEvent = setField " event"
383
391
----------------------------------------------------------------------------
384
392
setField
385
393
:: ToJSON value
386
394
=> Key
387
395
-> value
388
- -> Adaptor app r ()
396
+ -> Adaptor app request ()
389
397
setField key value = do
390
398
currentPayload <- gets payload
391
399
modify' $ \ adaptorState ->
@@ -395,7 +403,7 @@ setField key value = do
395
403
----------------------------------------------------------------------------
396
404
withConnectionLock
397
405
:: IO ()
398
- -> Adaptor app r ()
406
+ -> Adaptor app request ()
399
407
withConnectionLock action = do
400
408
lock <- asks handleLock
401
409
liftIO (withLock lock action)
@@ -418,19 +426,21 @@ getArguments = do
418
426
x -> do
419
427
logError (BL8. pack (show x))
420
428
liftIO $ throwIO (ParseException (show x))
421
-
422
429
----------------------------------------------------------------------------
423
430
-- | Evaluates Adaptor action by using and updating the state in the MVar
424
- runAdaptorWith :: AdaptorLocal app r -> AdaptorState -> String -> Adaptor app r () -> IO ()
425
- runAdaptorWith lcl st s (Adaptor action) = do
426
- runStateT (runReaderT (runExceptT action) lcl) st
427
- return ()
428
-
431
+ runAdaptorWith
432
+ :: AdaptorLocal app request
433
+ -> AdaptorState
434
+ -> Adaptor app request ()
435
+ -> IO ()
436
+ runAdaptorWith lcl st (Adaptor action) =
437
+ void (runStateT (runReaderT (runExceptT action) lcl) st)
429
438
----------------------------------------------------------------------------
430
439
-- | Utility for evaluating a monad transformer stack
431
440
runAdaptor :: AdaptorLocal app Request -> AdaptorState -> Adaptor app Request () -> IO ()
432
441
runAdaptor lcl s (Adaptor client) =
433
442
runStateT (runReaderT (runExceptT client) lcl) s >>= \ case
434
443
(Left (errorMessage, maybeMessage), s') ->
435
444
runAdaptor lcl s' (sendErrorResponse errorMessage maybeMessage)
436
- (Right () , s') -> pure ()
445
+ (Right () , _) -> pure ()
446
+ ----------------------------------------------------------------------------
0 commit comments