gameServer/NetRoutines.hs
author unc0rr
Thu, 26 Jun 2014 21:43:36 +0400
changeset 10342 16122539d2ea
parent 10093 ada172d33988
child 10460 8dcea9087d75
permissions -rw-r--r--
Fix build, and also make protocol a bit more consistent and flexible (only in docs though, to be implemented)

{-# 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 ()