@@ -76,19 +76,24 @@ stdoutLogger = do
7676data TerminateServer = TerminateServer
7777 deriving (Show , Exception )
7878
79+ -- | Simpler version of 'runDAPServerWithLogger'.
80+ --
81+ -- If you don't need a custom logger or to observe reverse request responses.
7982runDAPServer :: ServerConfig -> (Command -> Adaptor app Request () ) -> IO ()
8083runDAPServer config communicate = do
8184 l <- stdoutLogger
82- runDAPServerWithLogger (cmap renderDAPLog l) config communicate
85+ runDAPServerWithLogger (cmap renderDAPLog l) config communicate ( const ( pure () ))
8386
8487runDAPServerWithLogger
8588 :: LogAction IO DAPLog
8689 -> ServerConfig
8790 -- ^ Top-level Server configuration, global across all debug sessions
8891 -> (Command -> Adaptor app Request () )
8992 -- ^ A function to facilitate communication between DAP clients, debug adaptors and debuggers
93+ -> (ReverseRequestResponse -> Adaptor app () () )
94+ -- ^ A function to receive reverse-request-responses from DAP clients
9095 -> IO ()
91- runDAPServerWithLogger rawLogAction serverConfig@ ServerConfig {.. } communicate = withSocketsDo $ do
96+ runDAPServerWithLogger rawLogAction serverConfig@ ServerConfig {.. } communicate ackResp = withSocketsDo $ do
9297 let logAction = cfilter (\ msg -> if debugLogging then True else severity msg /= DEBUG ) rawLogAction
9398 logAction <& (mkDebugMessage $ (T. pack (" Running DAP server on " <> show port <> " ..." )))
9499 appStore <- newTVarIO mempty
@@ -99,7 +104,7 @@ runDAPServerWithLogger rawLogAction serverConfig@ServerConfig {..} communicate =
99104 handle <- socketToHandle socket ReadWriteMode
100105 hSetNewlineMode handle NewlineMode { inputNL = CRLF , outputNL = CRLF }
101106 adaptorStateMVar <- initAdaptorState logAction handle address appStore serverConfig
102- serviceClient communicate adaptorStateMVar
107+ serviceClient communicate ackResp adaptorStateMVar
103108 `catch` exceptionHandler logAction handle address debugLogging mainThread
104109 server `catch` \ (SomeAsyncException e) ->
105110 case fromException $ toException e of
@@ -127,13 +132,20 @@ initAdaptorState logAction handle address appStore serverConfig = do
127132-- Evaluates the current 'Request' located in the 'AdaptorState'
128133-- Fetches, updates and recurses on the next 'Request'
129134--
135+ -- Similarly, if the client responded to a reverse request then we execute the
136+ -- acknowledge action (which, notably, is not an @'Adaptor' _ 'Request'@
137+ -- because there's no 'Request' to reply to)
130138serviceClient
131139 :: (Command -> Adaptor app Request () )
140+ -> (ReverseRequestResponse -> Adaptor app r () )
132141 -> AdaptorLocal app r
133142 -> IO ()
134- serviceClient communicate lcl = forever $ runAdaptorWith lcl st $ do
135- nextRequest <- getRequest
136- withRequest nextRequest (communicate (command nextRequest))
143+ serviceClient communicate ackResp lcl = forever $ runAdaptorWith lcl st $ do
144+ either_nextRequest <- getRequest
145+ case either_nextRequest of
146+ Right nextRequest ->
147+ withRequest nextRequest (communicate (command nextRequest))
148+ Left rrr -> ackResp rrr
137149 where
138150 st = AdaptorState MessageTypeResponse []
139151----------------------------------------------------------------------------
@@ -172,7 +184,7 @@ exceptionHandler logAction handle address shouldLog serverThread (e :: SomeExcep
172184-- 'parseHeader' Attempts to parse 'Content-Length: <byte-count>'
173185-- Helper function for parsing message headers
174186-- e.g. ("Content-Length: 11\r\n")
175- getRequest :: Adaptor app r Request
187+ getRequest :: Adaptor app r ( Either ReverseRequestResponse Request )
176188getRequest = do
177189 handle <- getHandle
178190 header <- liftIO $ getHeaderHandle handle
@@ -186,10 +198,15 @@ getRequest = do
186198 (" \n " <> encodePretty (decodeStrict body :: Maybe Value ))
187199 case eitherDecode (BL8. fromStrict body) of
188200 Left couldn'tDecodeBody -> do
189- logError (T. pack couldn'tDecodeBody)
190- liftIO $ throwIO (ParseException couldn'tDecodeBody)
201+ -- As a fallback, try to parse a reverse request response
202+ case eitherDecode (BL8. fromStrict body) of
203+ Right rrr -> pure (Left rrr)
204+ Left _ -> do
205+ -- No luck, report fail to parse command:
206+ logError (T. pack couldn'tDecodeBody)
207+ liftIO $ throwIO (ParseException couldn'tDecodeBody)
191208 Right request ->
192- pure request
209+ pure ( Right request)
193210
194211getHeaderHandle :: Handle -> IO (Either String PayloadSize )
195212getHeaderHandle handle = do
0 commit comments