1import Network.Transport
2import Network.Transport.TCP (createTransport, defaultTCPParameters)
3import Control.Concurrent
5import Control.Exception
6import System.Environment
8-- | Server that echoes messages straight back to the origin endpoint.
9echoServer :: EndPoint -> MVar () -> IO ()
10echoServer endpoint serverDone = go empty
12 go :: Map ConnectionId (MVar Connection) -> IO ()
14 event <- receive endpoint
16 ConnectionOpened cid rel addr -> do
17 putStrLn$ " New connection: ID "++show cid++", reliability: "++show rel++", address: "++ show addr
18 connMVar <- newEmptyMVar
20 Right conn <- connect endpoint addr rel defaultConnectHints
22 go (insert cid connMVar cs)
23 Received cid payload -> do
25 conn <- readMVar (cs ! cid)
29 ConnectionClosed cid -> do
30 putStrLn$ " Closed connection: ID "++show cid
32 conn <- readMVar (cs ! cid)
36 putStrLn "Echo server exiting"
39onCtrlC :: IO a -> IO () -> IO a
40p `onCtrlC` q = catchJust isUserInterrupt p (const $ q >> p `onCtrlC` q)
42 isUserInterrupt :: AsyncException -> Maybe ()
43 isUserInterrupt UserInterrupt = Just ()
44 isUserInterrupt _ = Nothing
48 [host, port] <- getArgs
49 serverDone <- newEmptyMVar
50 Right transport <- createTransport host port defaultTCPParameters
51 Right endpoint <- newEndPoint transport
52 forkIO $ echoServer endpoint serverDone
53 putStrLn $ "Echo server started at " ++ show (address endpoint)
54 readMVar serverDone `onCtrlC` closeTransport transport
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list