- whileConnected :: (Maybe String) String ([MTaskMSGRecv], [MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool), [String], Bool)
- //We stop
- whileConnected _ _ (_,_,True) = (Ok "", Nothing, [], True)
- //No new data and nothing to send
- whileConnected Nothing acc (_,[],_) = (Ok acc, Nothing, [], False)
- //New data and possibly something to send
- whileConnected newdata acc (msgs,send,sendStopped)
- # (acc, nd) = process (acc +++ fromMaybe "" newdata)
- | isEmpty nd && isEmpty send = (Ok acc, Nothing, [], False)
- = (Ok acc, Just (msgs++map decode nd,[],sendStopped), map encode send, False)
-
- process :: String -> (String, [String])
- process s
- | not (trace_tn ("process: " +++ toString (toJSON s))) = undef
- = case indexOf "\n" s of
- -1 = (s, [])
- i = appSnd (\ss->[s % (0,i-1):ss]) (process (s % (i+1, size s)))
+ onData :: String String ChD -> (MaybeErrorString String, Maybe ChD, [String], Bool)
+ onData newdata acc (msgs,send,True) = (Ok acc, Nothing, [], True)
+ onData newdata acc (msgs,send,sendStopped)
+ # split = indexOf "\n" newdata
+ | split == -1 = (Ok acc, Just (msgs, send, True), [], False)
+ # newMsg = decode (newdata % (0, split-1))
+ // Recurse with smaller data, empty accumulator and new message
+ = onData (newdata % (split+1, size newdata - split))
+ "" (msgs ++ [newMsg], send, False)