king: ames: make udp serv reuse port on err
- If can't open the port, wait and try again repeatedly.
- If there is an error reading or writting from the open socket,
- close it and open another.
+ close it and open another, making sure, however, to reuse the
+ same port
2. Receives packets from the socket.
@@ -158,7 +159,7 @@ realUdpServ
-> HostAddress
-> AmesStat
-> RIO e UdpServ
-realUdpServ por hos sat = do
+realUdpServ startPort hos sat = do
logInfo $ displayShow ("AMES", "UDP", "Starting real UDP server.")
env <- ask
@@ -202,10 +203,13 @@ realUdpServ por hos sat = do
did <- atomically (tryWriteTBQueue qSend (a, b))
when (did == False) $ do
logWarn "AMES: UDP: Dropping outbound packet because queue is full."
-
- tOpen <- async $ forever $ do
+ let opener por = do
+ logInfo $ displayShow $ ("AMES", "UDP", "Trying to open socket, port",)
+ por
sk <- forceBind por hos
sn <- io $ getSocketName sk
+ sp <- io $ socketPort sk
+ logInfo $ displayShow $ ("AMES", "UDP", "Got socket", sn, sp)
let waitForRelease = do
atomically (writeTVar vSock (Just sk))
@@ -220,6 +224,10 @@ realUdpServ por hos sat = do
\() -> waitForRelease
_ -> waitForRelease
+ opener sp
+
+ tOpen <- async $ opener startPort
+
tSend <- async $ forever $ join $ atomically $ do
(adr, byt) <- readTBQueue qSend
readTVar vSock <&> \case
