gameServer/NetRoutines.hs
author sheepluva
Sat, 14 Jun 2014 23:59:20 +0200
changeset 10305 17f3ca06e39a
parent 10093 ada172d33988
child 10460 8dcea9087d75
permissions -rw-r--r--
enforce style on message boxes some more

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

import Network.Socket
import Control.Concurrent.Chan
import Data.Time
import Control.Monad
import Data.Unique
import qualified Codec.Binary.Base64 as Base64
import qualified Data.ByteString as BW
import qualified Data.ByteString.Char8 as B
import qualified Control.Exception as E
import System.Entropy
-----------------------------
import CoreTypes
import Utils


acceptLoop :: Socket -> Chan CoreMessage -> IO ()
acceptLoop servSock chan = E.bracket openHandle closeHandle f
    where
    f ch = forever $
        do
        (sock, sockAddr) <- Network.Socket.accept servSock

        clientHost <- sockAddr2String sockAddr

        currentTime <- getCurrentTime

        sendChan' <- newChan

        uid <- newUnique
        salt <- liftM (B.pack . Base64.encode . BW.unpack) $ hGetEntropy ch 18

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

        writeChan chan $ Accept newClient
        return ()