gameServer/NetRoutines.hs
author unc0rr
Thu, 26 Mar 2009 13:54:02 +0000
changeset 1917 c94045b70142
parent 1847 2178c0fc838c
child 1924 8f8fe856ce9d
permissions -rw-r--r--
- Better ip2string implementation - Update girder thing sprite
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
{-# LANGUAGE PatternSignatures #-}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
module NetRoutines where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
import Network
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
import Network.Socket
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
import System.IO
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import Control.Concurrent
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
import Control.Concurrent.Chan
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
import Control.Concurrent.STM
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    10
import Control.Exception
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    11
import Data.Time
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    12
-----------------------------
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
import CoreTypes
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    14
import ClientIO
1917
c94045b70142 - Better ip2string implementation
unc0rr
parents: 1847
diff changeset
    15
import Utils
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    16
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    17
acceptLoop :: Socket -> Chan CoreMessage -> Int -> IO ()
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    18
acceptLoop servSock coreChan clientCounter = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    19
	Control.Exception.handle
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    20
		(\(_ :: Exception) -> putStrLn "exception on connect") $
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    21
		do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    22
		(socket, sockAddr) <- Network.Socket.accept servSock
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    23
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    24
		cHandle <- socketToHandle socket ReadWriteMode
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    25
		hSetBuffering cHandle LineBuffering
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    26
		clientHost <- sockAddr2String sockAddr
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    27
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    28
		currentTime <- getCurrentTime
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1804
diff changeset
    29
		--putStrLn $ (show currentTime) ++ " new client id: " ++ (show nextID)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    30
		
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
		sendChan <- newChan
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    33
		let newClient =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    34
				(ClientInfo
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    35
					nextID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    36
					sendChan
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    37
					cHandle
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    38
					clientHost
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    39
					--currentTime
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    40
					""
1841
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
    41
					""
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
    42
					False
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    43
					0
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    44
					0
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    45
					False
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    46
					False
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    47
					False
1847
2178c0fc838c Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents: 1841
diff changeset
    48
					False
2178c0fc838c Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents: 1841
diff changeset
    49
					False
2178c0fc838c Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents: 1841
diff changeset
    50
					)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    51
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    52
		writeChan coreChan $ Accept newClient
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    53
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    54
		forkIO $ clientRecvLoop cHandle coreChan nextID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    55
		forkIO $ clientSendLoop cHandle coreChan sendChan nextID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    56
		return ()
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    57
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    58
	yield -- hm?
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    59
	acceptLoop servSock coreChan nextID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    60
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    61
		nextID = clientCounter + 1