--- /dev/null
+module miTask
+
+import StdDebug, StdMisc
+
+from Text import class Text(concat,join,split), instance Text String
+
+import iTasks
+import mTask
+
+Start :: *World -> *World
+Start world = startEngine mTaskTask world
+
+mTaskTask :: Task ()
+mTaskTask = withShared ([],False,[],False) (\ch->
+ syncNetworkChannel "localhost" 8123 "\n" id id ch ||-
+ updateSharedInformation "channels" [] ch @! ())
+
+syncNetworkChannel :: String Int String (String -> m) (m -> String) (Shared ([m],Bool,[m],Bool)) -> Task () | iTask m
+syncNetworkChannel server port msgSeparator decodeFun encodeFun channel
+ = tcpconnect server port channel {ConnectionHandlers|onConnect=onConnect,whileConnected=whileConnected,onDisconnect=onDisconnect} @! ()
+ where
+ onConnect _ (received,receiveStopped,send,sendStopped)
+ = (Ok "",if (not (isEmpty send)) (Just (received,False,[],sendStopped)) Nothing, map encodeFun send,False)
+ whileConnected Nothing acc (received,receiveStopped,send,sendStopped)
+ = (Ok acc, Nothing, [], False)
+ whileConnected (Just newData) acc (received,receiveStopped,send,sendStopped)
+ # [acc:msgs] = reverse (split msgSeparator (concat [acc,newData]))
+ # write = if (not (isEmpty msgs && isEmpty send))
+ (Just (received ++ map decodeFun (reverse msgs),receiveStopped,[],sendStopped))
+ Nothing
+ = (Ok acc,write,map encodeFun send,False)
+
+ onDisconnect l (received,receiveStopped,send,sendStopped)
+ = (Ok l,Just (received,True,send,sendStopped))
+
+consumeNetworkStream :: ([m] -> Task ()) (Shared ([m],Bool,[m],Bool)) -> Task () | iTask m
+consumeNetworkStream processTask channel
+ = ((watch channel >>* [OnValue (ifValue ifProcess process)]) <! id) @! ()
+ where
+ ifProcess (received,receiveStopped,_,_)
+ = receiveStopped || (not (isEmpty received))
+
+ process (received,receiveStopped,_,_)
+ = upd empty channel
+ >>| if (isEmpty received) (return ()) (processTask received)
+ @! receiveStopped
+
+ empty :: ([m],Bool,[m],Bool) -> ([m],Bool,[m],Bool)
+ empty (_,rs,s,ss) = ([],rs,s,ss)