gameServer/NetRoutines.hs
changeset 4905 7842d085acf4
parent 4568 f85243bf890e
child 4918 c6d3aec73f93
equal deleted inserted replaced
4904:0eab727d4717 4905:7842d085acf4
     1 {-# LANGUAGE ScopedTypeVariables #-}
     1 {-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-}
     2 module NetRoutines where
     2 module NetRoutines where
     3 
     3 
     4 import Network
       
     5 import Network.Socket
     4 import Network.Socket
     6 import System.IO
     5 import System.IO
     7 import Control.Concurrent
       
     8 import Control.Concurrent.Chan
     6 import Control.Concurrent.Chan
     9 import Control.Concurrent.STM
       
    10 import qualified Control.Exception as Exception
     7 import qualified Control.Exception as Exception
    11 import Data.Time
     8 import Data.Time
       
     9 import Control.Monad
    12 -----------------------------
    10 -----------------------------
    13 import CoreTypes
    11 import CoreTypes
    14 import ClientIO
       
    15 import Utils
    12 import Utils
       
    13 import RoomsAndClients
    16 
    14 
    17 acceptLoop :: Socket -> Chan CoreMessage -> Int -> IO ()
    15 acceptLoop :: Socket -> Chan CoreMessage -> IO ()
    18 acceptLoop servSock coreChan clientCounter = do
    16 acceptLoop servSock chan = forever $ do
    19     Exception.handle
    17     Exception.handle
    20         (\(_ :: Exception.IOException) -> putStrLn "exception on connect") $
    18         (\(_ :: Exception.IOException) -> putStrLn "exception on connect") $
    21         do
    19         do
    22         (socket, sockAddr) <- Network.Socket.accept servSock
    20         (sock, sockAddr) <- Network.Socket.accept servSock
    23 
    21 
    24         cHandle <- socketToHandle socket ReadWriteMode
       
    25         hSetBuffering cHandle LineBuffering
       
    26         clientHost <- sockAddr2String sockAddr
    22         clientHost <- sockAddr2String sockAddr
    27 
    23 
    28         currentTime <- getCurrentTime
    24         currentTime <- getCurrentTime
    29         
    25 
    30         sendChan <- newChan
    26         sendChan' <- newChan
    31 
    27 
    32         let newClient =
    28         let newClient =
    33                 (ClientInfo
    29                 (ClientInfo
    34                     nextID
    30                     sendChan'
    35                     sendChan
    31                     sock
    36                     cHandle
       
    37                     clientHost
    32                     clientHost
    38                     currentTime
    33                     currentTime
    39                     ""
    34                     ""
    40                     ""
    35                     ""
    41                     False
    36                     False
    42                     0
    37                     0
    43                     0
    38                     lobbyId
    44                     0
    39                     0
    45                     False
    40                     False
    46                     False
    41                     False
    47                     False
    42                     False
    48                     undefined
    43                     undefined
    49                     undefined
    44                     undefined
    50                     )
    45                     )
    51 
    46 
    52         writeChan coreChan $ Accept newClient
    47         writeChan chan $ Accept newClient
    53 
       
    54         forkIO $ clientRecvLoop cHandle coreChan nextID
       
    55         forkIO $ clientSendLoop cHandle coreChan sendChan nextID
       
    56         return ()
    48         return ()
    57 
       
    58     acceptLoop servSock coreChan nextID
       
    59     where
       
    60         nextID = clientCounter + 1