gameServer/NetRoutines.hs
author sheepluva
Wed, 05 Sep 2012 12:36:47 +0200
changeset 7673 e06b83cbde74
parent 5209 f7a610e2ef5f
child 7757 c20e6c80e249
permissions -rw-r--r--
fix a segfault related to tcp servers deleting themselves without removing themselves from the internal server list first. (e.g. happens if game fails to start due to missing hwengine binary. a second start attempt will lead to a segfault, even if you correctly install the binary.)

{-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-}
module NetRoutines where

import Network.Socket
import Control.Concurrent.Chan
import qualified Control.Exception as Exception
import Data.Time
import Control.Monad
import Data.Unique
-----------------------------
import CoreTypes
import Utils
import RoomsAndClients

acceptLoop :: Socket -> Chan CoreMessage -> IO ()
acceptLoop servSock chan = forever $
        do
        (sock, sockAddr) <- Network.Socket.accept servSock

        clientHost <- sockAddr2String sockAddr

        currentTime <- getCurrentTime

        sendChan' <- newChan

        uid <- newUnique

        let newClient =
                (ClientInfo
                    uid
                    sendChan'
                    sock
                    clientHost
                    currentTime
                    ""
                    ""
                    False
                    0
                    lobbyId
                    0
                    False
                    False
                    False
                    Nothing
                    0
                    )

        writeChan chan $ Accept newClient
        return ()